-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.py
136 lines (117 loc) · 4.55 KB
/
dijkstra.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
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.revent import *
from pox.lib.util import dpidToStr
from pox.lib.addresses import EthAddr
from collections import namedtuple
from pox.lib.addresses import IPAddr
import os
import csv
log = core.getLogger()
delayFile = "%s/pox/pox/misc/delay.csv" % os.environ[ 'HOME' ]
with open(delayFile, mode='r') as infile:
reader = csv.reader(infile)
delay_dict = {rows[0]:rows[1] for rows in reader}
delay_dict.pop("link")
for key in delay_dict:
delay_dict[key] = int(delay_dict[key])
hosts = ["h13", "h15", "h17", "h19"]
host_IPs = ["10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4"]
host_MACs = ["00:00:00:00:00:01","00:00:00:00:00:02","00:00:00:00:00:03","00:00:00:00:00:04"]
ports = {
's11': {'s12': 1, 's18': 2},
's12': {'s11':1, 's14': 2, 's16': 3, 's18': 4, 'h13': 5},
's14': {'s12': 1, 's16': 2, 's18': 3, 'h15': 4},
's16': {'s12': 1, 's14': 2, 's18': 3, 'h17': 4},
's18': {'s11': 1, 's12': 2, 's14': 3, 's16': 4, 'h19': 5}
}
def dijkstra(s,d):
source = s
delay = {}
adj_node = {}
queue = []
graph = {
'h13': {'s12':0},
'h15': {'s14':0},
'h17': {'s16':0},
'h19': {'s18':0},
's11': {'s12':delay_dict["g"], 's18':delay_dict["k"]},
's12': {'s11':delay_dict["g"], 's14':delay_dict["h"], 's16':delay_dict["m"], 's18':delay_dict["l"], 'h13':0},
's14': {'s12':delay_dict["h"], 's16':delay_dict["i"], 's18':delay_dict["n"], 'h15':0},
's16': {'s12':delay_dict["m"], 's14':delay_dict["i"], 's18':delay_dict["j"], 'h17':0},
's18': {'s11':delay_dict["k"], 's12':delay_dict["l"], 's14':delay_dict["n"], 's16':delay_dict["j"], 'h19':0}
}
for node in graph:
delay[node] = float("inf")
adj_node[node] = None
queue.append(node)
delay[source] = 0
while queue:
# find min distance which wasn't marked as current
key_min = queue[0]
min_val = delay[key_min]
for n in range(1, len(queue)):
if delay[queue[n]] < min_val:
key_min = queue[n]
min_val = delay[key_min]
cur = key_min
queue.remove(cur)
for i in graph[cur]:
alternate = graph[cur][i] + delay[cur]
if delay[i] > alternate:
delay[i] = alternate
adj_node[i] = cur
final_list = []
final_list.append(d)
while True:
d = adj_node[d]
if d is None:
break
final_list.append(d)
final_list.reverse()
return final_list
def next_in_path(s,d):
if (s == d):
return d
else:
return dijkstra(s,d)[1]
class Dijkstra (EventMixin):
def __init__ (self):
self.listenTo(core.openflow)
log.debug("Enabling Dijkstra Module")
def _handle_ConnectionUp (self, event):
current_switch = "s%s" % event.dpid
for i in range(0, 4):
# making rules based on MAC addresses
match = of.ofp_match()
match.dl_dst = EthAddr(host_MACs[i])
next_step = next_in_path(current_switch, hosts[i])
out_port = ports[current_switch][next_step]
# create a new flow table modification message
msg = of.ofp_flow_mod()
# assign this flow table message's match condition to the one above
msg.match = match
# make the action for this msg
msg.actions.append(of.ofp_action_output(port = out_port))
# send the flow table entry to the switch
event.connection.send(msg)
# making rules for ARP packets based on IP addresses
match = of.ofp_match()
match.dl_type = 0x806
match.nw_dst = IPAddr(host_IPs[i])
next_step = next_in_path(current_switch, hosts[i])
out_port = ports[current_switch][next_step]
# create a new flow table modification message
msg = of.ofp_flow_mod()
# assign this flow table message's match condition to the one above
msg.match = match
# make the action for this msg
msg.actions.append(of.ofp_action_output(port = out_port))
# send the flow table entry to the switch
event.connection.send(msg)
log.debug("Dijkstra installed on %s", dpidToStr(event.dpid))
def launch ():
'''
Starting the Dijkstra module
'''
core.registerNew(Dijkstra)