forked from Ali00/SDN-Restoration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithm1.py
207 lines (182 loc) · 5.81 KB
/
Algorithm1.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
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.revent import *
from pox.lib.recoco import Timer
from collections import defaultdict
from pox.openflow.discovery import Discovery
from pox.lib.util import dpid_to_str
import time
import copy
log = core.getLogger()
mac_map = {}
switches = {}
myswitches=[]
adjacency = defaultdict(lambda:defaultdict(lambda:None))
current_p=[]
def minimum_distance(distance, Q):
#print "distance=", distance
#print "Q=", Q
min = float('Inf')
node = 0
for v in Q:
if distance[v] < min:
min = distance[v]
node = v
#print "min=", min, " node=", node
return node
def _get_raw_path (src,dst):
#Dijkstra algorithm
print "src=",src," dst=", dst
#print "myswitches=", myswitches
distance = {}
previous = {}
sws = myswitches
for dpid in sws:
distance[dpid] = float('Inf')
previous[dpid] = None
distance[src]=0
Q=set(sws)
while len(Q)>0:
u = minimum_distance(distance, Q)
#print "u=", u
Q.remove(u)
for p in sws:
if adjacency[u][p]!=None:
w = 1
if distance[u] + w < distance[p]:
distance[p] = distance[u] + w
previous[p] = u
r=[]
p=dst
r.append(p)
q=previous[p]
while q is not None:
if q == src:
r.append(q)
break
p=q
r.append(p)
q=previous[p]
r.reverse()
return r
class Switch (EventMixin):
def __init__ (self):
self.connection = None
self.ports = None
self.dpid = None
self._listeners = None
self._connected_at = None
#To test the longest shortest path of ERnet topology
mac_map[str("00:00:00:00:00:01")]=(1,1)
mac_map[str("00:00:00:00:00:02")]=(36,1)
def __repr__ (self):
return dpid_to_str(self.dpid)
def _install (self, in_port, out_port, match, buf = None):
msg = of.ofp_flow_mod()
msg.match = match
msg.match.in_port = in_port
msg.idle_timeout = 0
msg.hard_timeout = 0
msg.actions.append(of.ofp_action_output(port = out_port))
msg.buffer_id = buf
self.connection.send(msg)
def _handle_PacketIn (self, event):
global current_p
print "_hanle_PacketIn() is called at", self.dpid
packet = event.parsed
print "packet.src=", str(packet.src), " packet.dst=", packet.dst
if str(packet.src) !="00:00:00:00:00:01" and str(packet.src) !="00:00:00:00:00:02":
return
#print "switches=", switches
#print "adjacency=", adjacency
path = _get_raw_path (mac_map[str(packet.src)][0], mac_map[str(packet.dst)][0])
#if len(current_p)!=0 and current_p != path:
# del current_p[:]
current_p=copy.deepcopy(path)
print "path=", path, "current_p=", current_p
if mac_map[str(packet.dst)][0]!=self.dpid:
next=path[path.index(self.dpid)+1]
#print "next=", next
output_port = adjacency[self.dpid][next]
#print "output_port=", adjacency[self.dpid][next]
match = of.ofp_match.from_packet(packet)
self._install(event.port, output_port, match)
else:
output_port=mac_map[str(packet.dst)][1]
msg = of.ofp_packet_out()
msg.actions.append(of.ofp_action_output(port = output_port))
msg.buffer_id = event.ofp.buffer_id
msg.in_port = event.port
self.connection.send(msg)
def disconnect (self):
if self.connection is not None:
log.debug("Disconnect %s" % (self.connection,))
self.connection.removeListeners(self._listeners)
self.connection = None
self._listeners = None
def connect (self, connection):
#print "type(conection.dpid)=", type(connection.dpid)
if self.dpid is None:
self.dpid = connection.dpid
assert self.dpid == connection.dpid
if self.ports is None:
self.ports = connection.features.ports
self.disconnect()
log.debug("Connect %s" % (connection,))
self.connection = connection
self._listeners = self.listenTo(connection)
self._connected_at = time.time()
def _handle_ConnectionDown (self, event):
self.disconnect()
class l2_multi (EventMixin):
def __init__ (self):
# Listen to dependencies
def startup ():
core.openflow.addListeners(self, priority=0)
core.openflow_discovery.addListeners(self)
core.call_when_ready(startup, ('openflow','openflow_discovery'))
def _handle_ConnectionUp (self, event):
sw = switches.get(event.dpid)
if sw is None:
# New switch
sw = Switch()
switches[event.dpid] = sw
sw.connect(event.connection)
myswitches.append(event.dpid)
else:
sw.connect(event.connection)
def _handle_LinkEvent(self, event):
global current_p
l = event.link
sw1 = l.dpid1
sw2 = l.dpid2
pt1 = l.port1
pt2 = l.port2
no_edges=0
for p in myswitches:
for q in myswitches:
if adjacency[p][q]!=None:
no_edges+=1
print "number of edges=", (no_edges*0.5)
print "current_p=", current_p
if len(myswitches)==37 and (no_edges*0.5) ==56: #we take as an example ERnet topology with N=37, E=57
if event.removed:
print sw1, "----", sw2, " is removed"
clear = of.ofp_flow_mod(command=of.OFPFC_DELETE)
for dpid in current_p:
if switches[dpid].connection is None: continue
switches[dpid].connection.send(clear)
if event.added:
#print "link is added"
if adjacency[sw1][sw2] is None:
adjacency[sw1][sw2] = l.port1
adjacency[sw2][sw1] = l.port2
if event.removed:
#print "link is removed"
try:
if sw2 in adjacency[sw1]: del adjacency[sw1][sw2]
if sw1 in adjacency[sw2]: del adjacency[sw2][sw1]
except:
print "remove edge error"
def launch ():
core.registerNew(l2_multi)