Total Complexity | 114 |
Total Lines | 631 |
Duplicated Lines | 5.39 % |
Coverage | 47% |
Changes | 0 |
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:
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 | from flask import jsonify, request |
|
6 | |||
7 | 1 | from kytos.core import KytosEvent, KytosNApp, log, rest |
|
8 | 1 | from kytos.core.helpers import listen_to |
|
9 | 1 | from kytos.core.interface import Interface |
|
10 | 1 | from kytos.core.link import Link |
|
11 | 1 | from kytos.core.switch import Switch |
|
12 | # from napps.kytos.topology import settings |
||
13 | 1 | from napps.kytos.topology.models import Topology |
|
14 | |||
15 | |||
16 | 1 | class Main(KytosNApp): # pylint: disable=too-many-public-methods |
|
17 | """Main class of kytos/topology NApp. |
||
18 | |||
19 | This class is the entry point for this napp. |
||
20 | """ |
||
21 | |||
22 | 1 | def setup(self): |
|
23 | """Initialize the NApp's links list.""" |
||
24 | 1 | self.links = {} |
|
25 | 1 | self.store_items = {} |
|
26 | |||
27 | 1 | self.verify_storehouse('switches') |
|
28 | 1 | self.verify_storehouse('interfaces') |
|
29 | 1 | self.verify_storehouse('links') |
|
30 | |||
31 | 1 | def execute(self): |
|
32 | """Do nothing.""" |
||
33 | |||
34 | 1 | def shutdown(self): |
|
35 | """Do nothing.""" |
||
36 | log.info('NApp kytos/topology shutting down.') |
||
37 | |||
38 | 1 | def _get_link_or_create(self, endpoint_a, endpoint_b): |
|
39 | new_link = Link(endpoint_a, endpoint_b) |
||
40 | |||
41 | for link in self.links.values(): |
||
42 | if new_link == link: |
||
43 | return link |
||
44 | |||
45 | self.links[new_link.id] = new_link |
||
46 | return new_link |
||
47 | |||
48 | 1 | def _get_switches_dict(self): |
|
49 | """Return a dictionary with the known switches.""" |
||
50 | return {'switches': {s.id: s.as_dict() for s in |
||
51 | self.controller.switches.values()}} |
||
52 | |||
53 | 1 | def _get_links_dict(self): |
|
54 | """Return a dictionary with the known links.""" |
||
55 | return {'links': {l.id: l.as_dict() for l in |
||
56 | self.links.values()}} |
||
57 | |||
58 | 1 | def _get_topology_dict(self): |
|
59 | """Return a dictionary with the known topology.""" |
||
60 | return {'topology': {**self._get_switches_dict(), |
||
61 | **self._get_links_dict()}} |
||
62 | |||
63 | 1 | def _get_topology(self): |
|
64 | """Return an object representing the topology.""" |
||
65 | 1 | return Topology(self.controller.switches, self.links) |
|
66 | |||
67 | 1 | def _get_link_from_interface(self, interface): |
|
68 | """Return the link of the interface, or None if it does not exist.""" |
||
69 | for link in self.links.values(): |
||
70 | if interface in (link.endpoint_a, link.endpoint_b): |
||
71 | return link |
||
72 | return None |
||
73 | |||
74 | 1 | @rest('v3/') |
|
75 | def get_topology(self): |
||
76 | """Return the latest known topology. |
||
77 | |||
78 | This topology is updated when there are network events. |
||
79 | """ |
||
80 | return jsonify(self._get_topology_dict()) |
||
81 | |||
82 | # Switch related methods |
||
83 | 1 | @rest('v3/switches') |
|
84 | def get_switches(self): |
||
85 | """Return a json with all the switches in the topology.""" |
||
86 | return jsonify(self._get_switches_dict()) |
||
87 | |||
88 | 1 | @rest('v3/switches/<dpid>/enable', methods=['POST']) |
|
89 | def enable_switch(self, dpid): |
||
90 | """Administratively enable a switch in the topology.""" |
||
91 | try: |
||
92 | self.controller.switches[dpid].enable() |
||
93 | return jsonify("Operation successful"), 201 |
||
94 | except KeyError: |
||
95 | return jsonify("Switch not found"), 404 |
||
96 | |||
97 | 1 | @rest('v3/switches/<dpid>/disable', methods=['POST']) |
|
98 | def disable_switch(self, dpid): |
||
99 | """Administratively disable a switch in the topology.""" |
||
100 | try: |
||
101 | self.controller.switches[dpid].disable() |
||
102 | return jsonify("Operation successful"), 201 |
||
103 | except KeyError: |
||
104 | return jsonify("Switch not found"), 404 |
||
105 | |||
106 | 1 | @rest('v3/switches/<dpid>/metadata') |
|
107 | def get_switch_metadata(self, dpid): |
||
108 | """Get metadata from a switch.""" |
||
109 | try: |
||
110 | return jsonify({"metadata": |
||
111 | self.controller.switches[dpid].metadata}), 200 |
||
112 | except KeyError: |
||
113 | return jsonify("Switch not found"), 404 |
||
114 | |||
115 | 1 | @rest('v3/switches/<dpid>/metadata', methods=['POST']) |
|
116 | def add_switch_metadata(self, dpid): |
||
117 | """Add metadata to a switch.""" |
||
118 | metadata = request.get_json() |
||
119 | try: |
||
120 | switch = self.controller.switches[dpid] |
||
121 | except KeyError: |
||
122 | return jsonify("Switch not found"), 404 |
||
123 | |||
124 | switch.extend_metadata(metadata) |
||
125 | self.notify_metadata_changes(switch, 'added') |
||
126 | return jsonify("Operation successful"), 201 |
||
127 | |||
128 | 1 | @rest('v3/switches/<dpid>/metadata/<key>', methods=['DELETE']) |
|
129 | def delete_switch_metadata(self, dpid, key): |
||
130 | """Delete metadata from a switch.""" |
||
131 | try: |
||
132 | switch = self.controller.switches[dpid] |
||
133 | except KeyError: |
||
134 | return jsonify("Switch not found"), 404 |
||
135 | |||
136 | switch.remove_metadata(key) |
||
137 | self.notify_metadata_changes(switch, 'removed') |
||
138 | return jsonify("Operation successful"), 200 |
||
139 | |||
140 | # Interface related methods |
||
141 | 1 | @rest('v3/interfaces') |
|
142 | def get_interfaces(self): |
||
143 | """Return a json with all the interfaces in the topology.""" |
||
144 | interfaces = {} |
||
145 | switches = self._get_switches_dict() |
||
146 | for switch in switches['switches'].values(): |
||
147 | for interface_id, interface in switch['interfaces'].items(): |
||
148 | interfaces[interface_id] = interface |
||
149 | |||
150 | return jsonify({'interfaces': interfaces}) |
||
151 | |||
152 | 1 | View Code Duplication | @rest('v3/interfaces/<interface_id>/enable', methods=['POST']) |
|
|||
153 | def enable_interface(self, interface_id): |
||
154 | """Administratively enable an interface in the topology.""" |
||
155 | switch_id = ":".join(interface_id.split(":")[:-1]) |
||
156 | interface_number = int(interface_id.split(":")[-1]) |
||
157 | |||
158 | try: |
||
159 | switch = self.controller.switches[switch_id] |
||
160 | except KeyError: |
||
161 | return jsonify("Switch not found"), 404 |
||
162 | |||
163 | try: |
||
164 | switch.interfaces[interface_number].enable() |
||
165 | except KeyError: |
||
166 | return jsonify("Interface not found"), 404 |
||
167 | |||
168 | return jsonify("Operation successful"), 201 |
||
169 | |||
170 | 1 | View Code Duplication | @rest('v3/interfaces/<interface_id>/disable', methods=['POST']) |
171 | def disable_interface(self, interface_id): |
||
172 | """Administratively disable an interface in the topology.""" |
||
173 | switch_id = ":".join(interface_id.split(":")[:-1]) |
||
174 | interface_number = int(interface_id.split(":")[-1]) |
||
175 | |||
176 | try: |
||
177 | switch = self.controller.switches[switch_id] |
||
178 | except KeyError: |
||
179 | return jsonify("Switch not found"), 404 |
||
180 | |||
181 | try: |
||
182 | switch.interfaces[interface_number].disable() |
||
183 | except KeyError: |
||
184 | return jsonify("Interface not found"), 404 |
||
185 | |||
186 | return jsonify("Operation successful"), 201 |
||
187 | |||
188 | 1 | @rest('v3/interfaces/<interface_id>/metadata') |
|
189 | def get_interface_metadata(self, interface_id): |
||
190 | """Get metadata from an interface.""" |
||
191 | switch_id = ":".join(interface_id.split(":")[:-1]) |
||
192 | interface_number = int(interface_id.split(":")[-1]) |
||
193 | try: |
||
194 | switch = self.controller.switches[switch_id] |
||
195 | except KeyError: |
||
196 | return jsonify("Switch not found"), 404 |
||
197 | |||
198 | try: |
||
199 | interface = switch.interfaces[interface_number] |
||
200 | except KeyError: |
||
201 | return jsonify("Interface not found"), 404 |
||
202 | |||
203 | return jsonify({"metadata": interface.metadata}), 200 |
||
204 | |||
205 | 1 | @rest('v3/interfaces/<interface_id>/metadata', methods=['POST']) |
|
206 | def add_interface_metadata(self, interface_id): |
||
207 | """Add metadata to an interface.""" |
||
208 | metadata = request.get_json() |
||
209 | |||
210 | switch_id = ":".join(interface_id.split(":")[:-1]) |
||
211 | interface_number = int(interface_id.split(":")[-1]) |
||
212 | try: |
||
213 | switch = self.controller.switches[switch_id] |
||
214 | except KeyError: |
||
215 | return jsonify("Switch not found"), 404 |
||
216 | |||
217 | try: |
||
218 | interface = switch.interfaces[interface_number] |
||
219 | except KeyError: |
||
220 | return jsonify("Interface not found"), 404 |
||
221 | |||
222 | interface.extend_metadata(metadata) |
||
223 | self.notify_metadata_changes(interface, 'added') |
||
224 | return jsonify("Operation successful"), 201 |
||
225 | |||
226 | 1 | @rest('v3/interfaces/<interface_id>/metadata/<key>', methods=['DELETE']) |
|
227 | def delete_interface_metadata(self, interface_id, key): |
||
228 | """Delete metadata from an interface.""" |
||
229 | switch_id = ":".join(interface_id.split(":")[:-1]) |
||
230 | interface_number = int(interface_id.split(":")[-1]) |
||
231 | |||
232 | try: |
||
233 | switch = self.controller.switches[switch_id] |
||
234 | except KeyError: |
||
235 | return jsonify("Switch not found"), 404 |
||
236 | |||
237 | try: |
||
238 | interface = switch.interfaces[interface_number] |
||
239 | except KeyError: |
||
240 | return jsonify("Interface not found"), 404 |
||
241 | |||
242 | if interface.remove_metadata(key) is False: |
||
243 | return jsonify("Metadata not found"), 404 |
||
244 | |||
245 | self.notify_metadata_changes(interface, 'removed') |
||
246 | return jsonify("Operation successful"), 200 |
||
247 | |||
248 | # Link related methods |
||
249 | 1 | @rest('v3/links') |
|
250 | def get_links(self): |
||
251 | """Return a json with all the links in the topology. |
||
252 | |||
253 | Links are connections between interfaces. |
||
254 | """ |
||
255 | return jsonify(self._get_links_dict()), 200 |
||
256 | |||
257 | 1 | @rest('v3/links/<link_id>/enable', methods=['POST']) |
|
258 | def enable_link(self, link_id): |
||
259 | """Administratively enable a link in the topology.""" |
||
260 | try: |
||
261 | self.links[link_id].enable() |
||
262 | except KeyError: |
||
263 | return jsonify("Link not found"), 404 |
||
264 | |||
265 | return jsonify("Operation successful"), 201 |
||
266 | |||
267 | 1 | @rest('v3/links/<link_id>/disable', methods=['POST']) |
|
268 | def disable_link(self, link_id): |
||
269 | """Administratively disable a link in the topology.""" |
||
270 | try: |
||
271 | self.links[link_id].disable() |
||
272 | except KeyError: |
||
273 | return jsonify("Link not found"), 404 |
||
274 | |||
275 | return jsonify("Operation successful"), 201 |
||
276 | |||
277 | 1 | @rest('v3/links/<link_id>/metadata') |
|
278 | def get_link_metadata(self, link_id): |
||
279 | """Get metadata from a link.""" |
||
280 | try: |
||
281 | return jsonify({"metadata": self.links[link_id].metadata}), 200 |
||
282 | except KeyError: |
||
283 | return jsonify("Link not found"), 404 |
||
284 | |||
285 | 1 | @rest('v3/links/<link_id>/metadata', methods=['POST']) |
|
286 | def add_link_metadata(self, link_id): |
||
287 | """Add metadata to a link.""" |
||
288 | metadata = request.get_json() |
||
289 | try: |
||
290 | link = self.links[link_id] |
||
291 | except KeyError: |
||
292 | return jsonify("Link not found"), 404 |
||
293 | |||
294 | link.extend_metadata(metadata) |
||
295 | self.notify_metadata_changes(link, 'added') |
||
296 | return jsonify("Operation successful"), 201 |
||
297 | |||
298 | 1 | @rest('v3/links/<link_id>/metadata/<key>', methods=['DELETE']) |
|
299 | def delete_link_metadata(self, link_id, key): |
||
300 | """Delete metadata from a link.""" |
||
301 | try: |
||
302 | link = self.links[link_id] |
||
303 | except KeyError: |
||
304 | return jsonify("Link not found"), 404 |
||
305 | |||
306 | if link.remove_metadata(key) is False: |
||
307 | return jsonify("Metadata not found"), 404 |
||
308 | |||
309 | self.notify_metadata_changes(link, 'removed') |
||
310 | return jsonify("Operation successful"), 200 |
||
311 | |||
312 | 1 | @listen_to('.*.switch.(new|reconnected)') |
|
313 | def handle_new_switch(self, event): |
||
314 | """Create a new Device on the Topology. |
||
315 | |||
316 | Handle the event of a new created switch and update the topology with |
||
317 | this new device. |
||
318 | """ |
||
319 | 1 | switch = event.content['switch'] |
|
320 | 1 | switch.activate() |
|
321 | 1 | log.debug('Switch %s added to the Topology.', switch.id) |
|
322 | 1 | self.notify_topology_update() |
|
323 | 1 | self.update_instance_metadata(switch) |
|
324 | |||
325 | 1 | @listen_to('.*.connection.lost') |
|
326 | def handle_connection_lost(self, event): |
||
327 | """Remove a Device from the topology. |
||
328 | |||
329 | Remove the disconnected Device and every link that has one of its |
||
330 | interfaces. |
||
331 | """ |
||
332 | 1 | switch = event.content['source'].switch |
|
333 | 1 | if switch: |
|
334 | 1 | switch.deactivate() |
|
335 | 1 | log.debug('Switch %s removed from the Topology.', switch.id) |
|
336 | 1 | self.notify_topology_update() |
|
337 | |||
338 | 1 | def handle_interface_up(self, event): |
|
339 | """Update the topology based on a Port Modify event. |
||
340 | |||
341 | The event notifies that an interface was changed to 'up'. |
||
342 | """ |
||
343 | 1 | interface = event.content['interface'] |
|
344 | 1 | interface.activate() |
|
345 | 1 | self.notify_topology_update() |
|
346 | 1 | self.update_instance_metadata(interface) |
|
347 | |||
348 | 1 | @listen_to('.*.switch.interface.created') |
|
349 | def handle_interface_created(self, event): |
||
350 | """Update the topology based on a Port Create event.""" |
||
351 | 1 | self.handle_interface_up(event) |
|
352 | |||
353 | 1 | def handle_interface_down(self, event): |
|
354 | """Update the topology based on a Port Modify event. |
||
355 | |||
356 | The event notifies that an interface was changed to 'down'. |
||
357 | """ |
||
358 | 1 | interface = event.content['interface'] |
|
359 | 1 | interface.deactivate() |
|
360 | 1 | self.handle_interface_link_down(event) |
|
361 | 1 | self.notify_topology_update() |
|
362 | |||
363 | 1 | @listen_to('.*.switch.interface.deleted') |
|
364 | def handle_interface_deleted(self, event): |
||
365 | """Update the topology based on a Port Delete event.""" |
||
366 | 1 | self.handle_interface_down(event) |
|
367 | |||
368 | 1 | @listen_to('.*.switch.interface.link_up') |
|
369 | def handle_interface_link_up(self, event): |
||
370 | """Update the topology based on a Port Modify event. |
||
371 | |||
372 | The event notifies that an interface's link was changed to 'up'. |
||
373 | """ |
||
374 | 1 | interface = event.content['interface'] |
|
375 | 1 | self.handle_link_up(interface) |
|
376 | |||
377 | 1 | @listen_to('kytos/maintenance.end_switch') |
|
378 | def handle_switch_maintenance_end(self, event): |
||
379 | """Handle the end of the maintenance of a switch.""" |
||
380 | 1 | switches = event.content['switches'] |
|
381 | 1 | for switch in switches: |
|
382 | 1 | switch.enable() |
|
383 | 1 | switch.activate() |
|
384 | 1 | for interface in switch.interfaces.values(): |
|
385 | 1 | interface.enable() |
|
386 | 1 | self.handle_link_up(interface) |
|
387 | |||
388 | 1 | def handle_link_up(self, interface): |
|
389 | """Notify a link is up.""" |
||
390 | 1 | link = self._get_link_from_interface(interface) |
|
391 | 1 | if link and not link.is_active(): |
|
392 | 1 | link.activate() |
|
393 | 1 | self.notify_topology_update() |
|
394 | 1 | self.update_instance_metadata(interface.link) |
|
395 | 1 | self.notify_link_status_change(link) |
|
396 | |||
397 | 1 | @listen_to('.*.switch.interface.link_down') |
|
398 | def handle_interface_link_down(self, event): |
||
399 | """Update the topology based on a Port Modify event. |
||
400 | |||
401 | The event notifies that an interface's link was changed to 'down'. |
||
402 | """ |
||
403 | 1 | interface = event.content['interface'] |
|
404 | 1 | self.handle_link_down(interface) |
|
405 | |||
406 | 1 | @listen_to('kytos/maintenance.start_switch') |
|
407 | def handle_switch_maintenance_start(self, event): |
||
408 | """Handle the start of the maintenance of a switch.""" |
||
409 | 1 | switches = event.content['switches'] |
|
410 | 1 | for switch in switches: |
|
411 | 1 | switch.disable() |
|
412 | 1 | switch.deactivate() |
|
413 | 1 | for interface in switch.interfaces.values(): |
|
414 | 1 | interface.disable() |
|
415 | 1 | if interface.is_active(): |
|
416 | 1 | self.handle_link_down(interface) |
|
417 | |||
418 | 1 | def handle_link_down(self, interface): |
|
419 | """Notify a link is down.""" |
||
420 | 1 | link = self._get_link_from_interface(interface) |
|
421 | 1 | if link and link.is_active(): |
|
422 | 1 | link.deactivate() |
|
423 | 1 | self.notify_topology_update() |
|
424 | 1 | self.notify_link_status_change(link) |
|
425 | |||
426 | 1 | @listen_to('.*.interface.is.nni') |
|
427 | def add_links(self, event): |
||
428 | """Update the topology with links related to the NNI interfaces.""" |
||
429 | 1 | interface_a = event.content['interface_a'] |
|
430 | 1 | interface_b = event.content['interface_b'] |
|
431 | |||
432 | 1 | link = self._get_link_or_create(interface_a, interface_b) |
|
433 | 1 | interface_a.update_link(link) |
|
434 | 1 | interface_b.update_link(link) |
|
435 | |||
436 | 1 | interface_a.nni = True |
|
437 | 1 | interface_b.nni = True |
|
438 | |||
439 | 1 | self.notify_topology_update() |
|
440 | |||
441 | # def add_host(self, event): |
||
442 | # """Update the topology with a new Host.""" |
||
443 | |||
444 | # interface = event.content['port'] |
||
445 | # mac = event.content['reachable_mac'] |
||
446 | |||
447 | # host = Host(mac) |
||
448 | # link = self.topology.get_link(interface.id) |
||
449 | # if link is not None: |
||
450 | # return |
||
451 | |||
452 | # self.topology.add_link(interface.id, host.id) |
||
453 | # self.topology.add_device(host) |
||
454 | |||
455 | # if settings.DISPLAY_FULL_DUPLEX_LINKS: |
||
456 | # self.topology.add_link(host.id, interface.id) |
||
457 | |||
458 | 1 | def notify_topology_update(self): |
|
459 | """Send an event to notify about updates on the topology.""" |
||
460 | 1 | name = 'kytos/topology.updated' |
|
461 | 1 | event = KytosEvent(name=name, content={'topology': |
|
462 | self._get_topology()}) |
||
463 | 1 | self.controller.buffers.app.put(event) |
|
464 | |||
465 | 1 | def notify_link_status_change(self, link): |
|
466 | """Send an event to notify about a status change on a link.""" |
||
467 | 1 | name = 'kytos/topology.' |
|
468 | 1 | if link.is_active(): |
|
469 | 1 | status = 'link_up' |
|
470 | else: |
||
471 | status = 'link_down' |
||
472 | 1 | event = KytosEvent(name=name+status, content={'link': link}) |
|
473 | 1 | self.controller.buffers.app.put(event) |
|
474 | |||
475 | 1 | def notify_metadata_changes(self, obj, action): |
|
476 | """Send an event to notify about metadata changes.""" |
||
477 | 1 | if isinstance(obj, Switch): |
|
478 | 1 | entity = 'switch' |
|
479 | 1 | entities = 'switches' |
|
480 | elif isinstance(obj, Interface): |
||
481 | entity = 'interface' |
||
482 | entities = 'interfaces' |
||
483 | elif isinstance(obj, Link): |
||
484 | entity = 'link' |
||
485 | entities = 'links' |
||
486 | |||
487 | 1 | name = f'kytos/topology.{entities}.metadata.{action}' |
|
488 | 1 | event = KytosEvent(name=name, content={entity: obj, |
|
489 | 'metadata': obj.metadata}) |
||
490 | 1 | self.controller.buffers.app.put(event) |
|
491 | 1 | log.debug(f'Metadata from {obj.id} was {action}.') |
|
492 | |||
493 | 1 | @listen_to('.*.switch.port.created') |
|
494 | def notify_port_created(self, original_event): |
||
495 | """Notify when a port is created.""" |
||
496 | 1 | name = f'kytos/topology.port.created' |
|
497 | 1 | event = KytosEvent(name=name, content=original_event.content) |
|
498 | 1 | self.controller.buffers.app.put(event) |
|
499 | |||
500 | 1 | @listen_to('kytos/topology.*.metadata.*') |
|
501 | def save_metadata_on_store(self, event): |
||
502 | """Send to storehouse the data updated.""" |
||
503 | name = 'kytos.storehouse.update' |
||
504 | if 'switch' in event.content: |
||
505 | store = self.store_items.get('switches') |
||
506 | obj = event.content.get('switch') |
||
507 | namespace = 'kytos.topology.switches.metadata' |
||
508 | elif 'interface' in event.content: |
||
509 | store = self.store_items.get('interfaces') |
||
510 | obj = event.content.get('interface') |
||
511 | namespace = 'kytos.topology.iterfaces.metadata' |
||
512 | elif 'link' in event.content: |
||
513 | store = self.store_items.get('links') |
||
514 | obj = event.content.get('link') |
||
515 | namespace = 'kytos.topology.links.metadata' |
||
516 | |||
517 | store.data[obj.id] = obj.metadata |
||
518 | content = {'namespace': namespace, |
||
519 | 'box_id': store.box_id, |
||
520 | 'data': store.data, |
||
521 | 'callback': self.update_instance} |
||
522 | |||
523 | event = KytosEvent(name=name, content=content) |
||
524 | self.controller.buffers.app.put(event) |
||
525 | |||
526 | 1 | @staticmethod |
|
527 | def update_instance(event, _data, error): |
||
528 | """Display in Kytos console if the data was updated.""" |
||
529 | entities = event.content.get('namespace', '').split('.')[-2] |
||
530 | if error: |
||
531 | log.error(f'Error trying to update storehouse {entities}.') |
||
532 | else: |
||
533 | log.debug(f'Storehouse update to entities: {entities}.') |
||
534 | |||
535 | 1 | def verify_storehouse(self, entities): |
|
536 | """Request a list of box saved by specific entity.""" |
||
537 | 1 | name = 'kytos.storehouse.list' |
|
538 | 1 | content = {'namespace': f'kytos.topology.{entities}.metadata', |
|
539 | 'callback': self.request_retrieve_entities} |
||
540 | 1 | event = KytosEvent(name=name, content=content) |
|
541 | 1 | self.controller.buffers.app.put(event) |
|
542 | 1 | log.info(f'verify data in storehouse for {entities}.') |
|
543 | |||
544 | 1 | def request_retrieve_entities(self, event, data, _error): |
|
545 | """Create a box or retrieve an existent box from storehouse.""" |
||
546 | msg = '' |
||
547 | content = {'namespace': event.content.get('namespace'), |
||
548 | 'callback': self.load_from_store, |
||
549 | 'data': {}} |
||
550 | |||
551 | if not data: |
||
552 | name = 'kytos.storehouse.create' |
||
553 | msg = 'Create new box in storehouse' |
||
554 | else: |
||
555 | name = 'kytos.storehouse.retrieve' |
||
556 | content['box_id'] = data[0] |
||
557 | msg = 'Retrieve data from storeohouse.' |
||
558 | |||
559 | event = KytosEvent(name=name, content=content) |
||
560 | self.controller.buffers.app.put(event) |
||
561 | log.debug(msg) |
||
562 | |||
563 | 1 | def load_from_store(self, event, box, error): |
|
564 | """Save the data retrived from storehouse.""" |
||
565 | entities = event.content.get('namespace', '').split('.')[-2] |
||
566 | if error: |
||
567 | log.error('Error while get a box from storehouse.') |
||
568 | else: |
||
569 | self.store_items[entities] = box |
||
570 | log.debug('Data updated') |
||
571 | |||
572 | 1 | def update_instance_metadata(self, obj): |
|
573 | """Update object instance with saved metadata.""" |
||
574 | metadata = None |
||
575 | if isinstance(obj, Interface): |
||
576 | all_metadata = self.store_items.get('interfaces', None) |
||
577 | if all_metadata: |
||
578 | metadata = all_metadata.data.get(obj.id) |
||
579 | elif isinstance(obj, Switch): |
||
580 | all_metadata = self.store_items.get('switches', None) |
||
581 | if all_metadata: |
||
582 | metadata = all_metadata.data.get(obj.id) |
||
583 | elif isinstance(obj, Link): |
||
584 | all_metadata = self.store_items.get('links', None) |
||
585 | if all_metadata: |
||
586 | metadata = all_metadata.data.get(obj.id) |
||
587 | |||
588 | if metadata: |
||
589 | obj.extend_metadata(metadata) |
||
590 | log.debug(f'Metadata to {obj.id} was updated') |
||
591 | |||
592 | 1 | @listen_to('kytos/maintenance.start_link') |
|
593 | def handle_link_maintenance_start(self, event): |
||
594 | """Deals with the start of links maintenance.""" |
||
595 | 1 | notify_links = [] |
|
596 | 1 | maintenance_links = event.content['links'] |
|
597 | 1 | for maintenance_link in maintenance_links: |
|
598 | 1 | try: |
|
599 | 1 | link = self.links[maintenance_link.id] |
|
600 | 1 | except KeyError: |
|
601 | 1 | continue |
|
602 | 1 | notify_links.append(link) |
|
603 | 1 | for link in notify_links: |
|
604 | 1 | link.disable() |
|
605 | 1 | link.deactivate() |
|
606 | 1 | link.endpoint_a.deactivate() |
|
607 | 1 | link.endpoint_b.deactivate() |
|
608 | 1 | link.endpoint_a.disable() |
|
609 | 1 | link.endpoint_b.disable() |
|
610 | 1 | self.notify_link_status_change(link) |
|
611 | |||
612 | 1 | @listen_to('kytos/maintenance.end_link') |
|
613 | def handle_link_maintenance_end(self, event): |
||
614 | """Deals with the end of links maintenance.""" |
||
615 | 1 | notify_links = [] |
|
616 | 1 | maintenance_links = event.content['links'] |
|
617 | 1 | for maintenance_link in maintenance_links: |
|
618 | 1 | try: |
|
619 | 1 | link = self.links[maintenance_link.id] |
|
620 | 1 | except KeyError: |
|
621 | 1 | continue |
|
622 | 1 | notify_links.append(link) |
|
623 | 1 | for link in notify_links: |
|
624 | 1 | link.enable() |
|
625 | 1 | link.activate() |
|
626 | 1 | link.endpoint_a.activate() |
|
627 | 1 | link.endpoint_b.activate() |
|
628 | 1 | link.endpoint_a.enable() |
|
629 | 1 | link.endpoint_b.enable() |
|
630 | self.notify_link_status_change(link) |
||
631 |