Test Failed
Pull Request — master (#253)
by
unknown
02:11
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
        # to keep track of potential unorded scheduled interface events
56 1
        self._intfs_lock = defaultdict(Lock)
57 1
        self._intfs_updated_at = {}
58 1
        self._intfs_tags_updated_at = {}
59 1
        self.link_up = set()
60 1
        self.link_status_lock = Lock()
61 1
        self._switch_lock = defaultdict(Lock)
62 1
        self.topo_controller = self.get_topo_controller()
63 1
        Link.register_status_func(f"{self.napp_id}_link_up_timer",
64
                                  self.link_status_hook_link_up_timer)
65 1
        self.link_status_change_cache = {}
66 1
        self.topo_controller.bootstrap_indexes()
67
        self.load_topology()
68 1
69 1
    @staticmethod
70
    def get_topo_controller() -> TopoController:
71
        """Get TopoController."""
72
        return TopoController()
73 1
74
    def execute(self):
75
        """Execute once when the napp is running."""
76
        pass
77 1
78
    def shutdown(self):
79
        """Do nothing."""
80
        log.info('NApp kytos/topology shutting down.')
81 1
82
    def _get_metadata(self, request: Request) -> dict:
83 1
        """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
        return metadata
89 1
90
    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 1
        """
96
        new_link = Link(endpoint_a, endpoint_b)
97 1
98 1
        if new_link.id in self.links:
99
            return (self.links[new_link.id], False)
100 1
101 1
        self.links[new_link.id] = new_link
102
        return (new_link, True)
103 1
104
    def _get_switches_dict(self):
105 1
        """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
            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 1
                switch_data['metadata']['lng'] = str(-30.0+idx*10.0)
114 1
            switches['switches'][switch.id] = switch_data
115
        return switches
116 1
117
    def _get_links_dict(self):
118 1
        """Return a dictionary with the known links."""
119
        return {'links': {link.id: link.as_dict() for link in
120
                          self.links.copy().values()}}
121 1
122
    def _get_topology_dict(self):
123 1
        """Return a dictionary with the known topology."""
124
        return {'topology': {**self._get_switches_dict(),
125
                             **self._get_links_dict()}}
126 1
127
    def _get_topology(self):
128 1
        """Return an object representing the topology."""
129
        return Topology(self.controller.switches.copy(), self.links.copy())
130 1
131
    def _get_link_from_interface(self, interface: Interface):
132 1
        """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
        return None
137 1
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
        interface_b = self.controller.get_interface_by_id(endpoint_b)
145 1
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
        if not interface_b:
150
            raise RestoreError(f"{error}, endpoint_b {endpoint_b} not found")
151 1
152 1
        with self._links_lock:
153
            link, _ = self._get_link_or_create(interface_a, interface_b)
154 1
155 1
        if link_att['enabled']:
156
            link.enable()
157 1
        else:
158
            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 1
        # Also network operators can follow logs to track this state changes
163
        for key in (
164
            "last_status_is_active", "last_status_change", "notified_up_at"
165 1
        ):
166
            link_att["metadata"].pop(key, None)
167 1
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
        interface_b.nni = True
173 1
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
            switch.enable()
179 1
        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
        switch.extend_metadata(switch_att["metadata"])
187 1
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
            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 1
                            speed=iface_att.get('speed', None))
195 1
            if iface_att['enabled']:
196
                interface.enable()
197 1
            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
            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 1
                                              })
212
            self.controller.buffers.app.put(event, timeout=1)
213 1
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
            self.load_interfaces_tags_values(switch, intf_details)
218
219 1
    # pylint: disable=attribute-defined-outside-init
220
    def load_topology(self):
221 1
        """Load network topology from DB."""
222 1
        topology = self.topo_controller.get_topology()
223 1
        switches = topology["topology"]["switches"]
224
        links = topology["topology"]["links"]
225 1
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
                log.error(f'Error loading switch: {err}')
234 1
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
                log.error(f'Error loading link {link_id}: {err}')
243 1
244 1
        name = 'kytos/topology.topology_loaded'
245
        event = KytosEvent(
246
            name=name,
247
            content={
248
                'topology': self._get_topology(),
249
                'failed_switches': failed_switches,
250
                'failed_links': failed_links
251 1
            })
252
        self.controller.buffers.app.put(event, timeout=1)
253 1
254 1
    @rest('v3/')
255
    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 1
        """
260
        return JSONResponse(self._get_topology_dict())
261
262 1
    # Switch related methods
263 1
    @rest('v3/switches')
264
    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 1
268 1
    @rest('v3/switches/{dpid}/enable', methods=['POST'])
269
    def enable_switch(self, request: Request) -> JSONResponse:
270 1
        """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
            raise HTTPException(404, detail="Switch not found")
278 1
279 1
        self.notify_topology_update()
280 1
        self.notify_switch_enabled(dpid)
281 1
        self.notify_switch_links_status(switch, "link enabled")
282
        return JSONResponse("Operation successful", status_code=201)
283 1
284 1
    @rest('v3/switches/{dpid}/disable', methods=['POST'])
285
    def disable_switch(self, request: Request) -> JSONResponse:
286 1
        """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
            raise HTTPException(404, detail="Switch not found")
301 1
302 1
        self.notify_topology_update()
303 1
        self.notify_switch_disabled(dpid)
304 1
        self.notify_switch_links_status(switch, "link disabled")
305
        return JSONResponse("Operation successful", status_code=201)
306 1
307 1
    @rest('v3/switches/{dpid}', methods=['DELETE'])
308
    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 1
        """
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
                    raise HTTPException(
323
                        409, detail="Switch should be disabled."
324 1
                    )
325 1
                for intf_id, interface in switch.interfaces.copy().items():
326 1
                    if not interface.all_tags_available():
327
                        detail = f"Interface {intf_id} vlans are being used."\
328 1
                                 " 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
                        if (dpid in
333
                                (link.endpoint_a.switch.dpid,
334 1
                                 link.endpoint_b.switch.dpid)):
335
                            raise HTTPException(
336
                                409, detail=f"Switch should not have links. "
337
                                            f"Link found {link_id}."
338 1
                            )
339 1
                try:
340
                    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 1
                    raise HTTPException(409, detail=detail)
345
                if flows:
346
                    raise HTTPException(409, detail="Switch has flows. Verify"
347 1
                                                    " 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
        return JSONResponse("Operation successful")
357 1
358 1
    @rest('v3/switches/{dpid}/metadata')
359
    def get_switch_metadata(self, request: Request) -> JSONResponse:
360 1
        """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
            raise HTTPException(404, detail="Switch not found")
367 1
368 1
    @rest('v3/switches/{dpid}/metadata', methods=['POST'])
369
    def add_switch_metadata(self, request: Request) -> JSONResponse:
370 1
        """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
            raise HTTPException(404, detail="Switch not found")
377 1
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
        return JSONResponse("Operation successful", status_code=201)
382 1
383 1
    @rest('v3/switches/{dpid}/metadata/{key}', methods=['DELETE'])
384
    def delete_switch_metadata(self, request: Request) -> JSONResponse:
385 1
        """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
            raise HTTPException(404, detail="Switch not found")
392 1
393 1
        try:
394 1
            _ = switch.metadata[key]
395 1
        except KeyError:
396
            raise HTTPException(404, "Metadata not found")
397 1
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
        return JSONResponse("Operation successful")
402
403 1
    # Interface related methods
404 1
    @rest('v3/interfaces')
405
    def get_interfaces(self, _request: Request) -> JSONResponse:
406 1
        """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
                interfaces[interface_id] = interface
412 1
413
        return JSONResponse({'interfaces': interfaces})
414 1
415 1
    @rest('v3/interfaces/switch/{dpid}/enable', methods=['POST'])
416 1
    @rest('v3/interfaces/{interface_enable_id}/enable', methods=['POST'])
417
    def enable_interface(self, request: Request) -> JSONResponse:
418 1
        """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
            raise HTTPException(404, detail="Switch not found")
429 1
430 1
        if interface_enable_id:
431
            interface_number = int(interface_enable_id.split(":")[-1])
432 1
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
                raise HTTPException(404, detail=msg)
441 1
        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
        return JSONResponse("Operation successful")
448 1
449 1
    @rest('v3/interfaces/switch/{dpid}/disable', methods=['POST'])
450 1
    @rest('v3/interfaces/{interface_disable_id}/disable', methods=['POST'])
451
    def disable_interface(self, request: Request) -> JSONResponse:
452 1
        """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
            raise HTTPException(404, detail="Switch not found")
461 1
462 1
        if interface_disable_id:
463
            interface_number = int(interface_disable_id.split(":")[-1])
464 1
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
                raise HTTPException(404, detail=msg)
477 1
        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
        return JSONResponse("Operation successful")
490 1
491 1
    @rest('v3/interfaces/{interface_id}/metadata')
492
    def get_interface_metadata(self, request: Request) -> JSONResponse:
493 1
        """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
            raise HTTPException(404, detail="Switch not found")
501 1
502 1
        try:
503 1
            interface = switch.interfaces[interface_number]
504 1
        except KeyError:
505
            raise HTTPException(404, detail="Interface not found")
506 1
507
        return JSONResponse({"metadata": interface.metadata})
508 1
509 1
    @rest('v3/interfaces/{interface_id}/metadata', methods=['POST'])
510
    def add_interface_metadata(self, request: Request) -> JSONResponse:
511 1
        """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
            raise HTTPException(404, detail="Switch not found")
520 1
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
            raise HTTPException(404, detail="Interface not found")
526 1
527 1
        interface.extend_metadata(metadata)
528 1
        self.notify_metadata_changes(interface, 'added')
529
        return JSONResponse("Operation successful", status_code=201)
530 1
531 1
    @rest('v3/interfaces/{interface_id}/metadata/{key}', methods=['DELETE'])
532
    def delete_interface_metadata(self, request: Request) -> JSONResponse:
533 1
        """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
            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 1
543 1
        try:
544 1
            switch = self.controller.switches[switch_id]
545 1
        except KeyError:
546
            raise HTTPException(404, detail="Switch not found")
547 1
548 1
        try:
549 1
            interface = switch.interfaces[interface_number]
550 1
        except KeyError:
551
            raise HTTPException(404, detail="Interface not found")
552 1
553 1
        try:
554 1
            _ = interface.metadata[key]
555 1
        except KeyError:
556
            raise HTTPException(404, detail="Metadata not found")
557 1
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
        return JSONResponse("Operation successful")
562 1
563 1
    @rest('v3/interfaces/{interface_id}/tag_ranges', methods=['POST'])
564 1
    @validate_openapi(spec)
565
    def set_tag_range(self, request: Request) -> JSONResponse:
566 1
        """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
            ranges = get_tag_ranges(content["tag_ranges"])
572
        except KytosInvalidTagRanges as err:
573 1
            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
        return JSONResponse("Operation Successful", status_code=200)
584 1
585 1
    @rest('v3/interfaces/{interface_id}/tag_ranges', methods=['DELETE'])
586 1
    @validate_openapi(spec)
587
    def delete_tag_range(self, request: Request) -> JSONResponse:
588 1
        """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
        return JSONResponse("Operation Successful", status_code=200)
601 1
602 1
    @rest('v3/interfaces/{interface_id}/special_tags', methods=['POST'])
603 1
    @validate_openapi(spec)
604
    def set_special_tags(self, request: Request) -> JSONResponse:
605 1
        """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
        return JSONResponse("Operation Successful", status_code=200)
620 1
621 1
    @rest('v3/interfaces/tag_ranges', methods=['GET'])
622 1
    @validate_openapi(spec)
623
    def get_all_tag_ranges(self, _: Request) -> JSONResponse:
624
        """Get all tag_ranges, available_tags, special_tags
625 1
         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
                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 1
                }
635
        return JSONResponse(result, status_code=200)
636 1
637 1
    @rest('v3/interfaces/{interface_id}/tag_ranges', methods=['GET'])
638 1
    @validate_openapi(spec)
639
    def get_tag_ranges_by_intf(self, request: Request) -> JSONResponse:
640
        """Get tag_ranges, available_tags, special_tags
641 1
         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
        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 1
        }
654
        return JSONResponse(result, status_code=200)
655
656 1
    # Link related methods
657 1
    @rest('v3/links')
658
    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 1
665 1
    @rest('v3/links/{link_id}/enable', methods=['POST'])
666
    def enable_link(self, request: Request) -> JSONResponse:
667 1
        """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
        self.notify_link_status_change(
685
            self.links[link_id],
686
            reason='link enabled'
687 1
        )
688 1
        self.notify_topology_update()
689
        return JSONResponse("Operation successful", status_code=201)
690 1
691 1
    @rest('v3/links/{link_id}/disable', methods=['POST'])
692
    def disable_link(self, request: Request) -> JSONResponse:
693 1
        """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
        self.notify_link_status_change(
705
            self.links[link_id],
706
            reason='link disabled'
707 1
        )
708 1
        self.notify_topology_update()
709
        return JSONResponse("Operation successful", status_code=201)
710 1
711
    def notify_link_enabled_state(self, link: Link, action: str):
712
        """Send a KytosEvent whether a link status (enabled/disabled)
713 1
         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
        self.controller.buffers.app.put(event)
718 1
719 1
    @rest('v3/links/{link_id}/metadata')
720
    def get_link_metadata(self, request: Request) -> JSONResponse:
721 1
        """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
            raise HTTPException(404, detail="Link not found")
727 1
728 1
    @rest('v3/links/{link_id}/metadata', methods=['POST'])
729
    def add_link_metadata(self, request: Request) -> JSONResponse:
730 1
        """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
            raise HTTPException(404, detail="Link not found")
737 1
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
        return JSONResponse("Operation successful", status_code=201)
743 1
744 1
    @rest('v3/links/{link_id}/metadata/{key}', methods=['DELETE'])
745
    def delete_link_metadata(self, request: Request) -> JSONResponse:
746 1
        """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
            raise HTTPException(404, detail="Link not found")
753 1
754 1
        try:
755 1
            _ = link.metadata[key]
756 1
        except KeyError:
757
            raise HTTPException(404, detail="Metadata not found")
758 1
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
        return JSONResponse("Operation successful")
764 1
765 1
    @rest('v3/links/{link_id}', methods=['DELETE'])
766
    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 1
        """
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
                    self.topo_controller.upsert_switch(
781
                        switch.id, switch.as_dict()
782 1
                    )
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
                    self.topo_controller.upsert_switch(
788
                        switch.id, switch.as_dict()
789 1
                    )
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
        return JSONResponse("Operation successful")
799 1
800 1
    @rest('v3/interfaces/{intf_id}', methods=['DELETE'])
801
    def delete_interface(self, request: Request) -> JSONResponse:
802 1
        """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
            raise HTTPException(404, detail="Interface not found.")
818 1
819 1
        usage = self.get_intf_usage(interface)
820 1
        if usage:
821
            raise HTTPException(409, detail=f"Interface could not be "
822 1
                                            f"deleted. Reason: {usage}")
823 1
        self._delete_interface(interface)
824
        return JSONResponse("Operation Successful", status_code=200)
825 1
826
    @listen_to(
827
        "kytos/.*.liveness.(up|down|disabled)",
828
        pool="dynamic_single"
829 1
    )
830
    def on_link_liveness(self, event) -> None:
831
        """Handle link liveness up|down|disabled event."""
832
        with self._links_lock:
833
            liveness_status = event.name.split(".")[-1]
834
            if liveness_status == "disabled":
835
                interfaces = event.content["interfaces"]
836
                self.handle_link_liveness_disabled(interfaces)
837
            elif liveness_status in ("up", "down"):
838
                link = Link(event.content["interface_a"],
839
                            event.content["interface_b"])
840
                try:
841
                    link = self.links[link.id]
842
                except KeyError:
843
                    log.error(f"Link id {link.id} not found, {link}")
844
                    return
845
                self.handle_link_liveness_status(self.links[link.id],
846
                                                 liveness_status)
847 1
848
    def handle_link_liveness_status(self, link, liveness_status) -> None:
849 1
        """Handle link liveness."""
850 1
        metadata = {"liveness_status": liveness_status}
851 1
        log.info(f"Link liveness {liveness_status}: {link}")
852 1
        link.extend_metadata(metadata)
853 1
        self.notify_topology_update()
854 1
        if link.status == EntityStatus.UP and liveness_status == "up":
855 1
            self.notify_link_status_change(link, reason="liveness_up")
856 1
        if link.status == EntityStatus.DOWN and liveness_status == "down":
857
            self.notify_link_status_change(link, reason="liveness_down")
858 1
859
    def get_links_from_interfaces(self, interfaces) -> dict:
860 1
        """Get links from interfaces."""
861 1
        links_found = {}
862 1
        for interface in interfaces:
863 1
            for link in list(self.links.values()):
864
                if any((
865
                    interface.id == link.endpoint_a.id,
866
                    interface.id == link.endpoint_b.id,
867 1
                )):
868 1
                    links_found[link.id] = link
869
        return links_found
870 1
871
    def handle_link_liveness_disabled(self, interfaces) -> None:
872 1
        """Handle link liveness disabled."""
873
        log.info(f"Link liveness disabled interfaces: {interfaces}")
874 1
875 1
        key = "liveness_status"
876 1
        links = self.get_links_from_interfaces(interfaces)
877 1
        for link in links.values():
878 1
            link.remove_metadata(key)
879 1
        self.notify_topology_update()
880 1
        for link in links.values():
881
            self.notify_link_status_change(link, reason="liveness_disabled")
882 1
883 1
    @listen_to("kytos/core.interface_tags")
884
    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 1
896
    def handle_on_interface_tags(self, interface):
897 1
        """Update interface details"""
898 1
        intf_id = interface.id
899
        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 1
905 1
    @listen_to('.*.switch.(new|reconnected)')
906
    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 1
914
    def handle_new_switch(self, event):
915 1
        """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
            self.notify_switch_enabled(switch.id)
923 1
924 1
    @listen_to('.*.connection.lost')
925
    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 1
933
    def handle_connection_lost(self, event):
934 1
        """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
            self.notify_topology_update()
940 1
941
    def handle_interfaces_created(self, event):
942 1
        """Update the topology based on the interfaces created."""
943 1
        interfaces = event.content["interfaces"]
944
        if not interfaces:
945 1
            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
            self.controller.buffers.app.put(event)
952 1
953
    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 1
        """
959 1
        interface = event.content['interface']
960 1
        if not interface.is_active():
961
            self.handle_interface_link_down(interface, event)
962 1
        else:
963
            self.handle_interface_link_up(interface, event)
964 1
965 1
    @listen_to('.*.topology.switch.interface.created')
966
    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 1
974 1
    @listen_to('.*.switch.interfaces.created')
975
    def on_interfaces_created(self, event):
976
        """Update the topology based on a list of created interfaces."""
977
        self.handle_interfaces_created(event)
978 1
979
    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 1
        """
984 1
        interface = event.content['interface']
985 1
        with self._intfs_lock[interface.id]:
986
            if (
987
                interface.id in self._intfs_updated_at
988
                and self._intfs_updated_at[interface.id] > event.timestamp
989
            ):
990 1
                return
991 1
            self._intfs_updated_at[interface.id] = event.timestamp
992 1
            interface.deactivate()
993
        self.handle_interface_link_down(interface, event)
994 1
995 1
    @listen_to('.*.switch.interface.deleted')
996
    def on_interface_deleted(self, event):
997
        """Update the topology based on a Port Delete event."""
998
        self.handle_interface_deleted(event)
999 1
1000
    def handle_interface_deleted(self, event):
1001 1
        """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
            log.info(f"Interface {interface.id} could not be safely removed."
1007
                     f" Reason: {usage}")
1008 1
        else:
1009
            self._delete_interface(interface)
1010 1
1011
    def get_intf_usage(self, interface: Interface) -> Optional[str]:
1012
        """Determines how an interface is used explained in a string,
1013 1
        returns None if unused."""
1014 1
        if interface.is_enabled() or interface.is_active():
1015
            return "It is enabled or active."
1016 1
1017 1
        link = self._get_link_from_interface(interface)
1018 1
        if link:
1019
            return f"It has a link, {link.id}."
1020 1
1021 1
        flow_id = self.get_flow_id_by_intf(interface)
1022 1
        if flow_id:
1023
            return f"There is a flow installed, {flow_id}."
1024 1
1025
        return None
1026 1
1027
    def get_flow_id_by_intf(self, interface: Interface) -> str:
1028 1
        """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
                return flow["flow_id"]
1035 1
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
                        if (action["action_type"] == "output"
1042 1
                                and action.get("port") == port_n):
1043
                            return flow["flow_id"]
1044 1
1045 1
            actions = flow["flow"].get("actions", [])
1046 1
            for action in actions:
1047
                if (action["action_type"] == "output"
1048 1
                        and action.get("port") == port_n):
1049 1
                    return flow["flow_id"]
1050
        return None
1051 1
1052
    def _delete_interface(self, interface: Interface):
1053
        """Delete any trace of an interface. Only use this method when
1054 1
         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
        self.topo_controller.delete_interface_from_details(interface.id)
1059 1
1060 1
    @listen_to('.*.switch.interface.link_up')
1061
    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 1
1069
    def handle_interface_link_up(self, interface, event):
1070 1
        """Update the topology based on a Port Modify event."""
1071 1
        with self._intfs_lock[interface.id]:
1072
            if (
1073
                interface.id in self._intfs_updated_at
1074
                and self._intfs_updated_at[interface.id] > event.timestamp
1075 1
            ):
1076 1
                return
1077 1
            self._intfs_updated_at[interface.id] = event.timestamp
1078
            self.handle_link_up(interface)
1079 1
1080
    @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 1
    )
1086
    def get_flows_by_switch(self, dpid: str) -> list:
1087 1
        """Get installed flows by switch from flow_manager."""
1088
        endpoint = settings.FLOW_MANAGER_URL +\
1089 1
            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
        return res.json().get(dpid, [])
1094 1
1095
    def link_status_hook_link_up_timer(
1096 1
        self,
1097 1
        link: Link
1098
    ) -> Optional[EntityStatus]:
1099
        """Link status hook link up timer."""
1100
        tnow = time.time()
1101
        if link.id not in self.link_status_change_cache:
1102
            return None
1103 1
        link_status_info = self.link_status_change_cache[link.id]
1104 1
        tdelta = tnow - link_status_info['last_status_change']
1105
        if tdelta < self.link_up_timer:
1106 1
            return EntityStatus.DOWN
1107
        return None
1108
1109
    def notify_link_up_if_status(self, link: Link, reason="link up") -> None:
1110 1
        """Tries to notify link up and topology changes based on its status
1111 1
1112
        Currently, it needs to wait up to a timer."""
1113 1
        time.sleep(self.link_up_timer)
1114 1
        if link.status != EntityStatus.UP:
1115 1
            return
1116
        with self._links_lock:
1117
            status_change_info = self.link_status_change_cache.setdefault(
1118
                link.id,
1119
                {}
1120 1
            )
1121 1
            notified_at = status_change_info.get("notified_up_at")
1122 1
            if (
1123 1
                notified_at
1124 1
                and (now() - notified_at.replace(tzinfo=timezone.utc)).seconds
1125
                < self.link_up_timer
1126 1
            ):
1127
                return
1128 1
            status_change_info["notified_up_at"] = now()
1129 1
            self.notify_topology_update()
1130 1
            self.notify_link_status_change(link, reason)
1131
1132
    def handle_link_up(self, interface):
1133 1
        """Handle link up for an interface."""
1134
        with self._links_lock:
1135
            link = self._get_link_from_interface(interface)
1136
            if not link:
1137 1
                self.notify_topology_update()
1138 1
                return
1139 1
            other_interface = (
1140 1
                link.endpoint_b if link.endpoint_a == interface
1141
                else link.endpoint_a
1142
            )
1143
            if other_interface.is_active() is False:
1144 1
                self.notify_topology_update()
1145 1
                return
1146 1
            if (
1147 1
                link.id not in self.link_status_change_cache or
1148
                not link.is_active()
1149
            ):
1150
                status_change_info = self.link_status_change_cache.setdefault(
1151 1
                    link.id,
1152
                    {}
1153 1
                )
1154 1
                status_change_info['last_status_change'] = time.time()
1155
                status_change_info['last_status_is_active'] = True
1156
                link.activate()
1157
            self.notify_topology_update()
1158
            event = KytosEvent(
1159
                name="kytos/topology.notify_link_up_if_status",
1160
                content={"reason": "link up", "link": link}
1161
            )
1162 1
            self.controller.buffers.app.put(event)
1163
1164 1
    @listen_to('.*.switch.interface.link_down')
1165 1
    def on_interface_link_down(self, event):
1166
        """Update the topology based on a Port Modify event.
1167
1168
        The event notifies that an interface's link was changed to 'down'.
1169 1
        """
1170 1
        interface = event.content['interface']
1171 1
        self.handle_interface_link_down(interface, event)
1172
1173 1
    def handle_interface_link_down(self, interface, event):
1174
        """Update the topology based on an interface."""
1175 1
        with self._intfs_lock[interface.id]:
1176 1
            if (
1177 1
                interface.id in self._intfs_updated_at
1178 1
                and self._intfs_updated_at[interface.id] > event.timestamp
1179 1
            ):
1180 1
                return
1181 1
            self._intfs_updated_at[interface.id] = event.timestamp
1182
            self.handle_link_down(interface)
1183
1184
    def handle_link_down(self, interface):
1185 1
        """Notify a link is down."""
1186 1
        with self._links_lock:
1187 1
            link = self._get_link_from_interface(interface)
1188
            if link and (
1189 1
                link.id not in self.link_status_change_cache or
1190 1
                link.is_active()
1191
            ):
1192
                status_change_info = self.link_status_change_cache.setdefault(
1193
                    link.id,
1194 1
                    {}
1195
                )
1196 1
                status_change_info['last_status_change'] = time.time()
1197 1
                status_change_info['last_status_is_active'] = False
1198
                link.deactivate()
1199 1
                self.notify_link_status_change(link, reason="link down")
1200 1
            self.notify_topology_update()
1201 1
1202
    @listen_to('.*.interface.is.nni')
1203 1
    def on_add_links(self, event):
1204 1
        """Update the topology with links related to the NNI interfaces."""
1205
        self.add_links(event)
1206 1
1207 1
    def add_links(self, event):
1208
        """Update the topology with links related to the NNI interfaces."""
1209 1
        interface_a = event.content['interface_a']
1210 1
        interface_b = event.content['interface_b']
1211
1212
        try:
1213
            with self._links_lock:
1214
                link, created = self._get_link_or_create(interface_a,
1215
                                                         interface_b)
1216 1
                interface_a.update_link(link)
1217
                interface_b.update_link(link)
1218
1219 1
                link.endpoint_a = interface_a
1220 1
                link.endpoint_b = interface_b
1221
1222
                interface_a.nni = True
1223 1
                interface_b.nni = True
1224
1225
        except KytosLinkCreationError as err:
1226
            log.error(f'Error creating link: {err}.')
1227 1
            return
1228 1
1229 1
        if not created:
1230
            return
1231 1
1232 1
        self.notify_topology_update()
1233
        if not link.is_active():
1234
            return
1235
        status_change_info = self.link_status_change_cache.setdefault(
1236 1
            link.id,
1237 1
            {}
1238
        )
1239
        status_change_info['last_status_change'] = time.time()
1240
        status_change_info['last_status_is_active'] = True
1241 1
1242
        self.topo_controller.upsert_link(link.id, link.as_dict())
1243 1
        self.notify_link_up_if_status(link, "link up")
1244
1245 1
    @listen_to('.*.of_lldp.network_status.updated')
1246
    def on_lldp_status_updated(self, event):
1247 1
        """Handle of_lldp.network_status.updated from of_lldp."""
1248 1
        self.handle_lldp_status_updated(event)
1249 1
1250 1
    @listen_to(".*.topo_controller.upsert_switch")
1251 1
    def on_topo_controller_upsert_switch(self, event) -> None:
1252 1
        """Listen to topo_controller_upsert_switch."""
1253 1
        self.handle_topo_controller_upsert_switch(event.content["switch"])
1254 1
1255
    def handle_topo_controller_upsert_switch(self, switch) -> Optional[dict]:
1256 1
        """Handle topo_controller_upsert_switch."""
1257 1
        return self.topo_controller.upsert_switch(switch.id, switch.as_dict())
1258 1
1259 1
    def handle_lldp_status_updated(self, event) -> None:
1260
        """Handle .*.network_status.updated events from of_lldp."""
1261 1
        content = event.content
1262
        interface_ids = content["interface_ids"]
1263 1
        switches = set()
1264 1
        for interface_id in interface_ids:
1265 1
            dpid = ":".join(interface_id.split(":")[:-1])
1266
            switch = self.controller.get_switch_by_dpid(dpid)
1267 1
            if switch:
1268
                switches.add(switch)
1269 1
1270 1
        name = "kytos/topology.topo_controller.upsert_switch"
1271 1
        for switch in switches:
1272 1
            event = KytosEvent(name=name, content={"switch": switch})
1273 1
            self.controller.buffers.app.put(event)
1274 1
1275 1
    def notify_switch_enabled(self, dpid):
1276 1
        """Send an event to notify that a switch is enabled."""
1277
        name = 'kytos/topology.switch.enabled'
1278 1
        event = KytosEvent(name=name, content={'dpid': dpid})
1279
        self.controller.buffers.app.put(event)
1280 1
1281
    def notify_switch_links_status(self, switch, reason):
1282 1
        """Send an event to notify the status of a link in a switch"""
1283 1
        with self._links_lock:
1284 1
            for link in self.links.values():
1285
                if switch in (link.endpoint_a.switch, link.endpoint_b.switch):
1286 1
                    if reason == "link enabled":
1287
                        name = 'kytos/topology.notify_link_up_if_status'
1288 1
                        content = {'reason': reason, "link": link}
1289 1
                        event = KytosEvent(name=name, content=content)
1290
                        self.controller.buffers.app.put(event)
1291 1
                    else:
1292
                        self.notify_link_status_change(link, reason)
1293 1
1294
    def notify_switch_disabled(self, dpid):
1295
        """Send an event to notify that a switch is disabled."""
1296 1
        name = 'kytos/topology.switch.disabled'
1297 1
        event = KytosEvent(name=name, content={'dpid': dpid})
1298 1
        self.controller.buffers.app.put(event)
1299 1
1300 1
    def notify_topology_update(self):
1301 1
        """Send an event to notify about updates on the topology."""
1302 1
        name = 'kytos/topology.updated'
1303
        event = KytosEvent(name=name, content={'topology':
1304 1
                                               self._get_topology()})
1305
        self.controller.buffers.app.put(event)
1306 1
1307
    def notify_interface_link_status(self, interface, reason):
1308
        """Send an event to notify the status of a link from
1309 1
        an interface."""
1310 1
        link = self._get_link_from_interface(interface)
1311 1
        if link:
1312
            if reason == "link enabled":
1313
                name = 'kytos/topology.notify_link_up_if_status'
1314
                content = {'reason': reason, "link": link}
1315 1
                event = KytosEvent(name=name, content=content)
1316
                self.controller.buffers.app.put(event)
1317 1
            else:
1318 1
                self.notify_link_status_change(link, reason)
1319
1320
    def notify_link_status_change(self, link: Link, reason='not given'):
1321
        """Send an event to notify (up/down) from a status change on
1322
         a link."""
1323
        link_id = link.id
1324
        with self.link_status_lock:
1325 1
            if (
1326
                (not link.status_reason and link.status == EntityStatus.UP)
1327
                and link_id not in self.link_up
1328
            ):
1329 1
                log.info(f"{link} changed status {link.status}, "
1330
                         f"reason: {reason}")
1331 1
                self.link_up.add(link_id)
1332 1
                event = KytosEvent(
1333
                    name='kytos/topology.link_up',
1334
                    content={
1335
                        'link': link,
1336
                        'reason': reason
1337
                    },
1338
                )
1339
            elif (
1340 1
                (link.status_reason or link.status != EntityStatus.UP)
1341 1
                and link_id in self.link_up
1342
            ):
1343 1
                log.info(f"{link} changed status {link.status}, "
1344
                         f"reason: {reason}")
1345 1
                self.link_up.remove(link_id)
1346 1
                event = KytosEvent(
1347 1
                    name='kytos/topology.link_down',
1348 1
                    content={
1349 1
                        'link': link,
1350 1
                        'reason': reason
1351 1
                    },
1352 1
                )
1353 1
            else:
1354
                return
1355 1
        self.controller.buffers.app.put(event)
1356
1357
    def notify_metadata_changes(self, obj, action):
1358
        """Send an event to notify about metadata changes."""
1359 1
        if isinstance(obj, Switch):
1360 1
            entity = 'switch'
1361 1
            entities = 'switches'
1362 1
        elif isinstance(obj, Interface):
1363 1
            entity = 'interface'
1364
            entities = 'interfaces'
1365 1
        elif isinstance(obj, Link):
1366 1
            entity = 'link'
1367
            entities = 'links'
1368
        else:
1369
            raise ValueError(
1370
                'Invalid object, supported: Switch, Interface, Link'
1371
            )
1372 1
1373 1
        name = f'kytos/topology.{entities}.metadata.{action}'
1374
        content = {entity: obj, 'metadata': obj.metadata.copy()}
1375
        event = KytosEvent(name=name, content=content)
1376
        self.controller.buffers.app.put(event)
1377 1
        log.debug(f'Metadata from {obj.id} was {action}.')
1378
1379 1
    @listen_to('kytos/topology.notify_link_up_if_status')
1380 1
    def on_notify_link_up_if_status(self, event):
1381 1
        """Tries to notify link up and topology changes"""
1382
        link = event.content["link"]
1383 1
        reason = event.content["reason"]
1384 1
        self.notify_link_up_if_status(link, reason)
1385
1386
    @listen_to('.*.switch.port.created')
1387 1
    def on_notify_port_created(self, event):
1388
        """Notify when a port is created."""
1389 1
        self.notify_port_created(event)
1390 1
1391 1
    def notify_port_created(self, event):
1392
        """Notify when a port is created."""
1393 1
        name = 'kytos/topology.port.created'
1394
        event = KytosEvent(name=name, content=event.content)
1395
        self.controller.buffers.app.put(event)
1396 1
1397 1
    @staticmethod
1398 1
    def load_interfaces_tags_values(switch: Switch,
1399
                                    interfaces_details: List[dict]) -> None:
1400
        """Load interfaces available tags (vlans)."""
1401
        if not interfaces_details:
1402
            return
1403
        for interface_details in interfaces_details:
1404
            available_tags = interface_details['available_tags']
1405 1
            if not available_tags:
1406
                continue
1407
            log.debug(f"Interface id {interface_details['id']} loading "
1408
                      f"{len(available_tags)} "
1409 1
                      "available tags")
1410
            port_number = int(interface_details["id"].split(":")[-1])
1411
            interface = switch.interfaces[port_number]
1412
            interface.set_available_tags_tag_ranges(
1413
                available_tags,
1414
                interface_details['tag_ranges'],
1415
                interface_details['special_available_tags'],
1416
                interface_details['special_tags'],
1417
            )
1418 1
1419
    @listen_to(
1420 1
        'topology.interruption.(start|end)',
1421 1
        pool="dynamic_single"
1422 1
    )
1423 1
    def on_interruption(self, event: KytosEvent):
1424 1
        """Deals with service interruptions."""
1425
        with self._links_lock:
1426
            _, _, interrupt_type = event.name.rpartition(".")
1427
            if interrupt_type == "start":
1428
                self.handle_interruption_start(event)
1429
            elif interrupt_type == "end":
1430
                self.handle_interruption_end(event)
1431
1432 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...
1433
        """Deals with the start of service interruption."""
1434
        interrupt_type = event.content['type']
1435
        switches = event.content.get('switches', [])
1436 1
        interfaces = event.content.get('interfaces', [])
1437 1
        links = event.content.get('links', [])
1438 1
        log.info(
1439
            'Received interruption start of type \'%s\' '
1440
            'affecting switches %s, interfaces %s, links %s',
1441
            interrupt_type,
1442
            switches,
1443
            interfaces,
1444
            links
1445 1
        )
1446 1
        # for switch_id in switches:
1447
        #     pass
1448 1
        # for interface_id in interfaces:
1449
        #     pass
1450 1
        for link_id in links:
1451 1
            link = self.links.get(link_id)
1452 1
            if link is None:
1453 1
                log.error(
1454 1
                    "Invalid link id '%s' for interruption of type '%s;",
1455
                    link_id,
1456
                    interrupt_type
1457
                )
1458
            else:
1459
                self.notify_link_status_change(link, interrupt_type)
1460
        self.notify_topology_update()
1461
1462 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...
1463
        """Deals with the end of service interruption."""
1464
        interrupt_type = event.content['type']
1465
        switches = event.content.get('switches', [])
1466 1
        interfaces = event.content.get('interfaces', [])
1467 1
        links = event.content.get('links', [])
1468 1
        log.info(
1469
            'Received interruption end of type \'%s\' '
1470
            'affecting switches %s, interfaces %s, links %s',
1471
            interrupt_type,
1472
            switches,
1473
            interfaces,
1474
            links
1475 1
        )
1476 1
        # for switch_id in switches:
1477
        #     pass
1478
        # for interface_id in interfaces:
1479
        #     pass
1480
        for link_id in links:
1481
            link = self.links.get(link_id)
1482
            if link is None:
1483
                log.error(
1484
                    "Invalid link id '%s' for interruption of type '%s;",
1485
                    link_id,
1486
                    interrupt_type
1487
                )
1488
            else:
1489
                self.notify_link_status_change(link, interrupt_type)
1490
        self.notify_topology_update()
1491