Passed
Pull Request — master (#153)
by Carlos
03:27
created

build.main   F

Complexity

Total Complexity 158

Size/Duplication

Total Lines 875
Duplicated Lines 6.74 %

Test Coverage

Coverage 89.56%

Importance

Changes 0
Metric Value
eloc 617
dl 59
loc 875
ccs 506
cts 565
cp 0.8956
rs 1.983
c 0
b 0
f 0
wmc 158

60 Methods

Rating   Name   Duplication   Size   Complexity  
A Main._get_switches_dict() 0 12 3
A Main.shutdown() 0 3 1
A Main.setup() 0 18 1
A Main.execute() 0 3 1
A Main._restore_link() 0 22 4
A Main._get_links_dict() 0 4 1
A Main._get_link_or_create() 0 9 3
A Main._get_link_from_interface() 0 6 3
A Main._get_topology_dict() 0 4 1
A Main._get_topology() 0 3 1
A Main.disable_switch() 0 13 2
A Main.enable_switch() 0 13 2
A Main.add_switch_metadata() 0 12 2
A Main.get_interfaces() 0 10 3
A Main.get_switches() 0 4 1
C Main._restore_switch() 0 64 11
A Main.get_switch_metadata() 0 8 2
B Main._load_network_status() 0 25 5
A Main.get_topology() 0 7 1
A Main.restore_network_status() 0 11 5
A Main.delete_switch_metadata() 0 11 2
A Main.notify_port_created() 0 6 1
A Main.verify_storehouse() 0 8 1
A Main.load_from_store() 0 8 2
A Main.request_retrieve_entities() 0 18 2
A Main.disable_link() 0 9 2
B Main.enable_interface() 30 30 7
A Main.delete_interface_metadata() 0 21 4
A Main.add_interface_metadata() 0 20 3
B Main.update_instance_metadata() 0 19 8
A Main.handle_link_maintenance_end() 0 19 4
A Main.notify_link_status_change() 0 9 2
A Main.handle_interface_down() 0 9 1
A Main.handle_interface_link_up() 0 8 1
B Main.handle_link_up() 0 27 7
A Main.notify_switch_enabled() 0 5 1
A Main.handle_interface_created() 0 4 1
A Main.get_interface_metadata() 0 16 3
A Main.delete_link_metadata() 0 13 3
A Main.notify_topology_update() 0 6 1
A Main.enable_link() 0 9 2
A Main.handle_link_down() 0 8 3
A Main.handle_new_switch() 0 13 1
A Main.save_status_on_storehouse() 0 13 2
A Main.update_instance() 0 8 2
A Main.handle_switch_maintenance_end() 0 10 3
A Main.handle_connection_lost() 0 12 2
A Main.get_links() 0 7 1
B Main.disable_interface() 29 29 7
A Main.handle_switch_maintenance_start() 0 11 4
A Main.save_metadata_on_store() 0 25 4
A Main.handle_interface_link_down() 0 8 1
A Main.handle_link_maintenance_start() 0 19 4
A Main.notify_switch_disabled() 0 5 1
A Main.add_links() 0 20 2
A Main.handle_interface_up() 0 9 1
A Main.handle_interface_deleted() 0 4 1
A Main.add_link_metadata() 0 12 2
A Main.notify_metadata_changes() 0 17 4
A Main.get_link_metadata() 0 7 2

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 1
import time
6
7 1
from flask import jsonify, request
8
9 1
from kytos.core import KytosEvent, KytosNApp, log, rest
10 1
from kytos.core.exceptions import KytosLinkCreationError
11 1
from kytos.core.helpers import listen_to
12 1
from kytos.core.interface import Interface
13 1
from kytos.core.link import Link
14 1
from kytos.core.switch import Switch
15 1
from napps.kytos.topology import settings
16 1
from napps.kytos.topology.exceptions import RestoreError
17 1
from napps.kytos.topology.models import Topology
18 1
from napps.kytos.topology.storehouse import StoreHouse
19
20 1
DEFAULT_LINK_UP_TIMER = 10
21 1
DEFAULT_INTERFACE_RESTORE_TIMER = 2
22 1
RESTORE_INTERFACE_ATTEMPTS = 20
23
24
25 1
class Main(KytosNApp):  # pylint: disable=too-many-public-methods
26
    """Main class of kytos/topology NApp.
27
28
    This class is the entry point for this napp.
29
    """
30
31 1
    def setup(self):
32
        """Initialize the NApp's links list."""
33 1
        self.links = {}
34 1
        self.store_items = {}
35 1
        self.switches_state = {}
36 1
        self.interfaces_state = {}
37 1
        self.links_state = {}
38 1
        self._verified_links = []
39 1
        self.link_up_timer = getattr(settings, 'LINK_UP_TIMER',
40
                                     DEFAULT_LINK_UP_TIMER)
41 1
        self.interface_restore = getattr(settings, 'INTERFACE_RESTORE_TIMER',
42
                                         DEFAULT_INTERFACE_RESTORE_TIMER)
43
44 1
        self.verify_storehouse('switches')
45 1
        self.verify_storehouse('interfaces')
46 1
        self.verify_storehouse('links')
47
48 1
        self.storehouse = StoreHouse(self.controller)
49
50 1
    def execute(self):
51
        """Execute once when the napp is running."""
52
        self._load_network_status()
53
54 1
    def shutdown(self):
55
        """Do nothing."""
56
        log.info('NApp kytos/topology shutting down.')
57
58 1
    def _get_link_or_create(self, endpoint_a, endpoint_b):
59 1
        new_link = Link(endpoint_a, endpoint_b)
60
61 1
        for link in self.links.values():
62
            if new_link == link:
63
                return link
64
65 1
        self.links[new_link.id] = new_link
66 1
        return new_link
67
68 1
    def _get_switches_dict(self):
69
        """Return a dictionary with the known switches."""
70 1
        switches = {'switches': {}}
71 1
        for idx, switch in enumerate(self.controller.switches.values()):
72 1
            switch_data = switch.as_dict()
73 1
            if not all(key in switch_data['metadata']
74
                       for key in ('lat', 'lng')):
75
                # Switches are initialized somewhere in the ocean
76
                switch_data['metadata']['lat'] = str(0.0)
77
                switch_data['metadata']['lng'] = str(-30.0+idx*10.0)
78 1
            switches['switches'][switch.id] = switch_data
79 1
        return switches
80
81 1
    def _get_links_dict(self):
82
        """Return a dictionary with the known links."""
83 1
        return {'links': {l.id: l.as_dict() for l in
84
                          self.links.values()}}
85
86 1
    def _get_topology_dict(self):
87
        """Return a dictionary with the known topology."""
88 1
        return {'topology': {**self._get_switches_dict(),
89
                             **self._get_links_dict()}}
90
91 1
    def _get_topology(self):
92
        """Return an object representing the topology."""
93 1
        return Topology(self.controller.switches, self.links)
94
95 1
    def _get_link_from_interface(self, interface):
96
        """Return the link of the interface, or None if it does not exist."""
97 1
        for link in self.links.values():
98 1
            if interface in (link.endpoint_a, link.endpoint_b):
99 1
                return link
100 1
        return None
101
102 1
    def _restore_link(self, link_id):
103
        """Restore link's administrative state from storehouse."""
104 1
        try:
105 1
            state = self.links_state[link_id]
106 1
        except KeyError:
107 1
            error = (f'The link {link_id} has no stored '
108
                     'administrative state to be restored.')
109 1
            raise RestoreError(error)
110
111 1
        try:
112 1
            link = self.links[link_id]
113 1
            if state['enabled']:
114 1
                link.enable()
115
            else:
116 1
                link.disable()
117 1
        except KeyError:
118 1
            error = ('Error restoring link status.'
119
                     f'The link {link_id} does not exist.')
120 1
            raise RestoreError(error)
121 1
        log.info(f'The state of link {link.id} has been restored.')
122 1
        self.notify_topology_update()
123 1
        self.update_instance_metadata(link)
124
125 1
    def _restore_switch(self, switch_id):
126
        """Restore switch's administrative state from storehouse."""
127 1
        try:
128 1
            state = self.switches_state[switch_id]
129 1
        except KeyError:
130 1
            error = (f'The switch {switch_id} has no stored'
131
                     ' administrative state to be restored.')
132 1
            raise RestoreError(error)
133
134 1
        try:
135 1
            switch = self.controller.switches[switch_id]
136 1
        except KeyError:
137
            # Maybe we should remove the switch from switches_state here
138 1
            error = ('Error while restoring switches status. The '
139
                     f'switch {switch_id} does not exist.')
140 1
            raise RestoreError(error)
141
142 1
        if state:
143 1
            switch.enable()
144 1
            self.notify_switch_enabled(switch_id)
145
        else:
146 1
            switch.disable()
147 1
            self.notify_switch_disabled(switch_id)
148
149 1
        log.debug('Waiting to restore administrative state of switch '
150
                  f'{switch_id} interfaces.')
151 1
        i = 0
152
        # wait to restore interfaces
153 1
        while not switch.interfaces and i < RESTORE_INTERFACE_ATTEMPTS:
154
            time.sleep(self.interface_restore)
155
            i += 1
156 1
        if not switch.interfaces:
157
            error = ('Error restoring administrative state of switch '
158
                     f'{switch_id} interfaces.')
159
            raise RestoreError(error)
160
161
        # restore interfaces
162 1
        for interface_id in switch.interfaces:
163 1
            iface_id = ":".join([switch_id, str(interface_id)])
164
            # restore only the administrative state of saved interfaces
165 1
            if iface_id not in self.interfaces_state:
166
                error = ("The stored topology is different from the current "
167
                         f"topology. The interface {iface_id} hasn't been "
168
                         "stored.")
169
                log.info(error)
170
                continue
171 1
            state = self.interfaces_state[iface_id]
172 1
            iface_number = int(interface_id)
173 1
            iface_status, lldp_status = state
174 1
            try:
175 1
                interface = switch.interfaces[iface_number]
176
            except KeyError:
177
                log.error('Error restoring interface status: '
178
                          '%s does not exist.', iface_id)
179
                continue
180
181 1
            if iface_status:
182 1
                interface.enable()
183
            else:
184 1
                interface.disable()
185 1
            interface.lldp = lldp_status
186 1
            self.update_instance_metadata(interface)
187
188 1
        log.info(f'The state of switch {switch_id} has been restored.')
189
190
    # pylint: disable=attribute-defined-outside-init
191 1
    def _load_network_status(self):
192
        """Load network status saved in storehouse."""
193 1
        try:
194 1
            status = self.storehouse.get_data()
195
        except FileNotFoundError as error:
196
            log.info(error)
197
            return
198 1
        if status:
199 1
            switches = status['network_status']['switches']
200 1
            self.links_state = status['network_status']['links']
201
202 1
            for switch_id, switch_att in switches.items():
203
                # get switches status
204 1
                self.switches_state[switch_id] = switch_att['enabled']
205 1
                iface = switch_att['interfaces']
206
                # get interface status
207 1
                for iface_id, iface_att in iface.items():
208 1
                    enabled_value = iface_att['enabled']
209 1
                    lldp_value = iface_att['lldp']
210 1
                    self.interfaces_state[iface_id] = (enabled_value,
211
                                                       lldp_value)
212
213
        else:
214
            error = 'There is no status saved to restore.'
215
            log.info(error)
216
217 1
    @rest('v3/')
218
    def get_topology(self):
219
        """Return the latest known topology.
220
221
        This topology is updated when there are network events.
222
        """
223 1
        return jsonify(self._get_topology_dict())
224
225 1
    def restore_network_status(self, obj):
226
        """Restore the network administrative status saved in storehouse."""
227 1
        try:
228 1
            if isinstance(obj, Switch):
229 1
                self._restore_switch(obj.id)
230 1
            elif isinstance(obj, Link):
231 1
                if obj.id not in self._verified_links:
232 1
                    self._verified_links.append(obj.id)
233 1
                    self._restore_link(obj.id)
234
        except RestoreError as exc:
235
            log.debug(exc)
236
237
    # Switch related methods
238 1
    @rest('v3/switches')
239
    def get_switches(self):
240
        """Return a json with all the switches in the topology."""
241
        return jsonify(self._get_switches_dict())
242
243 1
    @rest('v3/switches/<dpid>/enable', methods=['POST'])
244
    def enable_switch(self, dpid):
245
        """Administratively enable a switch in the topology."""
246 1
        try:
247 1
            self.controller.switches[dpid].enable()
248 1
        except KeyError:
249 1
            return jsonify("Switch not found"), 404
250
251 1
        log.info(f"Storing administrative state from switch {dpid}"
252
                 " to enabled.")
253 1
        self.save_status_on_storehouse()
254 1
        self.notify_switch_enabled(dpid)
255 1
        return jsonify("Operation successful"), 201
256
257 1
    @rest('v3/switches/<dpid>/disable', methods=['POST'])
258
    def disable_switch(self, dpid):
259
        """Administratively disable a switch in the topology."""
260 1
        try:
261 1
            self.controller.switches[dpid].disable()
262 1
        except KeyError:
263 1
            return jsonify("Switch not found"), 404
264
265 1
        log.info(f"Storing administrative state from switch {dpid}"
266
                 " to disabled.")
267 1
        self.save_status_on_storehouse()
268 1
        self.notify_switch_disabled(dpid)
269 1
        return jsonify("Operation successful"), 201
270
271 1
    @rest('v3/switches/<dpid>/metadata')
272
    def get_switch_metadata(self, dpid):
273
        """Get metadata from a switch."""
274 1
        try:
275 1
            return jsonify({"metadata":
276
                            self.controller.switches[dpid].metadata}), 200
277 1
        except KeyError:
278 1
            return jsonify("Switch not found"), 404
279
280 1
    @rest('v3/switches/<dpid>/metadata', methods=['POST'])
281
    def add_switch_metadata(self, dpid):
282
        """Add metadata to a switch."""
283 1
        metadata = request.get_json()
284 1
        try:
285 1
            switch = self.controller.switches[dpid]
286 1
        except KeyError:
287 1
            return jsonify("Switch not found"), 404
288
289 1
        switch.extend_metadata(metadata)
290 1
        self.notify_metadata_changes(switch, 'added')
291 1
        return jsonify("Operation successful"), 201
292
293 1
    @rest('v3/switches/<dpid>/metadata/<key>', methods=['DELETE'])
294
    def delete_switch_metadata(self, dpid, key):
295
        """Delete metadata from a switch."""
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
        switch.remove_metadata(key)
302 1
        self.notify_metadata_changes(switch, 'removed')
303 1
        return jsonify("Operation successful"), 200
304
305
    # Interface related methods
306 1
    @rest('v3/interfaces')
307
    def get_interfaces(self):
308
        """Return a json with all the interfaces in the topology."""
309
        interfaces = {}
310
        switches = self._get_switches_dict()
311
        for switch in switches['switches'].values():
312
            for interface_id, interface in switch['interfaces'].items():
313
                interfaces[interface_id] = interface
314
315
        return jsonify({'interfaces': interfaces})
316
317 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...
318 1
    @rest('v3/interfaces/<interface_enable_id>/enable', methods=['POST'])
319 1
    def enable_interface(self, interface_enable_id=None, dpid=None):
320
        """Administratively enable interfaces in the topology."""
321 1
        error_list = []  # List of interfaces that were not activated.
322 1
        msg_error = "Some interfaces couldn't be found and activated: "
323 1
        if dpid is None:
324 1
            dpid = ":".join(interface_enable_id.split(":")[:-1])
325 1
        try:
326 1
            switch = self.controller.switches[dpid]
327 1
        except KeyError as exc:
328 1
            return jsonify(f"Switch not found: {exc}"), 404
329
330 1
        if interface_enable_id:
331 1
            interface_number = int(interface_enable_id.split(":")[-1])
332
333 1
            try:
334 1
                switch.interfaces[interface_number].enable()
335 1
            except KeyError as exc:
336 1
                error_list.append(f"Switch {dpid} Interface {exc}")
337
        else:
338 1
            for interface in switch.interfaces.values():
339 1
                interface.enable()
340 1
        if not error_list:
341 1
            log.info(f"Storing administrative state for enabled interfaces.")
342 1
            self.save_status_on_storehouse()
343 1
            self.notify_switch_enabled(dpid)  # The switch is also enabled
344 1
            return jsonify("Operation successful"), 200
345 1
        return jsonify({msg_error:
346
                        error_list}), 409
347
348 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...
349 1
    @rest('v3/interfaces/<interface_disable_id>/disable', methods=['POST'])
350 1
    def disable_interface(self, interface_disable_id=None, dpid=None):
351
        """Administratively disable interfaces in the topology."""
352 1
        error_list = []  # List of interfaces that were not deactivated.
353 1
        msg_error = "Some interfaces couldn't be found and deactivated: "
354 1
        if dpid is None:
355 1
            dpid = ":".join(interface_disable_id.split(":")[:-1])
356 1
        try:
357 1
            switch = self.controller.switches[dpid]
358 1
        except KeyError as exc:
359 1
            return jsonify(f"Switch not found: {exc}"), 404
360
361 1
        if interface_disable_id:
362 1
            interface_number = int(interface_disable_id.split(":")[-1])
363
364 1
            try:
365 1
                switch.interfaces[interface_number].disable()
366 1
            except KeyError as exc:
367 1
                error_list.append(f"Switch {dpid} Interface {exc}")
368
        else:
369 1
            for interface in switch.interfaces.values():
370 1
                interface.disable()
371 1
        if not error_list:
372 1
            log.info(f"Storing administrative state for disabled interfaces.")
373 1
            self.save_status_on_storehouse()
374 1
            return jsonify("Operation successful"), 200
375 1
        return jsonify({msg_error:
376
                        error_list}), 409
377
378 1
    @rest('v3/interfaces/<interface_id>/metadata')
379
    def get_interface_metadata(self, interface_id):
380
        """Get metadata from an interface."""
381 1
        switch_id = ":".join(interface_id.split(":")[:-1])
382 1
        interface_number = int(interface_id.split(":")[-1])
383 1
        try:
384 1
            switch = self.controller.switches[switch_id]
385 1
        except KeyError:
386 1
            return jsonify("Switch not found"), 404
387
388 1
        try:
389 1
            interface = switch.interfaces[interface_number]
390 1
        except KeyError:
391 1
            return jsonify("Interface not found"), 404
392
393 1
        return jsonify({"metadata": interface.metadata}), 200
394
395 1
    @rest('v3/interfaces/<interface_id>/metadata', methods=['POST'])
396
    def add_interface_metadata(self, interface_id):
397
        """Add metadata to an interface."""
398 1
        metadata = request.get_json()
399
400 1
        switch_id = ":".join(interface_id.split(":")[:-1])
401 1
        interface_number = int(interface_id.split(":")[-1])
402 1
        try:
403 1
            switch = self.controller.switches[switch_id]
404 1
        except KeyError:
405 1
            return jsonify("Switch not found"), 404
406
407 1
        try:
408 1
            interface = switch.interfaces[interface_number]
409 1
        except KeyError:
410 1
            return jsonify("Interface not found"), 404
411
412 1
        interface.extend_metadata(metadata)
413 1
        self.notify_metadata_changes(interface, 'added')
414 1
        return jsonify("Operation successful"), 201
415
416 1
    @rest('v3/interfaces/<interface_id>/metadata/<key>', methods=['DELETE'])
417
    def delete_interface_metadata(self, interface_id, key):
418
        """Delete metadata from an interface."""
419 1
        switch_id = ":".join(interface_id.split(":")[:-1])
420 1
        interface_number = int(interface_id.split(":")[-1])
421
422 1
        try:
423 1
            switch = self.controller.switches[switch_id]
424 1
        except KeyError:
425 1
            return jsonify("Switch not found"), 404
426
427 1
        try:
428 1
            interface = switch.interfaces[interface_number]
429 1
        except KeyError:
430 1
            return jsonify("Interface not found"), 404
431
432 1
        if interface.remove_metadata(key) is False:
433 1
            return jsonify("Metadata not found"), 404
434
435 1
        self.notify_metadata_changes(interface, 'removed')
436 1
        return jsonify("Operation successful"), 200
437
438
    # Link related methods
439 1
    @rest('v3/links')
440
    def get_links(self):
441
        """Return a json with all the links in the topology.
442
443
        Links are connections between interfaces.
444
        """
445
        return jsonify(self._get_links_dict()), 200
446
447 1
    @rest('v3/links/<link_id>/enable', methods=['POST'])
448
    def enable_link(self, link_id):
449
        """Administratively enable a link in the topology."""
450 1
        try:
451 1
            self.links[link_id].enable()
452 1
        except KeyError:
453 1
            return jsonify("Link not found"), 404
454 1
        self.save_status_on_storehouse()
455 1
        return jsonify("Operation successful"), 201
456
457 1
    @rest('v3/links/<link_id>/disable', methods=['POST'])
458
    def disable_link(self, link_id):
459
        """Administratively disable a link in the topology."""
460 1
        try:
461 1
            self.links[link_id].disable()
462 1
        except KeyError:
463 1
            return jsonify("Link not found"), 404
464 1
        self.save_status_on_storehouse()
465 1
        return jsonify("Operation successful"), 201
466
467 1
    @rest('v3/links/<link_id>/metadata')
468
    def get_link_metadata(self, link_id):
469
        """Get metadata from a link."""
470 1
        try:
471 1
            return jsonify({"metadata": self.links[link_id].metadata}), 200
472 1
        except KeyError:
473 1
            return jsonify("Link not found"), 404
474
475 1
    @rest('v3/links/<link_id>/metadata', methods=['POST'])
476
    def add_link_metadata(self, link_id):
477
        """Add metadata to a link."""
478 1
        metadata = request.get_json()
479 1
        try:
480 1
            link = self.links[link_id]
481 1
        except KeyError:
482 1
            return jsonify("Link not found"), 404
483
484 1
        link.extend_metadata(metadata)
485 1
        self.notify_metadata_changes(link, 'added')
486 1
        return jsonify("Operation successful"), 201
487
488 1
    @rest('v3/links/<link_id>/metadata/<key>', methods=['DELETE'])
489
    def delete_link_metadata(self, link_id, key):
490
        """Delete metadata from a link."""
491 1
        try:
492 1
            link = self.links[link_id]
493 1
        except KeyError:
494 1
            return jsonify("Link not found"), 404
495
496 1
        if link.remove_metadata(key) is False:
497 1
            return jsonify("Metadata not found"), 404
498
499 1
        self.notify_metadata_changes(link, 'removed')
500 1
        return jsonify("Operation successful"), 200
501
502 1
    @listen_to('.*.switch.(new|reconnected)')
503
    def handle_new_switch(self, event):
504
        """Create a new Device on the Topology.
505
506
        Handle the event of a new created switch and update the topology with
507
        this new device.
508
        """
509 1
        switch = event.content['switch']
510 1
        switch.activate()
511 1
        log.debug('Switch %s added to the Topology.', switch.id)
512 1
        self.notify_topology_update()
513 1
        self.update_instance_metadata(switch)
514 1
        self.restore_network_status(switch)
515
516 1
    @listen_to('.*.connection.lost')
517
    def handle_connection_lost(self, event):
518
        """Remove a Device from the topology.
519
520
        Remove the disconnected Device and every link that has one of its
521
        interfaces.
522
        """
523 1
        switch = event.content['source'].switch
524 1
        if switch:
525 1
            switch.deactivate()
526 1
            log.debug('Switch %s removed from the Topology.', switch.id)
527 1
            self.notify_topology_update()
528
529 1
    def handle_interface_up(self, event):
530
        """Update the topology based on a Port Modify event.
531
532
        The event notifies that an interface was changed to 'up'.
533
        """
534 1
        interface = event.content['interface']
535 1
        interface.activate()
536 1
        self.notify_topology_update()
537 1
        self.update_instance_metadata(interface)
538
539 1
    @listen_to('.*.switch.interface.created')
540
    def handle_interface_created(self, event):
541
        """Update the topology based on a Port Create event."""
542 1
        self.handle_interface_up(event)
543
544 1
    def handle_interface_down(self, event):
545
        """Update the topology based on a Port Modify event.
546
547
        The event notifies that an interface was changed to 'down'.
548
        """
549 1
        interface = event.content['interface']
550 1
        interface.deactivate()
551 1
        self.handle_interface_link_down(event)
552 1
        self.notify_topology_update()
553
554 1
    @listen_to('.*.switch.interface.deleted')
555
    def handle_interface_deleted(self, event):
556
        """Update the topology based on a Port Delete event."""
557 1
        self.handle_interface_down(event)
558
559 1
    @listen_to('.*.switch.interface.link_up')
560
    def handle_interface_link_up(self, event):
561
        """Update the topology based on a Port Modify event.
562
563
        The event notifies that an interface's link was changed to 'up'.
564
        """
565 1
        interface = event.content['interface']
566 1
        self.handle_link_up(interface)
567
568 1
    @listen_to('kytos/maintenance.end_switch')
569
    def handle_switch_maintenance_end(self, event):
570
        """Handle the end of the maintenance of a switch."""
571 1
        switches = event.content['switches']
572 1
        for switch in switches:
573 1
            switch.enable()
574 1
            switch.activate()
575 1
            for interface in switch.interfaces.values():
576 1
                interface.enable()
577 1
                self.handle_link_up(interface)
578
579 1
    def handle_link_up(self, interface):
580
        """Notify a link is up."""
581 1
        link = self._get_link_from_interface(interface)
582 1
        if not link:
583
            return
584 1
        if link.endpoint_a == interface:
585 1
            other_interface = link.endpoint_b
586
        else:
587
            other_interface = link.endpoint_a
588 1
        interface.activate()
589 1
        if other_interface.is_active() is False:
590
            return
591 1
        if link.is_active() is False:
592 1
            link.update_metadata('last_status_change', time.time())
593 1
            link.activate()
594
595
            # As each run of this method uses a different thread,
596
            # there is no risk this sleep will lock the NApp.
597 1
            time.sleep(self.link_up_timer)
598
599 1
            last_status_change = link.get_metadata('last_status_change')
600 1
            now = time.time()
601 1
            if link.is_active() and \
602
                    now - last_status_change >= self.link_up_timer:
603 1
                self.notify_topology_update()
604 1
                self.update_instance_metadata(link)
605 1
                self.notify_link_status_change(link)
606
607 1
    @listen_to('.*.switch.interface.link_down')
608
    def handle_interface_link_down(self, event):
609
        """Update the topology based on a Port Modify event.
610
611
        The event notifies that an interface's link was changed to 'down'.
612
        """
613 1
        interface = event.content['interface']
614 1
        self.handle_link_down(interface)
615
616 1
    @listen_to('kytos/maintenance.start_switch')
617
    def handle_switch_maintenance_start(self, event):
618
        """Handle the start of the maintenance of a switch."""
619 1
        switches = event.content['switches']
620 1
        for switch in switches:
621 1
            switch.disable()
622 1
            switch.deactivate()
623 1
            for interface in switch.interfaces.values():
624 1
                interface.disable()
625 1
                if interface.is_active():
626 1
                    self.handle_link_down(interface)
627
628 1
    def handle_link_down(self, interface):
629
        """Notify a link is down."""
630 1
        link = self._get_link_from_interface(interface)
631 1
        if link and link.is_active():
632 1
            link.deactivate()
633 1
            link.update_metadata('last_status_change', time.time())
634 1
            self.notify_topology_update()
635 1
            self.notify_link_status_change(link)
636
637 1
    @listen_to('.*.interface.is.nni')
638
    def add_links(self, event):
639
        """Update the topology with links related to the NNI interfaces."""
640 1
        interface_a = event.content['interface_a']
641 1
        interface_b = event.content['interface_b']
642
643 1
        try:
644 1
            link = self._get_link_or_create(interface_a, interface_b)
645
        except KytosLinkCreationError as err:
646
            log.error(f'Error creating link: {err}.')
647
            return
648
649 1
        interface_a.update_link(link)
650 1
        interface_b.update_link(link)
651
652 1
        interface_a.nni = True
653 1
        interface_b.nni = True
654
655 1
        self.notify_topology_update()
656 1
        self.restore_network_status(link)
657
658
    # def add_host(self, event):
659
    #    """Update the topology with a new Host."""
660
661
    #    interface = event.content['port']
662
    #    mac = event.content['reachable_mac']
663
664
    #    host = Host(mac)
665
    #    link = self.topology.get_link(interface.id)
666
    #    if link is not None:
667
    #        return
668
669
    #    self.topology.add_link(interface.id, host.id)
670
    #    self.topology.add_device(host)
671
672
    #    if settings.DISPLAY_FULL_DUPLEX_LINKS:
673
    #        self.topology.add_link(host.id, interface.id)
674
675
    # pylint: disable=unused-argument
676 1
    @listen_to('.*.network_status.updated')
677 1
    def save_status_on_storehouse(self, event=None):
678
        """Save the network administrative status using storehouse."""
679 1
        status = self._get_switches_dict()
680 1
        status['id'] = 'network_status'
681 1
        if event:
682
            content = event.content
683
            log.info(f"Storing the administrative state of the"
684
                     f" {content['attribute']} attribute to"
685
                     f" {content['state']} in the interfaces"
686
                     f" {content['interface_ids']}")
687 1
        status.update(self._get_links_dict())
688 1
        self.storehouse.save_status(status)
689
690 1
    def notify_switch_enabled(self, dpid):
691
        """Send an event to notify that a switch is enabled."""
692 1
        name = 'kytos/topology.switch.enabled'
693 1
        event = KytosEvent(name=name, content={'dpid': dpid})
694 1
        self.controller.buffers.app.put(event)
695
696 1
    def notify_switch_disabled(self, dpid):
697
        """Send an event to notify that a switch is disabled."""
698 1
        name = 'kytos/topology.switch.disabled'
699 1
        event = KytosEvent(name=name, content={'dpid': dpid})
700 1
        self.controller.buffers.app.put(event)
701
702 1
    def notify_topology_update(self):
703
        """Send an event to notify about updates on the topology."""
704 1
        name = 'kytos/topology.updated'
705 1
        event = KytosEvent(name=name, content={'topology':
706
                                               self._get_topology()})
707 1
        self.controller.buffers.app.put(event)
708
709 1
    def notify_link_status_change(self, link):
710
        """Send an event to notify about a status change on a link."""
711 1
        name = 'kytos/topology.'
712 1
        if link.is_active():
713 1
            status = 'link_up'
714
        else:
715
            status = 'link_down'
716 1
        event = KytosEvent(name=name+status, content={'link': link})
717 1
        self.controller.buffers.app.put(event)
718
719 1
    def notify_metadata_changes(self, obj, action):
720
        """Send an event to notify about metadata changes."""
721 1
        if isinstance(obj, Switch):
722 1
            entity = 'switch'
723 1
            entities = 'switches'
724 1
        elif isinstance(obj, Interface):
725 1
            entity = 'interface'
726 1
            entities = 'interfaces'
727
        elif isinstance(obj, Link):
728
            entity = 'link'
729
            entities = 'links'
730
731 1
        name = f'kytos/topology.{entities}.metadata.{action}'
732 1
        event = KytosEvent(name=name, content={entity: obj,
0 ignored issues
show
introduced by
The variable entity does not seem to be defined for all execution paths.
Loading history...
733
                                               'metadata': obj.metadata})
734 1
        self.controller.buffers.app.put(event)
735 1
        log.debug(f'Metadata from {obj.id} was {action}.')
736
737 1
    @listen_to('.*.switch.port.created')
738
    def notify_port_created(self, original_event):
739
        """Notify when a port is created."""
740 1
        name = 'kytos/topology.port.created'
741 1
        event = KytosEvent(name=name, content=original_event.content)
742 1
        self.controller.buffers.app.put(event)
743
744 1
    @listen_to('kytos/topology.*.metadata.*')
745
    def save_metadata_on_store(self, event):
746
        """Send to storehouse the data updated."""
747 1
        name = 'kytos.storehouse.update'
748 1
        if 'switch' in event.content:
749 1
            store = self.store_items.get('switches')
750 1
            obj = event.content.get('switch')
751 1
            namespace = 'kytos.topology.switches.metadata'
752 1
        elif 'interface' in event.content:
753 1
            store = self.store_items.get('interfaces')
754 1
            obj = event.content.get('interface')
755 1
            namespace = 'kytos.topology.interfaces.metadata'
756 1
        elif 'link' in event.content:
757 1
            store = self.store_items.get('links')
758 1
            obj = event.content.get('link')
759 1
            namespace = 'kytos.topology.links.metadata'
760
761 1
        store.data[obj.id] = obj.metadata
0 ignored issues
show
introduced by
The variable store does not seem to be defined for all execution paths.
Loading history...
introduced by
The variable obj does not seem to be defined for all execution paths.
Loading history...
762 1
        content = {'namespace': namespace,
0 ignored issues
show
introduced by
The variable namespace does not seem to be defined for all execution paths.
Loading history...
763
                   'box_id': store.box_id,
764
                   'data': store.data,
765
                   'callback': self.update_instance}
766
767 1
        event = KytosEvent(name=name, content=content)
768 1
        self.controller.buffers.app.put(event)
769
770 1
    @staticmethod
771
    def update_instance(event, _data, error):
772
        """Display in Kytos console if the data was updated."""
773
        entities = event.content.get('namespace', '').split('.')[-2]
774
        if error:
775
            log.error(f'Error trying to update storehouse {entities}.')
776
        else:
777
            log.debug(f'Storehouse update to entities: {entities}.')
778
779 1
    def verify_storehouse(self, entities):
780
        """Request a list of box saved by specific entity."""
781 1
        name = 'kytos.storehouse.list'
782 1
        content = {'namespace': f'kytos.topology.{entities}.metadata',
783
                   'callback': self.request_retrieve_entities}
784 1
        event = KytosEvent(name=name, content=content)
785 1
        self.controller.buffers.app.put(event)
786 1
        log.info(f'verify data in storehouse for {entities}.')
787
788 1
    def request_retrieve_entities(self, event, data, _error):
789
        """Create a box or retrieve an existent box from storehouse."""
790 1
        msg = ''
791 1
        content = {'namespace': event.content.get('namespace'),
792
                   'callback': self.load_from_store,
793
                   'data': {}}
794
795 1
        if not data:
796 1
            name = 'kytos.storehouse.create'
797 1
            msg = 'Create new box in storehouse'
798
        else:
799 1
            name = 'kytos.storehouse.retrieve'
800 1
            content['box_id'] = data[0]
801 1
            msg = 'Retrieve data from storehouse.'
802
803 1
        event = KytosEvent(name=name, content=content)
804 1
        self.controller.buffers.app.put(event)
805 1
        log.debug(msg)
806
807 1
    def load_from_store(self, event, box, error):
808
        """Save the data retrived from storehouse."""
809
        entities = event.content.get('namespace', '').split('.')[-2]
810
        if error:
811
            log.error('Error while get a box from storehouse.')
812
        else:
813
            self.store_items[entities] = box
814
            log.debug('Data updated')
815
816 1
    def update_instance_metadata(self, obj):
817
        """Update object instance with saved metadata."""
818 1
        metadata = None
819 1
        if isinstance(obj, Interface):
820 1
            all_metadata = self.store_items.get('interfaces', None)
821 1
            if all_metadata:
822
                metadata = all_metadata.data.get(obj.id)
823 1
        elif isinstance(obj, Switch):
824
            all_metadata = self.store_items.get('switches', None)
825
            if all_metadata:
826
                metadata = all_metadata.data.get(obj.id)
827 1
        elif isinstance(obj, Link):
828 1
            all_metadata = self.store_items.get('links', None)
829 1
            if all_metadata:
830
                metadata = all_metadata.data.get(obj.id)
831
832 1
        if metadata:
833
            obj.extend_metadata(metadata)
834
            log.debug(f'Metadata to {obj.id} was updated')
835
836 1
    @listen_to('kytos/maintenance.start_link')
837
    def handle_link_maintenance_start(self, event):
838
        """Deals with the start of links maintenance."""
839 1
        notify_links = []
840 1
        maintenance_links = event.content['links']
841 1
        for maintenance_link in maintenance_links:
842 1
            try:
843 1
                link = self.links[maintenance_link.id]
844 1
            except KeyError:
845 1
                continue
846 1
            notify_links.append(link)
847 1
        for link in notify_links:
848 1
            link.disable()
849 1
            link.deactivate()
850 1
            link.endpoint_a.deactivate()
851 1
            link.endpoint_b.deactivate()
852 1
            link.endpoint_a.disable()
853 1
            link.endpoint_b.disable()
854 1
            self.notify_link_status_change(link)
855
856 1
    @listen_to('kytos/maintenance.end_link')
857
    def handle_link_maintenance_end(self, event):
858
        """Deals with the end of links maintenance."""
859 1
        notify_links = []
860 1
        maintenance_links = event.content['links']
861 1
        for maintenance_link in maintenance_links:
862 1
            try:
863 1
                link = self.links[maintenance_link.id]
864 1
            except KeyError:
865 1
                continue
866 1
            notify_links.append(link)
867 1
        for link in notify_links:
868 1
            link.enable()
869 1
            link.activate()
870 1
            link.endpoint_a.activate()
871 1
            link.endpoint_b.activate()
872 1
            link.endpoint_a.enable()
873 1
            link.endpoint_b.enable()
874
            self.notify_link_status_change(link)
875