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