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