Completed
Pull Request — master (#58)
by
unknown
02:54
created

build.main   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 139
Duplicated Lines 46.04 %

Test Coverage

Coverage 68.83%

Importance

Changes 0
Metric Value
eloc 91
dl 64
loc 139
rs 10
c 0
b 0
f 0
ccs 53
cts 77
cp 0.6883
wmc 24

8 Methods

Rating   Name   Duplication   Size   Complexity  
A Main.shortest_path() 0 18 2
A Main.shutdown() 0 2 1
A Main.setup() 0 4 1
A Main.execute() 0 2 1
D Main._filter_paths() 31 42 13
A Main.shortest_constrained_path() 17 17 2
A Main.shortest_constrained_path2() 16 16 2
A Main.update_topology() 0 12 2

How to fix   Duplicated Code   

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:

1
"""Main module of kytos/pathfinder Kytos Network Application."""
2
3 1
from flask import jsonify, request
4 1
from kytos.core import KytosNApp, log, rest
5 1
from kytos.core.helpers import listen_to
6
7
# pylint: disable=import-error
8 1
from napps.kytos.pathfinder.graph import KytosGraph
9
10
# pylint: enable=import-error
11
12
13 1
class Main(KytosNApp):
14
    """Main class of kytos/pathfinder NApp.
15
16
    This class is the entry point for this napp.
17
    """
18
19 1
    def setup(self):
20
        """Create a graph to handle the nodes and edges."""
21 1
        self.graph = KytosGraph()
22 1
        self._topology = None
23
24 1
    def execute(self):
25
        """Do nothing."""
26
27 1
    def shutdown(self):
28
        """Shutdown the napp."""
29
30 1
    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 1
        filtered_paths = []
37
38 1 View Code Duplication
        if desired:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
39 1
            for link_id in desired:
40 1
                try:
41 1
                    endpoint_a = self._topology.links[link_id].endpoint_a.id
42 1
                    endpoint_b = self._topology.links[link_id].endpoint_b.id
43
                except KeyError:
44
                    return []
45
46 1
                for path in paths:
47 1
                    head = path['hops'][:-1]
48 1
                    tail = path['hops'][1:]
49 1
                    if (((endpoint_a, endpoint_b) in zip(head, tail)) or
50
                            ((endpoint_b, endpoint_a) in zip(head, tail))):
51 1
                        filtered_paths.append(path)
52
        else:
53 1
            filtered_paths = paths
54
55 1 View Code Duplication
        if undesired:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56 1
            for link_id in undesired:
57 1
                try:
58 1
                    endpoint_a = self._topology.links[link_id].endpoint_a.id
59 1
                    endpoint_b = self._topology.links[link_id].endpoint_b.id
60
                except KeyError:
61
                    continue
62
63 1
                for path in paths:
64 1
                    head = path['hops'][:-1]
65 1
                    tail = path['hops'][1:]
66 1
                    if (((endpoint_a, endpoint_b) in zip(head, tail)) or
67
                            ((endpoint_b, endpoint_a) in zip(head, tail))):
68
69 1
                        filtered_paths.remove(path)
70
71 1
        return filtered_paths
72
73 1
    @rest('v2/', methods=['POST'])
74
    def shortest_path(self):
75
        """Calculate the best path between the source and destination."""
76 1
        data = request.get_json()
77
78 1
        desired = data.get('desired_links')
79 1
        undesired = data.get('undesired_links')
80 1
        parameter = data.get('parameter')
81
82 1
        paths = []
83 1
        for path in self.graph.shortest_paths(data['source'],
84
                                              data['destination'],
85
                                              parameter):
86
87 1
            paths.append({'hops': path})
88
89 1
        paths = self._filter_paths(paths, desired, undesired)
90 1
        return jsonify({'paths': paths})
91
92 1 View Code Duplication
    @rest('v3/', methods=['POST'])
0 ignored issues
show
Duplication introduced by
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
        depth = data.get('depth', 0)
100
        base_metrics = data.get('base_metrics', {})
101
        try:
102
            paths = self.graph.constrained_flexible_paths(source,
103
                                                          destination,
104
                                                          depth,
105
                                                          base=base_metrics)
106
            return jsonify(paths)
107
        except TypeError as err:
108
            return jsonify({"error": err})
109
110 1 View Code Duplication
    @ rest('v4/', methods=['POST'])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
111
    def shortest_constrained_path2(self):
112
        """Get the set of shortest paths between the source and destination."""
113
        data = request.get_json()
114
115
        source = data.get('source')
116
        destination = data.get('destination')
117
        base_metrics = data.get('base_metrics', {})
118
        fle_metrics = data.get('flexible_metrics', {})
119
        try:
120
            paths = self.graph.constrained_flexible_paths(source, destination,
121
                                                          base=base_metrics,
122
                                                          flexible=fle_metrics)
123
            return jsonify(paths)
124
        except TypeError as err:
125
            return jsonify({"error": err})
126
127 1
    @ listen_to('kytos.topology.updated')
128
    def update_topology(self, event):
129
        """Update the graph when the network topology was updated.
130
131
        Clear the current graph and create a new with the most topoly updated.
132
        """
133 1
        if 'topology' not in event.content:
134 1
            return
135 1
        topology = event.content['topology']
136 1
        self._topology = topology
137 1
        self.graph.update_topology(topology)
138
        log.debug('Topology graph updated.')
139