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