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