1
|
|
|
"""Main module of kytos/topology Kytos Network Application. |
2
|
|
|
|
3
|
|
|
Manage the network topology |
4
|
|
|
""" |
5
|
1 |
|
import time |
6
|
|
|
|
7
|
1 |
|
from flask import jsonify, request |
8
|
|
|
|
9
|
1 |
|
from kytos.core import KytosEvent, KytosNApp, log, rest |
10
|
1 |
|
from kytos.core.exceptions import KytosLinkCreationError |
11
|
1 |
|
from kytos.core.helpers import listen_to |
12
|
1 |
|
from kytos.core.interface import Interface |
13
|
1 |
|
from kytos.core.link import Link |
14
|
1 |
|
from kytos.core.switch import Switch |
15
|
1 |
|
from napps.kytos.topology import settings |
16
|
1 |
|
from napps.kytos.topology.exceptions import RestoreError |
17
|
1 |
|
from napps.kytos.topology.models import Topology |
18
|
1 |
|
from napps.kytos.topology.storehouse import StoreHouse |
19
|
|
|
|
20
|
1 |
|
DEFAULT_LINK_UP_TIMER = 10 |
21
|
1 |
|
DEFAULT_INTERFACE_RESTORE_TIMER = 2 |
22
|
1 |
|
RESTORE_INTERFACE_ATTEMPTS = 20 |
23
|
|
|
|
24
|
|
|
|
25
|
1 |
|
class Main(KytosNApp): # pylint: disable=too-many-public-methods |
26
|
|
|
"""Main class of kytos/topology NApp. |
27
|
|
|
|
28
|
|
|
This class is the entry point for this napp. |
29
|
|
|
""" |
30
|
|
|
|
31
|
1 |
|
def setup(self): |
32
|
|
|
"""Initialize the NApp's links list.""" |
33
|
1 |
|
self.links = {} |
34
|
1 |
|
self.store_items = {} |
35
|
1 |
|
self.switches_state = {} |
36
|
1 |
|
self.interfaces_state = {} |
37
|
1 |
|
self.links_state = {} |
38
|
1 |
|
self._verified_links = [] |
39
|
1 |
|
self.link_up_timer = getattr(settings, 'LINK_UP_TIMER', |
40
|
|
|
DEFAULT_LINK_UP_TIMER) |
41
|
1 |
|
self.interface_restore = getattr(settings, 'INTERFACE_RESTORE_TIMER', |
42
|
|
|
DEFAULT_INTERFACE_RESTORE_TIMER) |
43
|
|
|
|
44
|
1 |
|
self.verify_storehouse('switches') |
45
|
1 |
|
self.verify_storehouse('interfaces') |
46
|
1 |
|
self.verify_storehouse('links') |
47
|
|
|
|
48
|
1 |
|
self.storehouse = StoreHouse(self.controller) |
49
|
|
|
|
50
|
1 |
|
def execute(self): |
51
|
|
|
"""Execute once when the napp is running.""" |
52
|
|
|
self._load_network_status() |
53
|
|
|
|
54
|
1 |
|
def shutdown(self): |
55
|
|
|
"""Do nothing.""" |
56
|
|
|
log.info('NApp kytos/topology shutting down.') |
57
|
|
|
|
58
|
1 |
|
def _get_link_or_create(self, endpoint_a, endpoint_b): |
59
|
1 |
|
new_link = Link(endpoint_a, endpoint_b) |
60
|
|
|
|
61
|
1 |
|
for link in self.links.values(): |
62
|
|
|
if new_link == link: |
63
|
|
|
return link |
64
|
|
|
|
65
|
1 |
|
self.links[new_link.id] = new_link |
66
|
1 |
|
return new_link |
67
|
|
|
|
68
|
1 |
|
def _get_switches_dict(self): |
69
|
|
|
"""Return a dictionary with the known switches.""" |
70
|
1 |
|
switches = {'switches': {}} |
71
|
1 |
|
for idx, switch in enumerate(self.controller.switches.values()): |
72
|
1 |
|
switch_data = switch.as_dict() |
73
|
1 |
|
if not all(key in switch_data['metadata'] |
74
|
|
|
for key in ('lat', 'lng')): |
75
|
|
|
# Switches are initialized somewhere in the ocean |
76
|
|
|
switch_data['metadata']['lat'] = str(0.0) |
77
|
|
|
switch_data['metadata']['lng'] = str(-30.0+idx*10.0) |
78
|
1 |
|
switches['switches'][switch.id] = switch_data |
79
|
1 |
|
return switches |
80
|
|
|
|
81
|
1 |
|
def _get_links_dict(self): |
82
|
|
|
"""Return a dictionary with the known links.""" |
83
|
1 |
|
return {'links': {l.id: l.as_dict() for l in |
84
|
|
|
self.links.values()}} |
85
|
|
|
|
86
|
1 |
|
def _get_topology_dict(self): |
87
|
|
|
"""Return a dictionary with the known topology.""" |
88
|
1 |
|
return {'topology': {**self._get_switches_dict(), |
89
|
|
|
**self._get_links_dict()}} |
90
|
|
|
|
91
|
1 |
|
def _get_topology(self): |
92
|
|
|
"""Return an object representing the topology.""" |
93
|
1 |
|
return Topology(self.controller.switches, self.links) |
94
|
|
|
|
95
|
1 |
|
def _get_link_from_interface(self, interface): |
96
|
|
|
"""Return the link of the interface, or None if it does not exist.""" |
97
|
1 |
|
for link in self.links.values(): |
98
|
1 |
|
if interface in (link.endpoint_a, link.endpoint_b): |
99
|
1 |
|
return link |
100
|
1 |
|
return None |
101
|
|
|
|
102
|
1 |
|
def _restore_link(self, link_id): |
103
|
|
|
"""Restore link's administrative state from storehouse.""" |
104
|
1 |
|
try: |
105
|
1 |
|
state = self.links_state[link_id] |
106
|
1 |
|
except KeyError: |
107
|
1 |
|
error = (f'The link {link_id} has no stored ' |
108
|
|
|
'administrative state to be restored.') |
109
|
1 |
|
raise RestoreError(error) |
110
|
|
|
|
111
|
1 |
|
try: |
112
|
1 |
|
link = self.links[link_id] |
113
|
1 |
|
if state['enabled']: |
114
|
1 |
|
link.enable() |
115
|
|
|
else: |
116
|
1 |
|
link.disable() |
117
|
1 |
|
except KeyError: |
118
|
1 |
|
error = ('Error restoring link status.' |
119
|
|
|
f'The link {link_id} does not exist.') |
120
|
1 |
|
raise RestoreError(error) |
121
|
1 |
|
log.info(f'The state of link {link.id} has been restored.') |
122
|
1 |
|
self.notify_topology_update() |
123
|
1 |
|
self.update_instance_metadata(link) |
124
|
|
|
|
125
|
1 |
|
def _restore_switch(self, switch_id): |
126
|
|
|
"""Restore switch's administrative state from storehouse.""" |
127
|
1 |
|
try: |
128
|
1 |
|
state = self.switches_state[switch_id] |
129
|
1 |
|
except KeyError: |
130
|
1 |
|
error = (f'The switch {switch_id} has no stored' |
131
|
|
|
' administrative state to be restored.') |
132
|
1 |
|
raise RestoreError(error) |
133
|
|
|
|
134
|
1 |
|
try: |
135
|
1 |
|
switch = self.controller.switches[switch_id] |
136
|
1 |
|
except KeyError: |
137
|
|
|
# Maybe we should remove the switch from switches_state here |
138
|
1 |
|
error = ('Error while restoring switches status. The ' |
139
|
|
|
f'switch {switch_id} does not exist.') |
140
|
1 |
|
raise RestoreError(error) |
141
|
|
|
|
142
|
1 |
|
if state: |
143
|
1 |
|
switch.enable() |
144
|
1 |
|
self.notify_switch_enabled(switch_id) |
145
|
|
|
else: |
146
|
1 |
|
switch.disable() |
147
|
1 |
|
self.notify_switch_disabled(switch_id) |
148
|
|
|
|
149
|
1 |
|
log.debug('Waiting to restore administrative state of switch ' |
150
|
|
|
f'{switch_id} interfaces.') |
151
|
1 |
|
i = 0 |
152
|
|
|
# wait to restore interfaces |
153
|
1 |
|
while not switch.interfaces and i < RESTORE_INTERFACE_ATTEMPTS: |
154
|
|
|
time.sleep(self.interface_restore) |
155
|
|
|
i += 1 |
156
|
1 |
|
if not switch.interfaces: |
157
|
|
|
error = ('Error restoring administrative state of switch ' |
158
|
|
|
f'{switch_id} interfaces.') |
159
|
|
|
raise RestoreError(error) |
160
|
|
|
|
161
|
|
|
# restore interfaces |
162
|
1 |
|
for interface_id in switch.interfaces: |
163
|
1 |
|
iface_id = ":".join([switch_id, str(interface_id)]) |
164
|
|
|
# restore only the administrative state of saved interfaces |
165
|
1 |
|
if iface_id not in self.interfaces_state: |
166
|
|
|
error = ("The stored topology is different from the current " |
167
|
|
|
f"topology. The interface {iface_id} hasn't been " |
168
|
|
|
"stored.") |
169
|
|
|
log.info(error) |
170
|
|
|
continue |
171
|
1 |
|
state = self.interfaces_state[iface_id] |
172
|
1 |
|
iface_number = int(interface_id) |
173
|
1 |
|
iface_status, lldp_status = state |
174
|
1 |
|
try: |
175
|
1 |
|
interface = switch.interfaces[iface_number] |
176
|
|
|
except KeyError: |
177
|
|
|
log.error('Error restoring interface status: ' |
178
|
|
|
'%s does not exist.', iface_id) |
179
|
|
|
continue |
180
|
|
|
|
181
|
1 |
|
if iface_status: |
182
|
1 |
|
interface.enable() |
183
|
|
|
else: |
184
|
1 |
|
interface.disable() |
185
|
1 |
|
interface.lldp = lldp_status |
186
|
1 |
|
self.update_instance_metadata(interface) |
187
|
|
|
|
188
|
1 |
|
log.info(f'The state of switch {switch_id} has been restored.') |
189
|
|
|
|
190
|
|
|
# pylint: disable=attribute-defined-outside-init |
191
|
1 |
|
def _load_network_status(self): |
192
|
|
|
"""Load network status saved in storehouse.""" |
193
|
1 |
|
try: |
194
|
1 |
|
status = self.storehouse.get_data() |
195
|
|
|
except FileNotFoundError as error: |
196
|
|
|
log.info(error) |
197
|
|
|
return |
198
|
1 |
|
if status: |
199
|
1 |
|
switches = status['network_status']['switches'] |
200
|
1 |
|
self.links_state = status['network_status']['links'] |
201
|
|
|
|
202
|
1 |
|
for switch_id, switch_att in switches.items(): |
203
|
|
|
# get switches status |
204
|
1 |
|
self.switches_state[switch_id] = switch_att['enabled'] |
205
|
1 |
|
iface = switch_att['interfaces'] |
206
|
|
|
# get interface status |
207
|
1 |
|
for iface_id, iface_att in iface.items(): |
208
|
1 |
|
enabled_value = iface_att['enabled'] |
209
|
1 |
|
lldp_value = iface_att['lldp'] |
210
|
1 |
|
self.interfaces_state[iface_id] = (enabled_value, |
211
|
|
|
lldp_value) |
212
|
|
|
|
213
|
|
|
else: |
214
|
|
|
error = 'There is no status saved to restore.' |
215
|
|
|
log.info(error) |
216
|
|
|
|
217
|
1 |
|
@rest('v3/') |
218
|
|
|
def get_topology(self): |
219
|
|
|
"""Return the latest known topology. |
220
|
|
|
|
221
|
|
|
This topology is updated when there are network events. |
222
|
|
|
""" |
223
|
1 |
|
return jsonify(self._get_topology_dict()) |
224
|
|
|
|
225
|
1 |
|
def restore_network_status(self, obj): |
226
|
|
|
"""Restore the network administrative status saved in storehouse.""" |
227
|
1 |
|
try: |
228
|
1 |
|
if isinstance(obj, Switch): |
229
|
1 |
|
self._restore_switch(obj.id) |
230
|
1 |
|
elif isinstance(obj, Link): |
231
|
1 |
|
if obj.id not in self._verified_links: |
232
|
1 |
|
self._verified_links.append(obj.id) |
233
|
1 |
|
self._restore_link(obj.id) |
234
|
|
|
except RestoreError as exc: |
235
|
|
|
log.debug(exc) |
236
|
|
|
|
237
|
|
|
# Switch related methods |
238
|
1 |
|
@rest('v3/switches') |
239
|
|
|
def get_switches(self): |
240
|
|
|
"""Return a json with all the switches in the topology.""" |
241
|
|
|
return jsonify(self._get_switches_dict()) |
242
|
|
|
|
243
|
1 |
|
@rest('v3/switches/<dpid>/enable', methods=['POST']) |
244
|
|
|
def enable_switch(self, dpid): |
245
|
|
|
"""Administratively enable a switch in the topology.""" |
246
|
1 |
|
try: |
247
|
1 |
|
self.controller.switches[dpid].enable() |
248
|
1 |
|
except KeyError: |
249
|
1 |
|
return jsonify("Switch not found"), 404 |
250
|
|
|
|
251
|
1 |
|
log.info(f"Storing administrative state from switch {dpid}" |
252
|
|
|
" to enabled.") |
253
|
1 |
|
self.save_status_on_storehouse() |
254
|
1 |
|
self.notify_switch_enabled(dpid) |
255
|
1 |
|
return jsonify("Operation successful"), 201 |
256
|
|
|
|
257
|
1 |
|
@rest('v3/switches/<dpid>/disable', methods=['POST']) |
258
|
|
|
def disable_switch(self, dpid): |
259
|
|
|
"""Administratively disable a switch in the topology.""" |
260
|
1 |
|
try: |
261
|
1 |
|
self.controller.switches[dpid].disable() |
262
|
1 |
|
except KeyError: |
263
|
1 |
|
return jsonify("Switch not found"), 404 |
264
|
|
|
|
265
|
1 |
|
log.info(f"Storing administrative state from switch {dpid}" |
266
|
|
|
" to disabled.") |
267
|
1 |
|
self.save_status_on_storehouse() |
268
|
1 |
|
self.notify_switch_disabled(dpid) |
269
|
1 |
|
return jsonify("Operation successful"), 201 |
270
|
|
|
|
271
|
1 |
|
@rest('v3/switches/<dpid>/metadata') |
272
|
|
|
def get_switch_metadata(self, dpid): |
273
|
|
|
"""Get metadata from a switch.""" |
274
|
1 |
|
try: |
275
|
1 |
|
return jsonify({"metadata": |
276
|
|
|
self.controller.switches[dpid].metadata}), 200 |
277
|
1 |
|
except KeyError: |
278
|
1 |
|
return jsonify("Switch not found"), 404 |
279
|
|
|
|
280
|
1 |
|
@rest('v3/switches/<dpid>/metadata', methods=['POST']) |
281
|
|
|
def add_switch_metadata(self, dpid): |
282
|
|
|
"""Add metadata to a switch.""" |
283
|
1 |
|
metadata = request.get_json() |
284
|
1 |
|
try: |
285
|
1 |
|
switch = self.controller.switches[dpid] |
286
|
1 |
|
except KeyError: |
287
|
1 |
|
return jsonify("Switch not found"), 404 |
288
|
|
|
|
289
|
1 |
|
switch.extend_metadata(metadata) |
290
|
1 |
|
self.notify_metadata_changes(switch, 'added') |
291
|
1 |
|
return jsonify("Operation successful"), 201 |
292
|
|
|
|
293
|
1 |
|
@rest('v3/switches/<dpid>/metadata/<key>', methods=['DELETE']) |
294
|
|
|
def delete_switch_metadata(self, dpid, key): |
295
|
|
|
"""Delete metadata from a switch.""" |
296
|
1 |
|
try: |
297
|
1 |
|
switch = self.controller.switches[dpid] |
298
|
1 |
|
except KeyError: |
299
|
1 |
|
return jsonify("Switch not found"), 404 |
300
|
|
|
|
301
|
1 |
|
switch.remove_metadata(key) |
302
|
1 |
|
self.notify_metadata_changes(switch, 'removed') |
303
|
1 |
|
return jsonify("Operation successful"), 200 |
304
|
|
|
|
305
|
|
|
# Interface related methods |
306
|
1 |
|
@rest('v3/interfaces') |
307
|
|
|
def get_interfaces(self): |
308
|
|
|
"""Return a json with all the interfaces in the topology.""" |
309
|
|
|
interfaces = {} |
310
|
|
|
switches = self._get_switches_dict() |
311
|
|
|
for switch in switches['switches'].values(): |
312
|
|
|
for interface_id, interface in switch['interfaces'].items(): |
313
|
|
|
interfaces[interface_id] = interface |
314
|
|
|
|
315
|
|
|
return jsonify({'interfaces': interfaces}) |
316
|
|
|
|
317
|
1 |
View Code Duplication |
@rest('v3/interfaces/switch/<dpid>/enable', methods=['POST']) |
|
|
|
|
318
|
1 |
|
@rest('v3/interfaces/<interface_enable_id>/enable', methods=['POST']) |
319
|
1 |
|
def enable_interface(self, interface_enable_id=None, dpid=None): |
320
|
|
|
"""Administratively enable interfaces in the topology.""" |
321
|
1 |
|
error_list = [] # List of interfaces that were not activated. |
322
|
1 |
|
msg_error = "Some interfaces couldn't be found and activated: " |
323
|
1 |
|
if dpid is None: |
324
|
1 |
|
dpid = ":".join(interface_enable_id.split(":")[:-1]) |
325
|
1 |
|
try: |
326
|
1 |
|
switch = self.controller.switches[dpid] |
327
|
1 |
|
except KeyError as exc: |
328
|
1 |
|
return jsonify(f"Switch not found: {exc}"), 404 |
329
|
|
|
|
330
|
1 |
|
if interface_enable_id: |
331
|
1 |
|
interface_number = int(interface_enable_id.split(":")[-1]) |
332
|
|
|
|
333
|
1 |
|
try: |
334
|
1 |
|
switch.interfaces[interface_number].enable() |
335
|
1 |
|
except KeyError as exc: |
336
|
1 |
|
error_list.append(f"Switch {dpid} Interface {exc}") |
337
|
|
|
else: |
338
|
1 |
|
for interface in switch.interfaces.values(): |
339
|
1 |
|
interface.enable() |
340
|
1 |
|
if not error_list: |
341
|
1 |
|
log.info(f"Storing administrative state for enabled interfaces.") |
342
|
1 |
|
self.save_status_on_storehouse() |
343
|
1 |
|
return jsonify("Operation successful"), 200 |
344
|
1 |
|
return jsonify({msg_error: |
345
|
|
|
error_list}), 409 |
346
|
|
|
|
347
|
1 |
View Code Duplication |
@rest('v3/interfaces/switch/<dpid>/disable', methods=['POST']) |
|
|
|
|
348
|
1 |
|
@rest('v3/interfaces/<interface_disable_id>/disable', methods=['POST']) |
349
|
1 |
|
def disable_interface(self, interface_disable_id=None, dpid=None): |
350
|
|
|
"""Administratively disable interfaces in the topology.""" |
351
|
1 |
|
error_list = [] # List of interfaces that were not deactivated. |
352
|
1 |
|
msg_error = "Some interfaces couldn't be found and deactivated: " |
353
|
1 |
|
if dpid is None: |
354
|
1 |
|
dpid = ":".join(interface_disable_id.split(":")[:-1]) |
355
|
1 |
|
try: |
356
|
1 |
|
switch = self.controller.switches[dpid] |
357
|
1 |
|
except KeyError as exc: |
358
|
1 |
|
return jsonify(f"Switch not found: {exc}"), 404 |
359
|
|
|
|
360
|
1 |
|
if interface_disable_id: |
361
|
1 |
|
interface_number = int(interface_disable_id.split(":")[-1]) |
362
|
|
|
|
363
|
1 |
|
try: |
364
|
1 |
|
switch.interfaces[interface_number].disable() |
365
|
1 |
|
except KeyError as exc: |
366
|
1 |
|
error_list.append(f"Switch {dpid} Interface {exc}") |
367
|
|
|
else: |
368
|
1 |
|
for interface in switch.interfaces.values(): |
369
|
1 |
|
interface.disable() |
370
|
1 |
|
if not error_list: |
371
|
1 |
|
log.info(f"Storing administrative state for disabled interfaces.") |
372
|
1 |
|
self.save_status_on_storehouse() |
373
|
1 |
|
return jsonify("Operation successful"), 200 |
374
|
1 |
|
return jsonify({msg_error: |
375
|
|
|
error_list}), 409 |
376
|
|
|
|
377
|
1 |
|
@rest('v3/interfaces/<interface_id>/metadata') |
378
|
|
|
def get_interface_metadata(self, interface_id): |
379
|
|
|
"""Get metadata from an interface.""" |
380
|
1 |
|
switch_id = ":".join(interface_id.split(":")[:-1]) |
381
|
1 |
|
interface_number = int(interface_id.split(":")[-1]) |
382
|
1 |
|
try: |
383
|
1 |
|
switch = self.controller.switches[switch_id] |
384
|
1 |
|
except KeyError: |
385
|
1 |
|
return jsonify("Switch not found"), 404 |
386
|
|
|
|
387
|
1 |
|
try: |
388
|
1 |
|
interface = switch.interfaces[interface_number] |
389
|
1 |
|
except KeyError: |
390
|
1 |
|
return jsonify("Interface not found"), 404 |
391
|
|
|
|
392
|
1 |
|
return jsonify({"metadata": interface.metadata}), 200 |
393
|
|
|
|
394
|
1 |
|
@rest('v3/interfaces/<interface_id>/metadata', methods=['POST']) |
395
|
|
|
def add_interface_metadata(self, interface_id): |
396
|
|
|
"""Add metadata to an interface.""" |
397
|
1 |
|
metadata = request.get_json() |
398
|
|
|
|
399
|
1 |
|
switch_id = ":".join(interface_id.split(":")[:-1]) |
400
|
1 |
|
interface_number = int(interface_id.split(":")[-1]) |
401
|
1 |
|
try: |
402
|
1 |
|
switch = self.controller.switches[switch_id] |
403
|
1 |
|
except KeyError: |
404
|
1 |
|
return jsonify("Switch not found"), 404 |
405
|
|
|
|
406
|
1 |
|
try: |
407
|
1 |
|
interface = switch.interfaces[interface_number] |
408
|
1 |
|
except KeyError: |
409
|
1 |
|
return jsonify("Interface not found"), 404 |
410
|
|
|
|
411
|
1 |
|
interface.extend_metadata(metadata) |
412
|
1 |
|
self.notify_metadata_changes(interface, 'added') |
413
|
1 |
|
return jsonify("Operation successful"), 201 |
414
|
|
|
|
415
|
1 |
|
@rest('v3/interfaces/<interface_id>/metadata/<key>', methods=['DELETE']) |
416
|
|
|
def delete_interface_metadata(self, interface_id, key): |
417
|
|
|
"""Delete metadata from an interface.""" |
418
|
1 |
|
switch_id = ":".join(interface_id.split(":")[:-1]) |
419
|
1 |
|
interface_number = int(interface_id.split(":")[-1]) |
420
|
|
|
|
421
|
1 |
|
try: |
422
|
1 |
|
switch = self.controller.switches[switch_id] |
423
|
1 |
|
except KeyError: |
424
|
1 |
|
return jsonify("Switch not found"), 404 |
425
|
|
|
|
426
|
1 |
|
try: |
427
|
1 |
|
interface = switch.interfaces[interface_number] |
428
|
1 |
|
except KeyError: |
429
|
1 |
|
return jsonify("Interface not found"), 404 |
430
|
|
|
|
431
|
1 |
|
if interface.remove_metadata(key) is False: |
432
|
1 |
|
return jsonify("Metadata not found"), 404 |
433
|
|
|
|
434
|
1 |
|
self.notify_metadata_changes(interface, 'removed') |
435
|
1 |
|
return jsonify("Operation successful"), 200 |
436
|
|
|
|
437
|
|
|
# Link related methods |
438
|
1 |
|
@rest('v3/links') |
439
|
|
|
def get_links(self): |
440
|
|
|
"""Return a json with all the links in the topology. |
441
|
|
|
|
442
|
|
|
Links are connections between interfaces. |
443
|
|
|
""" |
444
|
|
|
return jsonify(self._get_links_dict()), 200 |
445
|
|
|
|
446
|
1 |
|
@rest('v3/links/<link_id>/enable', methods=['POST']) |
447
|
|
|
def enable_link(self, link_id): |
448
|
|
|
"""Administratively enable a link in the topology.""" |
449
|
1 |
|
try: |
450
|
1 |
|
self.links[link_id].enable() |
451
|
1 |
|
except KeyError: |
452
|
1 |
|
return jsonify("Link not found"), 404 |
453
|
1 |
|
self.save_status_on_storehouse() |
454
|
1 |
|
return jsonify("Operation successful"), 201 |
455
|
|
|
|
456
|
1 |
|
@rest('v3/links/<link_id>/disable', methods=['POST']) |
457
|
|
|
def disable_link(self, link_id): |
458
|
|
|
"""Administratively disable a link in the topology.""" |
459
|
1 |
|
try: |
460
|
1 |
|
self.links[link_id].disable() |
461
|
1 |
|
except KeyError: |
462
|
1 |
|
return jsonify("Link not found"), 404 |
463
|
1 |
|
self.save_status_on_storehouse() |
464
|
1 |
|
return jsonify("Operation successful"), 201 |
465
|
|
|
|
466
|
1 |
|
@rest('v3/links/<link_id>/metadata') |
467
|
|
|
def get_link_metadata(self, link_id): |
468
|
|
|
"""Get metadata from a link.""" |
469
|
1 |
|
try: |
470
|
1 |
|
return jsonify({"metadata": self.links[link_id].metadata}), 200 |
471
|
1 |
|
except KeyError: |
472
|
1 |
|
return jsonify("Link not found"), 404 |
473
|
|
|
|
474
|
1 |
|
@rest('v3/links/<link_id>/metadata', methods=['POST']) |
475
|
|
|
def add_link_metadata(self, link_id): |
476
|
|
|
"""Add metadata to a link.""" |
477
|
1 |
|
metadata = request.get_json() |
478
|
1 |
|
try: |
479
|
1 |
|
link = self.links[link_id] |
480
|
1 |
|
except KeyError: |
481
|
1 |
|
return jsonify("Link not found"), 404 |
482
|
|
|
|
483
|
1 |
|
link.extend_metadata(metadata) |
484
|
1 |
|
self.notify_metadata_changes(link, 'added') |
485
|
1 |
|
return jsonify("Operation successful"), 201 |
486
|
|
|
|
487
|
1 |
|
@rest('v3/links/<link_id>/metadata/<key>', methods=['DELETE']) |
488
|
|
|
def delete_link_metadata(self, link_id, key): |
489
|
|
|
"""Delete metadata from a link.""" |
490
|
1 |
|
try: |
491
|
1 |
|
link = self.links[link_id] |
492
|
1 |
|
except KeyError: |
493
|
1 |
|
return jsonify("Link not found"), 404 |
494
|
|
|
|
495
|
1 |
|
if link.remove_metadata(key) is False: |
496
|
1 |
|
return jsonify("Metadata not found"), 404 |
497
|
|
|
|
498
|
1 |
|
self.notify_metadata_changes(link, 'removed') |
499
|
1 |
|
return jsonify("Operation successful"), 200 |
500
|
|
|
|
501
|
1 |
|
@listen_to('.*.switch.(new|reconnected)') |
502
|
|
|
def handle_new_switch(self, event): |
503
|
|
|
"""Create a new Device on the Topology. |
504
|
|
|
|
505
|
|
|
Handle the event of a new created switch and update the topology with |
506
|
|
|
this new device. |
507
|
|
|
""" |
508
|
1 |
|
switch = event.content['switch'] |
509
|
1 |
|
switch.activate() |
510
|
1 |
|
log.debug('Switch %s added to the Topology.', switch.id) |
511
|
1 |
|
self.notify_topology_update() |
512
|
1 |
|
self.update_instance_metadata(switch) |
513
|
1 |
|
self.restore_network_status(switch) |
514
|
|
|
|
515
|
1 |
|
@listen_to('.*.connection.lost') |
516
|
|
|
def handle_connection_lost(self, event): |
517
|
|
|
"""Remove a Device from the topology. |
518
|
|
|
|
519
|
|
|
Remove the disconnected Device and every link that has one of its |
520
|
|
|
interfaces. |
521
|
|
|
""" |
522
|
1 |
|
switch = event.content['source'].switch |
523
|
1 |
|
if switch: |
524
|
1 |
|
switch.deactivate() |
525
|
1 |
|
log.debug('Switch %s removed from the Topology.', switch.id) |
526
|
1 |
|
self.notify_topology_update() |
527
|
|
|
|
528
|
1 |
|
def handle_interface_up(self, event): |
529
|
|
|
"""Update the topology based on a Port Modify event. |
530
|
|
|
|
531
|
|
|
The event notifies that an interface was changed to 'up'. |
532
|
|
|
""" |
533
|
1 |
|
interface = event.content['interface'] |
534
|
1 |
|
interface.activate() |
535
|
1 |
|
self.notify_topology_update() |
536
|
1 |
|
self.update_instance_metadata(interface) |
537
|
|
|
|
538
|
1 |
|
@listen_to('.*.switch.interface.created') |
539
|
|
|
def handle_interface_created(self, event): |
540
|
|
|
"""Update the topology based on a Port Create event.""" |
541
|
1 |
|
self.handle_interface_up(event) |
542
|
|
|
|
543
|
1 |
|
def handle_interface_down(self, event): |
544
|
|
|
"""Update the topology based on a Port Modify event. |
545
|
|
|
|
546
|
|
|
The event notifies that an interface was changed to 'down'. |
547
|
|
|
""" |
548
|
1 |
|
interface = event.content['interface'] |
549
|
1 |
|
interface.deactivate() |
550
|
1 |
|
self.handle_interface_link_down(event) |
551
|
1 |
|
self.notify_topology_update() |
552
|
|
|
|
553
|
1 |
|
@listen_to('.*.switch.interface.deleted') |
554
|
|
|
def handle_interface_deleted(self, event): |
555
|
|
|
"""Update the topology based on a Port Delete event.""" |
556
|
1 |
|
self.handle_interface_down(event) |
557
|
|
|
|
558
|
1 |
|
@listen_to('.*.switch.interface.link_up') |
559
|
|
|
def handle_interface_link_up(self, event): |
560
|
|
|
"""Update the topology based on a Port Modify event. |
561
|
|
|
|
562
|
|
|
The event notifies that an interface's link was changed to 'up'. |
563
|
|
|
""" |
564
|
1 |
|
interface = event.content['interface'] |
565
|
1 |
|
self.handle_link_up(interface) |
566
|
|
|
|
567
|
1 |
|
@listen_to('kytos/maintenance.end_switch') |
568
|
|
|
def handle_switch_maintenance_end(self, event): |
569
|
|
|
"""Handle the end of the maintenance of a switch.""" |
570
|
1 |
|
switches = event.content['switches'] |
571
|
1 |
|
for switch in switches: |
572
|
1 |
|
switch.enable() |
573
|
1 |
|
switch.activate() |
574
|
1 |
|
for interface in switch.interfaces.values(): |
575
|
1 |
|
interface.enable() |
576
|
1 |
|
self.handle_link_up(interface) |
577
|
|
|
|
578
|
1 |
|
def handle_link_up(self, interface): |
579
|
|
|
"""Notify a link is up.""" |
580
|
1 |
|
link = self._get_link_from_interface(interface) |
581
|
1 |
|
if not link: |
582
|
|
|
return |
583
|
1 |
|
if link.endpoint_a == interface: |
584
|
1 |
|
other_interface = link.endpoint_b |
585
|
|
|
else: |
586
|
|
|
other_interface = link.endpoint_a |
587
|
1 |
|
interface.activate() |
588
|
1 |
|
if other_interface.is_active() is False: |
589
|
|
|
return |
590
|
1 |
|
if link.is_active() is False: |
591
|
1 |
|
link.update_metadata('last_status_change', time.time()) |
592
|
1 |
|
link.activate() |
593
|
|
|
|
594
|
|
|
# As each run of this method uses a different thread, |
595
|
|
|
# there is no risk this sleep will lock the NApp. |
596
|
1 |
|
time.sleep(self.link_up_timer) |
597
|
|
|
|
598
|
1 |
|
last_status_change = link.get_metadata('last_status_change') |
599
|
1 |
|
now = time.time() |
600
|
1 |
|
if link.is_active() and \ |
601
|
|
|
now - last_status_change >= self.link_up_timer: |
602
|
1 |
|
self.notify_topology_update() |
603
|
1 |
|
self.update_instance_metadata(link) |
604
|
1 |
|
self.notify_link_status_change(link) |
605
|
|
|
|
606
|
1 |
|
@listen_to('.*.switch.interface.link_down') |
607
|
|
|
def handle_interface_link_down(self, event): |
608
|
|
|
"""Update the topology based on a Port Modify event. |
609
|
|
|
|
610
|
|
|
The event notifies that an interface's link was changed to 'down'. |
611
|
|
|
""" |
612
|
1 |
|
interface = event.content['interface'] |
613
|
1 |
|
self.handle_link_down(interface) |
614
|
|
|
|
615
|
1 |
|
@listen_to('kytos/maintenance.start_switch') |
616
|
|
|
def handle_switch_maintenance_start(self, event): |
617
|
|
|
"""Handle the start of the maintenance of a switch.""" |
618
|
1 |
|
switches = event.content['switches'] |
619
|
1 |
|
for switch in switches: |
620
|
1 |
|
switch.disable() |
621
|
1 |
|
switch.deactivate() |
622
|
1 |
|
for interface in switch.interfaces.values(): |
623
|
1 |
|
interface.disable() |
624
|
1 |
|
if interface.is_active(): |
625
|
1 |
|
self.handle_link_down(interface) |
626
|
|
|
|
627
|
1 |
|
def handle_link_down(self, interface): |
628
|
|
|
"""Notify a link is down.""" |
629
|
1 |
|
link = self._get_link_from_interface(interface) |
630
|
1 |
|
if link and link.is_active(): |
631
|
1 |
|
link.deactivate() |
632
|
1 |
|
link.update_metadata('last_status_change', time.time()) |
633
|
1 |
|
self.notify_topology_update() |
634
|
1 |
|
self.notify_link_status_change(link) |
635
|
|
|
|
636
|
1 |
|
@listen_to('.*.interface.is.nni') |
637
|
|
|
def add_links(self, event): |
638
|
|
|
"""Update the topology with links related to the NNI interfaces.""" |
639
|
1 |
|
interface_a = event.content['interface_a'] |
640
|
1 |
|
interface_b = event.content['interface_b'] |
641
|
|
|
|
642
|
1 |
|
try: |
643
|
1 |
|
link = self._get_link_or_create(interface_a, interface_b) |
644
|
|
|
except KytosLinkCreationError as err: |
645
|
|
|
log.error(f'Error creating link: {err}.') |
646
|
|
|
return |
647
|
|
|
|
648
|
1 |
|
interface_a.update_link(link) |
649
|
1 |
|
interface_b.update_link(link) |
650
|
|
|
|
651
|
1 |
|
interface_a.nni = True |
652
|
1 |
|
interface_b.nni = True |
653
|
|
|
|
654
|
1 |
|
self.notify_topology_update() |
655
|
1 |
|
self.restore_network_status(link) |
656
|
|
|
|
657
|
|
|
# def add_host(self, event): |
658
|
|
|
# """Update the topology with a new Host.""" |
659
|
|
|
|
660
|
|
|
# interface = event.content['port'] |
661
|
|
|
# mac = event.content['reachable_mac'] |
662
|
|
|
|
663
|
|
|
# host = Host(mac) |
664
|
|
|
# link = self.topology.get_link(interface.id) |
665
|
|
|
# if link is not None: |
666
|
|
|
# return |
667
|
|
|
|
668
|
|
|
# self.topology.add_link(interface.id, host.id) |
669
|
|
|
# self.topology.add_device(host) |
670
|
|
|
|
671
|
|
|
# if settings.DISPLAY_FULL_DUPLEX_LINKS: |
672
|
|
|
# self.topology.add_link(host.id, interface.id) |
673
|
|
|
|
674
|
|
|
# pylint: disable=unused-argument |
675
|
1 |
|
@listen_to('.*.network_status.updated') |
676
|
1 |
|
def save_status_on_storehouse(self, event=None): |
677
|
|
|
"""Save the network administrative status using storehouse.""" |
678
|
1 |
|
status = self._get_switches_dict() |
679
|
1 |
|
status['id'] = 'network_status' |
680
|
1 |
|
if event: |
681
|
|
|
content = event.content |
682
|
|
|
log.info(f"Storing the administrative state of the" |
683
|
|
|
f" {content['attribute']} attribute to" |
684
|
|
|
f" {content['state']} in the interfaces" |
685
|
|
|
f" {content['interface_ids']}") |
686
|
1 |
|
status.update(self._get_links_dict()) |
687
|
1 |
|
self.storehouse.save_status(status) |
688
|
|
|
|
689
|
1 |
|
def notify_switch_enabled(self, dpid): |
690
|
|
|
"""Send an event to notify that a switch is enabled.""" |
691
|
1 |
|
name = 'kytos/topology.switch.enabled' |
692
|
1 |
|
event = KytosEvent(name=name, content={'dpid': dpid}) |
693
|
1 |
|
self.controller.buffers.app.put(event) |
694
|
|
|
|
695
|
1 |
|
def notify_switch_disabled(self, dpid): |
696
|
|
|
"""Send an event to notify that a switch is disabled.""" |
697
|
1 |
|
name = 'kytos/topology.switch.disabled' |
698
|
1 |
|
event = KytosEvent(name=name, content={'dpid': dpid}) |
699
|
1 |
|
self.controller.buffers.app.put(event) |
700
|
|
|
|
701
|
1 |
|
def notify_topology_update(self): |
702
|
|
|
"""Send an event to notify about updates on the topology.""" |
703
|
1 |
|
name = 'kytos/topology.updated' |
704
|
1 |
|
event = KytosEvent(name=name, content={'topology': |
705
|
|
|
self._get_topology()}) |
706
|
1 |
|
self.controller.buffers.app.put(event) |
707
|
|
|
|
708
|
1 |
|
def notify_link_status_change(self, link): |
709
|
|
|
"""Send an event to notify about a status change on a link.""" |
710
|
1 |
|
name = 'kytos/topology.' |
711
|
1 |
|
if link.is_active(): |
712
|
1 |
|
status = 'link_up' |
713
|
|
|
else: |
714
|
|
|
status = 'link_down' |
715
|
1 |
|
event = KytosEvent(name=name+status, content={'link': link}) |
716
|
1 |
|
self.controller.buffers.app.put(event) |
717
|
|
|
|
718
|
1 |
|
def notify_metadata_changes(self, obj, action): |
719
|
|
|
"""Send an event to notify about metadata changes.""" |
720
|
1 |
|
if isinstance(obj, Switch): |
721
|
1 |
|
entity = 'switch' |
722
|
1 |
|
entities = 'switches' |
723
|
1 |
|
elif isinstance(obj, Interface): |
724
|
1 |
|
entity = 'interface' |
725
|
1 |
|
entities = 'interfaces' |
726
|
|
|
elif isinstance(obj, Link): |
727
|
|
|
entity = 'link' |
728
|
|
|
entities = 'links' |
729
|
|
|
|
730
|
1 |
|
name = f'kytos/topology.{entities}.metadata.{action}' |
731
|
1 |
|
event = KytosEvent(name=name, content={entity: obj, |
|
|
|
|
732
|
|
|
'metadata': obj.metadata}) |
733
|
1 |
|
self.controller.buffers.app.put(event) |
734
|
1 |
|
log.debug(f'Metadata from {obj.id} was {action}.') |
735
|
|
|
|
736
|
1 |
|
@listen_to('.*.switch.port.created') |
737
|
|
|
def notify_port_created(self, original_event): |
738
|
|
|
"""Notify when a port is created.""" |
739
|
1 |
|
name = 'kytos/topology.port.created' |
740
|
1 |
|
event = KytosEvent(name=name, content=original_event.content) |
741
|
1 |
|
self.controller.buffers.app.put(event) |
742
|
|
|
|
743
|
1 |
|
@listen_to('kytos/topology.*.metadata.*') |
744
|
|
|
def save_metadata_on_store(self, event): |
745
|
|
|
"""Send to storehouse the data updated.""" |
746
|
1 |
|
name = 'kytos.storehouse.update' |
747
|
1 |
|
if 'switch' in event.content: |
748
|
1 |
|
store = self.store_items.get('switches') |
749
|
1 |
|
obj = event.content.get('switch') |
750
|
1 |
|
namespace = 'kytos.topology.switches.metadata' |
751
|
1 |
|
elif 'interface' in event.content: |
752
|
1 |
|
store = self.store_items.get('interfaces') |
753
|
1 |
|
obj = event.content.get('interface') |
754
|
1 |
|
namespace = 'kytos.topology.interfaces.metadata' |
755
|
1 |
|
elif 'link' in event.content: |
756
|
1 |
|
store = self.store_items.get('links') |
757
|
1 |
|
obj = event.content.get('link') |
758
|
1 |
|
namespace = 'kytos.topology.links.metadata' |
759
|
|
|
|
760
|
1 |
|
store.data[obj.id] = obj.metadata |
|
|
|
|
761
|
1 |
|
content = {'namespace': namespace, |
|
|
|
|
762
|
|
|
'box_id': store.box_id, |
763
|
|
|
'data': store.data, |
764
|
|
|
'callback': self.update_instance} |
765
|
|
|
|
766
|
1 |
|
event = KytosEvent(name=name, content=content) |
767
|
1 |
|
self.controller.buffers.app.put(event) |
768
|
|
|
|
769
|
1 |
|
@staticmethod |
770
|
|
|
def update_instance(event, _data, error): |
771
|
|
|
"""Display in Kytos console if the data was updated.""" |
772
|
|
|
entities = event.content.get('namespace', '').split('.')[-2] |
773
|
|
|
if error: |
774
|
|
|
log.error(f'Error trying to update storehouse {entities}.') |
775
|
|
|
else: |
776
|
|
|
log.debug(f'Storehouse update to entities: {entities}.') |
777
|
|
|
|
778
|
1 |
|
def verify_storehouse(self, entities): |
779
|
|
|
"""Request a list of box saved by specific entity.""" |
780
|
1 |
|
name = 'kytos.storehouse.list' |
781
|
1 |
|
content = {'namespace': f'kytos.topology.{entities}.metadata', |
782
|
|
|
'callback': self.request_retrieve_entities} |
783
|
1 |
|
event = KytosEvent(name=name, content=content) |
784
|
1 |
|
self.controller.buffers.app.put(event) |
785
|
1 |
|
log.info(f'verify data in storehouse for {entities}.') |
786
|
|
|
|
787
|
1 |
|
def request_retrieve_entities(self, event, data, _error): |
788
|
|
|
"""Create a box or retrieve an existent box from storehouse.""" |
789
|
1 |
|
msg = '' |
790
|
1 |
|
content = {'namespace': event.content.get('namespace'), |
791
|
|
|
'callback': self.load_from_store, |
792
|
|
|
'data': {}} |
793
|
|
|
|
794
|
1 |
|
if not data: |
795
|
1 |
|
name = 'kytos.storehouse.create' |
796
|
1 |
|
msg = 'Create new box in storehouse' |
797
|
|
|
else: |
798
|
1 |
|
name = 'kytos.storehouse.retrieve' |
799
|
1 |
|
content['box_id'] = data[0] |
800
|
1 |
|
msg = 'Retrieve data from storehouse.' |
801
|
|
|
|
802
|
1 |
|
event = KytosEvent(name=name, content=content) |
803
|
1 |
|
self.controller.buffers.app.put(event) |
804
|
1 |
|
log.debug(msg) |
805
|
|
|
|
806
|
1 |
|
def load_from_store(self, event, box, error): |
807
|
|
|
"""Save the data retrived from storehouse.""" |
808
|
|
|
entities = event.content.get('namespace', '').split('.')[-2] |
809
|
|
|
if error: |
810
|
|
|
log.error('Error while get a box from storehouse.') |
811
|
|
|
else: |
812
|
|
|
self.store_items[entities] = box |
813
|
|
|
log.debug('Data updated') |
814
|
|
|
|
815
|
1 |
|
def update_instance_metadata(self, obj): |
816
|
|
|
"""Update object instance with saved metadata.""" |
817
|
1 |
|
metadata = None |
818
|
1 |
|
if isinstance(obj, Interface): |
819
|
1 |
|
all_metadata = self.store_items.get('interfaces', None) |
820
|
1 |
|
if all_metadata: |
821
|
|
|
metadata = all_metadata.data.get(obj.id) |
822
|
1 |
|
elif isinstance(obj, Switch): |
823
|
|
|
all_metadata = self.store_items.get('switches', None) |
824
|
|
|
if all_metadata: |
825
|
|
|
metadata = all_metadata.data.get(obj.id) |
826
|
1 |
|
elif isinstance(obj, Link): |
827
|
1 |
|
all_metadata = self.store_items.get('links', None) |
828
|
1 |
|
if all_metadata: |
829
|
|
|
metadata = all_metadata.data.get(obj.id) |
830
|
|
|
|
831
|
1 |
|
if metadata: |
832
|
|
|
obj.extend_metadata(metadata) |
833
|
|
|
log.debug(f'Metadata to {obj.id} was updated') |
834
|
|
|
|
835
|
1 |
|
@listen_to('kytos/maintenance.start_link') |
836
|
|
|
def handle_link_maintenance_start(self, event): |
837
|
|
|
"""Deals with the start of links maintenance.""" |
838
|
1 |
|
notify_links = [] |
839
|
1 |
|
maintenance_links = event.content['links'] |
840
|
1 |
|
for maintenance_link in maintenance_links: |
841
|
1 |
|
try: |
842
|
1 |
|
link = self.links[maintenance_link.id] |
843
|
1 |
|
except KeyError: |
844
|
1 |
|
continue |
845
|
1 |
|
notify_links.append(link) |
846
|
1 |
|
for link in notify_links: |
847
|
1 |
|
link.disable() |
848
|
1 |
|
link.deactivate() |
849
|
1 |
|
link.endpoint_a.deactivate() |
850
|
1 |
|
link.endpoint_b.deactivate() |
851
|
1 |
|
link.endpoint_a.disable() |
852
|
1 |
|
link.endpoint_b.disable() |
853
|
1 |
|
self.notify_link_status_change(link) |
854
|
|
|
|
855
|
1 |
|
@listen_to('kytos/maintenance.end_link') |
856
|
|
|
def handle_link_maintenance_end(self, event): |
857
|
|
|
"""Deals with the end of links maintenance.""" |
858
|
1 |
|
notify_links = [] |
859
|
1 |
|
maintenance_links = event.content['links'] |
860
|
1 |
|
for maintenance_link in maintenance_links: |
861
|
1 |
|
try: |
862
|
1 |
|
link = self.links[maintenance_link.id] |
863
|
1 |
|
except KeyError: |
864
|
1 |
|
continue |
865
|
1 |
|
notify_links.append(link) |
866
|
1 |
|
for link in notify_links: |
867
|
1 |
|
link.enable() |
868
|
1 |
|
link.activate() |
869
|
1 |
|
link.endpoint_a.activate() |
870
|
1 |
|
link.endpoint_b.activate() |
871
|
1 |
|
link.endpoint_a.enable() |
872
|
1 |
|
link.endpoint_b.enable() |
873
|
|
|
self.notify_link_status_change(link) |
874
|
|
|
|