Total Complexity | 290 |
Total Lines | 1523 |
Duplicated Lines | 3.81 % |
Coverage | 92.3% |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like build.main often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """Main module of kytos/topology Kytos Network Application. |
||
2 | |||
3 | Manage the network topology |
||
4 | """ |
||
5 | # pylint: disable=wrong-import-order |
||
6 | 1 | import pathlib |
|
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 | import httpx |
|
14 | 1 | import tenacity |
|
15 | 1 | from tenacity import (retry_if_exception_type, stop_after_attempt, |
|
16 | wait_combine, wait_fixed, wait_random) |
||
17 | |||
18 | 1 | from kytos.core import KytosEvent, KytosNApp, log, rest |
|
19 | 1 | from kytos.core.common import EntityStatus |
|
20 | 1 | from kytos.core.exceptions import (KytosInvalidTagRanges, |
|
21 | KytosLinkCreationError, KytosTagError) |
||
22 | 1 | from kytos.core.helpers import listen_to, load_spec, now, validate_openapi |
|
23 | 1 | from kytos.core.interface import Interface |
|
24 | 1 | from kytos.core.link import Link |
|
25 | 1 | from kytos.core.rest_api import (HTTPException, JSONResponse, Request, |
|
26 | content_type_json_or_415, get_json_or_400) |
||
27 | 1 | from kytos.core.retry import before_sleep |
|
28 | 1 | from kytos.core.switch import Switch |
|
29 | 1 | from kytos.core.tag_ranges import get_tag_ranges |
|
30 | 1 | from napps.kytos.topology import settings |
|
31 | |||
32 | 1 | from .controllers import TopoController |
|
33 | 1 | from .exceptions import RestoreError |
|
34 | 1 | from .models import Topology |
|
35 | |||
36 | 1 | DEFAULT_LINK_UP_TIMER = 10 |
|
37 | |||
38 | |||
39 | 1 | class Main(KytosNApp): # pylint: disable=too-many-public-methods |
|
40 | """Main class of kytos/topology NApp. |
||
41 | |||
42 | This class is the entry point for this napp. |
||
43 | """ |
||
44 | |||
45 | 1 | spec = load_spec(pathlib.Path(__file__).parent / "openapi.yml") |
|
46 | |||
47 | 1 | def setup(self): |
|
48 | """Initialize the NApp's links list.""" |
||
49 | 1 | self.links: dict[str, Link] = {} |
|
50 | 1 | self.intf_available_tags = {} |
|
51 | 1 | self.link_up_timer = getattr(settings, 'LINK_UP_TIMER', |
|
52 | DEFAULT_LINK_UP_TIMER) |
||
53 | |||
54 | 1 | self._links_lock = Lock() |
|
55 | # to keep track of potential unorded scheduled interface events |
||
56 | 1 | self._intfs_lock = defaultdict(Lock) |
|
57 | 1 | self._intfs_updated_at = {} |
|
58 | 1 | self._intfs_tags_updated_at = {} |
|
59 | 1 | self.link_up = set() |
|
60 | 1 | self.link_status_lock = Lock() |
|
61 | 1 | self._switch_lock = defaultdict(Lock) |
|
62 | 1 | self.topo_controller = self.get_topo_controller() |
|
63 | 1 | Link.register_status_func(f"{self.napp_id}_link_up_timer", |
|
64 | self.link_status_hook_link_up_timer) |
||
65 | 1 | self.topo_controller.bootstrap_indexes() |
|
66 | 1 | self.load_topology() |
|
67 | |||
68 | 1 | @staticmethod |
|
69 | 1 | def get_topo_controller() -> TopoController: |
|
70 | """Get TopoController.""" |
||
71 | return TopoController() |
||
72 | |||
73 | 1 | def execute(self): |
|
74 | """Execute once when the napp is running.""" |
||
75 | pass |
||
76 | |||
77 | 1 | def shutdown(self): |
|
78 | """Do nothing.""" |
||
79 | log.info('NApp kytos/topology shutting down.') |
||
80 | |||
81 | 1 | def _get_metadata(self, request: Request) -> dict: |
|
82 | """Return a JSON with metadata.""" |
||
83 | 1 | content_type_json_or_415(request) |
|
84 | 1 | metadata = get_json_or_400(request, self.controller.loop) |
|
85 | 1 | if not isinstance(metadata, dict): |
|
86 | 1 | raise HTTPException(400, "Invalid metadata value: {metadata}") |
|
87 | 1 | return metadata |
|
88 | |||
89 | 1 | def _get_link_or_create( |
|
90 | self, |
||
91 | endpoint_a: Interface, |
||
92 | endpoint_b: Interface |
||
93 | ): |
||
94 | """Get an existing link or create a new one. |
||
95 | |||
96 | Returns: |
||
97 | Tuple(Link, bool): Link and a boolean whether it has been created. |
||
98 | """ |
||
99 | 1 | new_link = Link(endpoint_a, endpoint_b) |
|
100 | |||
101 | 1 | if new_link.id in self.links: |
|
102 | 1 | return (self.links[new_link.id], False) |
|
103 | |||
104 | 1 | link_a = self._get_link_from_interface(endpoint_a) |
|
105 | 1 | if link_a: |
|
106 | 1 | self._disable_remove_link(link_a, endpoint_a) |
|
107 | |||
108 | 1 | link_b = self._get_link_from_interface(endpoint_b) |
|
109 | 1 | if link_b: |
|
110 | self._disable_remove_link(link_b, endpoint_b) |
||
111 | |||
112 | 1 | self.links[new_link.id] = new_link |
|
113 | 1 | return (new_link, True) |
|
114 | |||
115 | 1 | def _disable_remove_link(self, link: Link, endpoint: Interface): |
|
116 | """Disable and remove link""" |
||
117 | 1 | if link.is_enabled(): |
|
118 | 1 | link.disable() |
|
119 | 1 | self.notify_interface_link_status(link, 'disabled') |
|
120 | |||
121 | 1 | self.notify_link_status_change(link, reason='duplicated link disabled') |
|
122 | 1 | if link.endpoint_a.link and link == link.endpoint_a.link: |
|
123 | 1 | switch = link.endpoint_a.switch |
|
124 | 1 | link.endpoint_a.link = None |
|
125 | 1 | link.endpoint_a.nni = False |
|
126 | 1 | self.topo_controller.upsert_switch( |
|
127 | switch.id, switch.as_dict() |
||
128 | ) |
||
129 | |||
130 | 1 | if link.endpoint_b.link and link == link.endpoint_b.link: |
|
131 | switch = link.endpoint_b.switch |
||
132 | link.endpoint_b.link = None |
||
133 | link.endpoint_b.nni = False |
||
134 | self.topo_controller.upsert_switch( |
||
135 | switch.id, switch.as_dict() |
||
136 | ) |
||
137 | |||
138 | 1 | self.topo_controller.delete_link(link.id) |
|
139 | 1 | link = self.links.pop(link.id, None) |
|
140 | 1 | self.notify_topology_update() |
|
141 | |||
142 | 1 | name = 'kytos/topology.link.deleted' |
|
143 | 1 | event = KytosEvent(name=name, content={'link': link}) |
|
144 | 1 | self.controller.buffers.app.put(event) |
|
145 | |||
146 | 1 | log.error(f"Found that the interface {endpoint.id} received " |
|
147 | f"duplicated links. The {link} was disabled and deleted.") |
||
148 | |||
149 | 1 | def _get_switches_dict(self): |
|
150 | """Return a dictionary with the known switches.""" |
||
151 | 1 | switches = {'switches': {}} |
|
152 | 1 | for idx, switch in enumerate(self.controller.switches.copy().values()): |
|
153 | 1 | switch_data = switch.as_dict() |
|
154 | 1 | if not all(key in switch_data['metadata'] |
|
155 | for key in ('lat', 'lng')): |
||
156 | # Switches are initialized somewhere in the ocean |
||
157 | switch_data['metadata']['lat'] = str(0.0) |
||
158 | switch_data['metadata']['lng'] = str(-30.0+idx*10.0) |
||
159 | 1 | switches['switches'][switch.id] = switch_data |
|
160 | 1 | return switches |
|
161 | |||
162 | 1 | def _get_links_dict(self): |
|
163 | """Return a dictionary with the known links.""" |
||
164 | 1 | return {'links': {link.id: link.as_dict() for link in |
|
165 | self.links.copy().values()}} |
||
166 | |||
167 | 1 | def _get_topology_dict(self): |
|
168 | """Return a dictionary with the known topology.""" |
||
169 | 1 | return {'topology': {**self._get_switches_dict(), |
|
170 | **self._get_links_dict()}} |
||
171 | |||
172 | 1 | def _get_topology(self): |
|
173 | """Return an object representing the topology.""" |
||
174 | 1 | return Topology(self.controller.switches.copy(), self.links.copy()) |
|
175 | |||
176 | 1 | def _get_link_from_interface(self, interface: Interface): |
|
177 | """Return the link of the interface, or None if it does not exist.""" |
||
178 | 1 | for link in list(self.links.values()): |
|
179 | 1 | if interface in (link.endpoint_a, link.endpoint_b): |
|
180 | 1 | return link |
|
181 | 1 | return None |
|
182 | |||
183 | 1 | def _load_link(self, link_att): |
|
184 | 1 | endpoint_a = link_att['endpoint_a']['id'] |
|
185 | 1 | endpoint_b = link_att['endpoint_b']['id'] |
|
186 | 1 | link_str = link_att['id'] |
|
187 | 1 | log.info(f"Loading link: {link_str}") |
|
188 | 1 | interface_a = self.controller.get_interface_by_id(endpoint_a) |
|
189 | 1 | interface_b = self.controller.get_interface_by_id(endpoint_b) |
|
190 | |||
191 | 1 | error = f"Fail to load endpoints for link {link_str}. " |
|
192 | 1 | if not interface_a: |
|
193 | 1 | raise RestoreError(f"{error}, endpoint_a {endpoint_a} not found") |
|
194 | 1 | if not interface_b: |
|
195 | raise RestoreError(f"{error}, endpoint_b {endpoint_b} not found") |
||
196 | |||
197 | 1 | with self._links_lock: |
|
198 | 1 | link, _ = self._get_link_or_create(interface_a, interface_b) |
|
199 | |||
200 | 1 | if link_att['enabled']: |
|
201 | 1 | link.enable() |
|
202 | else: |
||
203 | 1 | link.disable() |
|
204 | |||
205 | # These ones are just runtime active southbound protocol data |
||
206 | # It won't be stored in the future, only kept in the runtime. |
||
207 | # Also network operators can follow logs to track this state changes |
||
208 | 1 | for key in ( |
|
209 | "last_status_is_active", "last_status_change", "notified_up_at" |
||
210 | ): |
||
211 | 1 | link_att["metadata"].pop(key, None) |
|
212 | |||
213 | 1 | link.extend_metadata(link_att["metadata"]) |
|
214 | 1 | interface_a.update_link(link) |
|
215 | 1 | interface_b.update_link(link) |
|
216 | 1 | interface_a.nni = True |
|
217 | 1 | interface_b.nni = True |
|
218 | |||
219 | 1 | def _load_switch(self, switch_id, switch_att): |
|
220 | 1 | log.info(f'Loading switch dpid: {switch_id}') |
|
221 | 1 | switch = self.controller.get_switch_or_create(switch_id) |
|
222 | 1 | if switch_att['enabled']: |
|
223 | 1 | switch.enable() |
|
224 | else: |
||
225 | 1 | switch.disable() |
|
226 | 1 | switch.description['manufacturer'] = switch_att.get('manufacturer', '') |
|
227 | 1 | switch.description['hardware'] = switch_att.get('hardware', '') |
|
228 | 1 | switch.description['software'] = switch_att.get('software') |
|
229 | 1 | switch.description['serial'] = switch_att.get('serial', '') |
|
230 | 1 | switch.description['data_path'] = switch_att.get('data_path', '') |
|
231 | 1 | switch.extend_metadata(switch_att["metadata"]) |
|
232 | |||
233 | 1 | for iface_id, iface_att in switch_att.get('interfaces', {}).items(): |
|
234 | 1 | log.info(f'Loading interface iface_id={iface_id}') |
|
235 | 1 | interface = switch.update_or_create_interface( |
|
236 | port_no=iface_att['port_number'], |
||
237 | name=iface_att['name'], |
||
238 | address=iface_att.get('mac', None), |
||
239 | speed=iface_att.get('speed', None)) |
||
240 | 1 | if iface_att['enabled']: |
|
241 | 1 | interface.enable() |
|
242 | else: |
||
243 | 1 | interface.disable() |
|
244 | 1 | interface.lldp = iface_att['lldp'] |
|
245 | 1 | interface.extend_metadata(iface_att["metadata"]) |
|
246 | 1 | interface.deactivate() |
|
247 | 1 | name = 'kytos/topology.port.created' |
|
248 | 1 | event = KytosEvent(name=name, content={ |
|
249 | 'switch': switch_id, |
||
250 | 'port': interface.port_number, |
||
251 | 'port_description': { |
||
252 | 'alias': interface.name, |
||
253 | 'mac': interface.address, |
||
254 | 'state': interface.state |
||
255 | } |
||
256 | }) |
||
257 | 1 | self.controller.buffers.app.put(event, timeout=1) |
|
258 | |||
259 | 1 | intf_ids = [v["id"] for v in switch_att.get("interfaces", {}).values()] |
|
260 | 1 | intf_details = self.topo_controller.get_interfaces_details(intf_ids) |
|
261 | 1 | with self._links_lock: |
|
262 | 1 | self.load_interfaces_tags_values(switch, intf_details) |
|
263 | |||
264 | # pylint: disable=attribute-defined-outside-init |
||
265 | 1 | def load_topology(self): |
|
266 | """Load network topology from DB.""" |
||
267 | 1 | topology = self.topo_controller.get_topology() |
|
268 | 1 | switches = topology["topology"]["switches"] |
|
269 | 1 | links = topology["topology"]["links"] |
|
270 | |||
271 | 1 | failed_switches = {} |
|
272 | 1 | log.debug(f"_load_network_status switches={switches}") |
|
273 | 1 | for switch_id, switch_att in switches.items(): |
|
274 | 1 | try: |
|
275 | 1 | self._load_switch(switch_id, switch_att) |
|
276 | 1 | except (KeyError, AttributeError, TypeError) as err: |
|
277 | 1 | failed_switches[switch_id] = err |
|
278 | 1 | log.error(f'Error loading switch: {err}') |
|
279 | |||
280 | 1 | failed_links = {} |
|
281 | 1 | log.debug(f"_load_network_status links={links}") |
|
282 | 1 | for link_id, link_att in links.items(): |
|
283 | 1 | try: |
|
284 | 1 | self._load_link(link_att) |
|
285 | 1 | except (KeyError, AttributeError, TypeError) as err: |
|
286 | 1 | failed_links[link_id] = err |
|
287 | 1 | log.error(f'Error loading link {link_id}: {err}') |
|
288 | |||
289 | 1 | name = 'kytos/topology.topology_loaded' |
|
290 | 1 | event = KytosEvent( |
|
291 | name=name, |
||
292 | content={ |
||
293 | 'topology': self._get_topology(), |
||
294 | 'failed_switches': failed_switches, |
||
295 | 'failed_links': failed_links |
||
296 | }) |
||
297 | 1 | self.controller.buffers.app.put(event, timeout=1) |
|
298 | |||
299 | 1 | @rest('v3/') |
|
300 | 1 | def get_topology(self, _request: Request) -> JSONResponse: |
|
301 | """Return the latest known topology. |
||
302 | |||
303 | This topology is updated when there are network events. |
||
304 | """ |
||
305 | 1 | return JSONResponse(self._get_topology_dict()) |
|
306 | |||
307 | # Switch related methods |
||
308 | 1 | @rest('v3/switches') |
|
309 | 1 | def get_switches(self, _request: Request) -> JSONResponse: |
|
310 | """Return a json with all the switches in the topology.""" |
||
311 | return JSONResponse(self._get_switches_dict()) |
||
312 | |||
313 | 1 | @rest('v3/switches/{dpid}/enable', methods=['POST']) |
|
314 | 1 | def enable_switch(self, request: Request) -> JSONResponse: |
|
315 | """Administratively enable a switch in the topology.""" |
||
316 | 1 | dpid = request.path_params["dpid"] |
|
317 | 1 | try: |
|
318 | 1 | switch = self.controller.switches[dpid] |
|
319 | 1 | self.topo_controller.enable_switch(dpid) |
|
320 | 1 | switch.enable() |
|
321 | 1 | except KeyError: |
|
322 | 1 | raise HTTPException(404, detail="Switch not found") |
|
323 | |||
324 | 1 | self.notify_topology_update() |
|
325 | 1 | self.notify_switch_enabled(dpid) |
|
326 | 1 | self.notify_switch_links_status(switch, "link enabled") |
|
327 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
328 | |||
329 | 1 | @rest('v3/switches/{dpid}/disable', methods=['POST']) |
|
330 | 1 | def disable_switch(self, request: Request) -> JSONResponse: |
|
331 | """Administratively disable a switch in the topology.""" |
||
332 | 1 | dpid = request.path_params["dpid"] |
|
333 | 1 | try: |
|
334 | 1 | switch = self.controller.switches[dpid] |
|
335 | 1 | link_ids = set() |
|
336 | 1 | for _, interface in switch.interfaces.copy().items(): |
|
337 | 1 | if (interface.link and interface.link.is_enabled()): |
|
338 | 1 | link_ids.add(interface.link.id) |
|
339 | 1 | interface.link.disable() |
|
340 | 1 | self.notify_link_enabled_state(interface.link, "disabled") |
|
341 | 1 | self.topo_controller.bulk_disable_links(link_ids) |
|
342 | 1 | self.topo_controller.disable_switch(dpid) |
|
343 | 1 | switch.disable() |
|
344 | 1 | except KeyError: |
|
345 | 1 | raise HTTPException(404, detail="Switch not found") |
|
346 | |||
347 | 1 | self.notify_topology_update() |
|
348 | 1 | self.notify_switch_disabled(dpid) |
|
349 | 1 | self.notify_switch_links_status(switch, "link disabled") |
|
350 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
351 | |||
352 | 1 | @rest('v3/switches/{dpid}', methods=['DELETE']) |
|
353 | 1 | def delete_switch(self, request: Request) -> JSONResponse: |
|
354 | """Delete a switch. |
||
355 | |||
356 | Requirements: |
||
357 | - There should not be installed flows related to switch. |
||
358 | - The switch should be disabled. |
||
359 | - All tags from switch interfaces should be available. |
||
360 | - The switch should not have links. |
||
361 | """ |
||
362 | 1 | dpid = request.path_params["dpid"] |
|
363 | 1 | try: |
|
364 | 1 | switch: Switch = self.controller.switches[dpid] |
|
365 | 1 | with self._switch_lock[dpid]: |
|
366 | 1 | if switch.status != EntityStatus.DISABLED: |
|
367 | 1 | raise HTTPException( |
|
368 | 409, detail="Switch should be disabled." |
||
369 | ) |
||
370 | 1 | for intf_id, interface in switch.interfaces.copy().items(): |
|
371 | 1 | if not interface.all_tags_available(): |
|
372 | 1 | detail = f"Interface {intf_id} vlans are being used."\ |
|
373 | " Delete any service using vlans." |
||
374 | 1 | raise HTTPException(409, detail=detail) |
|
375 | 1 | with self._links_lock: |
|
376 | 1 | for link_id, link in self.links.items(): |
|
377 | 1 | if (dpid in |
|
378 | (link.endpoint_a.switch.dpid, |
||
379 | link.endpoint_b.switch.dpid)): |
||
380 | 1 | raise HTTPException( |
|
381 | 409, detail=f"Switch should not have links. " |
||
382 | f"Link found {link_id}." |
||
383 | ) |
||
384 | 1 | try: |
|
385 | 1 | flows = self.get_flows_by_switch(dpid) |
|
386 | except tenacity.RetryError as err: |
||
387 | detail = "Error while getting flows: "\ |
||
388 | f"{err.last_attempt.exception()}." |
||
389 | raise HTTPException(409, detail=detail) |
||
390 | 1 | if flows: |
|
391 | raise HTTPException(409, detail="Switch has flows. Verify" |
||
392 | " if a switch is used.") |
||
393 | 1 | switch = self.controller.switches.pop(dpid) |
|
394 | 1 | self.topo_controller.delete_switch_data(dpid) |
|
395 | 1 | except KeyError: |
|
396 | 1 | raise HTTPException(404, detail="Switch not found.") |
|
397 | 1 | name = 'kytos/topology.switch.deleted' |
|
398 | 1 | event = KytosEvent(name=name, content={'switch': switch}) |
|
399 | 1 | self.controller.buffers.app.put(event) |
|
400 | 1 | self.notify_topology_update() |
|
401 | 1 | return JSONResponse("Operation successful") |
|
402 | |||
403 | 1 | @rest('v3/switches/{dpid}/metadata') |
|
404 | 1 | def get_switch_metadata(self, request: Request) -> JSONResponse: |
|
405 | """Get metadata from a switch.""" |
||
406 | 1 | dpid = request.path_params["dpid"] |
|
407 | 1 | try: |
|
408 | 1 | metadata = self.controller.switches[dpid].metadata |
|
409 | 1 | return JSONResponse({"metadata": metadata}) |
|
410 | 1 | except KeyError: |
|
411 | 1 | raise HTTPException(404, detail="Switch not found") |
|
412 | |||
413 | 1 | @rest('v3/switches/{dpid}/metadata', methods=['POST']) |
|
414 | 1 | def add_switch_metadata(self, request: Request) -> JSONResponse: |
|
415 | """Add metadata to a switch.""" |
||
416 | 1 | dpid = request.path_params["dpid"] |
|
417 | 1 | metadata = self._get_metadata(request) |
|
418 | 1 | try: |
|
419 | 1 | switch = self.controller.switches[dpid] |
|
420 | 1 | except KeyError: |
|
421 | 1 | raise HTTPException(404, detail="Switch not found") |
|
422 | |||
423 | 1 | self.topo_controller.add_switch_metadata(dpid, metadata) |
|
424 | 1 | switch.extend_metadata(metadata) |
|
425 | 1 | self.notify_metadata_changes(switch, 'added') |
|
426 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
427 | |||
428 | 1 | @rest('v3/switches/{dpid}/metadata/{key}', methods=['DELETE']) |
|
429 | 1 | def delete_switch_metadata(self, request: Request) -> JSONResponse: |
|
430 | """Delete metadata from a switch.""" |
||
431 | 1 | dpid = request.path_params["dpid"] |
|
432 | 1 | key = request.path_params["key"] |
|
433 | 1 | try: |
|
434 | 1 | switch = self.controller.switches[dpid] |
|
435 | 1 | except KeyError: |
|
436 | 1 | raise HTTPException(404, detail="Switch not found") |
|
437 | |||
438 | 1 | try: |
|
439 | 1 | _ = switch.metadata[key] |
|
440 | 1 | except KeyError: |
|
441 | 1 | raise HTTPException(404, "Metadata not found") |
|
442 | |||
443 | 1 | self.topo_controller.delete_switch_metadata_key(dpid, key) |
|
444 | 1 | switch.remove_metadata(key) |
|
445 | 1 | self.notify_metadata_changes(switch, 'removed') |
|
446 | 1 | return JSONResponse("Operation successful") |
|
447 | |||
448 | # Interface related methods |
||
449 | 1 | @rest('v3/interfaces') |
|
450 | 1 | def get_interfaces(self, _request: Request) -> JSONResponse: |
|
451 | """Return a json with all the interfaces in the topology.""" |
||
452 | 1 | interfaces = {} |
|
453 | 1 | switches = self._get_switches_dict() |
|
454 | 1 | for switch in switches['switches'].values(): |
|
455 | 1 | for interface_id, interface in switch['interfaces'].items(): |
|
456 | 1 | interfaces[interface_id] = interface |
|
457 | |||
458 | 1 | return JSONResponse({'interfaces': interfaces}) |
|
459 | |||
460 | 1 | @rest('v3/interfaces/switch/{dpid}/enable', methods=['POST']) |
|
461 | 1 | @rest('v3/interfaces/{interface_enable_id}/enable', methods=['POST']) |
|
462 | 1 | def enable_interface(self, request: Request) -> JSONResponse: |
|
463 | """Administratively enable interfaces in the topology.""" |
||
464 | 1 | interface_enable_id = request.path_params.get("interface_enable_id") |
|
465 | 1 | dpid = request.path_params.get("dpid") |
|
466 | 1 | if dpid is None: |
|
467 | 1 | dpid = ":".join(interface_enable_id.split(":")[:-1]) |
|
468 | 1 | try: |
|
469 | 1 | switch = self.controller.switches[dpid] |
|
470 | 1 | if not switch.is_enabled(): |
|
471 | 1 | raise HTTPException(409, detail="Enable Switch first") |
|
472 | 1 | except KeyError: |
|
473 | 1 | raise HTTPException(404, detail="Switch not found") |
|
474 | |||
475 | 1 | if interface_enable_id: |
|
476 | 1 | interface_number = int(interface_enable_id.split(":")[-1]) |
|
477 | |||
478 | 1 | try: |
|
479 | 1 | interface = switch.interfaces[interface_number] |
|
480 | 1 | self.topo_controller.enable_interface(interface.id) |
|
481 | 1 | interface.enable() |
|
482 | 1 | self.notify_interface_link_status(interface, "link enabled") |
|
483 | 1 | except KeyError: |
|
484 | 1 | msg = f"Switch {dpid} interface {interface_number} not found" |
|
485 | 1 | raise HTTPException(404, detail=msg) |
|
486 | else: |
||
487 | 1 | for interface in switch.interfaces.copy().values(): |
|
488 | 1 | interface.enable() |
|
489 | 1 | self.notify_interface_link_status(interface, "link enabled") |
|
490 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
|
491 | 1 | self.notify_topology_update() |
|
492 | 1 | return JSONResponse("Operation successful") |
|
493 | |||
494 | 1 | @rest('v3/interfaces/switch/{dpid}/disable', methods=['POST']) |
|
495 | 1 | @rest('v3/interfaces/{interface_disable_id}/disable', methods=['POST']) |
|
496 | 1 | def disable_interface(self, request: Request) -> JSONResponse: |
|
497 | """Administratively disable interfaces in the topology.""" |
||
498 | 1 | interface_disable_id = request.path_params.get("interface_disable_id") |
|
499 | 1 | dpid = request.path_params.get("dpid") |
|
500 | 1 | if dpid is None: |
|
501 | 1 | dpid = ":".join(interface_disable_id.split(":")[:-1]) |
|
502 | 1 | try: |
|
503 | 1 | switch = self.controller.switches[dpid] |
|
504 | 1 | except KeyError: |
|
505 | 1 | raise HTTPException(404, detail="Switch not found") |
|
506 | |||
507 | 1 | if interface_disable_id: |
|
508 | 1 | interface_number = int(interface_disable_id.split(":")[-1]) |
|
509 | |||
510 | 1 | try: |
|
511 | 1 | interface = switch.interfaces[interface_number] |
|
512 | 1 | self.topo_controller.disable_interface(interface.id) |
|
513 | 1 | if interface.link and interface.link.is_enabled(): |
|
514 | 1 | self.topo_controller.disable_link(interface.link.id) |
|
515 | 1 | interface.link.disable() |
|
516 | 1 | self.notify_link_enabled_state(interface.link, "disabled") |
|
517 | 1 | interface.disable() |
|
518 | 1 | self.notify_interface_link_status(interface, "link disabled") |
|
519 | 1 | except KeyError: |
|
520 | 1 | msg = f"Switch {dpid} interface {interface_number} not found" |
|
521 | 1 | raise HTTPException(404, detail=msg) |
|
522 | else: |
||
523 | 1 | link_ids = set() |
|
524 | 1 | for interface in switch.interfaces.copy().values(): |
|
525 | 1 | if interface.link and interface.link.is_enabled(): |
|
526 | 1 | link_ids.add(interface.link.id) |
|
527 | 1 | interface.link.disable() |
|
528 | 1 | self.notify_link_enabled_state(interface.link, "disabled") |
|
529 | 1 | interface.disable() |
|
530 | 1 | self.notify_interface_link_status(interface, "link disabled") |
|
531 | 1 | self.topo_controller.bulk_disable_links(link_ids) |
|
532 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
|
533 | 1 | self.notify_topology_update() |
|
534 | 1 | return JSONResponse("Operation successful") |
|
535 | |||
536 | 1 | @rest('v3/interfaces/{interface_id}/metadata') |
|
537 | 1 | def get_interface_metadata(self, request: Request) -> JSONResponse: |
|
538 | """Get metadata from an interface.""" |
||
539 | 1 | interface_id = request.path_params["interface_id"] |
|
540 | 1 | switch_id = ":".join(interface_id.split(":")[:-1]) |
|
541 | 1 | interface_number = int(interface_id.split(":")[-1]) |
|
542 | 1 | try: |
|
543 | 1 | switch = self.controller.switches[switch_id] |
|
544 | 1 | except KeyError: |
|
545 | 1 | raise HTTPException(404, detail="Switch not found") |
|
546 | |||
547 | 1 | try: |
|
548 | 1 | interface = switch.interfaces[interface_number] |
|
549 | 1 | except KeyError: |
|
550 | 1 | raise HTTPException(404, detail="Interface not found") |
|
551 | |||
552 | 1 | return JSONResponse({"metadata": interface.metadata}) |
|
553 | |||
554 | 1 | @rest('v3/interfaces/{interface_id}/metadata', methods=['POST']) |
|
555 | 1 | def add_interface_metadata(self, request: Request) -> JSONResponse: |
|
556 | """Add metadata to an interface.""" |
||
557 | 1 | interface_id = request.path_params["interface_id"] |
|
558 | 1 | metadata = self._get_metadata(request) |
|
559 | 1 | switch_id = ":".join(interface_id.split(":")[:-1]) |
|
560 | 1 | interface_number = int(interface_id.split(":")[-1]) |
|
561 | 1 | try: |
|
562 | 1 | switch = self.controller.switches[switch_id] |
|
563 | 1 | except KeyError: |
|
564 | 1 | raise HTTPException(404, detail="Switch not found") |
|
565 | |||
566 | 1 | try: |
|
567 | 1 | interface = switch.interfaces[interface_number] |
|
568 | 1 | self.topo_controller.add_interface_metadata(interface.id, metadata) |
|
569 | 1 | except KeyError: |
|
570 | 1 | raise HTTPException(404, detail="Interface not found") |
|
571 | |||
572 | 1 | interface.extend_metadata(metadata) |
|
573 | 1 | self.notify_metadata_changes(interface, 'added') |
|
574 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
575 | |||
576 | 1 | @rest('v3/interfaces/{interface_id}/metadata/{key}', methods=['DELETE']) |
|
577 | 1 | def delete_interface_metadata(self, request: Request) -> JSONResponse: |
|
578 | """Delete metadata from an interface.""" |
||
579 | 1 | interface_id = request.path_params["interface_id"] |
|
580 | 1 | key = request.path_params["key"] |
|
581 | 1 | switch_id = ":".join(interface_id.split(":")[:-1]) |
|
582 | 1 | try: |
|
583 | 1 | interface_number = int(interface_id.split(":")[-1]) |
|
584 | except ValueError: |
||
585 | detail = f"Invalid interface_id {interface_id}" |
||
586 | raise HTTPException(400, detail=detail) |
||
587 | |||
588 | 1 | try: |
|
589 | 1 | switch = self.controller.switches[switch_id] |
|
590 | 1 | except KeyError: |
|
591 | 1 | raise HTTPException(404, detail="Switch not found") |
|
592 | |||
593 | 1 | try: |
|
594 | 1 | interface = switch.interfaces[interface_number] |
|
595 | 1 | except KeyError: |
|
596 | 1 | raise HTTPException(404, detail="Interface not found") |
|
597 | |||
598 | 1 | try: |
|
599 | 1 | _ = interface.metadata[key] |
|
600 | 1 | except KeyError: |
|
601 | 1 | raise HTTPException(404, detail="Metadata not found") |
|
602 | |||
603 | 1 | self.topo_controller.delete_interface_metadata_key(interface.id, key) |
|
604 | 1 | interface.remove_metadata(key) |
|
605 | 1 | self.notify_metadata_changes(interface, 'removed') |
|
606 | 1 | return JSONResponse("Operation successful") |
|
607 | |||
608 | 1 | @rest('v3/interfaces/{interface_id}/tag_ranges', methods=['POST']) |
|
609 | 1 | @validate_openapi(spec) |
|
610 | 1 | def set_tag_range(self, request: Request) -> JSONResponse: |
|
611 | """Set tag range""" |
||
612 | 1 | content_type_json_or_415(request) |
|
613 | 1 | content = get_json_or_400(request, self.controller.loop) |
|
614 | 1 | tag_type = content.get("tag_type") |
|
615 | 1 | try: |
|
616 | 1 | ranges = get_tag_ranges(content["tag_ranges"]) |
|
617 | except KytosInvalidTagRanges as err: |
||
618 | raise HTTPException(400, detail=str(err)) |
||
619 | 1 | interface_id = request.path_params["interface_id"] |
|
620 | 1 | interface = self.controller.get_interface_by_id(interface_id) |
|
621 | 1 | if not interface: |
|
622 | 1 | raise HTTPException(404, detail="Interface not found") |
|
623 | 1 | try: |
|
624 | 1 | interface.set_tag_ranges(ranges, tag_type) |
|
625 | 1 | self.handle_on_interface_tags(interface) |
|
626 | 1 | except KytosTagError as err: |
|
627 | 1 | raise HTTPException(400, detail=str(err)) |
|
628 | 1 | return JSONResponse("Operation Successful", status_code=200) |
|
629 | |||
630 | 1 | @rest('v3/interfaces/{interface_id}/tag_ranges', methods=['DELETE']) |
|
631 | 1 | @validate_openapi(spec) |
|
632 | 1 | def delete_tag_range(self, request: Request) -> JSONResponse: |
|
633 | """Set tag_range from tag_type to default value [1, 4095]""" |
||
634 | 1 | interface_id = request.path_params["interface_id"] |
|
635 | 1 | params = request.query_params |
|
636 | 1 | tag_type = params.get("tag_type", 'vlan') |
|
637 | 1 | interface = self.controller.get_interface_by_id(interface_id) |
|
638 | 1 | if not interface: |
|
639 | 1 | raise HTTPException(404, detail="Interface not found") |
|
640 | 1 | try: |
|
641 | 1 | interface.remove_tag_ranges(tag_type) |
|
642 | 1 | self.handle_on_interface_tags(interface) |
|
643 | 1 | except KytosTagError as err: |
|
644 | 1 | raise HTTPException(400, detail=str(err)) |
|
645 | 1 | return JSONResponse("Operation Successful", status_code=200) |
|
646 | |||
647 | 1 | @rest('v3/interfaces/{interface_id}/special_tags', methods=['POST']) |
|
648 | 1 | @validate_openapi(spec) |
|
649 | 1 | def set_special_tags(self, request: Request) -> JSONResponse: |
|
650 | """Set special_tags""" |
||
651 | 1 | content_type_json_or_415(request) |
|
652 | 1 | content = get_json_or_400(request, self.controller.loop) |
|
653 | 1 | tag_type = content.get("tag_type") |
|
654 | 1 | special_tags = content["special_tags"] |
|
655 | 1 | interface_id = request.path_params["interface_id"] |
|
656 | 1 | interface = self.controller.get_interface_by_id(interface_id) |
|
657 | 1 | if not interface: |
|
658 | 1 | raise HTTPException(404, detail="Interface not found") |
|
659 | 1 | try: |
|
660 | 1 | interface.set_special_tags(tag_type, special_tags) |
|
661 | 1 | self.handle_on_interface_tags(interface) |
|
662 | 1 | except KytosTagError as err: |
|
663 | 1 | raise HTTPException(400, detail=str(err)) |
|
664 | 1 | return JSONResponse("Operation Successful", status_code=200) |
|
665 | |||
666 | 1 | @rest('v3/interfaces/tag_ranges', methods=['GET']) |
|
667 | 1 | @validate_openapi(spec) |
|
668 | 1 | def get_all_tag_ranges(self, _: Request) -> JSONResponse: |
|
669 | """Get all tag_ranges, available_tags, special_tags |
||
670 | and special_available_tags from interfaces""" |
||
671 | 1 | result = {} |
|
672 | 1 | for switch in self.controller.switches.copy().values(): |
|
673 | 1 | for interface in switch.interfaces.copy().values(): |
|
674 | 1 | result[interface.id] = { |
|
675 | "available_tags": interface.available_tags, |
||
676 | "tag_ranges": interface.tag_ranges, |
||
677 | "special_tags": interface.special_tags, |
||
678 | "special_available_tags": interface.special_available_tags |
||
679 | } |
||
680 | 1 | return JSONResponse(result, status_code=200) |
|
681 | |||
682 | 1 | @rest('v3/interfaces/{interface_id}/tag_ranges', methods=['GET']) |
|
683 | 1 | @validate_openapi(spec) |
|
684 | 1 | def get_tag_ranges_by_intf(self, request: Request) -> JSONResponse: |
|
685 | """Get tag_ranges, available_tags, special_tags |
||
686 | and special_available_tags from an interface""" |
||
687 | 1 | interface_id = request.path_params["interface_id"] |
|
688 | 1 | interface = self.controller.get_interface_by_id(interface_id) |
|
689 | 1 | if not interface: |
|
690 | 1 | raise HTTPException(404, detail="Interface not found") |
|
691 | 1 | result = { |
|
692 | interface_id: { |
||
693 | "available_tags": interface.available_tags, |
||
694 | "tag_ranges": interface.tag_ranges, |
||
695 | "special_tags": interface.special_tags, |
||
696 | "special_available_tags": interface.special_available_tags |
||
697 | } |
||
698 | } |
||
699 | 1 | return JSONResponse(result, status_code=200) |
|
700 | |||
701 | # Link related methods |
||
702 | 1 | @rest('v3/links') |
|
703 | 1 | def get_links(self, _request: Request) -> JSONResponse: |
|
704 | """Return a json with all the links in the topology. |
||
705 | |||
706 | Links are connections between interfaces. |
||
707 | """ |
||
708 | return JSONResponse(self._get_links_dict()) |
||
709 | |||
710 | 1 | @rest('v3/links/{link_id}/enable', methods=['POST']) |
|
711 | 1 | def enable_link(self, request: Request) -> JSONResponse: |
|
712 | """Administratively enable a link in the topology.""" |
||
713 | 1 | link_id = request.path_params["link_id"] |
|
714 | 1 | try: |
|
715 | 1 | with self._links_lock: |
|
716 | 1 | link = self.links[link_id] |
|
717 | 1 | if not link.endpoint_a.is_enabled(): |
|
718 | 1 | detail = f"{link.endpoint_a.id} needs enabling." |
|
719 | 1 | raise HTTPException(409, detail=detail) |
|
720 | 1 | if not link.endpoint_b.is_enabled(): |
|
721 | 1 | detail = f"{link.endpoint_b.id} needs enabling." |
|
722 | 1 | raise HTTPException(409, detail=detail) |
|
723 | 1 | if not link.is_enabled(): |
|
724 | 1 | self.topo_controller.enable_link(link.id) |
|
725 | 1 | link.enable() |
|
726 | 1 | self.notify_link_enabled_state(link, "enabled") |
|
727 | 1 | except KeyError: |
|
728 | 1 | raise HTTPException(404, detail="Link not found") |
|
729 | 1 | self.notify_link_status_change( |
|
730 | self.links[link_id], |
||
731 | reason='link enabled' |
||
732 | ) |
||
733 | 1 | self.notify_topology_update() |
|
734 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
735 | |||
736 | 1 | @rest('v3/links/{link_id}/disable', methods=['POST']) |
|
737 | 1 | def disable_link(self, request: Request) -> JSONResponse: |
|
738 | """Administratively disable a link in the topology.""" |
||
739 | 1 | link_id = request.path_params["link_id"] |
|
740 | 1 | try: |
|
741 | 1 | with self._links_lock: |
|
742 | 1 | link = self.links[link_id] |
|
743 | 1 | if link.is_enabled(): |
|
744 | 1 | self.topo_controller.disable_link(link.id) |
|
745 | 1 | link.disable() |
|
746 | 1 | self.notify_link_enabled_state(link, "disabled") |
|
747 | 1 | except KeyError: |
|
748 | 1 | raise HTTPException(404, detail="Link not found") |
|
749 | 1 | self.notify_link_status_change( |
|
750 | self.links[link_id], |
||
751 | reason='link disabled' |
||
752 | ) |
||
753 | 1 | self.notify_topology_update() |
|
754 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
755 | |||
756 | 1 | def notify_link_enabled_state(self, link: Link, action: str): |
|
757 | """Send a KytosEvent whether a link status (enabled/disabled) |
||
758 | has changed its status.""" |
||
759 | 1 | name = f'kytos/topology.link.{action}' |
|
760 | 1 | content = {'link': link} |
|
761 | 1 | event = KytosEvent(name=name, content=content) |
|
762 | 1 | self.controller.buffers.app.put(event) |
|
763 | |||
764 | 1 | @rest('v3/links/{link_id}/metadata') |
|
765 | 1 | def get_link_metadata(self, request: Request) -> JSONResponse: |
|
766 | """Get metadata from a link.""" |
||
767 | 1 | link_id = request.path_params["link_id"] |
|
768 | 1 | try: |
|
769 | 1 | return JSONResponse({"metadata": self.links[link_id].metadata}) |
|
770 | 1 | except KeyError: |
|
771 | 1 | raise HTTPException(404, detail="Link not found") |
|
772 | |||
773 | 1 | @rest('v3/links/{link_id}/metadata', methods=['POST']) |
|
774 | 1 | def add_link_metadata(self, request: Request) -> JSONResponse: |
|
775 | """Add metadata to a link.""" |
||
776 | 1 | link_id = request.path_params["link_id"] |
|
777 | 1 | metadata = self._get_metadata(request) |
|
778 | 1 | try: |
|
779 | 1 | link = self.links[link_id] |
|
780 | 1 | except KeyError: |
|
781 | 1 | raise HTTPException(404, detail="Link not found") |
|
782 | |||
783 | 1 | self.topo_controller.add_link_metadata(link_id, metadata) |
|
784 | 1 | link.extend_metadata(metadata) |
|
785 | 1 | self.notify_metadata_changes(link, 'added') |
|
786 | 1 | self.notify_topology_update() |
|
787 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
788 | |||
789 | 1 | @rest('v3/links/{link_id}/metadata/{key}', methods=['DELETE']) |
|
790 | 1 | def delete_link_metadata(self, request: Request) -> JSONResponse: |
|
791 | """Delete metadata from a link.""" |
||
792 | 1 | link_id = request.path_params["link_id"] |
|
793 | 1 | key = request.path_params["key"] |
|
794 | 1 | try: |
|
795 | 1 | link = self.links[link_id] |
|
796 | 1 | except KeyError: |
|
797 | 1 | raise HTTPException(404, detail="Link not found") |
|
798 | |||
799 | 1 | try: |
|
800 | 1 | _ = link.metadata[key] |
|
801 | 1 | except KeyError: |
|
802 | 1 | raise HTTPException(404, detail="Metadata not found") |
|
803 | |||
804 | 1 | self.topo_controller.delete_link_metadata_key(link.id, key) |
|
805 | 1 | link.remove_metadata(key) |
|
806 | 1 | self.notify_metadata_changes(link, 'removed') |
|
807 | 1 | self.notify_topology_update() |
|
808 | 1 | return JSONResponse("Operation successful") |
|
809 | |||
810 | 1 | @rest('v3/links/{link_id}', methods=['DELETE']) |
|
811 | 1 | def delete_link(self, request: Request) -> JSONResponse: |
|
812 | """Delete a disabled link from topology. |
||
813 | It won't work for link with other statuses. |
||
814 | """ |
||
815 | 1 | link_id = request.path_params["link_id"] |
|
816 | 1 | try: |
|
817 | 1 | with self._links_lock: |
|
818 | 1 | link = self.links[link_id] |
|
819 | 1 | if link.status != EntityStatus.DISABLED: |
|
820 | 1 | raise HTTPException(409, detail="Link is not disabled.") |
|
821 | 1 | if link.endpoint_a.link and link == link.endpoint_a.link: |
|
822 | 1 | switch = link.endpoint_a.switch |
|
823 | 1 | link.endpoint_a.link = None |
|
824 | 1 | link.endpoint_a.nni = False |
|
825 | 1 | self.topo_controller.upsert_switch( |
|
826 | switch.id, switch.as_dict() |
||
827 | ) |
||
828 | 1 | if link.endpoint_b.link and link == link.endpoint_b.link: |
|
829 | 1 | switch = link.endpoint_b.switch |
|
830 | 1 | link.endpoint_b.link = None |
|
831 | 1 | link.endpoint_b.nni = False |
|
832 | 1 | self.topo_controller.upsert_switch( |
|
833 | switch.id, switch.as_dict() |
||
834 | ) |
||
835 | 1 | self.topo_controller.delete_link(link_id) |
|
836 | 1 | link = self.links.pop(link_id) |
|
837 | 1 | except KeyError: |
|
838 | 1 | raise HTTPException(404, detail="Link not found.") |
|
839 | 1 | self.notify_topology_update() |
|
840 | 1 | name = 'kytos/topology.link.deleted' |
|
841 | 1 | event = KytosEvent(name=name, content={'link': link}) |
|
842 | 1 | self.controller.buffers.app.put(event) |
|
843 | 1 | return JSONResponse("Operation successful") |
|
844 | |||
845 | 1 | @rest('v3/interfaces/{intf_id}', methods=['DELETE']) |
|
846 | 1 | def delete_interface(self, request: Request) -> JSONResponse: |
|
847 | """Delete an interface only if it is not used.""" |
||
848 | 1 | intf_id = request.path_params.get("intf_id") |
|
849 | 1 | intf_split = intf_id.split(":") |
|
850 | 1 | switch_id = ":".join(intf_split[:-1]) |
|
851 | 1 | try: |
|
852 | 1 | intf_port = int(intf_split[-1]) |
|
853 | 1 | except ValueError: |
|
854 | 1 | raise HTTPException(400, detail="Invalid interface id.") |
|
855 | 1 | try: |
|
856 | 1 | switch = self.controller.switches[switch_id] |
|
857 | 1 | except KeyError: |
|
858 | 1 | raise HTTPException(404, detail="Switch not found.") |
|
859 | 1 | try: |
|
860 | 1 | interface = switch.interfaces[intf_port] |
|
861 | 1 | except KeyError: |
|
862 | 1 | raise HTTPException(404, detail="Interface not found.") |
|
863 | |||
864 | 1 | usage = self.get_intf_usage(interface) |
|
865 | 1 | if usage: |
|
866 | 1 | raise HTTPException(409, detail=f"Interface could not be " |
|
867 | f"deleted. Reason: {usage}") |
||
868 | 1 | self._delete_interface(interface) |
|
869 | 1 | return JSONResponse("Operation Successful", status_code=200) |
|
870 | |||
871 | 1 | @listen_to( |
|
872 | "kytos/.*.liveness.(up|down|disabled)", |
||
873 | pool="dynamic_single" |
||
874 | ) |
||
875 | 1 | def on_link_liveness(self, event) -> None: |
|
876 | """Handle link liveness up|down|disabled event.""" |
||
877 | with self._links_lock: |
||
878 | liveness_status = event.name.split(".")[-1] |
||
879 | if liveness_status == "disabled": |
||
880 | interfaces = event.content["interfaces"] |
||
881 | self.handle_link_liveness_disabled(interfaces) |
||
882 | elif liveness_status in ("up", "down"): |
||
883 | link = Link(event.content["interface_a"], |
||
884 | event.content["interface_b"]) |
||
885 | try: |
||
886 | link = self.links[link.id] |
||
887 | except KeyError: |
||
888 | log.error(f"Link id {link.id} not found, {link}") |
||
889 | return |
||
890 | self.handle_link_liveness_status(self.links[link.id], |
||
891 | liveness_status) |
||
892 | |||
893 | 1 | def handle_link_liveness_status(self, link, liveness_status) -> None: |
|
894 | """Handle link liveness.""" |
||
895 | 1 | metadata = {"liveness_status": liveness_status} |
|
896 | 1 | log.info(f"Link liveness {liveness_status}: {link}") |
|
897 | 1 | link.extend_metadata(metadata) |
|
898 | 1 | self.notify_topology_update() |
|
899 | 1 | if link.status == EntityStatus.UP and liveness_status == "up": |
|
900 | 1 | self.notify_link_status_change(link, reason="liveness_up") |
|
901 | 1 | if link.status == EntityStatus.DOWN and liveness_status == "down": |
|
902 | 1 | self.notify_link_status_change(link, reason="liveness_down") |
|
903 | |||
904 | 1 | def get_links_from_interfaces(self, interfaces) -> dict: |
|
905 | """Get links from interfaces.""" |
||
906 | 1 | links_found = {} |
|
907 | 1 | for interface in interfaces: |
|
908 | 1 | for link in list(self.links.values()): |
|
909 | 1 | if any(( |
|
910 | interface.id == link.endpoint_a.id, |
||
911 | interface.id == link.endpoint_b.id, |
||
912 | )): |
||
913 | 1 | links_found[link.id] = link |
|
914 | 1 | return links_found |
|
915 | |||
916 | 1 | def handle_link_liveness_disabled(self, interfaces) -> None: |
|
917 | """Handle link liveness disabled.""" |
||
918 | 1 | log.info(f"Link liveness disabled interfaces: {interfaces}") |
|
919 | |||
920 | 1 | key = "liveness_status" |
|
921 | 1 | links = self.get_links_from_interfaces(interfaces) |
|
922 | 1 | for link in links.values(): |
|
923 | 1 | link.remove_metadata(key) |
|
924 | 1 | self.notify_topology_update() |
|
925 | 1 | for link in links.values(): |
|
926 | 1 | self.notify_link_status_change(link, reason="liveness_disabled") |
|
927 | |||
928 | 1 | @listen_to("kytos/core.interface_tags") |
|
929 | 1 | def on_interface_tags(self, event): |
|
930 | """Handle on_interface_tags.""" |
||
931 | interface = event.content['interface'] |
||
932 | with self._intfs_lock[interface.id]: |
||
933 | if ( |
||
934 | interface.id in self._intfs_tags_updated_at |
||
935 | and self._intfs_tags_updated_at[interface.id] > event.timestamp |
||
936 | ): |
||
937 | return |
||
938 | self._intfs_tags_updated_at[interface.id] = event.timestamp |
||
939 | self.handle_on_interface_tags(interface) |
||
940 | |||
941 | 1 | def handle_on_interface_tags(self, interface): |
|
942 | """Update interface details""" |
||
943 | 1 | intf_id = interface.id |
|
944 | 1 | self.topo_controller.upsert_interface_details( |
|
945 | intf_id, interface.available_tags, interface.tag_ranges, |
||
946 | interface.special_available_tags, |
||
947 | interface.special_tags |
||
948 | ) |
||
949 | |||
950 | 1 | @listen_to('.*.switch.(new|reconnected)') |
|
951 | 1 | def on_new_switch(self, event): |
|
952 | """Create a new Device on the Topology. |
||
953 | |||
954 | Handle the event of a new created switch and update the topology with |
||
955 | this new device. Also notify if the switch is enabled. |
||
956 | """ |
||
957 | self.handle_new_switch(event) |
||
958 | |||
959 | 1 | def handle_new_switch(self, event): |
|
960 | """Create a new Device on the Topology.""" |
||
961 | 1 | switch = event.content['switch'] |
|
962 | 1 | switch.activate() |
|
963 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
|
964 | 1 | log.debug('Switch %s added to the Topology.', switch.id) |
|
965 | 1 | self.notify_topology_update() |
|
966 | 1 | if switch.is_enabled(): |
|
967 | 1 | self.notify_switch_enabled(switch.id) |
|
968 | |||
969 | 1 | @listen_to('.*.connection.lost') |
|
970 | 1 | def on_connection_lost(self, event): |
|
971 | """Remove a Device from the topology. |
||
972 | |||
973 | Remove the disconnected Device and every link that has one of its |
||
974 | interfaces. |
||
975 | """ |
||
976 | self.handle_connection_lost(event) |
||
977 | |||
978 | 1 | def handle_connection_lost(self, event): |
|
979 | """Remove a Device from the topology.""" |
||
980 | 1 | switch = event.content['source'].switch |
|
981 | 1 | if switch: |
|
982 | 1 | switch.deactivate() |
|
983 | 1 | log.debug('Switch %s removed from the Topology.', switch.id) |
|
984 | 1 | self.notify_topology_update() |
|
985 | |||
986 | 1 | def handle_interfaces_created(self, event): |
|
987 | """Update the topology based on the interfaces created.""" |
||
988 | 1 | interfaces = event.content["interfaces"] |
|
989 | 1 | if not interfaces: |
|
990 | return |
||
991 | 1 | switch = interfaces[0].switch |
|
992 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
|
993 | 1 | name = "kytos/topology.switch.interface.created" |
|
994 | 1 | for interface in interfaces: |
|
995 | 1 | event = KytosEvent(name=name, content={'interface': interface}) |
|
996 | 1 | self.controller.buffers.app.put(event) |
|
997 | |||
998 | 1 | def handle_interface_created(self, event): |
|
999 | """Update the topology based on an interface created event. |
||
1000 | |||
1001 | It's handled as a link_up in case a switch send a |
||
1002 | created event again and it can be belong to a link. |
||
1003 | """ |
||
1004 | 1 | interface = event.content['interface'] |
|
1005 | 1 | if not interface.is_active(): |
|
1006 | 1 | self.handle_interface_link_down(interface, event) |
|
1007 | else: |
||
1008 | 1 | self.handle_interface_link_up(interface, event) |
|
1009 | |||
1010 | 1 | @listen_to('.*.topology.switch.interface.created') |
|
1011 | 1 | def on_interface_created(self, event): |
|
1012 | """Handle individual interface create event. |
||
1013 | |||
1014 | It's handled as a link_up in case a switch send a |
||
1015 | created event it can belong to an existign link. |
||
1016 | """ |
||
1017 | self.handle_interface_created(event) |
||
1018 | |||
1019 | 1 | @listen_to('.*.switch.interfaces.created') |
|
1020 | 1 | def on_interfaces_created(self, event): |
|
1021 | """Update the topology based on a list of created interfaces.""" |
||
1022 | self.handle_interfaces_created(event) |
||
1023 | |||
1024 | 1 | def handle_interface_down(self, event): |
|
1025 | """Update the topology based on a Port Modify event. |
||
1026 | |||
1027 | The event notifies that an interface was changed to 'down'. |
||
1028 | """ |
||
1029 | 1 | interface = event.content['interface'] |
|
1030 | 1 | with self._intfs_lock[interface.id]: |
|
1031 | 1 | if ( |
|
1032 | interface.id in self._intfs_updated_at |
||
1033 | and self._intfs_updated_at[interface.id] > event.timestamp |
||
1034 | ): |
||
1035 | return |
||
1036 | 1 | self._intfs_updated_at[interface.id] = event.timestamp |
|
1037 | 1 | interface.deactivate() |
|
1038 | 1 | self.handle_interface_link_down(interface, event) |
|
1039 | |||
1040 | 1 | @listen_to('.*.switch.interface.deleted') |
|
1041 | 1 | def on_interface_deleted(self, event): |
|
1042 | """Update the topology based on a Port Delete event.""" |
||
1043 | self.handle_interface_deleted(event) |
||
1044 | |||
1045 | 1 | def handle_interface_deleted(self, event): |
|
1046 | """Update the topology based on a Port Delete event.""" |
||
1047 | 1 | self.handle_interface_down(event) |
|
1048 | 1 | interface = event.content['interface'] |
|
1049 | 1 | usage = self.get_intf_usage(interface) |
|
1050 | 1 | if usage: |
|
1051 | 1 | log.info(f"Interface {interface.id} could not be safely removed." |
|
1052 | f" Reason: {usage}") |
||
1053 | else: |
||
1054 | 1 | self._delete_interface(interface) |
|
1055 | |||
1056 | 1 | def get_intf_usage(self, interface: Interface) -> Optional[str]: |
|
1057 | """Determines how an interface is used explained in a string, |
||
1058 | returns None if unused.""" |
||
1059 | 1 | if interface.is_enabled() or interface.is_active(): |
|
1060 | 1 | return "It is enabled or active." |
|
1061 | |||
1062 | 1 | link = self._get_link_from_interface(interface) |
|
1063 | 1 | if link: |
|
1064 | 1 | return f"It has a link, {link.id}." |
|
1065 | |||
1066 | 1 | flow_id = self.get_flow_id_by_intf(interface) |
|
1067 | 1 | if flow_id: |
|
1068 | 1 | return f"There is a flow installed, {flow_id}." |
|
1069 | |||
1070 | 1 | return None |
|
1071 | |||
1072 | 1 | def get_flow_id_by_intf(self, interface: Interface) -> str: |
|
1073 | """Return flow_id from first found flow used by interface.""" |
||
1074 | 1 | flows = self.get_flows_by_switch(interface.switch.id) |
|
1075 | 1 | port_n = int(interface.id.split(":")[-1]) |
|
1076 | 1 | for flow in flows: |
|
1077 | 1 | in_port = flow["flow"].get("match", {}).get("in_port") |
|
1078 | 1 | if in_port == port_n: |
|
1079 | 1 | return flow["flow_id"] |
|
1080 | |||
1081 | 1 | instructions = flow["flow"].get("instructions", []) |
|
1082 | 1 | for instruction in instructions: |
|
1083 | 1 | if instruction["instruction_type"] == "apply_actions": |
|
1084 | 1 | actions = instruction["actions"] |
|
1085 | 1 | for action in actions: |
|
1086 | 1 | if (action["action_type"] == "output" |
|
1087 | and action.get("port") == port_n): |
||
1088 | 1 | return flow["flow_id"] |
|
1089 | |||
1090 | 1 | actions = flow["flow"].get("actions", []) |
|
1091 | 1 | for action in actions: |
|
1092 | 1 | if (action["action_type"] == "output" |
|
1093 | and action.get("port") == port_n): |
||
1094 | 1 | return flow["flow_id"] |
|
1095 | 1 | return None |
|
1096 | |||
1097 | 1 | def _delete_interface(self, interface: Interface): |
|
1098 | """Delete any trace of an interface. Only use this method when |
||
1099 | it was confirmed that the interface is not used.""" |
||
1100 | 1 | switch: Switch = interface.switch |
|
1101 | 1 | switch.remove_interface(interface) |
|
1102 | 1 | self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
|
1103 | 1 | self.topo_controller.delete_interface_from_details(interface.id) |
|
1104 | |||
1105 | 1 | @listen_to('.*.switch.interface.link_up') |
|
1106 | 1 | def on_interface_link_up(self, event): |
|
1107 | """Update the topology based on a Port Modify event. |
||
1108 | |||
1109 | The event notifies that an interface's link was changed to 'up'. |
||
1110 | """ |
||
1111 | interface = event.content['interface'] |
||
1112 | self.handle_interface_link_up(interface, event) |
||
1113 | |||
1114 | 1 | def handle_interface_link_up(self, interface, event): |
|
1115 | """Update the topology based on a Port Modify event.""" |
||
1116 | 1 | with self._intfs_lock[interface.id]: |
|
1117 | 1 | if ( |
|
1118 | interface.id in self._intfs_updated_at |
||
1119 | and self._intfs_updated_at[interface.id] > event.timestamp |
||
1120 | ): |
||
1121 | 1 | return |
|
1122 | 1 | self._intfs_updated_at[interface.id] = event.timestamp |
|
1123 | 1 | self.handle_link_up(interface) |
|
1124 | |||
1125 | 1 | @tenacity.retry( |
|
1126 | stop=stop_after_attempt(3), |
||
1127 | wait=wait_combine(wait_fixed(3), wait_random(min=2, max=7)), |
||
1128 | before_sleep=before_sleep, |
||
1129 | retry=retry_if_exception_type(httpx.RequestError), |
||
1130 | ) |
||
1131 | 1 | def get_flows_by_switch(self, dpid: str) -> list: |
|
1132 | """Get installed flows by switch from flow_manager.""" |
||
1133 | 1 | endpoint = settings.FLOW_MANAGER_URL +\ |
|
1134 | f'/stored_flows?state=installed&dpid={dpid}' |
||
1135 | 1 | res = httpx.get(endpoint) |
|
1136 | 1 | if res.is_server_error or res.status_code in (404, 400): |
|
1137 | 1 | raise httpx.RequestError(res.text) |
|
1138 | 1 | return res.json().get(dpid, []) |
|
1139 | |||
1140 | 1 | def link_status_hook_link_up_timer(self, link) -> Optional[EntityStatus]: |
|
1141 | """Link status hook link up timer.""" |
||
1142 | 1 | tnow = time.time() |
|
1143 | 1 | if ( |
|
1144 | link.is_active() |
||
1145 | and link.is_enabled() |
||
1146 | and "last_status_change" in link.metadata |
||
1147 | and tnow - link.metadata['last_status_change'] < self.link_up_timer |
||
1148 | ): |
||
1149 | 1 | return EntityStatus.DOWN |
|
1150 | 1 | return None |
|
1151 | |||
1152 | 1 | def notify_link_up_if_status(self, link, reason="link up") -> None: |
|
1153 | """Tries to notify link up and topology changes based on its status |
||
1154 | |||
1155 | Currently, it needs to wait up to a timer.""" |
||
1156 | 1 | time.sleep(self.link_up_timer) |
|
1157 | 1 | if link.status != EntityStatus.UP: |
|
1158 | return |
||
1159 | 1 | with self._links_lock: |
|
1160 | 1 | notified_at = link.get_metadata("notified_up_at") |
|
1161 | 1 | if ( |
|
1162 | notified_at |
||
1163 | and (now() - notified_at.replace(tzinfo=timezone.utc)).seconds |
||
1164 | < self.link_up_timer |
||
1165 | ): |
||
1166 | 1 | return |
|
1167 | 1 | key, notified_at = "notified_up_at", now() |
|
1168 | 1 | link.update_metadata(key, now()) |
|
1169 | 1 | self.notify_topology_update() |
|
1170 | 1 | self.notify_link_status_change(link, reason) |
|
1171 | |||
1172 | 1 | def handle_link_up(self, interface): |
|
1173 | """Handle link up for an interface.""" |
||
1174 | 1 | with self._links_lock: |
|
1175 | 1 | link = self._get_link_from_interface(interface) |
|
1176 | 1 | if not link: |
|
1177 | self.notify_topology_update() |
||
1178 | return |
||
1179 | 1 | other_interface = ( |
|
1180 | link.endpoint_b if link.endpoint_a == interface |
||
1181 | else link.endpoint_a |
||
1182 | ) |
||
1183 | 1 | if other_interface.is_active() is False: |
|
1184 | 1 | self.notify_topology_update() |
|
1185 | 1 | return |
|
1186 | 1 | metadata = { |
|
1187 | 'last_status_change': time.time(), |
||
1188 | 'last_status_is_active': True |
||
1189 | } |
||
1190 | 1 | link.extend_metadata(metadata) |
|
1191 | 1 | link.activate() |
|
1192 | 1 | self.notify_topology_update() |
|
1193 | 1 | event = KytosEvent( |
|
1194 | name="kytos/topology.notify_link_up_if_status", |
||
1195 | content={"reason": "link up", "link": link} |
||
1196 | ) |
||
1197 | 1 | self.controller.buffers.app.put(event) |
|
1198 | |||
1199 | 1 | @listen_to('.*.switch.interface.link_down') |
|
1200 | 1 | def on_interface_link_down(self, event): |
|
1201 | """Update the topology based on a Port Modify event. |
||
1202 | |||
1203 | The event notifies that an interface's link was changed to 'down'. |
||
1204 | """ |
||
1205 | interface = event.content['interface'] |
||
1206 | self.handle_interface_link_down(interface, event) |
||
1207 | |||
1208 | 1 | def handle_interface_link_down(self, interface, event): |
|
1209 | """Update the topology based on an interface.""" |
||
1210 | 1 | with self._intfs_lock[interface.id]: |
|
1211 | 1 | if ( |
|
1212 | interface.id in self._intfs_updated_at |
||
1213 | and self._intfs_updated_at[interface.id] > event.timestamp |
||
1214 | ): |
||
1215 | 1 | return |
|
1216 | 1 | self._intfs_updated_at[interface.id] = event.timestamp |
|
1217 | 1 | self.handle_link_down(interface) |
|
1218 | |||
1219 | 1 | def handle_link_down(self, interface): |
|
1220 | """Notify a link is down.""" |
||
1221 | 1 | with self._links_lock: |
|
1222 | 1 | link = self._get_link_from_interface(interface) |
|
1223 | 1 | if not link or not link.get_metadata("last_status_is_active"): |
|
1224 | 1 | self.notify_topology_update() |
|
1225 | 1 | return |
|
1226 | 1 | link.deactivate() |
|
1227 | 1 | metadata = { |
|
1228 | "last_status_change": time.time(), |
||
1229 | "last_status_is_active": False, |
||
1230 | } |
||
1231 | 1 | link.extend_metadata(metadata) |
|
1232 | 1 | self.notify_link_status_change(link, reason="link down") |
|
1233 | 1 | self.notify_topology_update() |
|
1234 | |||
1235 | 1 | @listen_to('.*.interface.is.nni') |
|
1236 | 1 | def on_add_links(self, event): |
|
1237 | """Update the topology with links related to the NNI interfaces.""" |
||
1238 | self.add_links(event) |
||
1239 | |||
1240 | 1 | def add_links(self, event): |
|
1241 | """Update the topology with links related to the NNI interfaces.""" |
||
1242 | 1 | interface_a = event.content['interface_a'] |
|
1243 | 1 | interface_b = event.content['interface_b'] |
|
1244 | |||
1245 | 1 | try: |
|
1246 | 1 | with self._links_lock: |
|
1247 | 1 | link, created = self._get_link_or_create(interface_a, |
|
1248 | interface_b) |
||
1249 | 1 | interface_a.update_link(link) |
|
1250 | 1 | interface_b.update_link(link) |
|
1251 | |||
1252 | 1 | link.endpoint_a = interface_a |
|
1253 | 1 | link.endpoint_b = interface_b |
|
1254 | |||
1255 | 1 | interface_a.nni = True |
|
1256 | 1 | interface_b.nni = True |
|
1257 | |||
1258 | except KytosLinkCreationError as err: |
||
1259 | log.error(f'Error creating link: {err}.') |
||
1260 | return |
||
1261 | |||
1262 | 1 | if not created: |
|
1263 | return |
||
1264 | |||
1265 | 1 | self.notify_topology_update() |
|
1266 | 1 | if not link.is_active(): |
|
1267 | return |
||
1268 | |||
1269 | 1 | metadata = { |
|
1270 | 'last_status_change': time.time(), |
||
1271 | 'last_status_is_active': True |
||
1272 | } |
||
1273 | 1 | link.extend_metadata(metadata) |
|
1274 | 1 | self.topo_controller.upsert_link(link.id, link.as_dict()) |
|
1275 | 1 | self.notify_link_up_if_status(link, "link up") |
|
1276 | |||
1277 | 1 | @listen_to('.*.of_lldp.network_status.updated') |
|
1278 | 1 | def on_lldp_status_updated(self, event): |
|
1279 | """Handle of_lldp.network_status.updated from of_lldp.""" |
||
1280 | self.handle_lldp_status_updated(event) |
||
1281 | |||
1282 | 1 | @listen_to(".*.topo_controller.upsert_switch") |
|
1283 | 1 | def on_topo_controller_upsert_switch(self, event) -> None: |
|
1284 | """Listen to topo_controller_upsert_switch.""" |
||
1285 | self.handle_topo_controller_upsert_switch(event.content["switch"]) |
||
1286 | |||
1287 | 1 | def handle_topo_controller_upsert_switch(self, switch) -> Optional[dict]: |
|
1288 | """Handle topo_controller_upsert_switch.""" |
||
1289 | 1 | return self.topo_controller.upsert_switch(switch.id, switch.as_dict()) |
|
1290 | |||
1291 | 1 | def handle_lldp_status_updated(self, event) -> None: |
|
1292 | """Handle .*.network_status.updated events from of_lldp.""" |
||
1293 | 1 | content = event.content |
|
1294 | 1 | interface_ids = content["interface_ids"] |
|
1295 | 1 | switches = set() |
|
1296 | 1 | for interface_id in interface_ids: |
|
1297 | 1 | dpid = ":".join(interface_id.split(":")[:-1]) |
|
1298 | 1 | switch = self.controller.get_switch_by_dpid(dpid) |
|
1299 | 1 | if switch: |
|
1300 | 1 | switches.add(switch) |
|
1301 | |||
1302 | 1 | name = "kytos/topology.topo_controller.upsert_switch" |
|
1303 | 1 | for switch in switches: |
|
1304 | 1 | event = KytosEvent(name=name, content={"switch": switch}) |
|
1305 | 1 | self.controller.buffers.app.put(event) |
|
1306 | |||
1307 | 1 | def notify_switch_enabled(self, dpid): |
|
1308 | """Send an event to notify that a switch is enabled.""" |
||
1309 | 1 | name = 'kytos/topology.switch.enabled' |
|
1310 | 1 | event = KytosEvent(name=name, content={'dpid': dpid}) |
|
1311 | 1 | self.controller.buffers.app.put(event) |
|
1312 | |||
1313 | 1 | def notify_switch_links_status(self, switch, reason): |
|
1314 | """Send an event to notify the status of a link in a switch""" |
||
1315 | 1 | with self._links_lock: |
|
1316 | 1 | for link in self.links.values(): |
|
1317 | 1 | if switch in (link.endpoint_a.switch, link.endpoint_b.switch): |
|
1318 | 1 | if reason == "link enabled": |
|
1319 | 1 | name = 'kytos/topology.notify_link_up_if_status' |
|
1320 | 1 | content = {'reason': reason, "link": link} |
|
1321 | 1 | event = KytosEvent(name=name, content=content) |
|
1322 | 1 | self.controller.buffers.app.put(event) |
|
1323 | else: |
||
1324 | 1 | self.notify_link_status_change(link, reason) |
|
1325 | |||
1326 | 1 | def notify_switch_disabled(self, dpid): |
|
1327 | """Send an event to notify that a switch is disabled.""" |
||
1328 | 1 | name = 'kytos/topology.switch.disabled' |
|
1329 | 1 | event = KytosEvent(name=name, content={'dpid': dpid}) |
|
1330 | 1 | self.controller.buffers.app.put(event) |
|
1331 | |||
1332 | 1 | def notify_topology_update(self): |
|
1333 | """Send an event to notify about updates on the topology.""" |
||
1334 | 1 | name = 'kytos/topology.updated' |
|
1335 | 1 | event = KytosEvent(name=name, content={'topology': |
|
1336 | self._get_topology()}) |
||
1337 | 1 | self.controller.buffers.app.put(event) |
|
1338 | |||
1339 | 1 | def notify_interface_link_status(self, interface, reason): |
|
1340 | """Send an event to notify the status of a link from |
||
1341 | an interface.""" |
||
1342 | 1 | link = self._get_link_from_interface(interface) |
|
1343 | 1 | if link: |
|
1344 | 1 | if reason == "link enabled": |
|
1345 | 1 | name = 'kytos/topology.notify_link_up_if_status' |
|
1346 | 1 | content = {'reason': reason, "link": link} |
|
1347 | 1 | event = KytosEvent(name=name, content=content) |
|
1348 | 1 | self.controller.buffers.app.put(event) |
|
1349 | else: |
||
1350 | 1 | self.notify_link_status_change(link, reason) |
|
1351 | |||
1352 | 1 | def notify_link_status_change(self, link, reason='not given'): |
|
1353 | """Send an event to notify (up/down) from a status change on |
||
1354 | a link.""" |
||
1355 | 1 | link_id = link.id |
|
1356 | 1 | with self.link_status_lock: |
|
1357 | 1 | if ( |
|
1358 | (not link.status_reason and link.status == EntityStatus.UP) |
||
1359 | and link_id not in self.link_up |
||
1360 | ): |
||
1361 | 1 | log.info(f"{link} changed status {link.status}, " |
|
1362 | f"reason: {reason}") |
||
1363 | 1 | self.link_up.add(link_id) |
|
1364 | 1 | event = KytosEvent( |
|
1365 | name='kytos/topology.link_up', |
||
1366 | content={ |
||
1367 | 'link': link, |
||
1368 | 'reason': reason |
||
1369 | }, |
||
1370 | ) |
||
1371 | 1 | elif ( |
|
1372 | (link.status_reason or link.status != EntityStatus.UP) |
||
1373 | and link_id in self.link_up |
||
1374 | ): |
||
1375 | 1 | log.info(f"{link} changed status {link.status}, " |
|
1376 | f"reason: {reason}") |
||
1377 | 1 | self.link_up.remove(link_id) |
|
1378 | 1 | event = KytosEvent( |
|
1379 | name='kytos/topology.link_down', |
||
1380 | content={ |
||
1381 | 'link': link, |
||
1382 | 'reason': reason |
||
1383 | }, |
||
1384 | ) |
||
1385 | else: |
||
1386 | 1 | return |
|
1387 | 1 | self.controller.buffers.app.put(event) |
|
1388 | |||
1389 | 1 | def notify_metadata_changes(self, obj, action): |
|
1390 | """Send an event to notify about metadata changes.""" |
||
1391 | 1 | if isinstance(obj, Switch): |
|
1392 | 1 | entity = 'switch' |
|
1393 | 1 | entities = 'switches' |
|
1394 | 1 | elif isinstance(obj, Interface): |
|
1395 | 1 | entity = 'interface' |
|
1396 | 1 | entities = 'interfaces' |
|
1397 | 1 | elif isinstance(obj, Link): |
|
1398 | 1 | entity = 'link' |
|
1399 | 1 | entities = 'links' |
|
1400 | else: |
||
1401 | 1 | raise ValueError( |
|
1402 | 'Invalid object, supported: Switch, Interface, Link' |
||
1403 | ) |
||
1404 | |||
1405 | 1 | name = f'kytos/topology.{entities}.metadata.{action}' |
|
1406 | 1 | content = {entity: obj, 'metadata': obj.metadata.copy()} |
|
1407 | 1 | event = KytosEvent(name=name, content=content) |
|
1408 | 1 | self.controller.buffers.app.put(event) |
|
1409 | 1 | log.debug(f'Metadata from {obj.id} was {action}.') |
|
1410 | |||
1411 | 1 | @listen_to('kytos/topology.notify_link_up_if_status') |
|
1412 | 1 | def on_notify_link_up_if_status(self, event): |
|
1413 | """Tries to notify link up and topology changes""" |
||
1414 | link = event.content["link"] |
||
1415 | reason = event.content["reason"] |
||
1416 | self.notify_link_up_if_status(link, reason) |
||
1417 | |||
1418 | 1 | @listen_to('.*.switch.port.created') |
|
1419 | 1 | def on_notify_port_created(self, event): |
|
1420 | """Notify when a port is created.""" |
||
1421 | self.notify_port_created(event) |
||
1422 | |||
1423 | 1 | def notify_port_created(self, event): |
|
1424 | """Notify when a port is created.""" |
||
1425 | 1 | name = 'kytos/topology.port.created' |
|
1426 | 1 | event = KytosEvent(name=name, content=event.content) |
|
1427 | 1 | self.controller.buffers.app.put(event) |
|
1428 | |||
1429 | 1 | @staticmethod |
|
1430 | 1 | def load_interfaces_tags_values(switch: Switch, |
|
1431 | interfaces_details: List[dict]) -> None: |
||
1432 | """Load interfaces available tags (vlans).""" |
||
1433 | 1 | if not interfaces_details: |
|
1434 | return |
||
1435 | 1 | for interface_details in interfaces_details: |
|
1436 | 1 | available_tags = interface_details['available_tags'] |
|
1437 | 1 | if not available_tags: |
|
1438 | continue |
||
1439 | 1 | log.debug(f"Interface id {interface_details['id']} loading " |
|
1440 | f"{len(available_tags)} " |
||
1441 | "available tags") |
||
1442 | 1 | port_number = int(interface_details["id"].split(":")[-1]) |
|
1443 | 1 | interface = switch.interfaces[port_number] |
|
1444 | 1 | interface.set_available_tags_tag_ranges( |
|
1445 | available_tags, |
||
1446 | interface_details['tag_ranges'], |
||
1447 | interface_details['special_available_tags'], |
||
1448 | interface_details['special_tags'], |
||
1449 | ) |
||
1450 | |||
1451 | 1 | @listen_to( |
|
1452 | 'topology.interruption.(start|end)', |
||
1453 | pool="dynamic_single" |
||
1454 | ) |
||
1455 | 1 | def on_interruption(self, event: KytosEvent): |
|
1456 | """Deals with service interruptions.""" |
||
1457 | with self._links_lock: |
||
1458 | _, _, interrupt_type = event.name.rpartition(".") |
||
1459 | if interrupt_type == "start": |
||
1460 | self.handle_interruption_start(event) |
||
1461 | elif interrupt_type == "end": |
||
1462 | self.handle_interruption_end(event) |
||
1463 | |||
1464 | 1 | View Code Duplication | def handle_interruption_start(self, event: KytosEvent): |
|
|||
1465 | """Deals with the start of service interruption.""" |
||
1466 | 1 | interrupt_type = event.content['type'] |
|
1467 | 1 | switches = event.content.get('switches', []) |
|
1468 | 1 | interfaces = event.content.get('interfaces', []) |
|
1469 | 1 | links = event.content.get('links', []) |
|
1470 | 1 | log.info( |
|
1471 | 'Received interruption start of type \'%s\' ' |
||
1472 | 'affecting switches %s, interfaces %s, links %s', |
||
1473 | interrupt_type, |
||
1474 | switches, |
||
1475 | interfaces, |
||
1476 | links |
||
1477 | ) |
||
1478 | # for switch_id in switches: |
||
1479 | # pass |
||
1480 | # for interface_id in interfaces: |
||
1481 | # pass |
||
1482 | 1 | for link_id in links: |
|
1483 | 1 | link = self.links.get(link_id) |
|
1484 | 1 | if link is None: |
|
1485 | log.error( |
||
1486 | "Invalid link id '%s' for interruption of type '%s;", |
||
1487 | link_id, |
||
1488 | interrupt_type |
||
1489 | ) |
||
1490 | else: |
||
1491 | 1 | self.notify_link_status_change(link, interrupt_type) |
|
1492 | 1 | self.notify_topology_update() |
|
1493 | |||
1494 | 1 | View Code Duplication | def handle_interruption_end(self, event: KytosEvent): |
1495 | """Deals with the end of service interruption.""" |
||
1496 | 1 | interrupt_type = event.content['type'] |
|
1497 | 1 | switches = event.content.get('switches', []) |
|
1498 | 1 | interfaces = event.content.get('interfaces', []) |
|
1499 | 1 | links = event.content.get('links', []) |
|
1500 | 1 | log.info( |
|
1501 | 'Received interruption end of type \'%s\' ' |
||
1502 | 'affecting switches %s, interfaces %s, links %s', |
||
1503 | interrupt_type, |
||
1504 | switches, |
||
1505 | interfaces, |
||
1506 | links |
||
1507 | ) |
||
1508 | # for switch_id in switches: |
||
1509 | # pass |
||
1510 | # for interface_id in interfaces: |
||
1511 | # pass |
||
1512 | 1 | for link_id in links: |
|
1513 | 1 | link = self.links.get(link_id) |
|
1514 | 1 | if link is None: |
|
1515 | log.error( |
||
1516 | "Invalid link id '%s' for interruption of type '%s;", |
||
1517 | link_id, |
||
1518 | interrupt_type |
||
1519 | ) |
||
1520 | else: |
||
1521 | 1 | self.notify_link_status_change(link, interrupt_type) |
|
1522 | self.notify_topology_update() |
||
1523 |