Passed
Pull Request — master (#98)
by Vinicius
07:25
created

build.main   F

Complexity

Total Complexity 179

Size/Duplication

Total Lines 1005
Duplicated Lines 5.37 %

Test Coverage

Coverage 92.05%

Importance

Changes 0
Metric Value
eloc 730
dl 54
loc 1005
rs 1.87
c 0
b 0
f 0
ccs 625
cts 679
cp 0.9205
wmc 179

76 Methods

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