Passed
Pull Request — master (#84)
by Vinicius
02:44
created

build.main   F

Complexity

Total Complexity 173

Size/Duplication

Total Lines 992
Duplicated Lines 5.44 %

Test Coverage

Coverage 93.21%

Importance

Changes 0
Metric Value
eloc 717
dl 54
loc 992
ccs 577
cts 619
cp 0.9321
rs 1.883
c 0
b 0
f 0
wmc 173

72 Methods

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