kytos /
pathfinder
| 1 | """Main module of kytos/pathfinder Kytos Network Application.""" |
||
| 2 | |||
| 3 | from flask import jsonify, request |
||
| 4 | from kytos.core import KytosNApp, log, rest |
||
| 5 | from kytos.core.helpers import listen_to |
||
| 6 | |||
| 7 | # pylint: disable=import-error |
||
| 8 | from napps.kytos.pathfinder.graph import KytosGraph |
||
| 9 | |||
| 10 | # pylint: enable=import-error |
||
| 11 | |||
| 12 | |||
| 13 | class Main(KytosNApp): |
||
| 14 | """Main class of kytos/pathfinder NApp. |
||
| 15 | |||
| 16 | This class is the entry point for this napp. |
||
| 17 | """ |
||
| 18 | |||
| 19 | def __init__(self): |
||
| 20 | self._topology = None |
||
| 21 | |||
| 22 | def setup(self): |
||
| 23 | """Create a graph to handle the nodes and edges.""" |
||
| 24 | self.graph = KytosGraph() |
||
| 25 | |||
| 26 | def execute(self): |
||
| 27 | """Do nothing.""" |
||
| 28 | |||
| 29 | def shutdown(self): |
||
| 30 | """Shutdown the napp.""" |
||
| 31 | |||
| 32 | def _filter_paths(self, paths, desired, undesired): |
||
| 33 | """Apply filters to the paths list. |
||
| 34 | |||
| 35 | Make sure that each path in the list has all the desired links and none |
||
| 36 | of the undesired ones. |
||
| 37 | """ |
||
| 38 | filtered_paths = [] |
||
| 39 | |||
| 40 | View Code Duplication | if desired: |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 41 | for link_id in desired: |
||
| 42 | try: |
||
| 43 | endpoint_a = self._topology.links[link_id].endpoint_a.id |
||
| 44 | endpoint_b = self._topology.links[link_id].endpoint_b.id |
||
| 45 | except KeyError: |
||
| 46 | return [] |
||
| 47 | |||
| 48 | for path in paths: |
||
| 49 | head = path['hops'][:-1] |
||
| 50 | tail = path['hops'][1:] |
||
| 51 | if (((endpoint_a, endpoint_b) in zip(head, tail)) or |
||
| 52 | ((endpoint_b, endpoint_a) in zip(head, tail))): |
||
| 53 | filtered_paths.append(path) |
||
| 54 | else: |
||
| 55 | filtered_paths = paths |
||
| 56 | |||
| 57 | View Code Duplication | if undesired: |
|
|
0 ignored issues
–
show
|
|||
| 58 | for link_id in undesired: |
||
| 59 | try: |
||
| 60 | endpoint_a = self._topology.links[link_id].endpoint_a.id |
||
| 61 | endpoint_b = self._topology.links[link_id].endpoint_b.id |
||
| 62 | except KeyError: |
||
| 63 | continue |
||
| 64 | |||
| 65 | for path in paths: |
||
| 66 | head = path['hops'][:-1] |
||
| 67 | tail = path['hops'][1:] |
||
| 68 | if (((endpoint_a, endpoint_b) in zip(head, tail)) or |
||
| 69 | ((endpoint_b, endpoint_a) in zip(head, tail))): |
||
| 70 | |||
| 71 | filtered_paths.remove(path) |
||
| 72 | |||
| 73 | return filtered_paths |
||
| 74 | |||
| 75 | @rest('v2/', methods=['POST']) |
||
| 76 | def shortest_path(self): |
||
| 77 | """Calculate the best path between the source and destination.""" |
||
| 78 | data = request.get_json() |
||
| 79 | |||
| 80 | desired = data.get('desired_links') |
||
| 81 | undesired = data.get('undesired_links') |
||
| 82 | parameter = data.get('parameter') |
||
| 83 | |||
| 84 | paths = [] |
||
| 85 | for path in self.graph.shortest_paths(data['source'], |
||
| 86 | data['destination'], |
||
| 87 | parameter): |
||
| 88 | |||
| 89 | paths.append({'hops': path}) |
||
| 90 | |||
| 91 | paths = self._filter_paths(paths, desired, undesired) |
||
| 92 | return jsonify({'paths': paths}) |
||
| 93 | |||
| 94 | @listen_to('kytos.topology.updated') |
||
| 95 | def update_topology(self, event): |
||
| 96 | """Update the graph when the network topology was updated. |
||
| 97 | |||
| 98 | Clear the current graph and create a new with the most topoly updated. |
||
| 99 | """ |
||
| 100 | if 'topology' not in event.content: |
||
| 101 | return |
||
| 102 | topology = event.content['topology'] |
||
| 103 | self._topology = topology |
||
| 104 | self.graph.update_topology(topology) |
||
| 105 | log.debug('Topology graph updated.') |
||
| 106 |