-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·413 lines (346 loc) · 10.4 KB
/
main.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/python3
import os, re, signal, sys
import rest_service as service
import view
from exceptions import GoBack, InvalidNodeId
from lxml import etree
import libxml2
import ipaddress
from dijkstar import Graph, find_path
## Signal handler
def ctrl_c_handler(sig, frame):
close()
## Controller data transformations M->V
## Purge and modify data from the model to provide it to the view.
def _clean_namespaces(xml):
xml = re.sub(' xmlns="[^"]+"', '', xml)
xml = re.sub(' xmlns:a="[^"]+"', '', xml)
return xml
def _retrieve_topology(xml):
xml = _clean_namespaces(xml)
view_tree = etree.Element("nodes")
context = libxml2.parseDoc(xml)
# Store representation of nodes and links received from model
m_nodes = context.xpathEval("/network-topology/topology/node")
m_links = context.xpathEval("/network-topology/topology/link")
_clean_graph()
for n in m_nodes:
node_id = str(libxml2.parseDoc(str(n)).xpathEval('/node/node-id/text()')[0])
if node_id[0:4] == "host":
node_ip = str(libxml2.parseDoc(str(n)).xpathEval('/node/addresses/ip/text()')[0])
_add_node(node_id, node_ip)
else:
_add_node(node_id, "")
for l in m_links:
aux = str(libxml2.parseDoc(str(l)).xpathEval('/link/link-id/text()')[0])
# A pair of port ids => for hosts matches with id, for switch it does not
lsrc = str(libxml2.parseDoc(str(l)).xpathEval('/link/source/source-tp/text()')[0])
ldst = str(libxml2.parseDoc(str(l)).xpathEval('/link/destination/dest-tp/text()')[0])
src_ref, dst_ref = _get_node_pos(lsrc), _get_node_pos(ldst)
_add_link(src_ref, dst_ref)
_build_graph()
if not flows_ready:
_init_flows()
_init_flows_ports()
enable_all()
def _get_node_flows_view_xml(xml):
xml = _clean_namespaces(xml)
return xml
####
## Controller logic
## Perform actions on controller variables
def _clean_graph():
graph = Graph()
nodes = []
links = []
def _add_node(node_id, node_ip):
nodes.append((node_id, node_ip))
def _get_node_pos(node_ref):
# Get index i of next element i,v in nodes wich value v matches node_ref
try:
return nodes.index(next(i for i in nodes if node_ref.find(i[0]) != -1 or node_ref == i[1] ))
except:
pass
def _add_link(node1_ref, node2_ref):
links.append((node1_ref, node2_ref))
def _build_graph():
for l in links:
graph.add_edge(l[0],l[1],1)
def _get_path(ref1, ref2):
id1 = _get_node_pos(ref1)
id2 = _get_node_pos(ref2)
return find_path(graph, id1, id2)
####
## MVC communications
## Call service functions, model-view transformations and controller logic functions
def _is_ip(string):
try:
ipaddress.IPv4Address(string)
print("IPv4 detected")
True
except ipaddress.AddressValueError:
print("Node id detected")
False
def _is_switch(identifier):
return not identifier[:4] == "host"
def _is_error(func, *args, **kw):
try:
func(*args, **kw)
return False
except Exception:
return True
def _remove_duplicateds(links):
nodups= []
for i in range(len(links)-1):
if not (links[i][1],links[i][0]) in nodups:
nodups.append((links[i][0],links[i][1]))
return nodups
def _get_flows_ids(xml):
xml = _clean_namespaces(xml)
context = libxml2.parseDoc(xml)
# Store representation of nodes and links received from model
ids = [str(i) for i in libxml2.parseDoc(str(xml)).xpathEval('/table/flow/id/text()')]
return ids
def _get_first_ports_refs(xml):
xml = _clean_namespaces(xml)
context = libxml2.parseDoc(xml)
# Store representation of ports received from model
ids = [str(i).split(":")[-1] for i in libxml2.parseDoc(str(xml)).xpathEval('/node/node-connector/id/text()')]
clean_ids = [i for i in ids if not _is_error(int,i)]
return clean_ids
def _get_ports_refs(node_ref):
if node_ref in flows:
return [k for k,v in flows[node_ref].items()]
else:
raise InvalidNodeId
def _get_flow_id_for_port(xml, port_ref):
xml = _clean_namespaces(xml)
context = libxml2.parseDoc(xml)
# Store representation of nodes and links received from model
flows = context.xpathEval("/table/flow")
for f in flows:
if str(libxml2.parseDoc(str(f)).xpathEval('/flow/flow-name/text()')[0]) == ("block_port_"+ port_ref):
return str(libxml2.parseDoc(str(f)).xpathEval('/flow/id/text()')[0])
return -1
def _get_port_with(node_ref, neighbor_ref, xml):
context = libxml2.parseDoc(xml)
links = context.xpathEval("/network-topology/topology/link")
for l in links:
if str(libxml2.parseDoc(str(l)).xpathEval('/link/source/source-node/text()')[0]) == node_ref and str(libxml2.parseDoc(str(l)).xpathEval('/link/destination/dest-node/text()')[0]) == neighbor_ref:
return str(libxml2.parseDoc(str(l)).xpathEval('/link/source/source-tp/text()')[0])
def _set_node_port_mode(node_ref, port_ref, mode="enabled"):
node_flows = flows[node_ref]
if port_ref in node_flows:
node_flows[port_ref] = mode
if mode == "enabled":
service.enable_port(node_ref, port_ref)
print("Enabled " + node_ref + ":" + port_ref )
else:
service.disable_port(node_ref, port_ref)
print("Disabled " + node_ref + ":" + port_ref )
else:
print("Invalid port.")
def _init_flows():
for n in nodes:
if _is_switch(n[0]):
flows[n[0]] = {}
flows_ready = True
def _init_flows_ports():
for n in nodes:
if _is_switch(n[0]):
response = service.get_all_for(n[0])
if response[0] == "ok":
ports = _get_first_ports_refs(response[1])
for p in ports:
flows[n[0]][p] = "enabled"
def _show_flows_for_node(node_ref):
print("Flows for node " + node_ref)
if (node_ref,'') in nodes:
for port,state in flows[node_ref].items():
print(port + " : " + state)
else:
print("No valid node with that id.")
def show_topo():
response = service.get_topo()
_retrieve_topology(response[1])
view.clean()
view.show_hosts(nodes)
view.show_links(_remove_duplicateds(links), nodes)
def show_node_flows():
print("(Node id) ", end= '')
node_id = input("> ")
_show_flows_for_node(node_id)
def enable_all():
view.clean()
if nodes==[]:
print("No nodes detected. Have you 'shown network topology' ?")
back()
else:
for n in (n for n in nodes if _is_switch(n[0])):
node_ref = n[0]
for key,val in flows[node_ref].items():
if val == "disabled":
_set_node_port_mode(node_ref, key, "enabled")
print("All flows enabled")
def block_all():
view.clean()
if nodes==[]:
print("No nodes detected. Have you 'shown network topology' ?")
back()
else:
for n in (n for n in nodes if _is_switch(n[0])):
node_ref = n[0]
for key,val in flows[node_ref].items():
if val == "enabled":
_set_node_port_mode(node_ref, key, "disabled")
print("All flows disabled")
def enable_port(node_ref=None, port_ref=None):
if nodes==[]:
print("No nodes detected. Have you 'shown network topology' ?")
back()
else:
if node_ref == None:
view.clean()
print("(Node (switch) id) ", end='')
node_ref = input("> ")
try:
ports = _get_ports_refs(node_ref)
print("(Port id) " + str(ports) , end='')
port_ref = input(" > ")
except InvalidNodeId:
print("Invalid node id.")
if node_ref in flows:
if flows[node_ref][port_ref] == "disabled":
_set_node_port_mode(node_ref, port_ref, "enabled")
else:
print("Port already enabled.")
else:
print("This is not a topology switch")
def block_port(node_ref=None, port_ref=None):
if nodes==[]:
print("No nodes detected. Have you 'shown network topology' ?")
back()
else:
if node_ref == None:
view.clean()
print("(Node (switch) id) ", end='')
node_ref = input("> ")
try:
ports = _get_ports_refs(node_ref)
print("(Port id) " + str(ports) , end='')
port_ref = input(" > ")
if node_ref in flows:
if flows[node_ref][port_ref] == "enabled":
_set_node_port_mode(node_ref, port_ref, "disabled")
else:
print("Port already disabled.")
else:
print("This is not a topology switch")
except InvalidNodeId:
print("Invalid node id.")
def enable_path():
view.clean()
if nodes==[]:
print("No nodes detected. Have you 'shown network topology' ?")
back()
else:
print("(Node 1 id | ip ) ", end='')
node1_ref = input("> ")
print("(Node 2 id | ip ) ", end='')
node2_ref = input("> ")
path = _get_path(node1_ref, node2_ref)
response = service.get_topo()
if response[0] == "ok":
xml = _clean_namespaces(response[1])
# Iterate over path.nodes (list of positions on nodes)
for i in range(len(path.nodes)-1):
# path.nodes[i] is the position in nodes of the i-th node of the path
if _is_switch(nodes[path.nodes[i]][0]):
port = _get_port_with(nodes[path.nodes[i]][0], nodes[path.nodes[i-1]][0], xml)
enable_port(nodes[path.nodes[i]][0], port.split(":")[-1])
port = _get_port_with(nodes[path.nodes[i]][0], nodes[path.nodes[i+1]][0], xml)
enable_port(nodes[path.nodes[i]][0], port.split(":")[-1])
else:
print("Error! Could not retrieve needed topology.")
####
def back():
view.clean()
raise GoBack()
def close():
view.clean()
print("\nClosing application ...")
sys.exit(0)
# Enable nodes menu
def enable_block_menu():
print("\nEnable/Block nodes menu.")
print("[0] Go back.")
print("[1] Enable all flows.")
print("[2] Block all flows.")
print("[3] Enable a switch port.")
print("[4] Block a switch port.")
print("[5] Enable switch ports in a path.")
return(input("> "))
# Enable nodes loop
def enable_block_loop():
actions = {
0: back,
1: enable_all,
2: block_all,
3: enable_port,
4: block_port,
5: enable_path
}
while True:
try:
val = enable_block_menu()
os.system("clear")
actions[int(val)]()
except KeyError as e:
os.system("clear")
print("Error! Invalid option \"", val, "\"")
except GoBack:
break
# Application menu
def main_menu():
print("\nMain menu.")
print("[0] Close application.")
print("[1] Show network topology.")
print("[2] Show flows for node.")
print("[3] Block/Enable nodes ...")
return input("> ")
# Application loop
def main_loop():
# Map used to implement switch case
actions = {
0: close,
1: show_topo,
2: show_node_flows,
3: enable_block_loop,
}
while True:
try:
val = main_menu()
view.clean()
actions[int(val)]()
except KeyError as e:
view.clean()
print("Error! Invalid option \"", val, "\"")
try:
# Initialization
signal.signal(signal.SIGINT, ctrl_c_handler) # Register handler
graph = Graph()
nodes = []
links = []
flows = {}
flows_ready = False
# Display Wellcome
os.system("clear")
print("Wellcome to odl-multicast!")
main_loop()
close()
# Unhandled exceptions terminate application
except Exception as e:
print(e)
print("\nError!", e.__class__.__name__, "Exception occurred.")
print("Terminating ...")
sys.exit(1)