1
|
|
|
"""Main module of kytos/topology Kytos Network Application. |
2
|
|
|
|
3
|
|
|
Manage the network topology |
4
|
|
|
""" |
5
|
|
|
# pylint: disable=wrong-import-order |
6
|
|
|
|
7
|
1 |
|
import time |
8
|
1 |
|
from threading import Lock |
9
|
1 |
|
from typing import List, Optional |
10
|
|
|
|
11
|
1 |
|
from flask import jsonify, request |
12
|
1 |
|
from werkzeug.exceptions import BadRequest, UnsupportedMediaType |
13
|
|
|
|
14
|
1 |
|
from kytos.core import KytosEvent, KytosNApp, log, rest |
15
|
1 |
|
from kytos.core.common import EntityStatus |
16
|
1 |
|
from kytos.core.exceptions import KytosLinkCreationError |
17
|
1 |
|
from kytos.core.helpers import listen_to |
18
|
1 |
|
from kytos.core.interface import Interface |
19
|
1 |
|
from kytos.core.link import Link |
20
|
1 |
|
from kytos.core.switch import Switch |
21
|
1 |
|
from napps.kytos.topology import settings |
22
|
|
|
|
23
|
1 |
|
from .controllers import TopoController |
24
|
1 |
|
from .exceptions import RestoreError |
25
|
1 |
|
from .models import Topology |
26
|
|
|
|
27
|
1 |
|
DEFAULT_LINK_UP_TIMER = 10 |
28
|
|
|
|
29
|
|
|
|
30
|
1 |
|
class Main(KytosNApp): # pylint: disable=too-many-public-methods |
31
|
|
|
"""Main class of kytos/topology NApp. |
32
|
|
|
|
33
|
|
|
This class is the entry point for this napp. |
34
|
|
|
""" |
35
|
|
|
|
36
|
1 |
|
def setup(self): |
37
|
|
|
"""Initialize the NApp's links list.""" |
38
|
1 |
|
self.links = {} |
39
|
1 |
|
self.intf_available_tags = {} |
40
|
1 |
|
self.link_up_timer = getattr(settings, 'LINK_UP_TIMER', |
41
|
|
|
DEFAULT_LINK_UP_TIMER) |
42
|
|
|
|
43
|
1 |
|
self._lock = Lock() |
44
|
1 |
|
self._links_lock = Lock() |
45
|
1 |
|
self.topo_controller = self.get_topo_controller() |
46
|
1 |
|
self.topo_controller.bootstrap_indexes() |
47
|
1 |
|
self.load_topology() |
48
|
|
|
|
49
|
1 |
|
@staticmethod |
50
|
1 |
|
def get_topo_controller() -> TopoController: |
51
|
|
|
"""Get TopoController.""" |
52
|
|
|
return TopoController() |
53
|
|
|
|
54
|
1 |
|
def execute(self): |
55
|
|
|
"""Execute once when the napp is running.""" |
56
|
|
|
pass |
57
|
|
|
|
58
|
1 |
|
def shutdown(self): |
59
|
|
|
"""Do nothing.""" |
60
|
|
|
log.info('NApp kytos/topology shutting down.') |
61
|
|
|
|
62
|
1 |
|
@staticmethod |
63
|
1 |
|
def _get_metadata(): |
64
|
|
|
"""Return a JSON with metadata.""" |
65
|
1 |
|
try: |
66
|
1 |
|
metadata = request.get_json() |
67
|
1 |
|
content_type = request.content_type |
68
|
1 |
|
except BadRequest as err: |
69
|
1 |
|
result = 'The request body is not a well-formed JSON.' |
70
|
1 |
|
raise BadRequest(result) from err |
71
|
1 |
|
if content_type is None: |
72
|
|
|
result = 'The request body is empty.' |
73
|
|
|
raise BadRequest(result) |
74
|
1 |
|
if metadata is None: |
75
|
1 |
|
if content_type != 'application/json': |
76
|
|
|
result = ('The content type must be application/json ' |
77
|
|
|
f'(received {content_type}).') |
78
|
|
|
else: |
79
|
1 |
|
result = 'Metadata is empty.' |
80
|
1 |
|
raise UnsupportedMediaType(result) |
81
|
1 |
|
return metadata |
82
|
|
|
|
83
|
1 |
|
def _get_link_or_create(self, endpoint_a, endpoint_b): |
84
|
|
|
"""Get an existing link or create a new one. |
85
|
|
|
|
86
|
|
|
Returns: |
87
|
|
|
Tuple(Link, bool): Link and a boolean whether it has been created. |
88
|
|
|
""" |
89
|
1 |
|
new_link = Link(endpoint_a, endpoint_b) |
90
|
|
|
|
91
|
1 |
|
for link in self.links.values(): |
92
|
1 |
|
if new_link == link: |
93
|
1 |
|
return (link, False) |
94
|
|
|
|
95
|
1 |
|
self.links[new_link.id] = new_link |
96
|
1 |
|
return (new_link, True) |
97
|
|
|
|
98
|
1 |
|
def _get_switches_dict(self): |
99
|
|
|
"""Return a dictionary with the known switches.""" |
100
|
1 |
|
switches = {'switches': {}} |
101
|
1 |
|
for idx, switch in enumerate(self.controller.switches.values()): |
102
|
1 |
|
switch_data = switch.as_dict() |
103
|
1 |
|
if not all(key in switch_data['metadata'] |
104
|
|
|
for key in ('lat', 'lng')): |
105
|
|
|
# Switches are initialized somewhere in the ocean |
106
|
|
|
switch_data['metadata']['lat'] = str(0.0) |
107
|
|
|
switch_data['metadata']['lng'] = str(-30.0+idx*10.0) |
108
|
1 |
|
switches['switches'][switch.id] = switch_data |
109
|
1 |
|
return switches |
110
|
|
|
|
111
|
1 |
|
def _get_links_dict(self): |
112
|
|
|
"""Return a dictionary with the known links.""" |
113
|
1 |
|
return {'links': {link.id: link.as_dict() for link in |
114
|
|
|
self.links.values()}} |
115
|
|
|
|
116
|
1 |
|
def _get_topology_dict(self): |
117
|
|
|
"""Return a dictionary with the known topology.""" |
118
|
1 |
|
return {'topology': {**self._get_switches_dict(), |
119
|
|
|
**self._get_links_dict()}} |
120
|
|
|
|
121
|
1 |
|
def _get_topology(self): |
122
|
|
|
"""Return an object representing the topology.""" |
123
|
1 |
|
return Topology(self.controller.switches, self.links) |
124
|
|
|
|
125
|
1 |
|
def _get_link_from_interface(self, interface): |
126
|
|
|
"""Return the link of the interface, or None if it does not exist.""" |
127
|
1 |
|
for link in self.links.values(): |
128
|
1 |
|
if interface in (link.endpoint_a, link.endpoint_b): |
129
|
1 |
|
return link |
130
|
1 |
|
return None |
131
|
|
|
|
132
|
1 |
|
def _load_link(self, link_att): |
133
|
1 |
|
endpoint_a = link_att['endpoint_a']['id'] |
134
|
1 |
|
endpoint_b = link_att['endpoint_b']['id'] |
135
|
1 |
|
link_str = link_att['id'] |
136
|
1 |
|
log.info(f"Loading link: {link_str}") |
137
|
1 |
|
interface_a = self.controller.get_interface_by_id(endpoint_a) |
138
|
1 |
|
interface_b = self.controller.get_interface_by_id(endpoint_b) |
139
|
|
|
|
140
|
1 |
|
error = f"Fail to load endpoints for link {link_str}. " |
141
|
1 |
|
if not interface_a: |
142
|
1 |
|
raise RestoreError(f"{error}, endpoint_a {endpoint_a} not found") |
143
|
1 |
|
if not interface_b: |
144
|
|
|
raise RestoreError(f"{error}, endpoint_b {endpoint_b} not found") |
145
|
|
|
|
146
|
1 |
|
with self._links_lock: |
147
|
1 |
|
link, _ = self._get_link_or_create(interface_a, interface_b) |
148
|
|
|
|
149
|
1 |
|
if link_att['enabled']: |
150
|
1 |
|
link.enable() |
151
|
|
|
else: |
152
|
1 |
|
link.disable() |
153
|
|
|
|
154
|
1 |
|
link.extend_metadata(link_att["metadata"]) |
155
|
1 |
|
interface_a.update_link(link) |
156
|
1 |
|
interface_b.update_link(link) |
157
|
1 |
|
interface_a.nni = True |
158
|
1 |
|
interface_b.nni = True |
159
|
|
|
|
160
|
1 |
|
def _load_switch(self, switch_id, switch_att): |
161
|
1 |
|
log.info(f'Loading switch dpid: {switch_id}') |
162
|
1 |
|
switch = self.controller.get_switch_or_create(switch_id) |
163
|
1 |
|
if switch_att['enabled']: |
164
|
1 |
|
switch.enable() |
165
|
|
|
else: |
166
|
1 |
|
switch.disable() |
167
|
1 |
|
switch.description['manufacturer'] = switch_att.get('manufacturer', '') |
168
|
1 |
|
switch.description['hardware'] = switch_att.get('hardware', '') |
169
|
1 |
|
switch.description['software'] = switch_att.get('software') |
170
|
1 |
|
switch.description['serial'] = switch_att.get('serial', '') |
171
|
1 |
|
switch.description['data_path'] = switch_att.get('data_path', '') |
172
|
1 |
|
switch.extend_metadata(switch_att["metadata"]) |
173
|
|
|
|
174
|
1 |
|
for iface_id, iface_att in switch_att.get('interfaces', {}).items(): |
175
|
1 |
|
log.info(f'Loading interface iface_id={iface_id}') |
176
|
1 |
|
interface = switch.update_or_create_interface( |
177
|
|
|
port_no=iface_att['port_number'], |
178
|
|
|
name=iface_att['name'], |
179
|
|
|
address=iface_att.get('mac', None), |
180
|
|
|
speed=iface_att.get('speed', None)) |
181
|
1 |
|
if iface_att['enabled']: |
182
|
1 |
|
interface.enable() |
183
|
|
|
else: |
184
|
1 |
|
interface.disable() |
185
|
1 |
|
interface.lldp = iface_att['lldp'] |
186
|
1 |
|
interface.extend_metadata(iface_att["metadata"]) |
187
|
1 |
|
name = 'kytos/topology.port.created' |
188
|
1 |
|
event = KytosEvent(name=name, content={ |
189
|
|
|
'switch': switch_id, |
190
|
|
|
'port': interface.port_number, |
191
|
|
|
'port_description': { |
192
|
|
|
'alias': interface.name, |
193
|
|
|
'mac': interface.address, |
194
|
|
|
'state': interface.state |
195
|
|
|
} |
196
|
|
|
}) |
197
|
1 |
|
self.controller.buffers.app.put(event) |
198
|
|
|
|
199
|
1 |
|
intf_ids = [v["id"] for v in switch_att.get("interfaces", {}).values()] |
200
|
1 |
|
intf_details = self.topo_controller.get_interfaces_details(intf_ids) |
201
|
1 |
|
with self._links_lock: |
202
|
1 |
|
self.load_interfaces_available_tags(switch, intf_details) |
203
|
|
|
|
204
|
|
|
# pylint: disable=attribute-defined-outside-init |
205
|
1 |
|
def load_topology(self): |
206
|
|
|
"""Load network topology from DB.""" |
207
|
1 |
|
topology = self.topo_controller.get_topology() |
208
|
1 |
|
switches = topology["topology"]["switches"] |
209
|
1 |
|
links = topology["topology"]["links"] |
210
|
|
|
|
211
|
1 |
|
failed_switches = {} |
212
|
1 |
|
log.debug(f"_load_network_status switches={switches}") |
213
|
1 |
|
for switch_id, switch_att in switches.items(): |
214
|
1 |
|
try: |
215
|
1 |
|
self._load_switch(switch_id, switch_att) |
216
|
|
|
# pylint: disable=broad-except |
217
|
1 |
|
except Exception as err: |
218
|
1 |
|
failed_switches[switch_id] = err |
219
|
1 |
|
log.error(f'Error loading switch: {err}') |
220
|
|
|
|
221
|
1 |
|
failed_links = {} |
222
|
1 |
|
log.debug(f"_load_network_status links={links}") |
223
|
1 |
|
for link_id, link_att in links.items(): |
224
|
1 |
|
try: |
225
|
1 |
|
self._load_link(link_att) |
226
|
|
|
# pylint: disable=broad-except |
227
|
1 |
|
except Exception as err: |
228
|
1 |
|
failed_links[link_id] = err |
229
|
1 |
|
log.error(f'Error loading link {link_id}: {err}') |
230
|
|
|
|
231
|
1 |
|
name = 'kytos/topology.topology_loaded' |
232
|
1 |
|
event = KytosEvent( |
233
|
|
|
name=name, |
234
|
|
|
content={ |
235
|
|
|
'topology': self._get_topology(), |
236
|
|
|
'failed_switches': failed_switches, |
237
|
|
|
'failed_links': failed_links |
238
|
|
|
}) |
239
|
1 |
|
self.controller.buffers.app.put(event) |
240
|
|
|
|
241
|
1 |
|
@rest('v3/') |
242
|
1 |
|
def get_topology(self): |
243
|
|
|
"""Return the latest known topology. |
244
|
|
|
|
245
|
|
|
This topology is updated when there are network events. |
246
|
|
|
""" |
247
|
1 |
|
return jsonify(self._get_topology_dict()) |
248
|
|
|
|
249
|
|
|
# Switch related methods |
250
|
1 |
|
@rest('v3/switches') |
251
|
1 |
|
def get_switches(self): |
252
|
|
|
"""Return a json with all the switches in the topology.""" |
253
|
|
|
return jsonify(self._get_switches_dict()) |
254
|
|
|
|
255
|
1 |
|
@rest('v3/switches/<dpid>/enable', methods=['POST']) |
256
|
1 |
|
def enable_switch(self, dpid): |
257
|
|
|
"""Administratively enable a switch in the topology.""" |
258
|
1 |
|
try: |
259
|
1 |
|
switch = self.controller.switches[dpid] |
260
|
1 |
|
self.topo_controller.enable_switch(dpid) |
261
|
1 |
|
switch.enable() |
262
|
1 |
|
except KeyError: |
263
|
1 |
|
return jsonify("Switch not found"), 404 |
264
|
|
|
|
265
|
1 |
|
self.notify_switch_enabled(dpid) |
266
|
1 |
|
self.notify_topology_update() |
267
|
1 |
|
return jsonify("Operation successful"), 201 |
268
|
|
|
|
269
|
1 |
|
@rest('v3/switches/<dpid>/disable', methods=['POST']) |
270
|
1 |
|
def disable_switch(self, dpid): |
271
|
|
|
"""Administratively disable a switch in the topology.""" |
272
|
1 |
|
try: |
273
|
1 |
|
switch = self.controller.switches[dpid] |
274
|
1 |
|
self.topo_controller.disable_switch(dpid) |
275
|
1 |
|
switch.disable() |
276
|
1 |
|
except KeyError: |
277
|
1 |
|
return jsonify("Switch not found"), 404 |
278
|
|
|
|
279
|
1 |
|
self.notify_switch_disabled(dpid) |
280
|
1 |
|
self.notify_topology_update() |
281
|
1 |
|
return jsonify("Operation successful"), 201 |
282
|
|
|
|
283
|
1 |
|
@rest('v3/switches/<dpid>/metadata') |
284
|
1 |
|
def get_switch_metadata(self, dpid): |
285
|
|
|
"""Get metadata from a switch.""" |
286
|
1 |
|
try: |
287
|
1 |
|
return jsonify({"metadata": |
288
|
|
|
self.controller.switches[dpid].metadata}), 200 |
289
|
1 |
|
except KeyError: |
290
|
1 |
|
return jsonify("Switch not found"), 404 |
291
|
|
|
|
292
|
1 |
|
@rest('v3/switches/<dpid>/metadata', methods=['POST']) |
293
|
1 |
|
def add_switch_metadata(self, dpid): |
294
|
|
|
"""Add metadata to a switch.""" |
295
|
1 |
|
metadata = self._get_metadata() |
296
|
|
|
|
297
|
1 |
|
try: |
298
|
1 |
|
switch = self.controller.switches[dpid] |
299
|
1 |
|
except KeyError: |
300
|
1 |
|
return jsonify("Switch not found"), 404 |
301
|
|
|
|
302
|
1 |
|
self.topo_controller.add_switch_metadata(dpid, metadata) |
303
|
1 |
|
switch.extend_metadata(metadata) |
304
|
1 |
|
self.notify_metadata_changes(switch, 'added') |
305
|
1 |
|
return jsonify("Operation successful"), 201 |
306
|
|
|
|
307
|
1 |
|
@rest('v3/switches/<dpid>/metadata/<key>', methods=['DELETE']) |
308
|
1 |
|
def delete_switch_metadata(self, dpid, key): |
309
|
|
|
"""Delete metadata from a switch.""" |
310
|
1 |
|
try: |
311
|
1 |
|
switch = self.controller.switches[dpid] |
312
|
1 |
|
except KeyError: |
313
|
1 |
|
return jsonify("Switch not found"), 404 |
314
|
|
|
|
315
|
1 |
|
try: |
316
|
1 |
|
_ = switch.metadata[key] |
317
|
|
|
except KeyError: |
318
|
|
|
return jsonify("Metadata not found"), 404 |
319
|
|
|
|
320
|
1 |
|
self.topo_controller.delete_switch_metadata_key(dpid, key) |
321
|
1 |
|
switch.remove_metadata(key) |
322
|
1 |
|
self.notify_metadata_changes(switch, 'removed') |
323
|
1 |
|
return jsonify("Operation successful"), 200 |
324
|
|
|
|
325
|
|
|
# Interface related methods |
326
|
1 |
|
@rest('v3/interfaces') |
327
|
1 |
|
def get_interfaces(self): |
328
|
|
|
"""Return a json with all the interfaces in the topology.""" |
329
|
1 |
|
interfaces = {} |
330
|
1 |
|
switches = self._get_switches_dict() |
331
|
1 |
|
for switch in switches['switches'].values(): |
332
|
1 |
|
for interface_id, interface in switch['interfaces'].items(): |
333
|
1 |
|
interfaces[interface_id] = interface |
334
|
|
|
|
335
|
1 |
|
return jsonify({'interfaces': interfaces}) |
336
|
|
|
|
337
|
1 |
View Code Duplication |
@rest('v3/interfaces/switch/<dpid>/enable', methods=['POST']) |
|
|
|
|
338
|
1 |
|
@rest('v3/interfaces/<interface_enable_id>/enable', methods=['POST']) |
339
|
1 |
|
def enable_interface(self, interface_enable_id=None, dpid=None): |
340
|
|
|
"""Administratively enable interfaces in the topology.""" |
341
|
1 |
|
if dpid is None: |
342
|
1 |
|
dpid = ":".join(interface_enable_id.split(":")[:-1]) |
343
|
1 |
|
try: |
344
|
1 |
|
switch = self.controller.switches[dpid] |
345
|
1 |
|
except KeyError as exc: |
346
|
1 |
|
return jsonify(f"Switch not found: {exc}"), 404 |
347
|
|
|
|
348
|
1 |
|
if interface_enable_id: |
349
|
1 |
|
interface_number = int(interface_enable_id.split(":")[-1]) |
350
|
|
|
|
351
|
1 |
|
try: |
352
|
1 |
|
interface = switch.interfaces[interface_number] |
353
|
1 |
|
self.topo_controller.enable_interface(interface.id) |
354
|
1 |
|
interface.enable() |
355
|
1 |
|
except KeyError: |
356
|
1 |
|
msg = f"Switch {dpid} interface {interface_number} not found" |
357
|
1 |
|
return jsonify(msg), 404 |
358
|
|
|
else: |
359
|
1 |
|
for interface in switch.interfaces.values(): |
360
|
1 |
|
interface.enable() |
361
|
1 |
|
self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
362
|
1 |
|
self.notify_topology_update() |
363
|
1 |
|
return jsonify("Operation successful"), 200 |
364
|
|
|
|
365
|
1 |
View Code Duplication |
@rest('v3/interfaces/switch/<dpid>/disable', methods=['POST']) |
|
|
|
|
366
|
1 |
|
@rest('v3/interfaces/<interface_disable_id>/disable', methods=['POST']) |
367
|
1 |
|
def disable_interface(self, interface_disable_id=None, dpid=None): |
368
|
|
|
"""Administratively disable interfaces in the topology.""" |
369
|
1 |
|
if dpid is None: |
370
|
1 |
|
dpid = ":".join(interface_disable_id.split(":")[:-1]) |
371
|
1 |
|
try: |
372
|
1 |
|
switch = self.controller.switches[dpid] |
373
|
1 |
|
except KeyError as exc: |
374
|
1 |
|
return jsonify(f"Switch not found: {exc}"), 404 |
375
|
|
|
|
376
|
1 |
|
if interface_disable_id: |
377
|
1 |
|
interface_number = int(interface_disable_id.split(":")[-1]) |
378
|
|
|
|
379
|
1 |
|
try: |
380
|
1 |
|
interface = switch.interfaces[interface_number] |
381
|
1 |
|
self.topo_controller.disable_interface(interface.id) |
382
|
1 |
|
interface.disable() |
383
|
1 |
|
except KeyError: |
384
|
1 |
|
msg = f"Switch {dpid} interface {interface_number} not found" |
385
|
1 |
|
return jsonify(msg), 404 |
386
|
|
|
else: |
387
|
1 |
|
for interface in switch.interfaces.values(): |
388
|
1 |
|
interface.disable() |
389
|
1 |
|
self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
390
|
1 |
|
self.notify_topology_update() |
391
|
1 |
|
return jsonify("Operation successful"), 200 |
392
|
|
|
|
393
|
1 |
|
@rest('v3/interfaces/<interface_id>/metadata') |
394
|
1 |
|
def get_interface_metadata(self, interface_id): |
395
|
|
|
"""Get metadata from an interface.""" |
396
|
1 |
|
switch_id = ":".join(interface_id.split(":")[:-1]) |
397
|
1 |
|
interface_number = int(interface_id.split(":")[-1]) |
398
|
1 |
|
try: |
399
|
1 |
|
switch = self.controller.switches[switch_id] |
400
|
1 |
|
except KeyError: |
401
|
1 |
|
return jsonify("Switch not found"), 404 |
402
|
|
|
|
403
|
1 |
|
try: |
404
|
1 |
|
interface = switch.interfaces[interface_number] |
405
|
1 |
|
except KeyError: |
406
|
1 |
|
return jsonify("Interface not found"), 404 |
407
|
|
|
|
408
|
1 |
|
return jsonify({"metadata": interface.metadata}), 200 |
409
|
|
|
|
410
|
1 |
|
@rest('v3/interfaces/<interface_id>/metadata', methods=['POST']) |
411
|
1 |
|
def add_interface_metadata(self, interface_id): |
412
|
|
|
"""Add metadata to an interface.""" |
413
|
1 |
|
metadata = self._get_metadata() |
414
|
1 |
|
switch_id = ":".join(interface_id.split(":")[:-1]) |
415
|
1 |
|
interface_number = int(interface_id.split(":")[-1]) |
416
|
1 |
|
try: |
417
|
1 |
|
switch = self.controller.switches[switch_id] |
418
|
1 |
|
except KeyError: |
419
|
1 |
|
return jsonify("Switch not found"), 404 |
420
|
|
|
|
421
|
1 |
|
try: |
422
|
1 |
|
interface = switch.interfaces[interface_number] |
423
|
1 |
|
self.topo_controller.add_interface_metadata(interface.id, metadata) |
424
|
1 |
|
except KeyError: |
425
|
1 |
|
return jsonify("Interface not found"), 404 |
426
|
|
|
|
427
|
1 |
|
interface.extend_metadata(metadata) |
428
|
1 |
|
self.notify_metadata_changes(interface, 'added') |
429
|
1 |
|
return jsonify("Operation successful"), 201 |
430
|
|
|
|
431
|
1 |
|
@rest('v3/interfaces/<interface_id>/metadata/<key>', methods=['DELETE']) |
432
|
1 |
|
def delete_interface_metadata(self, interface_id, key): |
433
|
|
|
"""Delete metadata from an interface.""" |
434
|
1 |
|
switch_id = ":".join(interface_id.split(":")[:-1]) |
435
|
1 |
|
interface_number = int(interface_id.split(":")[-1]) |
436
|
|
|
|
437
|
1 |
|
try: |
438
|
1 |
|
switch = self.controller.switches[switch_id] |
439
|
1 |
|
except KeyError: |
440
|
1 |
|
return jsonify("Switch not found"), 404 |
441
|
|
|
|
442
|
1 |
|
try: |
443
|
1 |
|
interface = switch.interfaces[interface_number] |
444
|
1 |
|
except KeyError: |
445
|
1 |
|
return jsonify("Interface not found"), 404 |
446
|
|
|
|
447
|
1 |
|
try: |
448
|
1 |
|
_ = interface.metadata[key] |
449
|
1 |
|
except KeyError: |
450
|
1 |
|
return jsonify("Metadata not found"), 404 |
451
|
|
|
|
452
|
1 |
|
self.topo_controller.delete_interface_metadata_key(interface.id, key) |
453
|
1 |
|
interface.remove_metadata(key) |
454
|
1 |
|
self.notify_metadata_changes(interface, 'removed') |
455
|
1 |
|
return jsonify("Operation successful"), 200 |
456
|
|
|
|
457
|
|
|
# Link related methods |
458
|
1 |
|
@rest('v3/links') |
459
|
1 |
|
def get_links(self): |
460
|
|
|
"""Return a json with all the links in the topology. |
461
|
|
|
|
462
|
|
|
Links are connections between interfaces. |
463
|
|
|
""" |
464
|
|
|
return jsonify(self._get_links_dict()), 200 |
465
|
|
|
|
466
|
1 |
|
@rest('v3/links/<link_id>/enable', methods=['POST']) |
467
|
1 |
|
def enable_link(self, link_id): |
468
|
|
|
"""Administratively enable a link in the topology.""" |
469
|
1 |
|
try: |
470
|
1 |
|
with self._links_lock: |
471
|
1 |
|
link = self.links[link_id] |
472
|
1 |
|
self.topo_controller.enable_link(link_id) |
473
|
1 |
|
link.enable() |
474
|
1 |
|
except KeyError: |
475
|
1 |
|
return jsonify("Link not found"), 404 |
476
|
1 |
|
self.notify_link_status_change( |
477
|
|
|
self.links[link_id], |
478
|
|
|
reason='link enabled' |
479
|
|
|
) |
480
|
1 |
|
self.notify_topology_update() |
481
|
1 |
|
return jsonify("Operation successful"), 201 |
482
|
|
|
|
483
|
1 |
|
@rest('v3/links/<link_id>/disable', methods=['POST']) |
484
|
1 |
|
def disable_link(self, link_id): |
485
|
|
|
"""Administratively disable a link in the topology.""" |
486
|
1 |
|
try: |
487
|
1 |
|
with self._links_lock: |
488
|
1 |
|
link = self.links[link_id] |
489
|
1 |
|
self.topo_controller.disable_link(link_id) |
490
|
1 |
|
link.disable() |
491
|
1 |
|
except KeyError: |
492
|
1 |
|
return jsonify("Link not found"), 404 |
493
|
1 |
|
self.notify_link_status_change( |
494
|
|
|
self.links[link_id], |
495
|
|
|
reason='link disabled' |
496
|
|
|
) |
497
|
1 |
|
self.notify_topology_update() |
498
|
1 |
|
return jsonify("Operation successful"), 201 |
499
|
|
|
|
500
|
1 |
|
@rest('v3/links/<link_id>/metadata') |
501
|
1 |
|
def get_link_metadata(self, link_id): |
502
|
|
|
"""Get metadata from a link.""" |
503
|
1 |
|
try: |
504
|
1 |
|
return jsonify({"metadata": self.links[link_id].metadata}), 200 |
505
|
1 |
|
except KeyError: |
506
|
1 |
|
return jsonify("Link not found"), 404 |
507
|
|
|
|
508
|
1 |
|
@rest('v3/links/<link_id>/metadata', methods=['POST']) |
509
|
1 |
|
def add_link_metadata(self, link_id): |
510
|
|
|
"""Add metadata to a link.""" |
511
|
1 |
|
metadata = self._get_metadata() |
512
|
1 |
|
try: |
513
|
1 |
|
link = self.links[link_id] |
514
|
1 |
|
except KeyError: |
515
|
1 |
|
return jsonify("Link not found"), 404 |
516
|
|
|
|
517
|
1 |
|
self.topo_controller.add_link_metadata(link_id, metadata) |
518
|
1 |
|
link.extend_metadata(metadata) |
519
|
1 |
|
self.notify_metadata_changes(link, 'added') |
520
|
1 |
|
return jsonify("Operation successful"), 201 |
521
|
|
|
|
522
|
1 |
|
@rest('v3/links/<link_id>/metadata/<key>', methods=['DELETE']) |
523
|
1 |
|
def delete_link_metadata(self, link_id, key): |
524
|
|
|
"""Delete metadata from a link.""" |
525
|
1 |
|
try: |
526
|
1 |
|
link = self.links[link_id] |
527
|
1 |
|
except KeyError: |
528
|
1 |
|
return jsonify("Link not found"), 404 |
529
|
|
|
|
530
|
1 |
|
try: |
531
|
1 |
|
_ = link.metadata[key] |
532
|
1 |
|
except KeyError: |
533
|
1 |
|
return jsonify("Metadata not found"), 404 |
534
|
|
|
|
535
|
1 |
|
self.topo_controller.delete_link_metadata_key(link.id, key) |
536
|
1 |
|
link.remove_metadata(key) |
537
|
1 |
|
self.notify_metadata_changes(link, 'removed') |
538
|
1 |
|
return jsonify("Operation successful"), 200 |
539
|
|
|
|
540
|
1 |
|
@listen_to("kytos/.*.liveness.(up|down)") |
541
|
1 |
|
def on_link_liveness_status(self, event) -> None: |
542
|
|
|
"""Handle link liveness up|down status event.""" |
543
|
|
|
link = Link(event.content["interface_a"], event.content["interface_b"]) |
544
|
|
|
try: |
545
|
|
|
link = self.links[link.id] |
546
|
|
|
except KeyError: |
547
|
|
|
log.error(f"Link id {link.id} not found, {link}") |
548
|
|
|
return |
549
|
|
|
liveness_status = event.name.split(".")[-1] |
550
|
|
|
self.handle_link_liveness_status(self.links[link.id], liveness_status) |
551
|
|
|
|
552
|
1 |
|
def handle_link_liveness_status(self, link, liveness_status) -> None: |
553
|
|
|
"""Handle link liveness.""" |
554
|
1 |
|
metadata = {"liveness_status": liveness_status} |
555
|
1 |
|
log.info(f"Link liveness {liveness_status}: {link}") |
556
|
1 |
|
self.topo_controller.add_link_metadata(link.id, metadata) |
557
|
1 |
|
link.extend_metadata(metadata) |
558
|
1 |
|
self.notify_topology_update() |
559
|
1 |
|
self.notify_link_status_change(link, |
560
|
|
|
reason=f"liveness_{liveness_status}") |
561
|
|
|
|
562
|
1 |
|
@listen_to("kytos/.*.liveness.disabled") |
563
|
1 |
|
def on_link_liveness_disabled(self, event) -> None: |
564
|
|
|
"""Handle link liveness disabled event.""" |
565
|
|
|
interfaces = event.content["interfaces"] |
566
|
|
|
self.handle_link_liveness_disabled(interfaces) |
567
|
|
|
|
568
|
1 |
|
def get_links_from_interfaces(self, interfaces) -> dict: |
569
|
|
|
"""Get links from interfaces.""" |
570
|
1 |
|
links = {} |
571
|
1 |
|
for interface in interfaces: |
572
|
1 |
|
for link in self.links.values(): |
573
|
1 |
|
if any(( |
574
|
|
|
interface.id == link.endpoint_a.id, |
575
|
|
|
interface.id == link.endpoint_b.id, |
576
|
|
|
)): |
577
|
1 |
|
links[link.id] = link |
578
|
1 |
|
return links |
579
|
|
|
|
580
|
1 |
|
def handle_link_liveness_disabled(self, interfaces) -> None: |
581
|
|
|
"""Handle link liveness disabled.""" |
582
|
1 |
|
log.info(f"Link liveness disabled interfaces: {interfaces}") |
583
|
|
|
|
584
|
1 |
|
key = "liveness_status" |
585
|
1 |
|
links = self.get_links_from_interfaces(interfaces) |
586
|
1 |
|
for link in links.values(): |
587
|
1 |
|
link.remove_metadata(key) |
588
|
1 |
|
link_ids = list(links.keys()) |
589
|
1 |
|
self.topo_controller.bulk_delete_link_metadata_key(link_ids, key) |
590
|
1 |
|
self.notify_topology_update() |
591
|
1 |
|
for link in links.values(): |
592
|
1 |
|
self.notify_link_status_change(link, reason="liveness_disabled") |
593
|
|
|
|
594
|
1 |
|
@listen_to("kytos/.*.link_available_tags") |
595
|
1 |
|
def on_link_available_tags(self, event): |
596
|
|
|
"""Handle on_link_available_tags.""" |
597
|
|
|
with self._links_lock: |
598
|
|
|
self.handle_on_link_available_tags(event.content.get("link")) |
599
|
|
|
|
600
|
1 |
|
def handle_on_link_available_tags(self, link): |
601
|
|
|
"""Handle on_link_available_tags.""" |
602
|
1 |
|
if link.id not in self.links: |
603
|
|
|
return |
604
|
1 |
|
endpoint_a = self.links[link.id].endpoint_a |
605
|
1 |
|
endpoint_b = self.links[link.id].endpoint_b |
606
|
1 |
|
values_a = [tag.value for tag in endpoint_a.available_tags] |
607
|
1 |
|
values_b = [tag.value for tag in endpoint_b.available_tags] |
608
|
1 |
|
ids_details = [ |
609
|
|
|
(endpoint_a.id, {"_id": endpoint_a.id, |
610
|
|
|
"available_vlans": values_a}), |
611
|
|
|
(endpoint_b.id, {"_id": endpoint_b.id, |
612
|
|
|
"available_vlans": values_b}) |
613
|
|
|
] |
614
|
1 |
|
self.topo_controller.bulk_upsert_interface_details(ids_details) |
615
|
|
|
|
616
|
1 |
|
@listen_to('.*.switch.(new|reconnected)') |
617
|
1 |
|
def on_new_switch(self, event): |
618
|
|
|
"""Create a new Device on the Topology. |
619
|
|
|
|
620
|
|
|
Handle the event of a new created switch and update the topology with |
621
|
|
|
this new device. Also notify if the switch is enabled. |
622
|
|
|
""" |
623
|
|
|
self.handle_new_switch(event) |
624
|
|
|
|
625
|
1 |
|
def handle_new_switch(self, event): |
626
|
|
|
"""Create a new Device on the Topology.""" |
627
|
1 |
|
switch = event.content['switch'] |
628
|
1 |
|
switch.activate() |
629
|
1 |
|
self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
630
|
1 |
|
log.debug('Switch %s added to the Topology.', switch.id) |
631
|
1 |
|
self.notify_topology_update() |
632
|
1 |
|
if switch.is_enabled(): |
633
|
1 |
|
self.notify_switch_enabled(switch.id) |
634
|
|
|
|
635
|
1 |
|
@listen_to('.*.connection.lost') |
636
|
1 |
|
def on_connection_lost(self, event): |
637
|
|
|
"""Remove a Device from the topology. |
638
|
|
|
|
639
|
|
|
Remove the disconnected Device and every link that has one of its |
640
|
|
|
interfaces. |
641
|
|
|
""" |
642
|
|
|
self.handle_connection_lost(event) |
643
|
|
|
|
644
|
1 |
|
def handle_connection_lost(self, event): |
645
|
|
|
"""Remove a Device from the topology.""" |
646
|
1 |
|
switch = event.content['source'].switch |
647
|
1 |
|
if switch: |
648
|
1 |
|
switch.deactivate() |
649
|
1 |
|
self.topo_controller.deactivate_switch(switch.id) |
650
|
1 |
|
log.debug('Switch %s removed from the Topology.', switch.id) |
651
|
1 |
|
self.notify_topology_update() |
652
|
|
|
|
653
|
1 |
|
def handle_interfaces_created(self, event): |
654
|
|
|
"""Update the topology based on the interfaces created.""" |
655
|
1 |
|
interfaces = event.content["interfaces"] |
656
|
1 |
|
if not interfaces: |
657
|
|
|
return |
658
|
1 |
|
switch = interfaces[0].switch |
659
|
1 |
|
self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
660
|
1 |
|
name = "kytos/topology.switch.interface.created" |
661
|
1 |
|
for interface in interfaces: |
662
|
1 |
|
event = KytosEvent(name=name, content={'interface': interface}) |
663
|
1 |
|
self.controller.buffers.app.put(event) |
664
|
|
|
|
665
|
1 |
|
def handle_interface_created(self, event): |
666
|
|
|
"""Update the topology based on an interface created event. |
667
|
|
|
|
668
|
|
|
It's handled as a link_up in case a switch send a |
669
|
|
|
created event again and it can be belong to a link. |
670
|
|
|
""" |
671
|
1 |
|
interface = event.content['interface'] |
672
|
1 |
|
self.handle_interface_link_up(interface) |
673
|
|
|
|
674
|
1 |
|
@listen_to('.*.topology.switch.interface.created') |
675
|
1 |
|
def on_interface_created(self, event): |
676
|
|
|
"""Handle individual interface create event. |
677
|
|
|
|
678
|
|
|
It's handled as a link_up in case a switch send a |
679
|
|
|
created event it can belong to an existign link. |
680
|
|
|
""" |
681
|
|
|
self.handle_interface_created(event) |
682
|
|
|
|
683
|
1 |
|
@listen_to('.*.switch.interfaces.created') |
684
|
1 |
|
def on_interfaces_created(self, event): |
685
|
|
|
"""Update the topology based on a list of created interfaces.""" |
686
|
|
|
self.handle_interfaces_created(event) |
687
|
|
|
|
688
|
1 |
|
def handle_interface_down(self, event): |
689
|
|
|
"""Update the topology based on a Port Modify event. |
690
|
|
|
|
691
|
|
|
The event notifies that an interface was changed to 'down'. |
692
|
|
|
""" |
693
|
1 |
|
interface = event.content['interface'] |
694
|
1 |
|
interface.deactivate() |
695
|
1 |
|
self.topo_controller.deactivate_interface(interface.id) |
696
|
1 |
|
self.handle_interface_link_down(interface) |
697
|
|
|
|
698
|
1 |
|
@listen_to('.*.switch.interface.deleted') |
699
|
1 |
|
def on_interface_deleted(self, event): |
700
|
|
|
"""Update the topology based on a Port Delete event.""" |
701
|
|
|
self.handle_interface_deleted(event) |
702
|
|
|
|
703
|
1 |
|
def handle_interface_deleted(self, event): |
704
|
|
|
"""Update the topology based on a Port Delete event.""" |
705
|
1 |
|
self.handle_interface_down(event) |
706
|
|
|
|
707
|
1 |
|
@listen_to('.*.switch.interface.link_up') |
708
|
1 |
|
def on_interface_link_up(self, event): |
709
|
|
|
"""Update the topology based on a Port Modify event. |
710
|
|
|
|
711
|
|
|
The event notifies that an interface's link was changed to 'up'. |
712
|
|
|
""" |
713
|
|
|
interface = event.content['interface'] |
714
|
|
|
self.handle_interface_link_up(interface) |
715
|
|
|
|
716
|
1 |
|
def handle_interface_link_up(self, interface): |
717
|
|
|
"""Update the topology based on a Port Modify event.""" |
718
|
1 |
|
self.handle_link_up(interface) |
719
|
|
|
|
720
|
1 |
|
@listen_to('kytos/maintenance.end_switch') |
721
|
1 |
|
def on_switch_maintenance_end(self, event): |
722
|
|
|
"""Handle the end of the maintenance of a switch.""" |
723
|
|
|
self.handle_switch_maintenance_end(event) |
724
|
|
|
|
725
|
1 |
|
def handle_switch_maintenance_end(self, event): |
726
|
|
|
"""Handle the end of the maintenance of a switch.""" |
727
|
1 |
|
switches = event.content['switches'] |
728
|
1 |
|
for switch in switches: |
729
|
1 |
|
switch.enable() |
730
|
1 |
|
switch.activate() |
731
|
1 |
|
for interface in switch.interfaces.values(): |
732
|
1 |
|
interface.enable() |
733
|
1 |
|
self.handle_link_up(interface) |
734
|
|
|
|
735
|
1 |
|
def handle_link_up(self, interface): |
736
|
|
|
"""Notify a link is up.""" |
737
|
1 |
|
interface.activate() |
738
|
1 |
|
self.topo_controller.activate_interface(interface.id) |
739
|
1 |
|
self.notify_topology_update() |
740
|
1 |
|
with self._links_lock: |
741
|
1 |
|
link = self._get_link_from_interface(interface) |
742
|
1 |
|
if not link: |
743
|
|
|
return |
744
|
1 |
|
if link.endpoint_a == interface: |
745
|
1 |
|
other_interface = link.endpoint_b |
746
|
|
|
else: |
747
|
1 |
|
other_interface = link.endpoint_a |
748
|
1 |
|
if other_interface.is_active() is False: |
749
|
1 |
|
return |
750
|
1 |
|
if link.is_active() is False: |
751
|
1 |
|
link.update_metadata('last_status_change', time.time()) |
752
|
1 |
|
link.activate() |
753
|
|
|
|
754
|
|
|
# As each run of this method uses a different thread, |
755
|
|
|
# there is no risk this sleep will lock the NApp. |
756
|
1 |
|
time.sleep(self.link_up_timer) |
757
|
|
|
|
758
|
1 |
|
last_status_change = link.get_metadata('last_status_change') |
759
|
1 |
|
now = time.time() |
760
|
1 |
|
if link.is_active() and \ |
761
|
|
|
now - last_status_change >= self.link_up_timer: |
762
|
1 |
|
link.update_metadata('last_status_is_active', True) |
763
|
1 |
|
self.topo_controller.activate_link(link.id, last_status_change, |
764
|
|
|
last_status_is_active=True) |
765
|
1 |
|
if link.status == EntityStatus.UP: |
766
|
1 |
|
self.notify_topology_update() |
767
|
1 |
|
self.notify_link_status_change(link, reason='link up') |
768
|
|
|
else: |
769
|
1 |
|
last_status_change = time.time() |
770
|
1 |
|
metadata = {'last_status_change': last_status_change, |
771
|
|
|
'last_status_is_active': True} |
772
|
1 |
|
link.extend_metadata(metadata) |
773
|
1 |
|
self.topo_controller.activate_link(link.id, last_status_change, |
774
|
|
|
last_status_is_active=True) |
775
|
1 |
|
if link.status == EntityStatus.UP: |
776
|
1 |
|
self.notify_topology_update() |
777
|
1 |
|
self.notify_link_status_change(link, reason='link up') |
778
|
|
|
|
779
|
1 |
|
@listen_to('.*.switch.interface.link_down') |
780
|
1 |
|
def on_interface_link_down(self, event): |
781
|
|
|
"""Update the topology based on a Port Modify event. |
782
|
|
|
|
783
|
|
|
The event notifies that an interface's link was changed to 'down'. |
784
|
|
|
""" |
785
|
|
|
interface = event.content['interface'] |
786
|
|
|
self.handle_interface_link_down(interface) |
787
|
|
|
|
788
|
1 |
|
def handle_interface_link_down(self, interface): |
789
|
|
|
"""Update the topology based on an interface.""" |
790
|
1 |
|
self.handle_link_down(interface) |
791
|
|
|
|
792
|
1 |
|
@listen_to('kytos/maintenance.start_switch') |
793
|
1 |
|
def on_switch_maintenance_start(self, event): |
794
|
|
|
"""Handle the start of the maintenance of a switch.""" |
795
|
|
|
self.handle_switch_maintenance_start(event) |
796
|
|
|
|
797
|
1 |
|
def handle_switch_maintenance_start(self, event): |
798
|
|
|
"""Handle the start of the maintenance of a switch.""" |
799
|
1 |
|
switches = event.content['switches'] |
800
|
1 |
|
for switch in switches: |
801
|
1 |
|
switch.disable() |
802
|
1 |
|
switch.deactivate() |
803
|
1 |
|
for interface in switch.interfaces.values(): |
804
|
1 |
|
interface.disable() |
805
|
1 |
|
if interface.is_active(): |
806
|
1 |
|
self.handle_link_down(interface) |
807
|
|
|
|
808
|
1 |
|
def handle_link_down(self, interface): |
809
|
|
|
"""Notify a link is down.""" |
810
|
1 |
|
link = self._get_link_from_interface(interface) |
811
|
1 |
|
if link and link.is_active(): |
812
|
1 |
|
link.deactivate() |
813
|
1 |
|
last_status_change = time.time() |
814
|
1 |
|
last_status_is_active = False |
815
|
1 |
|
metadata = { |
816
|
|
|
"last_status_change": last_status_change, |
817
|
|
|
"last_status_is_active": last_status_is_active, |
818
|
|
|
} |
819
|
1 |
|
link.extend_metadata(metadata) |
820
|
1 |
|
self.topo_controller.deactivate_link(link.id, last_status_change, |
821
|
|
|
last_status_is_active) |
822
|
1 |
|
self.notify_link_status_change(link, reason="link down") |
823
|
1 |
|
if link and not link.is_active(): |
824
|
1 |
|
with self._links_lock: |
825
|
1 |
|
last_status = link.get_metadata('last_status_is_active') |
826
|
1 |
|
last_status_change = link.get_metadata('last_status_change') |
827
|
1 |
|
metadata = { |
828
|
|
|
"last_status_change": last_status_change, |
829
|
|
|
"last_status_is_active": last_status, |
830
|
|
|
} |
831
|
1 |
|
if last_status: |
832
|
|
|
link.extend_metadata(metadata) |
833
|
|
|
self.topo_controller.deactivate_link(link.id, |
834
|
|
|
last_status_change, |
835
|
|
|
last_status) |
836
|
|
|
self.notify_link_status_change(link, reason='link down') |
837
|
1 |
|
interface.deactivate() |
838
|
1 |
|
self.topo_controller.deactivate_interface(interface.id) |
839
|
1 |
|
self.notify_topology_update() |
840
|
|
|
|
841
|
1 |
|
@listen_to('.*.interface.is.nni') |
842
|
1 |
|
def on_add_links(self, event): |
843
|
|
|
"""Update the topology with links related to the NNI interfaces.""" |
844
|
|
|
self.add_links(event) |
845
|
|
|
|
846
|
1 |
|
def add_links(self, event): |
847
|
|
|
"""Update the topology with links related to the NNI interfaces.""" |
848
|
1 |
|
interface_a = event.content['interface_a'] |
849
|
1 |
|
interface_b = event.content['interface_b'] |
850
|
|
|
|
851
|
1 |
|
try: |
852
|
1 |
|
with self._links_lock: |
853
|
1 |
|
link, created = self._get_link_or_create(interface_a, |
854
|
|
|
interface_b) |
855
|
1 |
|
interface_a.update_link(link) |
856
|
1 |
|
interface_b.update_link(link) |
857
|
|
|
|
858
|
1 |
|
link.endpoint_a = interface_a |
859
|
1 |
|
link.endpoint_b = interface_b |
860
|
|
|
|
861
|
1 |
|
interface_a.nni = True |
862
|
1 |
|
interface_b.nni = True |
863
|
|
|
|
864
|
|
|
except KytosLinkCreationError as err: |
865
|
|
|
log.error(f'Error creating link: {err}.') |
866
|
|
|
return |
867
|
|
|
|
868
|
1 |
|
if created: |
869
|
1 |
|
link.update_metadata('last_status_is_active', True) |
870
|
1 |
|
self.notify_link_status_change(link, reason='link up') |
871
|
1 |
|
self.notify_topology_update() |
872
|
1 |
|
self.topo_controller.upsert_link(link.id, link.as_dict()) |
873
|
|
|
|
874
|
1 |
|
@listen_to('.*.of_lldp.network_status.updated') |
875
|
1 |
|
def on_lldp_status_updated(self, event): |
876
|
|
|
"""Handle of_lldp.network_status.updated from of_lldp.""" |
877
|
|
|
self.handle_lldp_status_updated(event) |
878
|
|
|
|
879
|
1 |
|
@listen_to(".*.topo_controller.upsert_switch") |
880
|
1 |
|
def on_topo_controller_upsert_switch(self, event) -> None: |
881
|
|
|
"""Listen to topo_controller_upsert_switch.""" |
882
|
|
|
self.handle_topo_controller_upsert_switch(event.content["switch"]) |
883
|
|
|
|
884
|
1 |
|
def handle_topo_controller_upsert_switch(self, switch) -> Optional[dict]: |
885
|
|
|
"""Handle topo_controller_upsert_switch.""" |
886
|
1 |
|
return self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
887
|
|
|
|
888
|
1 |
|
def handle_lldp_status_updated(self, event) -> None: |
889
|
|
|
"""Handle .*.network_status.updated events from of_lldp.""" |
890
|
1 |
|
content = event.content |
891
|
1 |
|
interface_ids = content["interface_ids"] |
892
|
1 |
|
switches = set() |
893
|
1 |
|
for interface_id in interface_ids: |
894
|
1 |
|
dpid = ":".join(interface_id.split(":")[:-1]) |
895
|
1 |
|
switch = self.controller.get_switch_by_dpid(dpid) |
896
|
1 |
|
if switch: |
897
|
1 |
|
switches.add(switch) |
898
|
|
|
|
899
|
1 |
|
name = "kytos/topology.topo_controller.upsert_switch" |
900
|
1 |
|
for switch in switches: |
901
|
1 |
|
event = KytosEvent(name=name, content={"switch": switch}) |
902
|
1 |
|
self.controller.buffers.app.put(event) |
903
|
|
|
|
904
|
1 |
|
def notify_switch_enabled(self, dpid): |
905
|
|
|
"""Send an event to notify that a switch is enabled.""" |
906
|
1 |
|
name = 'kytos/topology.switch.enabled' |
907
|
1 |
|
event = KytosEvent(name=name, content={'dpid': dpid}) |
908
|
1 |
|
self.controller.buffers.app.put(event) |
909
|
|
|
|
910
|
1 |
|
def notify_switch_disabled(self, dpid): |
911
|
|
|
"""Send an event to notify that a switch is disabled.""" |
912
|
1 |
|
name = 'kytos/topology.switch.disabled' |
913
|
1 |
|
event = KytosEvent(name=name, content={'dpid': dpid}) |
914
|
1 |
|
self.controller.buffers.app.put(event) |
915
|
|
|
|
916
|
1 |
|
def notify_topology_update(self): |
917
|
|
|
"""Send an event to notify about updates on the topology.""" |
918
|
1 |
|
name = 'kytos/topology.updated' |
919
|
1 |
|
event = KytosEvent(name=name, content={'topology': |
920
|
|
|
self._get_topology()}) |
921
|
1 |
|
self.controller.buffers.app.put(event) |
922
|
|
|
|
923
|
1 |
|
def notify_link_status_change(self, link, reason='not given'): |
924
|
|
|
"""Send an event to notify about a status change on a link.""" |
925
|
1 |
|
name = 'kytos/topology.' |
926
|
1 |
|
if link.status == EntityStatus.UP: |
927
|
|
|
status = 'link_up' |
928
|
|
|
else: |
929
|
1 |
|
status = 'link_down' |
930
|
1 |
|
event = KytosEvent( |
931
|
|
|
name=name+status, |
932
|
|
|
content={ |
933
|
|
|
'link': link, |
934
|
|
|
'reason': reason |
935
|
|
|
}) |
936
|
1 |
|
self.controller.buffers.app.put(event) |
937
|
|
|
|
938
|
1 |
|
def notify_metadata_changes(self, obj, action): |
939
|
|
|
"""Send an event to notify about metadata changes.""" |
940
|
1 |
|
if isinstance(obj, Switch): |
941
|
1 |
|
entity = 'switch' |
942
|
1 |
|
entities = 'switches' |
943
|
1 |
|
elif isinstance(obj, Interface): |
944
|
1 |
|
entity = 'interface' |
945
|
1 |
|
entities = 'interfaces' |
946
|
1 |
|
elif isinstance(obj, Link): |
947
|
1 |
|
entity = 'link' |
948
|
1 |
|
entities = 'links' |
949
|
|
|
else: |
950
|
1 |
|
raise ValueError( |
951
|
|
|
'Invalid object, supported: Switch, Interface, Link' |
952
|
|
|
) |
953
|
|
|
|
954
|
1 |
|
name = f'kytos/topology.{entities}.metadata.{action}' |
955
|
1 |
|
event = KytosEvent(name=name, content={entity: obj, |
956
|
|
|
'metadata': obj.metadata}) |
957
|
1 |
|
self.controller.buffers.app.put(event) |
958
|
1 |
|
log.debug(f'Metadata from {obj.id} was {action}.') |
959
|
|
|
|
960
|
1 |
|
@listen_to('.*.switch.port.created') |
961
|
1 |
|
def on_notify_port_created(self, event): |
962
|
|
|
"""Notify when a port is created.""" |
963
|
|
|
self.notify_port_created(event) |
964
|
|
|
|
965
|
1 |
|
def notify_port_created(self, event): |
966
|
|
|
"""Notify when a port is created.""" |
967
|
1 |
|
name = 'kytos/topology.port.created' |
968
|
1 |
|
event = KytosEvent(name=name, content=event.content) |
969
|
1 |
|
self.controller.buffers.app.put(event) |
970
|
|
|
|
971
|
1 |
|
@staticmethod |
972
|
1 |
|
def load_interfaces_available_tags(switch: Switch, |
973
|
|
|
interfaces_details: List[dict]) -> None: |
974
|
|
|
"""Load interfaces available tags (vlans).""" |
975
|
1 |
|
if not interfaces_details: |
976
|
|
|
return |
977
|
1 |
|
for interface_details in interfaces_details: |
978
|
1 |
|
available_vlans = interface_details["available_vlans"] |
979
|
1 |
|
if not available_vlans: |
980
|
|
|
continue |
981
|
1 |
|
log.debug(f"Interface id {interface_details['id']} loading " |
982
|
|
|
f"{len(interface_details['available_vlans'])} " |
983
|
|
|
"available tags") |
984
|
1 |
|
port_number = int(interface_details["id"].split(":")[-1]) |
985
|
1 |
|
interface = switch.interfaces[port_number] |
986
|
1 |
|
interface.set_available_tags(interface_details['available_vlans']) |
987
|
|
|
|
988
|
1 |
|
@listen_to('kytos/maintenance.start_link') |
989
|
1 |
|
def on_link_maintenance_start(self, event): |
990
|
|
|
"""Deals with the start of links maintenance.""" |
991
|
|
|
with self._links_lock: |
992
|
|
|
self.handle_link_maintenance_start(event) |
993
|
|
|
|
994
|
1 |
|
def handle_link_maintenance_start(self, event): |
995
|
|
|
"""Deals with the start of links maintenance.""" |
996
|
1 |
|
notify_links = [] |
997
|
1 |
|
maintenance_links = event.content['links'] |
998
|
1 |
|
for maintenance_link in maintenance_links: |
999
|
1 |
|
try: |
1000
|
1 |
|
link = self.links[maintenance_link.id] |
1001
|
1 |
|
except KeyError: |
1002
|
1 |
|
continue |
1003
|
1 |
|
notify_links.append(link) |
1004
|
1 |
|
for link in notify_links: |
1005
|
1 |
|
link.disable() |
1006
|
1 |
|
link.deactivate() |
1007
|
1 |
|
link.endpoint_a.deactivate() |
1008
|
1 |
|
link.endpoint_b.deactivate() |
1009
|
1 |
|
link.endpoint_a.disable() |
1010
|
1 |
|
link.endpoint_b.disable() |
1011
|
1 |
|
self.notify_link_status_change(link, reason='maintenance') |
1012
|
|
|
|
1013
|
1 |
|
@listen_to('kytos/maintenance.end_link') |
1014
|
1 |
|
def on_link_maintenance_end(self, event): |
1015
|
|
|
"""Deals with the end of links maintenance.""" |
1016
|
|
|
with self._links_lock: |
1017
|
|
|
self.handle_link_maintenance_end(event) |
1018
|
|
|
|
1019
|
1 |
|
def handle_link_maintenance_end(self, event): |
1020
|
|
|
"""Deals with the end of links maintenance.""" |
1021
|
1 |
|
notify_links = [] |
1022
|
1 |
|
maintenance_links = event.content['links'] |
1023
|
1 |
|
for maintenance_link in maintenance_links: |
1024
|
1 |
|
try: |
1025
|
1 |
|
link = self.links[maintenance_link.id] |
1026
|
1 |
|
except KeyError: |
1027
|
1 |
|
continue |
1028
|
1 |
|
notify_links.append(link) |
1029
|
1 |
|
for link in notify_links: |
1030
|
1 |
|
link.enable() |
1031
|
1 |
|
link.activate() |
1032
|
1 |
|
link.endpoint_a.activate() |
1033
|
1 |
|
link.endpoint_b.activate() |
1034
|
1 |
|
link.endpoint_a.enable() |
1035
|
1 |
|
link.endpoint_b.enable() |
1036
|
|
|
self.notify_link_status_change(link, reason='maintenance') |
1037
|
|
|
|