Test Failed
Pull Request — master (#132)
by Vinicius
06:30
created

build.main   F

Complexity

Total Complexity 216

Size/Duplication

Total Lines 1155
Duplicated Lines 5.37 %

Test Coverage

Coverage 91.53%

Importance

Changes 0
Metric Value
eloc 853
dl 62
loc 1155
ccs 713
cts 779
cp 0.9153
rs 1.747
c 0
b 0
f 0
wmc 216

86 Methods

Rating   Name   Duplication   Size   Complexity  
A Main.on_notify_port_created() 0 4 1
A Main.disable_switch() 0 15 2
A Main.get_links_from_interfaces() 0 12 5
A Main.notify_port_created() 0 5 1
A Main.on_connection_lost() 0 8 1
A Main.on_interface_link_down() 0 8 1
A Main.disable_link() 0 17 3
A Main.enable_switch() 0 15 2
A Main.on_link_available_tags() 0 5 2
B Main.enable_interface() 31 31 6
A Main.link_status_hook_link_up_timer() 0 11 5
B Main.load_topology() 0 35 5
A Main.add_switch_metadata() 0 14 2
A Main.on_notify_link_up_if_status() 0 6 1
A Main._get_switches_dict() 0 12 3
A Main.get_interfaces() 0 10 3
B Main.delete_interface_metadata() 0 31 5
A Main.add_interface_metadata() 0 21 3
A Main.on_link_liveness_status() 0 11 2
A Main.on_interfaces_created() 0 4 1
A Main.get_switches() 0 4 1
A Main.handle_topo_controller_upsert_switch() 0 3 1
A Main.handle_link_maintenance_end() 0 18 4
A Main.notify_link_status_change() 0 14 2
A Main.handle_interface_down() 0 9 1
A Main.on_interface_created() 0 8 1
A Main.handle_interface_link_up() 0 10 4
A Main.on_interface_link_up() 0 8 1
A Main.handle_link_up() 0 22 4
A Main.notify_switch_enabled() 0 5 1
A Main.handle_interface_created() 0 10 2
A Main.on_link_maintenance_end() 0 5 2
A Main.get_interface_metadata() 0 17 3
A Main.notify_current_topology() 0 6 1
A Main.handle_lldp_status_updated() 0 15 4
A Main.delete_link_metadata() 0 19 3
A Main.on_lldp_status_updated() 0 4 1
A Main.shutdown() 0 3 1
A Main.notify_topology_update() 0 6 1
A Main.on_interface_deleted() 0 4 1
A Main.notify_link_up_if_status() 0 20 5
A Main.enable_link() 0 17 3
B Main.handle_link_down() 0 33 7
A Main.setup() 0 17 1
A Main.notify_interface_link_status() 0 12 3
A Main.on_new_switch() 0 8 1
A Main.handle_new_switch() 0 9 2
A Main.execute() 0 3 1
B Main._load_switch() 0 44 5
A Main.get_switch_metadata() 0 9 2
A Main._get_metadata() 0 7 2
A Main.handle_switch_maintenance_end() 0 13 4
A Main.handle_connection_lost() 0 8 2
A Main.on_get_topology() 0 4 1
A Main.handle_on_link_available_tags() 0 15 2
A Main.on_switch_maintenance_end() 0 4 1
B Main.disable_interface() 31 31 6
A Main.get_links() 0 7 1
A Main.handle_switch_maintenance_start() 0 14 5
A Main.on_link_maintenance_start() 0 5 2
A Main.get_topology() 0 7 1
A Main.handle_interface_link_down() 0 10 4
A Main._get_links_dict() 0 4 1
A Main.handle_interfaces_created() 0 11 3
A Main.handle_link_liveness_status() 0 11 5
A Main.handle_link_maintenance_start() 0 18 4
A Main.notify_switch_disabled() 0 5 1
B Main.add_links() 0 36 5
A Main.on_switch_maintenance_start() 0 4 1
A Main.on_link_liveness_disabled() 0 5 1
A Main.handle_interface_deleted() 0 3 1
A Main._get_link_or_create() 0 14 3
A Main.on_topo_controller_upsert_switch() 0 4 1
A Main._get_link_from_interface() 0 7 4
A Main.add_link_metadata() 0 14 2
A Main.notify_metadata_changes() 0 21 4
A Main.notify_switch_links_status() 0 12 5
B Main._load_link() 0 27 5
A Main.delete_switch_metadata() 0 19 3
A Main.get_topo_controller() 0 4 1
A Main._get_topology_dict() 0 4 1
A Main.handle_link_liveness_disabled() 0 13 3
A Main.load_interfaces_available_tags() 0 16 4
A Main.on_add_links() 0 4 1
A Main.get_link_metadata() 0 8 2
A Main._get_topology() 0 3 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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

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

1
"""Main module of kytos/topology Kytos Network Application.
2
3
Manage the network topology
4
"""
5
# pylint: disable=wrong-import-order
6
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
from kytos.core import KytosEvent, KytosNApp, log, rest
14 1
from kytos.core.common import EntityStatus
15 1
from kytos.core.exceptions import KytosLinkCreationError
16 1
from kytos.core.helpers import listen_to, now
17 1
from kytos.core.interface import Interface
18 1
from kytos.core.link import Link
19 1
from kytos.core.rest_api import (HTTPException, JSONResponse, Request,
20
                                 content_type_json_or_415, get_json_or_400)
21 1
from kytos.core.switch import Switch
22 1
from napps.kytos.topology import settings
23
24 1
from .controllers import TopoController
25 1
from .exceptions import RestoreError
26 1
from .models import Topology
27
28 1
DEFAULT_LINK_UP_TIMER = 10
29
30
31 1
class Main(KytosNApp):  # pylint: disable=too-many-public-methods
32
    """Main class of kytos/topology NApp.
33
34
    This class is the entry point for this napp.
35
    """
36
37 1
    def setup(self):
38
        """Initialize the NApp's links list."""
39 1
        self.links = {}
40 1
        self.intf_available_tags = {}
41 1
        self.link_up_timer = getattr(settings, 'LINK_UP_TIMER',
42
                                     DEFAULT_LINK_UP_TIMER)
43
44 1
        self._links_lock = Lock()
45 1
        self._links_notify_lock = defaultdict(Lock)
46 1
        # to keep track of potential unorded scheduled interface events
47 1
        self._intfs_lock = defaultdict(Lock)
48
        self._intfs_updated_at = {}
49 1
        self.topo_controller = self.get_topo_controller()
50 1
        Link.register_status_func(f"{self.napp_id}_link_up_timer",
51
                                  self.link_status_hook_link_up_timer)
52 1
        self.topo_controller.bootstrap_indexes()
53 1
        self.load_topology()
54
55
    @staticmethod
56
    def get_topo_controller() -> TopoController:
57 1
        """Get TopoController."""
58
        return TopoController()
59
60
    def execute(self):
61 1
        """Execute once when the napp is running."""
62
        pass
63
64
    def shutdown(self):
65 1
        """Do nothing."""
66
        log.info('NApp kytos/topology shutting down.')
67 1
68 1
    def _get_metadata(self, request: Request) -> dict:
69 1
        """Return a JSON with metadata."""
70 1
        content_type_json_or_415(request)
71 1
        metadata = get_json_or_400(request, self.controller.loop)
72
        if not isinstance(metadata, dict):
73 1
            raise HTTPException(400, "Invalid metadata value: {metadata}")
74
        return metadata
75
76
    def _get_link_or_create(self, endpoint_a, endpoint_b):
77
        """Get an existing link or create a new one.
78
79 1
        Returns:
80
            Tuple(Link, bool): Link and a boolean whether it has been created.
81 1
        """
82 1
        new_link = Link(endpoint_a, endpoint_b)
83 1
84
        for link in self.links.values():
85 1
            if new_link == link:
86 1
                return (link, False)
87
88 1
        self.links[new_link.id] = new_link
89
        return (new_link, True)
90 1
91 1
    def _get_switches_dict(self):
92 1
        """Return a dictionary with the known switches."""
93 1
        switches = {'switches': {}}
94
        for idx, switch in enumerate(self.controller.switches.copy().values()):
95
            switch_data = switch.as_dict()
96
            if not all(key in switch_data['metadata']
97
                       for key in ('lat', 'lng')):
98 1
                # Switches are initialized somewhere in the ocean
99 1
                switch_data['metadata']['lat'] = str(0.0)
100
                switch_data['metadata']['lng'] = str(-30.0+idx*10.0)
101 1
            switches['switches'][switch.id] = switch_data
102
        return switches
103 1
104
    def _get_links_dict(self):
105
        """Return a dictionary with the known links."""
106 1
        return {'links': {link.id: link.as_dict() for link in
107
                          self.links.copy().values()}}
108 1
109
    def _get_topology_dict(self):
110
        """Return a dictionary with the known topology."""
111 1
        return {'topology': {**self._get_switches_dict(),
112
                             **self._get_links_dict()}}
113 1
114
    def _get_topology(self):
115 1
        """Return an object representing the topology."""
116
        return Topology(self.controller.switches.copy(), self.links.copy())
117 1
118 1
    def _get_link_from_interface(self, interface):
119 1
        """Return the link of the interface, or None if it does not exist."""
120 1
        with self._links_lock:
121 1
            for link in self.links.values():
122
                if interface in (link.endpoint_a, link.endpoint_b):
123 1
                    return link
124 1
            return None
125 1
126 1
    def _load_link(self, link_att):
127 1
        endpoint_a = link_att['endpoint_a']['id']
128 1
        endpoint_b = link_att['endpoint_b']['id']
129 1
        link_str = link_att['id']
130
        log.info(f"Loading link: {link_str}")
131 1
        interface_a = self.controller.get_interface_by_id(endpoint_a)
132 1
        interface_b = self.controller.get_interface_by_id(endpoint_b)
133 1
134 1
        error = f"Fail to load endpoints for link {link_str}. "
135
        if not interface_a:
136
            raise RestoreError(f"{error}, endpoint_a {endpoint_a} not found")
137 1
        if not interface_b:
138 1
            raise RestoreError(f"{error}, endpoint_b {endpoint_b} not found")
139
140 1
        with self._links_lock:
141 1
            link, _ = self._get_link_or_create(interface_a, interface_b)
142
143 1
        if link_att['enabled']:
144
            link.enable()
145 1
        else:
146 1
            link.disable()
147 1
148 1
        link.extend_metadata(link_att["metadata"])
149 1
        interface_a.update_link(link)
150
        interface_b.update_link(link)
151 1
        interface_a.nni = True
152 1
        interface_b.nni = True
153 1
154 1
    def _load_switch(self, switch_id, switch_att):
155 1
        log.info(f'Loading switch dpid: {switch_id}')
156
        switch = self.controller.get_switch_or_create(switch_id)
157 1
        if switch_att['enabled']:
158 1
            switch.enable()
159 1
        else:
160 1
            switch.disable()
161 1
        switch.description['manufacturer'] = switch_att.get('manufacturer', '')
162 1
        switch.description['hardware'] = switch_att.get('hardware', '')
163 1
        switch.description['software'] = switch_att.get('software')
164
        switch.description['serial'] = switch_att.get('serial', '')
165 1
        switch.description['data_path'] = switch_att.get('data_path', '')
166 1
        switch.extend_metadata(switch_att["metadata"])
167 1
168
        for iface_id, iface_att in switch_att.get('interfaces', {}).items():
169
            log.info(f'Loading interface iface_id={iface_id}')
170
            interface = switch.update_or_create_interface(
171
                            port_no=iface_att['port_number'],
172 1
                            name=iface_att['name'],
173 1
                            address=iface_att.get('mac', None),
174
                            speed=iface_att.get('speed', None))
175 1
            if iface_att['enabled']:
176 1
                interface.enable()
177 1
            else:
178 1
                interface.disable()
179 1
            interface.lldp = iface_att['lldp']
180 1
            interface.extend_metadata(iface_att["metadata"])
181
            interface.deactivate()
182
            name = 'kytos/topology.port.created'
183
            event = KytosEvent(name=name, content={
184
                                              'switch': switch_id,
185
                                              'port': interface.port_number,
186
                                              'port_description': {
187
                                                  'alias': interface.name,
188
                                                  'mac': interface.address,
189 1
                                                  'state': interface.state
190
                                                  }
191 1
                                              })
192 1
            self.controller.buffers.app.put(event)
193 1
194 1
        intf_ids = [v["id"] for v in switch_att.get("interfaces", {}).values()]
195
        intf_details = self.topo_controller.get_interfaces_details(intf_ids)
196
        with self._links_lock:
197 1
            self.load_interfaces_available_tags(switch, intf_details)
198
199 1
    # pylint: disable=attribute-defined-outside-init
200 1
    def load_topology(self):
201 1
        """Load network topology from DB."""
202
        topology = self.topo_controller.get_topology()
203 1
        switches = topology["topology"]["switches"]
204 1
        links = topology["topology"]["links"]
205 1
206 1
        failed_switches = {}
207 1
        log.debug(f"_load_network_status switches={switches}")
208
        for switch_id, switch_att in switches.items():
209 1
            try:
210 1
                self._load_switch(switch_id, switch_att)
211 1
            # pylint: disable=broad-except
212
            except Exception as err:
213 1
                failed_switches[switch_id] = err
214 1
                log.error(f'Error loading switch: {err}')
215 1
216 1
        failed_links = {}
217 1
        log.debug(f"_load_network_status links={links}")
218
        for link_id, link_att in links.items():
219 1
            try:
220 1
                self._load_link(link_att)
221 1
            # pylint: disable=broad-except
222
            except Exception as err:
223 1
                failed_links[link_id] = err
224 1
                log.error(f'Error loading link {link_id}: {err}')
225
226
        name = 'kytos/topology.topology_loaded'
227
        event = KytosEvent(
228
            name=name,
229
            content={
230
                'topology': self._get_topology(),
231 1
                'failed_switches': failed_switches,
232
                'failed_links': failed_links
233 1
            })
234 1
        self.controller.buffers.app.put(event)
235
236
    @rest('v3/')
237
    def get_topology(self, _request: Request) -> JSONResponse:
238
        """Return the latest known topology.
239 1
240
        This topology is updated when there are network events.
241
        """
242 1
        return JSONResponse(self._get_topology_dict())
243 1
244
    # Switch related methods
245
    @rest('v3/switches')
246
    def get_switches(self, _request: Request) -> JSONResponse:
247 1
        """Return a json with all the switches in the topology."""
248 1
        return JSONResponse(self._get_switches_dict())
249
250 1
    @rest('v3/switches/{dpid}/enable', methods=['POST'])
251 1
    def enable_switch(self, request: Request) -> JSONResponse:
252 1
        """Administratively enable a switch in the topology."""
253 1
        dpid = request.path_params["dpid"]
254 1
        try:
255 1
            switch = self.controller.switches[dpid]
256 1
            self.topo_controller.enable_switch(dpid)
257
            switch.enable()
258 1
        except KeyError:
259 1
            raise HTTPException(404, detail="Switch not found")
260 1
261 1
        self.notify_switch_enabled(dpid)
262
        self.notify_topology_update()
263 1
        self.notify_switch_links_status(switch, "link enabled")
264 1
        return JSONResponse("Operation successful", status_code=201)
265
266 1
    @rest('v3/switches/{dpid}/disable', methods=['POST'])
267 1
    def disable_switch(self, request: Request) -> JSONResponse:
268 1
        """Administratively disable a switch in the topology."""
269 1
        dpid = request.path_params["dpid"]
270 1
        try:
271 1
            switch = self.controller.switches[dpid]
272 1
            self.topo_controller.disable_switch(dpid)
273
            switch.disable()
274 1
        except KeyError:
275 1
            raise HTTPException(404, detail="Switch not found")
276 1
277 1
        self.notify_switch_disabled(dpid)
278
        self.notify_topology_update()
279 1
        self.notify_switch_links_status(switch, "link disabled")
280 1
        return JSONResponse("Operation successful", status_code=201)
281
282 1
    @rest('v3/switches/{dpid}/metadata')
283 1
    def get_switch_metadata(self, request: Request) -> JSONResponse:
284 1
        """Get metadata from a switch."""
285 1
        dpid = request.path_params["dpid"]
286 1
        try:
287 1
            metadata = self.controller.switches[dpid].metadata
288
            return JSONResponse({"metadata": metadata})
289 1
        except KeyError:
290 1
            raise HTTPException(404, detail="Switch not found")
291
292 1
    @rest('v3/switches/{dpid}/metadata', methods=['POST'])
293 1
    def add_switch_metadata(self, request: Request) -> JSONResponse:
294 1
        """Add metadata to a switch."""
295 1
        dpid = request.path_params["dpid"]
296 1
        metadata = self._get_metadata(request)
297 1
        try:
298
            switch = self.controller.switches[dpid]
299 1
        except KeyError:
300 1
            raise HTTPException(404, detail="Switch not found")
301 1
302 1
        self.topo_controller.add_switch_metadata(dpid, metadata)
303
        switch.extend_metadata(metadata)
304 1
        self.notify_metadata_changes(switch, 'added')
305 1
        return JSONResponse("Operation successful", status_code=201)
306
307 1
    @rest('v3/switches/{dpid}/metadata/{key}', methods=['DELETE'])
308 1
    def delete_switch_metadata(self, request: Request) -> JSONResponse:
309 1
        """Delete metadata from a switch."""
310 1
        dpid = request.path_params["dpid"]
311 1
        key = request.path_params["key"]
312 1
        try:
313
            switch = self.controller.switches[dpid]
314 1
        except KeyError:
315 1
            raise HTTPException(404, detail="Switch not found")
316
317
        try:
318
            _ = switch.metadata[key]
319 1
        except KeyError:
320 1
            raise HTTPException(404, "Metadata not found")
321 1
322 1
        self.topo_controller.delete_switch_metadata_key(dpid, key)
323
        switch.remove_metadata(key)
324
        self.notify_metadata_changes(switch, 'removed')
325 1
        return JSONResponse("Operation successful")
326 1
327
    # Interface related methods
328 1
    @rest('v3/interfaces')
329 1
    def get_interfaces(self, _request: Request) -> JSONResponse:
330 1
        """Return a json with all the interfaces in the topology."""
331 1
        interfaces = {}
332 1
        switches = self._get_switches_dict()
333
        for switch in switches['switches'].values():
334 1
            for interface_id, interface in switch['interfaces'].items():
335
                interfaces[interface_id] = interface
336 1
337 1
        return JSONResponse({'interfaces': interfaces})
338 1
339 View Code Duplication
    @rest('v3/interfaces/switch/{dpid}/enable', methods=['POST'])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
340 1
    @rest('v3/interfaces/{interface_enable_id}/enable', methods=['POST'])
341 1
    def enable_interface(self, request: Request) -> JSONResponse:
342 1
        """Administratively enable interfaces in the topology."""
343 1
        interface_enable_id = request.path_params.get("interface_enable_id")
344 1
        dpid = request.path_params.get("dpid")
345 1
        if dpid is None:
346
            dpid = ":".join(interface_enable_id.split(":")[:-1])
347
        try:
348
            switch = self.controller.switches[dpid]
349 1
        except KeyError:
350 1
            raise HTTPException(404, detail="Switch not found")
351
352 1
        if interface_enable_id:
353 1
            interface_number = int(interface_enable_id.split(":")[-1])
354 1
355 1
            try:
356 1
                interface = switch.interfaces[interface_number]
357 1
                self.topo_controller.enable_interface(interface.id)
358 1
                interface.enable()
359 1
                self.notify_interface_link_status(interface, "link enabled")
360
            except KeyError:
361 1
                msg = f"Switch {dpid} interface {interface_number} not found"
362 1
                raise HTTPException(404, detail=msg)
363 1
        else:
364 1
            for interface in switch.interfaces.copy().values():
365 1
                interface.enable()
366 1
                self.notify_interface_link_status(interface, "link enabled")
367
            self.topo_controller.upsert_switch(switch.id, switch.as_dict())
368 1
        self.notify_topology_update()
369 1
        return JSONResponse("Operation successful")
370 1
371 View Code Duplication
    @rest('v3/interfaces/switch/{dpid}/disable', methods=['POST'])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
372 1
    @rest('v3/interfaces/{interface_disable_id}/disable', methods=['POST'])
373 1
    def disable_interface(self, request: Request) -> JSONResponse:
374 1
        """Administratively disable interfaces in the topology."""
375 1
        interface_disable_id = request.path_params.get("interface_disable_id")
376 1
        dpid = request.path_params.get("dpid")
377 1
        if dpid is None:
378 1
            dpid = ":".join(interface_disable_id.split(":")[:-1])
379 1
        try:
380
            switch = self.controller.switches[dpid]
381 1
        except KeyError:
382 1
            raise HTTPException(404, detail="Switch not found")
383
384 1
        if interface_disable_id:
385 1
            interface_number = int(interface_disable_id.split(":")[-1])
386 1
387 1
            try:
388 1
                interface = switch.interfaces[interface_number]
389 1
                self.topo_controller.disable_interface(interface.id)
390 1
                interface.disable()
391 1
                self.notify_interface_link_status(interface, "link disabled")
392
            except KeyError:
393 1
                msg = f"Switch {dpid} interface {interface_number} not found"
394 1
                raise HTTPException(404, detail=msg)
395 1
        else:
396 1
            for interface in switch.interfaces.copy().values():
397 1
                interface.disable()
398 1
                self.notify_interface_link_status(interface, "link disabled")
399
            self.topo_controller.upsert_switch(switch.id, switch.as_dict())
400 1
        self.notify_topology_update()
401 1
        return JSONResponse("Operation successful")
402
403 1
    @rest('v3/interfaces/{interface_id}/metadata')
404 1
    def get_interface_metadata(self, request: Request) -> JSONResponse:
405 1
        """Get metadata from an interface."""
406 1
        interface_id = request.path_params["interface_id"]
407 1
        switch_id = ":".join(interface_id.split(":")[:-1])
408 1
        interface_number = int(interface_id.split(":")[-1])
409 1
        try:
410
            switch = self.controller.switches[switch_id]
411 1
        except KeyError:
412 1
            raise HTTPException(404, detail="Switch not found")
413 1
414 1
        try:
415
            interface = switch.interfaces[interface_number]
416 1
        except KeyError:
417
            raise HTTPException(404, detail="Interface not found")
418 1
419 1
        return JSONResponse({"metadata": interface.metadata})
420
421 1
    @rest('v3/interfaces/{interface_id}/metadata', methods=['POST'])
422 1
    def add_interface_metadata(self, request: Request) -> JSONResponse:
423 1
        """Add metadata to an interface."""
424 1
        interface_id = request.path_params["interface_id"]
425 1
        metadata = self._get_metadata(request)
426 1
        switch_id = ":".join(interface_id.split(":")[:-1])
427 1
        interface_number = int(interface_id.split(":")[-1])
428 1
        try:
429
            switch = self.controller.switches[switch_id]
430 1
        except KeyError:
431 1
            raise HTTPException(404, detail="Switch not found")
432 1
433 1
        try:
434 1
            interface = switch.interfaces[interface_number]
435
            self.topo_controller.add_interface_metadata(interface.id, metadata)
436 1
        except KeyError:
437 1
            raise HTTPException(404, detail="Interface not found")
438 1
439
        interface.extend_metadata(metadata)
440 1
        self.notify_metadata_changes(interface, 'added')
441 1
        return JSONResponse("Operation successful", status_code=201)
442
443 1
    @rest('v3/interfaces/{interface_id}/metadata/{key}', methods=['DELETE'])
444 1
    def delete_interface_metadata(self, request: Request) -> JSONResponse:
445 1
        """Delete metadata from an interface."""
446 1
        interface_id = request.path_params["interface_id"]
447 1
        key = request.path_params["key"]
448
        switch_id = ":".join(interface_id.split(":")[:-1])
449
        try:
450
            interface_number = int(interface_id.split(":")[-1])
451
        except ValueError:
452 1
            detail = f"Invalid interface_id {interface_id}"
453 1
            raise HTTPException(400, detail=detail)
454 1
455 1
        try:
456
            switch = self.controller.switches[switch_id]
457 1
        except KeyError:
458 1
            raise HTTPException(404, detail="Switch not found")
459 1
460 1
        try:
461
            interface = switch.interfaces[interface_number]
462 1
        except KeyError:
463 1
            raise HTTPException(404, detail="Interface not found")
464 1
465 1
        try:
466
            _ = interface.metadata[key]
467 1
        except KeyError:
468 1
            raise HTTPException(404, detail="Metadata not found")
469 1
470 1
        self.topo_controller.delete_interface_metadata_key(interface.id, key)
471
        interface.remove_metadata(key)
472
        self.notify_metadata_changes(interface, 'removed')
473 1
        return JSONResponse("Operation successful")
474 1
475
    # Link related methods
476
    @rest('v3/links')
477
    def get_links(self, _request: Request) -> JSONResponse:
478
        """Return a json with all the links in the topology.
479
480
        Links are connections between interfaces.
481 1
        """
482 1
        return JSONResponse(self._get_links_dict())
483
484 1
    @rest('v3/links/{link_id}/enable', methods=['POST'])
485 1
    def enable_link(self, request: Request) -> JSONResponse:
486 1
        """Administratively enable a link in the topology."""
487 1
        link_id = request.path_params["link_id"]
488 1
        try:
489 1
            with self._links_lock:
490 1
                link = self.links[link_id]
491 1
                self.topo_controller.enable_link(link_id)
492 1
                link.enable()
493
        except KeyError:
494
            raise HTTPException(404, detail="Link not found")
495
        self.notify_link_status_change(
496 1
            self.links[link_id],
497 1
            reason='link enabled'
498
        )
499 1
        self.notify_topology_update()
500 1
        return JSONResponse("Operation successful", status_code=201)
501
502 1
    @rest('v3/links/{link_id}/disable', methods=['POST'])
503 1
    def disable_link(self, request: Request) -> JSONResponse:
504 1
        """Administratively disable a link in the topology."""
505 1
        link_id = request.path_params["link_id"]
506 1
        try:
507 1
            with self._links_lock:
508 1
                link = self.links[link_id]
509 1
                self.topo_controller.disable_link(link_id)
510 1
                link.disable()
511
        except KeyError:
512
            raise HTTPException(404, detail="Link not found")
513
        self.notify_link_status_change(
514 1
            self.links[link_id],
515 1
            reason='link disabled'
516
        )
517 1
        self.notify_topology_update()
518 1
        return JSONResponse("Operation successful", status_code=201)
519
520 1
    @rest('v3/links/{link_id}/metadata')
521 1
    def get_link_metadata(self, request: Request) -> JSONResponse:
522 1
        """Get metadata from a link."""
523 1
        link_id = request.path_params["link_id"]
524 1
        try:
525
            return JSONResponse({"metadata": self.links[link_id].metadata})
526 1
        except KeyError:
527 1
            raise HTTPException(404, detail="Link not found")
528
529 1
    @rest('v3/links/{link_id}/metadata', methods=['POST'])
530 1
    def add_link_metadata(self, request: Request) -> JSONResponse:
531 1
        """Add metadata to a link."""
532 1
        link_id = request.path_params["link_id"]
533 1
        metadata = self._get_metadata(request)
534 1
        try:
535
            link = self.links[link_id]
536 1
        except KeyError:
537 1
            raise HTTPException(404, detail="Link not found")
538 1
539 1
        self.topo_controller.add_link_metadata(link_id, metadata)
540
        link.extend_metadata(metadata)
541 1
        self.notify_metadata_changes(link, 'added')
542 1
        return JSONResponse("Operation successful", status_code=201)
543
544 1
    @rest('v3/links/{link_id}/metadata/{key}', methods=['DELETE'])
545 1
    def delete_link_metadata(self, request: Request) -> JSONResponse:
546 1
        """Delete metadata from a link."""
547 1
        link_id = request.path_params["link_id"]
548 1
        key = request.path_params["key"]
549 1
        try:
550
            link = self.links[link_id]
551 1
        except KeyError:
552 1
            raise HTTPException(404, detail="Link not found")
553 1
554 1
        try:
555
            _ = link.metadata[key]
556 1
        except KeyError:
557 1
            raise HTTPException(404, detail="Metadata not found")
558 1
559 1
        self.topo_controller.delete_link_metadata_key(link.id, key)
560
        link.remove_metadata(key)
561 1
        self.notify_metadata_changes(link, 'removed')
562
        return JSONResponse("Operation successful")
563 1
564 1
    def notify_current_topology(self) -> None:
565
        """Notify current topology."""
566 1
        name = "kytos/topology.current"
567
        event = KytosEvent(name=name, content={"topology":
568 1
                                               self._get_topology()})
569 1
        self.controller.buffers.app.put(event)
570
571
    @listen_to("kytos/topology.get")
572
    def on_get_topology(self, _event) -> None:
573 1
        """Handle kytos/topology.get."""
574 1
        self.notify_current_topology()
575
576
    @listen_to("kytos/.*.liveness.(up|down)")
577
    def on_link_liveness_status(self, event) -> None:
578
        """Handle link liveness up|down status event."""
579
        link = Link(event.content["interface_a"], event.content["interface_b"])
580
        try:
581
            link = self.links[link.id]
582
        except KeyError:
583
            log.error(f"Link id {link.id} not found, {link}")
584
            return
585 1
        liveness_status = event.name.split(".")[-1]
586
        self.handle_link_liveness_status(self.links[link.id], liveness_status)
587 1
588 1
    def handle_link_liveness_status(self, link, liveness_status) -> None:
589 1
        """Handle link liveness."""
590 1
        metadata = {"liveness_status": liveness_status}
591 1
        log.info(f"Link liveness {liveness_status}: {link}")
592 1
        self.topo_controller.add_link_metadata(link.id, metadata)
593 1
        link.extend_metadata(metadata)
594 1
        self.notify_topology_update()
595 1
        if link.status == EntityStatus.UP and liveness_status == "up":
596
            self.notify_link_status_change(link, reason="liveness_up")
597 1
        if link.status == EntityStatus.DOWN and liveness_status == "down":
598 1
            self.notify_link_status_change(link, reason="liveness_down")
599
600
    @listen_to("kytos/.*.liveness.disabled")
601
    def on_link_liveness_disabled(self, event) -> None:
602
        """Handle link liveness disabled event."""
603 1
        interfaces = event.content["interfaces"]
604
        self.handle_link_liveness_disabled(interfaces)
605 1
606 1
    def get_links_from_interfaces(self, interfaces) -> dict:
607 1
        """Get links from interfaces."""
608 1
        links_found = {}
609 1
        with self._links_lock:
610
            for interface in interfaces:
611
                for link in self.links.values():
612
                    if any((
613 1
                        interface.id == link.endpoint_a.id,
614 1
                        interface.id == link.endpoint_b.id,
615
                    )):
616 1
                        links_found[link.id] = link
617
        return links_found
618 1
619
    def handle_link_liveness_disabled(self, interfaces) -> None:
620 1
        """Handle link liveness disabled."""
621 1
        log.info(f"Link liveness disabled interfaces: {interfaces}")
622 1
623 1
        key = "liveness_status"
624 1
        links = self.get_links_from_interfaces(interfaces)
625 1
        for link in links.values():
626 1
            link.remove_metadata(key)
627 1
        link_ids = list(links.keys())
628 1
        self.topo_controller.bulk_delete_link_metadata_key(link_ids, key)
629
        self.notify_topology_update()
630 1
        for link in links.values():
631 1
            self.notify_link_status_change(link, reason="liveness_disabled")
632
633
    @listen_to("kytos/.*.link_available_tags")
634
    def on_link_available_tags(self, event):
635
        """Handle on_link_available_tags."""
636 1
        with self._links_lock:
637
            self.handle_on_link_available_tags(event.content.get("link"))
638 1
639
    def handle_on_link_available_tags(self, link):
640 1
        """Handle on_link_available_tags."""
641 1
        if link.id not in self.links:
642 1
            return
643 1
        endpoint_a = self.links[link.id].endpoint_a
644 1
        endpoint_b = self.links[link.id].endpoint_b
645
        values_a = [tag.value for tag in endpoint_a.available_tags]
646
        values_b = [tag.value for tag in endpoint_b.available_tags]
647
        ids_details = [
648
            (endpoint_a.id, {"_id": endpoint_a.id,
649
                             "available_vlans": values_a}),
650 1
            (endpoint_b.id, {"_id": endpoint_b.id,
651
                             "available_vlans": values_b})
652 1
        ]
653 1
        self.topo_controller.bulk_upsert_interface_details(ids_details)
654
655
    @listen_to('.*.switch.(new|reconnected)')
656
    def on_new_switch(self, event):
657
        """Create a new Device on the Topology.
658
659
        Handle the event of a new created switch and update the topology with
660
        this new device. Also notify if the switch is enabled.
661 1
        """
662
        self.handle_new_switch(event)
663 1
664 1
    def handle_new_switch(self, event):
665 1
        """Create a new Device on the Topology."""
666 1
        switch = event.content['switch']
667 1
        switch.activate()
668 1
        self.topo_controller.upsert_switch(switch.id, switch.as_dict())
669 1
        log.debug('Switch %s added to the Topology.', switch.id)
670
        self.notify_topology_update()
671 1
        if switch.is_enabled():
672 1
            self.notify_switch_enabled(switch.id)
673
674
    @listen_to('.*.connection.lost')
675
    def on_connection_lost(self, event):
676
        """Remove a Device from the topology.
677
678
        Remove the disconnected Device and every link that has one of its
679
        interfaces.
680 1
        """
681
        self.handle_connection_lost(event)
682 1
683 1
    def handle_connection_lost(self, event):
684 1
        """Remove a Device from the topology."""
685 1
        switch = event.content['source'].switch
686 1
        if switch:
687 1
            switch.deactivate()
688
            self.topo_controller.deactivate_switch(switch.id)
689 1
            log.debug('Switch %s removed from the Topology.', switch.id)
690
            self.notify_topology_update()
691 1
692 1
    def handle_interfaces_created(self, event):
693
        """Update the topology based on the interfaces created."""
694 1
        interfaces = event.content["interfaces"]
695 1
        if not interfaces:
696 1
            return
697 1
        switch = interfaces[0].switch
698 1
        self.topo_controller.upsert_switch(switch.id, switch.as_dict())
699 1
        name = "kytos/topology.switch.interface.created"
700
        for interface in interfaces:
701 1
            event = KytosEvent(name=name, content={'interface': interface})
702
            self.controller.buffers.app.put(event)
703
704
    def handle_interface_created(self, event):
705
        """Update the topology based on an interface created event.
706
707 1
        It's handled as a link_up in case a switch send a
708 1
        created event again and it can be belong to a link.
709 1
        """
710 1
        interface = event.content['interface']
711
        if not interface.is_active():
712 1
            return
713 1
        self.handle_interface_link_up(interface, event)
714
715
    @listen_to('.*.topology.switch.interface.created')
716
    def on_interface_created(self, event):
717
        """Handle individual interface create event.
718
719
        It's handled as a link_up in case a switch send a
720
        created event it can belong to an existign link.
721 1
        """
722 1
        self.handle_interface_created(event)
723
724
    @listen_to('.*.switch.interfaces.created')
725
    def on_interfaces_created(self, event):
726 1
        """Update the topology based on a list of created interfaces."""
727
        self.handle_interfaces_created(event)
728
729
    def handle_interface_down(self, event):
730
        """Update the topology based on a Port Modify event.
731 1
732 1
        The event notifies that an interface was changed to 'down'.
733 1
        """
734 1
        interface = event.content['interface']
735
        interface.deactivate()
736 1
        self.topo_controller.deactivate_interface(interface.id)
737 1
        self.handle_interface_link_down(interface, event)
738
739
    @listen_to('.*.switch.interface.deleted')
740
    def on_interface_deleted(self, event):
741 1
        """Update the topology based on a Port Delete event."""
742
        self.handle_interface_deleted(event)
743 1
744
    def handle_interface_deleted(self, event):
745 1
        """Update the topology based on a Port Delete event."""
746 1
        self.handle_interface_down(event)
747
748
    @listen_to('.*.switch.interface.link_up')
749
    def on_interface_link_up(self, event):
750
        """Update the topology based on a Port Modify event.
751
752
        The event notifies that an interface's link was changed to 'up'.
753
        """
754 1
        interface = event.content['interface']
755
        self.handle_interface_link_up(interface, event)
756 1
757
    def handle_interface_link_up(self, interface, event):
758 1
        """Update the topology based on a Port Modify event."""
759 1
        with self._intfs_lock[interface.id]:
760
            if (
761
                interface.id in self._intfs_updated_at
762
                and self._intfs_updated_at[interface.id] > event.timestamp
763 1
            ):
764
                return
765 1
            self._intfs_updated_at[interface.id] = event.timestamp
766 1
        self.handle_link_up(interface)
767 1
768 1
    @listen_to('kytos/maintenance.end_switch')
769
    def on_switch_maintenance_end(self, event):
770
        """Handle the end of the maintenance of a switch."""
771 1
        self.handle_switch_maintenance_end(event)
772 1
773 1
    def handle_switch_maintenance_end(self, event):
774 1
        """Handle the end of the maintenance of a switch."""
775 1
        switches = event.content['switches']
776
        for switch_id in switches:
777 1
            try:
778
                switch = self.controller.switches[switch_id]
779 1
            except KeyError:
780 1
                continue
781
            switch.enable()
782
            switch.activate()
783
            for interface in switch.interfaces.values():
784
                interface.enable()
785
                self.handle_link_up(interface)
786 1
787 1
    def link_status_hook_link_up_timer(self, link) -> Optional[EntityStatus]:
788
        """Link status hook link up timer."""
789 1
        tnow = time.time()
790
        if (
791
            link.is_active()
792
            and link.is_enabled()
793 1
            and "last_status_change" in link.metadata
794 1
            and tnow - link.metadata['last_status_change'] < self.link_up_timer
795
        ):
796 1
            return EntityStatus.DOWN
797 1
        return None
798 1
799
    def notify_link_up_if_status(self, link, reason="link up") -> None:
800
        """Tries to notify link up and topology changes based on its status
801
802
        Currently, it needs to wait up to a timer."""
803 1
        time.sleep(self.link_up_timer)
804 1
        if link.status != EntityStatus.UP:
805 1
            return
806 1
        with self._links_notify_lock[link.id]:
807 1
            notified_at = link.get_metadata("notified_up_at")
808 1
            if (
809
                notified_at
810 1
                and (now() - notified_at.replace(tzinfo=timezone.utc)).seconds
811
                < self.link_up_timer
812 1
            ):
813 1
                return
814 1
            key, notified_at = "notified_up_at", now()
815 1
            link.update_metadata(key, now())
816 1
            self.topo_controller.add_link_metadata(link.id, {key: notified_at})
817
            self.notify_topology_update()
818 1
            self.notify_link_status_change(link, reason)
819 1
820
    def handle_link_up(self, interface):
821 1
        """Handle link up for an interface."""
822 1
        interface.activate()
823 1
        self.topo_controller.activate_interface(interface.id)
824 1
        self.notify_topology_update()
825
        link = self._get_link_from_interface(interface)
826
        if not link:
827
            return
828 1
        if link.endpoint_a == interface:
829 1
            other_interface = link.endpoint_b
830 1
        else:
831 1
            other_interface = link.endpoint_a
832
        if other_interface.is_active() is False:
833 1
            return
834 1
        metadata = {
835
            'last_status_change': time.time(),
836
            'last_status_is_active': True
837
        }
838
        link.extend_metadata(metadata)
839
        link.activate()
840
        self.topo_controller.activate_link(link.id, **metadata)
841
        self.notify_link_up_if_status(link, "link up")
842 1
843
    @listen_to('.*.switch.interface.link_down')
844 1
    def on_interface_link_down(self, event):
845
        """Update the topology based on a Port Modify event.
846 1
847 1
        The event notifies that an interface's link was changed to 'down'.
848
        """
849
        interface = event.content['interface']
850
        self.handle_interface_link_down(interface, event)
851 1
852
    def handle_interface_link_down(self, interface, event):
853 1
        """Update the topology based on an interface."""
854 1
        with self._intfs_lock[interface.id]:
855 1
            if (
856 1
                interface.id in self._intfs_updated_at
857
                and self._intfs_updated_at[interface.id] > event.timestamp
858
            ):
859 1
                return
860 1
            self._intfs_updated_at[interface.id] = event.timestamp
861 1
        self.handle_link_down(interface)
862 1
863 1
    @listen_to('kytos/maintenance.start_switch')
864 1
    def on_switch_maintenance_start(self, event):
865
        """Handle the start of the maintenance of a switch."""
866 1
        self.handle_switch_maintenance_start(event)
867
868 1
    def handle_switch_maintenance_start(self, event):
869 1
        """Handle the start of the maintenance of a switch."""
870 1
        switches = event.content['switches']
871 1
        for switch_id in switches:
872 1
            try:
873 1
                switch = self.controller.switches[switch_id]
874
            except KeyError:
875
                continue
876
            switch.disable()
877 1
            switch.deactivate()
878 1
            for interface in switch.interfaces.values():
879
                interface.disable()
880 1
                if interface.is_active():
881 1
                    self.handle_link_down(interface)
882 1
883 1
    def handle_link_down(self, interface):
884 1
        """Notify a link is down."""
885 1
        link = self._get_link_from_interface(interface)
886 1
        if link and link.is_active():
887
            link.deactivate()
888
            last_status_change = time.time()
889
            last_status_is_active = False
890 1
            metadata = {
891 1
                "last_status_change": last_status_change,
892 1
                "last_status_is_active": last_status_is_active,
893
            }
894
            link.extend_metadata(metadata)
895 1
            self.topo_controller.deactivate_link(link.id, last_status_change,
896 1
                                                 last_status_is_active)
897 1
            self.notify_link_status_change(link, reason="link down")
898 1
        if link and not link.is_active():
899
            with self._links_lock:
900 1
                last_status = link.get_metadata('last_status_is_active')
901 1
                last_status_change = time.time()
902
                last_status_is_active = False
903
                metadata = {
904
                    "last_status_change": last_status_change,
905 1
                    "last_status_is_active": last_status_is_active,
906
                }
907 1
                if last_status:
908 1
                    link.extend_metadata(metadata)
909
                    self.topo_controller.deactivate_link(link.id,
910 1
                                                         last_status_change,
911 1
                                                         last_status_is_active)
912 1
                    self.notify_link_status_change(link, reason='link down')
913
        interface.deactivate()
914 1
        self.topo_controller.deactivate_interface(interface.id)
915 1
        self.notify_topology_update()
916
917 1
    @listen_to('.*.interface.is.nni')
918 1
    def on_add_links(self, event):
919
        """Update the topology with links related to the NNI interfaces."""
920 1
        self.add_links(event)
921 1
922
    def add_links(self, event):
923
        """Update the topology with links related to the NNI interfaces."""
924
        interface_a = event.content['interface_a']
925
        interface_b = event.content['interface_b']
926
927 1
        try:
928
            with self._links_lock:
929
                link, created = self._get_link_or_create(interface_a,
930 1
                                                         interface_b)
931 1
                interface_a.update_link(link)
932
                interface_b.update_link(link)
933
934 1
                link.endpoint_a = interface_a
935
                link.endpoint_b = interface_b
936
937
                interface_a.nni = True
938 1
                interface_b.nni = True
939 1
940 1
        except KytosLinkCreationError as err:
941
            log.error(f'Error creating link: {err}.')
942 1
            return
943 1
944
        if not created:
945
            return
946
947 1
        self.notify_topology_update()
948 1
        if not link.is_active():
949
            return
950
951
        metadata = {
952 1
            'last_status_change': time.time(),
953
            'last_status_is_active': True
954 1
        }
955
        link.extend_metadata(metadata)
956 1
        self.topo_controller.upsert_link(link.id, link.as_dict())
957
        self.notify_link_up_if_status(link, "link up")
958 1
959 1
    @listen_to('.*.of_lldp.network_status.updated')
960 1
    def on_lldp_status_updated(self, event):
961 1
        """Handle of_lldp.network_status.updated from of_lldp."""
962 1
        self.handle_lldp_status_updated(event)
963 1
964 1
    @listen_to(".*.topo_controller.upsert_switch")
965 1
    def on_topo_controller_upsert_switch(self, event) -> None:
966
        """Listen to topo_controller_upsert_switch."""
967 1
        self.handle_topo_controller_upsert_switch(event.content["switch"])
968 1
969 1
    def handle_topo_controller_upsert_switch(self, switch) -> Optional[dict]:
970 1
        """Handle topo_controller_upsert_switch."""
971
        return self.topo_controller.upsert_switch(switch.id, switch.as_dict())
972 1
973
    def handle_lldp_status_updated(self, event) -> None:
974 1
        """Handle .*.network_status.updated events from of_lldp."""
975 1
        content = event.content
976 1
        interface_ids = content["interface_ids"]
977
        switches = set()
978 1
        for interface_id in interface_ids:
979
            dpid = ":".join(interface_id.split(":")[:-1])
980 1
            switch = self.controller.get_switch_by_dpid(dpid)
981 1
            if switch:
982 1
                switches.add(switch)
983 1
984 1
        name = "kytos/topology.topo_controller.upsert_switch"
985 1
        for switch in switches:
986 1
            event = KytosEvent(name=name, content={"switch": switch})
987 1
            self.controller.buffers.app.put(event)
988
989 1
    def notify_switch_enabled(self, dpid):
990
        """Send an event to notify that a switch is enabled."""
991 1
        name = 'kytos/topology.switch.enabled'
992
        event = KytosEvent(name=name, content={'dpid': dpid})
993 1
        self.controller.buffers.app.put(event)
994 1
995 1
    def notify_switch_links_status(self, switch, reason):
996
        """Send an event to notify the status of a link in a switch"""
997 1
        with self._links_lock:
998
            for link in self.links.values():
999 1
                if switch in (link.endpoint_a.switch, link.endpoint_b.switch):
1000 1
                    if reason == "link enabled":
1001
                        name = 'kytos/topology.notify_link_up_if_status'
1002 1
                        content = {'reason': reason, "link": link}
1003
                        event = KytosEvent(name=name, content=content)
1004 1
                        self.controller.buffers.app.put(event)
1005
                    else:
1006
                        self.notify_link_status_change(link, reason)
1007 1
1008 1
    def notify_switch_disabled(self, dpid):
1009 1
        """Send an event to notify that a switch is disabled."""
1010 1
        name = 'kytos/topology.switch.disabled'
1011 1
        event = KytosEvent(name=name, content={'dpid': dpid})
1012 1
        self.controller.buffers.app.put(event)
1013 1
1014
    def notify_topology_update(self):
1015 1
        """Send an event to notify about updates on the topology."""
1016
        name = 'kytos/topology.updated'
1017 1
        event = KytosEvent(name=name, content={'topology':
1018
                                               self._get_topology()})
1019 1
        self.controller.buffers.app.put(event)
1020 1
1021
    def notify_interface_link_status(self, interface, reason):
1022
        """Send an event to notify the status of a link from
1023 1
        an interface."""
1024 1
        link = self._get_link_from_interface(interface)
1025
        if link:
1026
            if reason == "link enabled":
1027
                name = 'kytos/topology.notify_link_up_if_status'
1028
                content = {'reason': reason, "link": link}
1029
                event = KytosEvent(name=name, content=content)
1030 1
                self.controller.buffers.app.put(event)
1031
            else:
1032 1
                self.notify_link_status_change(link, reason)
1033
1034 1
    def notify_link_status_change(self, link, reason='not given'):
1035 1
        """Send an event to notify about a status change on a link."""
1036 1
        name = 'kytos/topology.'
1037 1
        if link.status == EntityStatus.UP:
1038 1
            status = 'link_up'
1039 1
        else:
1040 1
            status = 'link_down'
1041 1
        event = KytosEvent(
1042 1
            name=name+status,
1043
            content={
1044 1
                'link': link,
1045
                'reason': reason
1046
            })
1047
        self.controller.buffers.app.put(event)
1048 1
1049 1
    def notify_metadata_changes(self, obj, action):
1050 1
        """Send an event to notify about metadata changes."""
1051 1
        if isinstance(obj, Switch):
1052 1
            entity = 'switch'
1053
            entities = 'switches'
1054 1
        elif isinstance(obj, Interface):
1055 1
            entity = 'interface'
1056
            entities = 'interfaces'
1057
        elif isinstance(obj, Link):
1058
            entity = 'link'
1059
            entities = 'links'
1060
        else:
1061 1
            raise ValueError(
1062 1
                'Invalid object, supported: Switch, Interface, Link'
1063
            )
1064
1065
        name = f'kytos/topology.{entities}.metadata.{action}'
1066 1
        content = {entity: obj, 'metadata': obj.metadata.copy()}
1067
        event = KytosEvent(name=name, content=content)
1068 1
        self.controller.buffers.app.put(event)
1069 1
        log.debug(f'Metadata from {obj.id} was {action}.')
1070 1
1071
    @listen_to('kytos/topology.notify_link_up_if_status')
1072 1
    def on_notify_link_up_if_status(self, event):
1073 1
        """Tries to notify link up and topology changes"""
1074
        link = event.content["link"]
1075
        reason = event.content["reason"]
1076 1
        self.notify_link_up_if_status(link, reason)
1077
1078 1
    @listen_to('.*.switch.port.created')
1079 1
    def on_notify_port_created(self, event):
1080 1
        """Notify when a port is created."""
1081
        self.notify_port_created(event)
1082 1
1083
    def notify_port_created(self, event):
1084
        """Notify when a port is created."""
1085 1
        name = 'kytos/topology.port.created'
1086 1
        event = KytosEvent(name=name, content=event.content)
1087 1
        self.controller.buffers.app.put(event)
1088
1089 1
    @staticmethod
1090 1
    def load_interfaces_available_tags(switch: Switch,
1091
                                       interfaces_details: List[dict]) -> None:
1092
        """Load interfaces available tags (vlans)."""
1093
        if not interfaces_details:
1094
            return
1095 1
        for interface_details in interfaces_details:
1096
            available_vlans = interface_details["available_vlans"]
1097 1
            if not available_vlans:
1098 1
                continue
1099 1
            log.debug(f"Interface id {interface_details['id']} loading "
1100 1
                      f"{len(interface_details['available_vlans'])} "
1101 1
                      "available tags")
1102 1
            port_number = int(interface_details["id"].split(":")[-1])
1103 1
            interface = switch.interfaces[port_number]
1104 1
            interface.set_available_tags(interface_details['available_vlans'])
1105 1
1106 1
    @listen_to('kytos/maintenance.start_link')
1107 1
    def on_link_maintenance_start(self, event):
1108 1
        """Deals with the start of links maintenance."""
1109 1
        with self._links_lock:
1110 1
            self.handle_link_maintenance_start(event)
1111 1
1112 1
    def handle_link_maintenance_start(self, event):
1113
        """Deals with the start of links maintenance."""
1114 1
        notify_links = []
1115 1
        maintenance_links = event.content['links']
1116
        for maintenance_link_id in maintenance_links:
1117
            try:
1118
                link = self.links[maintenance_link_id]
1119
            except KeyError:
1120 1
                continue
1121
            notify_links.append(link)
1122 1
        for link in notify_links:
1123 1
            link.disable()
1124 1
            link.deactivate()
1125 1
            link.endpoint_a.deactivate()
1126 1
            link.endpoint_b.deactivate()
1127 1
            link.endpoint_a.disable()
1128 1
            link.endpoint_b.disable()
1129 1
            self.notify_link_status_change(link, reason='maintenance')
1130 1
1131 1
    @listen_to('kytos/maintenance.end_link')
1132 1
    def on_link_maintenance_end(self, event):
1133 1
        """Deals with the end of links maintenance."""
1134 1
        with self._links_lock:
1135 1
            self.handle_link_maintenance_end(event)
1136 1
1137 1
    def handle_link_maintenance_end(self, event):
1138
        """Deals with the end of links maintenance."""
1139
        notify_links = []
1140
        maintenance_links = event.content['links']
1141
        for maintenance_link_id in maintenance_links:
1142
            try:
1143
                link = self.links[maintenance_link_id]
1144
            except KeyError:
1145
                continue
1146
            notify_links.append(link)
1147
        for link in notify_links:
1148
            link.enable()
1149
            link.activate()
1150
            link.endpoint_a.activate()
1151
            link.endpoint_b.activate()
1152
            link.endpoint_a.enable()
1153
            link.endpoint_b.enable()
1154
            self.notify_link_status_change(link, reason='maintenance')
1155