This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
odl.py
278 lines (215 loc) · 10.2 KB
/
odl.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
#!/usr/bin/env python
import requests
from requests.auth import HTTPBasicAuth
import json
import unicodedata
from subprocess import Popen, PIPE
import time
import networkx as nx
from sys import exit
# Method To Get REST Data In JSON Format
def getResponse(url,choice):
response = requests.get(url, auth=HTTPBasicAuth('admin', 'admin'))
if(response.ok):
jData = json.loads(response.content)
if(choice=="topology"):
topologyInformation(jData)
elif(choice=="statistics"):
getStats(jData)
else:
response.raise_for_status()
def topologyInformation(data):
global switch
global deviceMAC
global deviceIP
global hostPorts
global linkPorts
global G
global cost
for i in data["network-topology"]["topology"]:
if "node" in i:
for j in i["node"]:
# Device MAC and IP
if "host-tracker-service:addresses" in j:
for k in j["host-tracker-service:addresses"]:
ip = k["ip"].encode('ascii','ignore')
mac = k["mac"].encode('ascii','ignore')
deviceMAC[ip] = mac
deviceIP[mac] = ip
# Device Switch Connection and Port
if "host-tracker-service:attachment-points" in j:
for k in j["host-tracker-service:attachment-points"]:
mac = k["corresponding-tp"].encode('ascii','ignore')
mac = mac.split(":",1)[1]
ip = deviceIP[mac]
temp = k["tp-id"].encode('ascii','ignore')
switchID = temp.split(":")
port = switchID[2]
hostPorts[ip] = port
switchID = switchID[0] + ":" + switchID[1]
switch[ip] = switchID
# Link Port Mapping
for i in data["network-topology"]["topology"]:
if "link" in i:
for j in i["link"]:
if "host" not in j['link-id']:
src = j["link-id"].encode('ascii','ignore').split(":")
srcPort = src[2]
dst = j["destination"]["dest-tp"].encode('ascii','ignore').split(":")
dstPort = dst[2]
srcToDst = src[1] + "::" + dst[1]
linkPorts[srcToDst] = srcPort + "::" + dstPort
G.add_edge((int)(src[1]),(int)(dst[1]))
def getStats(data):
print "\nCost Computation....\n"
global cost
txRate = 0
for i in data["node-connector"]:
tx = int(i["opendaylight-port-statistics:flow-capable-node-connector-statistics"]["packets"]["transmitted"])
rx = int(i["opendaylight-port-statistics:flow-capable-node-connector-statistics"]["packets"]["received"])
txRate = tx + rx
#print txRate
time.sleep(2)
response = requests.get(stats, auth=HTTPBasicAuth('admin', 'admin'))
tempJSON = ""
if(response.ok):
tempJSON = json.loads(response.content)
for i in tempJSON["node-connector"]:
tx = int(i["opendaylight-port-statistics:flow-capable-node-connector-statistics"]["packets"]["transmitted"])
rx = int(i["opendaylight-port-statistics:flow-capable-node-connector-statistics"]["packets"]["received"])
cost = cost + tx + rx - txRate
#cost = cost + txRate
#print cost
def systemCommand(cmd):
terminalProcess = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
terminalOutput, stderr = terminalProcess.communicate()
print "\n*** Flow Pushed\n"
def pushFlowRules(bestPath):
bestPath = bestPath.split("::")
for currentNode in range(0, len(bestPath)-1):
if (currentNode==0):
inport = hostPorts[h2]
srcNode = bestPath[currentNode]
dstNode = bestPath[currentNode+1]
outport = linkPorts[srcNode + "::" + dstNode]
outport = outport[0]
else:
prevNode = bestPath[currentNode-1]
#print prevNode
srcNode = bestPath[currentNode]
#print srcNode
dstNode = bestPath[currentNode+1]
inport = linkPorts[prevNode + "::" + srcNode]
inport = inport.split("::")[1]
outport = linkPorts[srcNode + "::" + dstNode]
outport = outport.split("::")[0]
xmlSrcToDst = '\'<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><flow xmlns=\"urn:opendaylight:flow:inventory\"><priority>32767</priority><flow-name>Load Balance 1</flow-name><match><in-port>' + str(inport) +'</in-port><ipv4-destination>10.0.0.1/32</ipv4-destination><ipv4-source>10.0.0.4/32</ipv4-source><ethernet-match><ethernet-type><type>2048</type></ethernet-type></ethernet-match></match><id>1</id><table_id>0</table_id><instructions><instruction><order>0</order><apply-actions><action><order>0</order><output-action><output-node-connector>' + str(outport) +'</output-node-connector></output-action></action></apply-actions></instruction></instructions></flow>\''
xmlDstToSrc = '\'<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><flow xmlns=\"urn:opendaylight:flow:inventory\"><priority>32767</priority><flow-name>Load Balance 2</flow-name><match><in-port>' + str(outport) +'</in-port><ipv4-destination>10.0.0.4/32</ipv4-destination><ipv4-source>10.0.0.1/32</ipv4-source><ethernet-match><ethernet-type><type>2048</type></ethernet-type></ethernet-match></match><id>2</id><table_id>0</table_id><instructions><instruction><order>0</order><apply-actions><action><order>0</order><output-action><output-node-connector>' + str(inport) +'</output-node-connector></output-action></action></apply-actions></instruction></instructions></flow>\''
flowURL = "http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:"+ bestPath[currentNode] +"/table/0/flow/1"
command = 'curl --user "admin":"admin" -H "Accept: application/xml" -H "Content-type: application/xml" -X PUT ' + flowURL + ' -d ' + xmlSrcToDst
systemCommand(command)
flowURL = "http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:"+ bestPath[currentNode] +"/table/0/flow/2"
command = 'curl --user "admin":"admin" -H "Accept: application/xml" -H "Content-type: application/xml" -X PUT ' + flowURL + ' -d ' + xmlDstToSrc
systemCommand(command)
srcNode = bestPath[-1]
prevNode = bestPath[-2]
inport = linkPorts[prevNode + "::" + srcNode]
inport = inport.split("::")[1]
outport = hostPorts[h1]
xmlSrcToDst = '\'<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><flow xmlns=\"urn:opendaylight:flow:inventory\"><priority>32767</priority><flow-name>Load Balance 1</flow-name><match><in-port>' + str(inport) +'</in-port><ipv4-destination>10.0.0.1/32</ipv4-destination><ipv4-source>10.0.0.4/32</ipv4-source><ethernet-match><ethernet-type><type>2048</type></ethernet-type></ethernet-match></match><id>1</id><table_id>0</table_id><instructions><instruction><order>0</order><apply-actions><action><order>0</order><output-action><output-node-connector>' + str(outport) +'</output-node-connector></output-action></action></apply-actions></instruction></instructions></flow>\''
xmlDstToSrc = '\'<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><flow xmlns=\"urn:opendaylight:flow:inventory\"><priority>32767</priority><flow-name>Load Balance 2</flow-name><match><in-port>' + str(outport) +'</in-port><ipv4-destination>10.0.0.4/32</ipv4-destination><ipv4-source>10.0.0.1/32</ipv4-source><ethernet-match><ethernet-type><type>2048</type></ethernet-type></ethernet-match></match><id>2</id><table_id>0</table_id><instructions><instruction><order>0</order><apply-actions><action><order>0</order><output-action><output-node-connector>' + str(inport) +'</output-node-connector></output-action></action></apply-actions></instruction></instructions></flow>\''
flowURL = "http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:"+ bestPath[-1] +"/table/0/flow/1"
command = 'curl --user \"admin\":\"admin\" -H \"Accept: application/xml\" -H \"Content-type: application/xml\" -X PUT ' + flowURL + ' -d ' + xmlSrcToDst
systemCommand(command)
flowURL = "http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:"+ bestPath[-1] +"/table/0/flow/2"
command = 'curl --user "admin":"admin" -H "Accept: application/xml" -H "Content-type: application/xml" -X PUT ' + flowURL + ' -d ' + xmlDstToSrc
systemCommand(command)
# Main
# Stores H1 and H2 from user
global h1,h2,h3
h1 = ""
h2 = ""
print "Enter Host 1"
h1 = int(input())
print "\nEnter Host 2"
h2 = int(input())
print "\nEnter Host 3 (H2's Neighbour)"
h3 = int(input())
h1 = "10.0.0." + str(h1)
h2 = "10.0.0." + str(h2)
h3 = "10.0.0." + str(h3)
flag = True
while flag:
#Creating Graph
G = nx.Graph()
# Stores Info About H3 And H4's Switch
switch = {}
# MAC of Hosts i.e. IP:MAC
deviceMAC = {}
# IP of Hosts i.e. MAC:IP
deviceIP = {}
# Stores Switch Links To H3 and H4's Switch
switchLinks = {}
# Stores Host Switch Ports
hostPorts = {}
# Stores Switch To Switch Path
path = {}
# Stores Link Ports
linkPorts = {}
# Stores Final Link Rates
finalLinkTX = {}
# Store Port Key For Finding Link Rates
portKey = ""
# Statistics
global stats
stats = ""
# Stores Link Cost
global cost
cost = 0
try:
# Device Info (Switch To Which The Device Is Connected & The MAC Address Of Each Device)
topology = "http://127.0.0.1:8181/restconf/operational/network-topology:network-topology"
getResponse(topology,"topology")
# Print Device:MAC Info
print "\nDevice IP & MAC\n"
print deviceMAC
# Print Switch:Device Mapping
print "\nSwitch:Device Mapping\n"
print switch
# Print Host:Port Mapping
print "\nHost:Port Mapping To Switch\n"
print hostPorts
# Print Switch:Switch Port:Port Mapping
print "\nSwitch:Switch Port:Port Mapping\n"
print linkPorts
# Paths
print "\nAll Paths\n"
#for path in nx.all_simple_paths(G, source=2, target=1):
#print(path)
for path in nx.all_shortest_paths(G, source=int(switch[h2].split(":",1)[1]), target=int(switch[h1].split(":",1)[1]), weight=None):
print path
# Cost Computation
tmp = ""
for currentPath in nx.all_shortest_paths(G, source=int(switch[h2].split(":",1)[1]), target=int(switch[h1].split(":",1)[1]), weight=None):
for node in range(0,len(currentPath)-1):
tmp = tmp + str(currentPath[node]) + "::"
key = str(currentPath[node])+ "::" + str(currentPath[node+1])
port = linkPorts[key]
port = port.split(":",1)[0]
port = int(port)
stats = "http://localhost:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:"+str(currentPath[node])+"/node-connector/openflow:"+str(currentPath[node])+":"+str(port)
getResponse(stats,"statistics")
tmp = tmp + str(currentPath[len(currentPath)-1])
tmp = tmp.strip("::")
finalLinkTX[tmp] = cost
cost = 0
tmp = ""
print "\nFinal Link Cost\n"
print finalLinkTX
shortestPath = min(finalLinkTX, key=finalLinkTX.get)
print "\n\nShortest Path: ",shortestPath
pushFlowRules(shortestPath)
time.sleep(60)
except KeyboardInterrupt:
break
exit