-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflows_generator.py
166 lines (133 loc) · 5 KB
/
flows_generator.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
import igraph as ig
import matplotlib.pyplot as plt
import copy
import json
import requests
from hosts_switches import hs
def find_link(src, dst):
"""Function that finds link between source device and destination device and returns list with two tuples
containing deviceId and port"""
if src == 0:
src = 'a'
if dst == 0:
dst = 'a'
src_device_id = "of:000000000000000" + str(src)
dst_device_id = "of:000000000000000" + str(dst)
headers = {
'Accept': 'application/json',
}
response = requests.get('http://192.168.1.75:8181/onos/v1/links', headers=headers, auth=('karaf', 'karaf'))
links = response.json()['links']
for link in links:
if link['src']['device'] == src_device_id and link['dst']['device'] == dst_device_id:
sd_port = link['src']['port']
dd_port = link['dst']['port']
return [(src_device_id, int(sd_port)), (dst_device_id, int(dd_port))]
flows = {'flows': []}
is_on = True
while is_on:
user_src = input('Źródło: ')
user_dst = input('Kierunek: ')
strumien = int(input('Wielkość strumienia danych [Mbps]: '))
src_switch = hs[user_src]['switch']
src_switch_nr = src_switch[-1::]
if src_switch_nr == 'a':
src_switch_nr = 0
src_switch_nr = int(src_switch_nr)
src_port = hs[user_src]['port']
src_host = hs[user_src]['host']
dst_switch = hs[user_dst]['switch']
dst_switch_nr = dst_switch[-1::]
if dst_switch_nr == 'a':
dst_switch_nr = 0
dst_switch_nr = int(dst_switch_nr)
dst_port = hs[user_dst]['port']
dst_host = hs[user_dst]['host']
# finding the shortest path
graph_links = [(1, 2), (1, 3), (2, 3), (3, 4), (3, 6), (3, 8), (6, 8), (4, 6),
(4, 7), (6, 7), (5, 7), (6, 9), (9, 0), (7, 8), (7, 9)]
g = ig.Graph(
10,
graph_links
)
g.es["weight"] = [2, 2, 2, 5, 2, 5, 5, 5, 2, 2, 5, 5, 5, 2, 5]
results = g.get_shortest_paths(src_switch_nr, to=dst_switch_nr, weights=g.es["weight"], output="epath")
if len(results[0]) > 0:
# Add up the weights across all edges on the shortest path
distance = 0
for e in results[0]:
distance += g.es[e]["weight"]
g.es[e]["weight"] += ((4 * strumien) / 10)
# print(g.es["weight"])
print("Shortest weighted distance is: ", distance)
else:
print("End node could not be reached!")
with open('sample.json') as file:
sample = json.load(file)
# flows between switches and hosts
flow = copy.deepcopy(sample)
flow['deviceId'] = src_switch
flow['treatment']['instructions'][0]['port'] = src_port
flow['selector']['criteria'][0]['mac'] = src_host
flows['flows'].append(flow)
flow = copy.deepcopy(sample)
flow['deviceId'] = dst_switch
flow['treatment']['instructions'][0]['port'] = dst_port
flow['selector']['criteria'][0]['mac'] = dst_host
flows['flows'].append(flow)
route = [graph_links[result] for result in results[0]]
# print(route)
# setting the switches in correct order
for connection in route:
if route.index(connection) != 0:
if connection[0] != route[route.index(connection) - 1][1]:
new_connection = (connection[1], connection[0])
route[route.index(connection)] = new_connection
elif src_switch_nr != connection[0]:
new_connection = (connection[1], connection[0])
route[route.index(connection)] = new_connection
# print(route)
# creating flows between switches
for connection in route:
src_dst = connection
link1 = find_link(src_dst[0], src_dst[1])
# print(link1)
flow = copy.deepcopy(sample)
flow['deviceId'] = link1[0][0]
flow['treatment']['instructions'][0]['port'] = link1[0][1]
flow['selector']['criteria'][0]['mac'] = dst_host
flows['flows'].append(flow)
flow = copy.deepcopy(sample)
flow['deviceId'] = link1[1][0]
flow['treatment']['instructions'][0]['port'] = link1[1][1]
flow['selector']['criteria'][0]['mac'] = src_host
flows['flows'].append(flow)
# print(flows)
flows_json = json.dumps(flows, indent=4)
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
response = requests.post('http://192.168.1.75:8181/onos/v1/flows', headers=headers, data=flows_json,
auth=('karaf', 'karaf'))
print(response)
# Graph for shortest path
g.es['width'] = 0.5
g.es[results[0]]['width'] = 2.5
fig, ax = plt.subplots()
ig.plot(
g,
target=ax,
layout='circle',
vertex_color='steelblue',
vertex_label=range(g.vcount()),
edge_width=g.es['width'],
edge_label=g.es["weight"],
edge_color='#666',
edge_align_label=True,
edge_background='white'
)
plt.show()
turn_off = input('Checesz kontynuować? (y/n): ')
if turn_off == 'n':
is_on = False