-
Notifications
You must be signed in to change notification settings - Fork 2
/
patch_node.py
258 lines (215 loc) · 9.46 KB
/
patch_node.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
import json
import collections
import patch_port
import patch_link
import patch_error
# Node class
class TestEnvNode(object):
def __init__(self, node_name, node_data):
self.name = node_name
self.data = node_data # dictionary
self.port_index = {}
self.datapath_id = 0
self.role = None
# setup port index: dictionary of port object
self._setup_port_index()
def port_names(self):
return self.port_index.keys()
def port_by_name(self, port_name):
if port_name in self.port_index:
return self.port_index[port_name]
return None
def _setup_port_index(self):
port_index = self.data['port-index']
for port_name, port_data in port_index.items():
self.port_index[port_name] = patch_port.NodePort(port_name, port_data)
def __str__(self):
return "Node: %s (dpid:%d)" % (self.name, self.datapath_id)
def dump(self):
print self
for port in self.port_index.values():
print " %s" % port
class TestHost(TestEnvNode):
def __init__(self, node_name, node_data):
super(TestHost, self).__init__(node_name, node_data)
self.role = 'test-host'
class DUTHost(TestEnvNode):
def __init__(self, node_name, node_data):
super(DUTHost, self).__init__(node_name, node_data)
self.role = 'dut-host'
class Dispatcher(TestEnvNode):
def __init__(self, node_name, node_data):
super(Dispatcher, self).__init__(node_name, node_data)
self.role = "switch"
self.datapath_id = node_data['datapath-id']
# Node Manager
class NodeManager(object):
def __init__(self, node_data):
self.node_data = node_data
# use OrderedDict to keep order defined in config file
# (also, must use OrderedDict as node_data)
self.dispatcher_index = collections.OrderedDict()
self.test_host_index = collections.OrderedDict()
self.dut_host_index = collections.OrderedDict()
self.linkmgr = patch_link.NodeLinkManager(self.node_data['link-list'])
self._setup_dispatchers()
self._setup_test_hosts()
self._setup_dut_hosts()
self._setup_node_port()
def _setup_dispatchers(self):
try:
dispatcher_data = self.node_data['dispatchers']
except KeyError:
msg = "Could not find 'dispatcher' data in node info"
raise patch_error.PatchDefinitionError(msg)
for di_name, di_data in dispatcher_data.items():
self.dispatcher_index[di_name] = Dispatcher(di_name, di_data)
def _setup_test_hosts(self):
try:
test_hosts_data = self.node_data['test-hosts']
except KeyError:
msg = "Could not find 'test-hosts' data in node info"
raise patch_error.PatchDefinitionError(msg)
for th_name, th_data in test_hosts_data.items():
self.test_host_index[th_name] = TestHost(th_name, th_data)
def _setup_dut_hosts(self):
try:
dut_hosts_data = self.node_data['dut-hosts']
except KeyError:
msg = "Could not find 'dut-hosts' data in node info"
raise patch_error.PatchDefinitionError(msg)
for dut_name, dut_data in dut_hosts_data.items():
self.dut_host_index[dut_name] = DUTHost(dut_name, dut_data)
@staticmethod
def __do_each_node_port(node_index, func):
"""
do some func() for each node/port
"""
for node_name, node in node_index.items():
for port_name in node.port_names():
func(node, node_name, port_name)
def __counterpart_node_role(self, node_name, port_name):
link = self.linkmgr.find_link_by_name(node_name, port_name)
if link:
counterpart_port = link.counterpart_by_name(node_name, port_name)
counterpart_node = self.node_by_name(counterpart_port.node)
return counterpart_node.role
else:
msg = "Cound not find link counterpart of Node,Port=%s,%s" % (node_name, port_name)
raise patch_error.PatchDefinitionError(msg)
def __create_test_host_port(self, host, host_name, port_name):
cp_role = self.__counterpart_node_role(host_name, port_name)
port_data = host.port_index[port_name].data
if cp_role == 'switch':
host.port_index[port_name]\
= patch_port.TestHostPort(port_name, port_data)
else:
msg = "Test-Host,Port=%s,%s does not connect switch" % (host_name, port_name)
raise patch_error.PatchDefinitionError(msg)
def __create_dut_host_port(self, host, host_name, port_name):
cp_role = self.__counterpart_node_role(host_name, port_name)
port_data = host.port_index[port_name].data
if cp_role == 'switch':
host.port_index[port_name]\
= patch_port.DUTHostPort(port_name, port_data)
else:
msg = "DUT-Host,Port=%s,%s does not connect switch" % (host_name, port_name)
raise patch_error.PatchDefinitionError(msg)
def __create_dispatcher_port(self, dispatcher, dispatcher_name, port_name):
cp_role = self.__counterpart_node_role(dispatcher_name, port_name)
# print "// switch:%s, port:%s, role:%s" % (dispatcher_name, port_name, cp_role)
port_data = dispatcher.port_index[port_name].data
if cp_role == 'test-host':
dispatcher.port_index[port_name]\
= patch_port.HostEdgePort(port_name, port_data)
elif cp_role == 'dut-host':
dispatcher.port_index[port_name]\
= patch_port.DUTEdgePort(port_name, port_data)
elif cp_role == 'switch':
dispatcher.port_index[port_name]\
= patch_port.InterSwitchPort(port_name, port_data)
else:
msg = "Dispatcher,Port=%s,%s connected unknown host/port"
raise patch_error.PatchDefinitionError(msg)
def _setup_node_port(self):
self.__do_each_node_port(
self.test_host_index, self.__create_test_host_port)
self.__do_each_node_port(
self.dut_host_index, self.__create_dut_host_port)
self.__do_each_node_port(
self.dispatcher_index, self.__create_dispatcher_port)
def dump_dispatchers(self):
""" for debug """
for dispatcher in self.dispatcher_index.values():
dispatcher.dump()
def dump_test_hosts(self):
""" for debug """
for test_host in self.test_host_index.values():
test_host.dump()
def dump_dut_hosts(self):
""" for debug """
for dut_host in self.dut_host_index.values():
dut_host.dump()
def dump_links(self):
""" for debug """
self.linkmgr.dump()
def has_test_host(self, test_host_name):
return test_host_name in self.test_host_index
def has_dut_host(self, dut_host_name):
return dut_host_name in self.dut_host_index
def has_dispatcher(self, dispatcher_name):
return dispatcher_name in self.dispatcher_index
def has_node(self, node_name):
return (self.has_test_host(node_name)
or self.has_dispatcher(node_name)
or self.has_dut_host(node_name))
def node_by_name(self, node_name):
if self.has_test_host(node_name):
return self.test_host_index[node_name]
elif self.has_dut_host(node_name):
return self.dut_host_index[node_name]
elif self.has_dispatcher(node_name):
return self.dispatcher_index[node_name]
return None
def test_host_port_by_name(self, test_host_name, port_name):
if test_host_name in self.test_host_index:
return self.test_host_index[test_host_name].port_by_name(port_name)
return None
def dut_host_port_by_name(self, dut_host_name, port_name):
if dut_host_name in self.dut_host_index:
return self.dut_host_index[dut_host_name].port_by_name(port_name)
return None
def dispatcher_port_by_name(self, dispatcher_name, port_name):
if self.has_dispatcher(dispatcher_name):
return self.dispatcher_index[dispatcher_name].port_by_name(port_name)
return None
def node_port_by_name(self, node_name, port_name):
return (self.test_host_port_by_name(node_name, port_name)
or self.dut_host_port_by_name(node_name, port_name)
or self.dispatcher_port_by_name(node_name, port_name))
if __name__ == '__main__':
node_data_file_name = 'nodeinfo_topo2.json'
node_data_file = open(node_data_file_name, 'r')
node_data = json.load(node_data_file)
node_data_file.close()
# print "## data"
# print json.dumps(node_data, indent=2)
node_mgr = NodeManager(node_data)
# print "# dump links"
# node_mgr.dump_links()
# print "# dump dispatchers"
# node_mgr.dump_dispatchers()
# print "# dump test hosts"
# node_mgr.dump_test_hosts()
# print "# dump dut hosts"
# node_mgr.dump_dut_hosts()
print "# host ip/mac settings for mininet"
for host_name, host_obj in node_mgr.test_host_index.items():
print "- host: %s" % host_name
for port_name, port_obj in host_obj.port_index.items():
print " - port:%s, mac:%s, ip:%s" % (
port_obj.name, port_obj.mac_addr, port_obj.ip_addr
)
print "# dispatcher list and its dpid"
for dispatcher_name, dispatcher_obj in node_mgr.dispatcher_index.items():
print "- dispatcher: %s, dpid: %d" % (dispatcher_name, dispatcher_obj.datapath_id)