Test Failed
Pull Request — master (#53)
by
unknown
02:06
created
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 setup(self):
20
        """Create a graph to handle the nodes and edges."""
21
        self.graph = KytosGraph()
22
        self._topology = None
23
24
    def execute(self):
25
        """Do nothing."""
26
27
    def shutdown(self):
28
        """Shutdown the napp."""
29
30
    def _filter_paths(self, paths, desired, undesired):
31
        """Apply filters to the paths list.
32
33
        Make sure that each path in the list has all the desired links and none
34
        of the undesired ones.
35
        """
36
        filtered_paths = []
37
38 View Code Duplication
        if desired:
39
            for link_id in desired:
40
                try:
41
                    endpoint_a = self._topology.links[link_id].endpoint_a.id
42
                    endpoint_b = self._topology.links[link_id].endpoint_b.id
43
                except KeyError:
44
                    return []
45
46
                for path in paths:
47
                    head = path['hops'][:-1]
48
                    tail = path['hops'][1:]
49
                    if (((endpoint_a, endpoint_b) in zip(head, tail)) or
50
                            ((endpoint_b, endpoint_a) in zip(head, tail))):
51
                        filtered_paths.append(path)
52
        else:
53
            filtered_paths = paths
54
55 View Code Duplication
        if undesired:
56
            for link_id in undesired:
57
                try:
58
                    endpoint_a = self._topology.links[link_id].endpoint_a.id
59
                    endpoint_b = self._topology.links[link_id].endpoint_b.id
60
                except KeyError:
61
                    continue
62
63
                for path in paths:
64
                    head = path['hops'][:-1]
65
                    tail = path['hops'][1:]
66
                    if (((endpoint_a, endpoint_b) in zip(head, tail)) or
67
                            ((endpoint_b, endpoint_a) in zip(head, tail))):
68
69
                        filtered_paths.remove(path)
70
71
        return filtered_paths
72
73
    @rest('v2/', methods=['POST'])
74
    def shortest_path(self):
75
        """Calculate the best path between the source and destination."""
76
        data = request.get_json()
77
78
        desired = data.get('desired_links')
79
        undesired = data.get('undesired_links')
80
        parameter = data.get('parameter')
81
82
        paths = []
83
        for path in self.graph.shortest_paths(data['source'],
84
                                              data['destination'],
85
                                              parameter):
86
87
            paths.append({'hops': path})
88
89
        paths = self._filter_paths(paths, desired, undesired)
90
        return jsonify({'paths': paths})
91
92 View Code Duplication
    @rest('v3/', methods=['POST'])
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
93
    def shortest_constrained_path(self):
94
        """Get the set of shortest paths between the source and destination."""
95
        data = request.get_json()
96
97
        source = data.get('source')
98
        destination = data.get('destination')
99
        flexible = data.get('flexible', 0)
100
        metrics = data.get('metrics',{})
101
        try:
102
            paths = self.graph.constrained_flexible_paths(source, destination,{},metrics,flexible)
103
            return jsonify(paths)
104
        except TypeError as err:
105
            return jsonify({"error":err})
106
107
108 View Code Duplication
    @rest('v4/', methods=['POST'])
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
109
    def shortest_constrained_path2(self):
110
        """Get the set of shortest paths between the source and destination."""
111
        data = request.get_json()
112
113
        source = data.get('source')
114
        destination = data.get('destination')
115
        metrics = data.get('metrics',{})
116
        flexible_metrics = data.get('flexibleMetrics', {})
117
        try:
118
            paths = self.graph.constrained_flexible_paths(source, destination,
119
                                            metrics, flexible_metrics)
120
            return jsonify(paths)
121
        except TypeError as err:
122
            return jsonify({"error":err})
123
        
124
125
    @listen_to('kytos.topology.updated')
126
    def update_topology(self, event):
127
        """Update the graph when the network topology was updated.
128
129
        Clear the current graph and create a new with the most topoly updated.
130
        """
131
        if 'topology' not in event.content:
132
            return
133
        topology = event.content['topology']
134
        self._topology = topology
135
        self.graph.update_topology(topology)
136
        log.debug('Topology graph updated.')
137