-
Notifications
You must be signed in to change notification settings - Fork 3
/
SDN_CustomCLI.py
313 lines (296 loc) · 11.6 KB
/
SDN_CustomCLI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python
"""
Documentation: Pending . . .
"""
# Global Imports
from SDN_global import *;
# Import STREAM method
from SDN_Stream import STREAM;
# Custom Class for CLI
class CustomCLI(CLI):
def __init__(self, *args, **kwargs):
self.cliArg = {
'argv' : None,
'cur' : 0,
'test' : 0,
'test_file' : None,
};
if gArg['argv']:
self.cliArg['test_file'] = gArg['argv'][-1] or self.cliArg['test_file'];
try:
with open(BASE_DIR + os.path.sep + self.cliArg['test_file'], 'rt') as f:
for _line in f:
self.cliArg['argv'] = shlex.split(_line);
self.cliArg['cur'] = 0;
self.cliArg['test'] += 1;
self.do_stream();
except IOError:
info('*** Error: Couldn\'t open Stream test file: \'' + self.cliArg['test_file'] + '\'!\n');
Cleanup.cleanup();
sys.exit(0);
CLI.__init__(self, *args, **kwargs);
def _get_input(self, _str):
if self.cliArg['argv']:
if self.cliArg['cur'] < len(self.cliArg['argv']):
_input = self.cliArg['argv'][self.cliArg['cur']];
self.cliArg['cur'] += 1;
else:
_input = '';
else:
_input = raw_input(_str);
return _input;
def do_stream(self, *args, **kwargs):
""" Method to initialize Streaming """
if self.cliArg['argv']:
info('\n\n****** STREAMING TEST #' + str(self.cliArg['test']) + '\n');
# Initialize Host Pool
# HOST_POOL = {}
# HOST_POOL['SOURCE'] = set([x for x in gMain['host_list']]);
# HOST_POOL['DESTINATION'] = set([x for x in gMain['host_list']]);
# HOST_POOL['NOISE'] = set([x for x in gMain['host_list']]);
# Get Streaming Cluster Count
CLUSTER_COUNT = 1; # Default
while True:
try:
CLUSTER_COUNT = int(self._get_input('Enter No. of Streaming Clusters (>= 1): ') or str(CLUSTER_COUNT));
except ValueError:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if CLUSTER_COUNT < 1:
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
thread_cluster_list = [];
# Determine CASE_ID
global BASE_DIR;
OUTPUT_DIR = BASE_DIR + os.path.sep + 'output';
makedirs_s(OUTPUT_DIR);
CASE_ID = 1; # Default
CASE_DIR = OUTPUT_DIR + os.path.sep + 'CASE_';
while os.path.exists(CASE_DIR + str(CASE_ID)) is True:
CASE_ID = CASE_ID + 1;
# Configure Streaming Clusters
for CLUSTER_ID in range(1, CLUSTER_COUNT + 1):
# Default Configuration for STREAM
VIDEO = gStreamConfig['VIDEO'];
DESTINATION_RATIO = gStreamConfig['DESTINATION_RATIO'];
STREAM_IP = gStreamConfig['STREAM_IP'];
STREAM_PORT = gStreamConfig['STREAM_PORT'];
NOISE_RATIO = gStreamConfig['NOISE_RATIO'];
NOISE_TYPE = gStreamConfig['NOISE_TYPE'];
NOISE_PORT = gStreamConfig['NOISE_PORT'];
NOISE_DATA_RATE = gStreamConfig['NOISE_DATA_RATE'];
NOISE_PACKET_DELAY = gStreamConfig['NOISE_PACKET_DELAY'];
SAP_PORT = gStreamConfig['SAP_PORT'];
# Set Streaming Parameters
if not self.cliArg['argv']:
info ('\n*** Setup CLUSTER #' + str(CLUSTER_ID) + '\n');
while True:
try:
VIDEO = os.path.abspath(self._get_input('Enter Stream Source file path: ') or VIDEO);
except:
info ('*** Error: Couldn\'t open Stream Source file\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
while True:
try:
DESTINATION_RATIO = float(self._get_input('Enter Destination Ratio: ') or str(DESTINATION_RATIO));
except ValueError:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if DESTINATION_RATIO < 0.0 or DESTINATION_RATIO > 1.0:
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
while True:
try:
STREAM_IP = IP2INT(self._get_input('Enter Mutlicast Source IP: ') or INT2IP(STREAM_IP));
except:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if STREAM_IP == 0:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
IP_RANGE_MIN = IP2INT('224.0.0.1');
IP_RANGE_MAX = IP2INT('239.255.255.255');
if STREAM_IP < IP_RANGE_MIN or STREAM_IP > IP_RANGE_MAX:
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
while True:
try:
STREAM_PORT = int(self._get_input('Enter Mutlicast Source Port (>= 1024 & <= 65535): ') or str(STREAM_PORT));
except ValueError:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if STREAM_PORT < 1024 or STREAM_PORT > 65535:
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
while True:
try:
NOISE_RATIO = float(self._get_input('Enter Noise Ratio: ') or str(NOISE_RATIO));
except ValueError:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if NOISE_RATIO < 0.0 or NOISE_RATIO > 1.0:
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
while True:
try:
NOISE_TYPE = int(self._get_input('Enter Noise Type (1:Broadcast, 2:Unicast): ') or str(NOISE_TYPE));
except ValueError:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if NOISE_TYPE not in range(1, 2 + 1):
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
while True:
try:
NOISE_PORT = int(self._get_input('Enter Noise Destination Port (>= 1024 & <= 65535): ') or str(NOISE_PORT));
except ValueError:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if NOISE_PORT < 1024 or NOISE_PORT > 65535:
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
while True:
try:
NOISE_DATA_RATE = int(self._get_input('Enter Noise Data Rate per Host (>= 0) (in bps): ') or str(NOISE_DATA_RATE));
except ValueError:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if NOISE_DATA_RATE < 0:
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
while True:
try:
SAP_PORT = int(self._get_input('Enter SAP Source Port (>= 1024 & <= 65535): ') or str(SAP_PORT));
except ValueError:
info ('*** Error: Invalid Input\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
if SAP_PORT < 1024 or SAP_PORT > 65535:
info ('*** Error: Input out of range\n');
if self.cliArg['argv']:
info ('*** Check arg #' + str(self.cliArg['cur']) + ' at Test #' + str(self.cliArg['test']) + ' in \'' + self.cliArg['test_file'] + '\'\n');
return;
continue;
break;
try:
NOISE_PACKET_DELAY = float(8000 * gPacketConfig['MTU']) / float(NOISE_DATA_RATE);
except:
NOISE_PACKET_DELAY = sys.float_info.max;
# Call STREAM method on Separate Thread
thread_cluster = threading.Thread(target = STREAM, args = (
CASE_ID,
CLUSTER_ID,
VIDEO,
DESTINATION_RATIO,
STREAM_IP,
STREAM_PORT,
NOISE_RATIO,
NOISE_TYPE,
NOISE_PORT,
NOISE_DATA_RATE,
NOISE_PACKET_DELAY,
SAP_PORT));
thread_cluster_list.append(thread_cluster);
# Start all Clusters
for thread_cluster in thread_cluster_list:
thread_cluster.start();
# Wait for Clusters
def _wait_for_clusters(*args, **kwargs):
for thread_cluster in thread_cluster_list:
thread_cluster.join();
# Clear OVS Flows & Groups
# info('\n*** Clearing all OVS Flows & Groups . . . ');
# for x in gMain['switch_list']:
# x.sendCmd('ovs-ofctl --protocols=OpenFlow13 replace-flows ' + x.name + ' \'' + BASE_DIR + os.path.sep + 'ovs_flows_default.txt\'');
# for x in gMain['switch_list']:
# x.waitOutput(verbose=True);
# for x in gMain['switch_list']:
# x.sendCmd('ovs-ofctl --protocols=OpenFlow13 del-groups ' + x.name);
# for x in gMain['switch_list']:
# x.waitOutput(verbose=True);
# Reset Session Manager (Only works for Multicast Session Manager)
# info('\n*** Resetting Session Manager . . . ');
# try:
# _request = urllib2.Request("http://127.0.0.1:8080/wm/sessionmanager/reset/json");
# _response = urllib2.build_opener(urllib2.ProxyHandler({})).open(_request).read(); # Bypass Proxy
# _len = len(_response);
# if _len < 4 or _response[:4] not in ("Fail", "Succ"):
# raise Exception('Something went wrong!');
# except urllib2.HTTPError as e:
# if e.code == 404:
# warn('\nWARNING: Session Manager not Found')
# else:
# warn('\nERROR: ' + str(e));
# except Exception as e:
# warn('\nERROR: ' + str(e));
# Check for Automation
if self.cliArg['argv']:
_wait_for_clusters();
else:
t = threading.Thread(target = _wait_for_clusters);
t.start();