Passed
Pull Request — master (#255)
by Aldo
04:46
created

build.main.Main._get_link_or_create()   C

Complexity

Conditions 10

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 23
nop 3
dl 0
loc 39
ccs 15
cts 15
cp 1
crap 10
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like build.main.Main._get_link_or_create() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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