Passed
Pull Request — master (#30)
by Rogerio
07:01
created

build.utils   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 98.77%

Importance

Changes 0
Metric Value
eloc 100
dl 0
loc 132
rs 8.96
c 0
b 0
f 0
ccs 80
cts 81
cp 0.9877
wmc 43

6 Functions

Rating   Name   Duplication   Size   Complexity  
A find_endpoint() 0 10 3
F _compare_endpoints() 0 20 15
D clean_circuits() 0 36 13
A convert_entries() 0 17 5
A prepare_json() 0 6 2
A format_result() 0 14 5

How to fix   Complexity   

Complexity

Complex classes like build.utils 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
"""Utility functions to be used in this Napp"""
2
3 1
from kytos.core import KytosEvent
4 1
from napps.amlight.sdntrace_cp import settings
5
6 1
TRANSLATE_NAMES = {
7
    'dl_src': 'eth_src',
8
    'dl_dst': 'eth_dst',
9
    'dl_type': 'eth_type',
10
    'dl_vlan': 'vlan_vid',
11
    'nw_src': 'ip4_src',
12
    'nw_dst': 'ip4_dst',
13
    'nw_tos': 'ip_tos',
14
    'nw_proto': 'ip_proto',
15
}
16
17
18 1
def convert_entries(entries):
19
    """ Transform entries dictionary in a plain dictionary suitable for
20
        matching
21
22
    :param entries: dict
23
    :return: plain dict
24
    """
25 1
    new_entries = {}
26 1
    for entry in entries['trace'].values():
27 1
        for field, value in entry.items():
28 1
            if field in TRANSLATE_NAMES:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TRANSLATE_NAMES does not seem to be defined.
Loading history...
29 1
                new_entries[TRANSLATE_NAMES[field]] = value
30
            else:
31 1
                new_entries[field] = value
32 1
    if 'vlan_vid' in new_entries:
33 1
        new_entries['vlan_vid'] = [new_entries['vlan_vid']]
34 1
    return new_entries
35
36
37 1
def find_endpoint(switch, port):
38
    """ Find where switch/port is connected. If it is another switch,
39
    returns the interface it is connected to, otherwise returns None """
40
41 1
    interface = switch.get_interface_by_port_no(port)
42 1
    if interface.link:
43 1
        if interface == interface.link.endpoint_a:
44 1
            return interface.link.endpoint_b
45 1
        return interface.link.endpoint_a
46 1
    return None
47
48
49 1
def prepare_json(trace_result):
50
    """Prepare return json for REST call."""
51 1
    result = []
52 1
    for trace_step in trace_result:
53 1
        result.append(trace_step['in'])
54 1
    return {'result': result}
55
56
57 1
def format_result(trace):
58
    """Format the result for automate circuit finding"""
59 1
    result = []
60 1
    for step in trace:
61 1
        new_result = {'dpid': step['in']['dpid'],
62
                      'in_port': step['in']['port']}
63 1
        if 'out' in step:
64 1
            new_result.update({'out_port': step['out']['port']})
65 1
            if 'vlan' in step['out']:
66 1
                new_result.update({'out_vlan': step['out']['vlan']})
67 1
        if 'vlan' in step['in']:
68 1
            new_result.update({'in_vlan': step['in']['vlan']})
69 1
        result.append(new_result)
70 1
    return result
71
72
73 1
def clean_circuits(circuits, controller):
74
    """Remove sub-circuits."""
75 1
    cleaned_circuits = []
76 1
    event = KytosEvent(name='amlight/kytos_courier.slack_send')
77 1
    content = {
78
        'channel': settings.SLACK_CHANNEL,
79
        'source': 'amlight/sdntrace_cp'
80
    }
81 1
    for circuit in circuits:
82 1
        sub = False
83 1
        for other in circuits:
84 1
            if circuit['circuit'] == other['circuit']:
85 1
                continue
86 1
            sub = True
87 1
            for step in circuit['circuit']:
88 1
                if step not in other['circuit']:
89 1
                    sub = False
90 1
                    break
91 1
            if sub:
92 1
                break
93 1
        if not sub:
94 1
            cleaned_circuits.append(circuit)
95
96 1
    for circuit in cleaned_circuits:
97 1
        has_return = False
98 1
        for other in cleaned_circuits:
99 1
            if _compare_endpoints(circuit['circuit'][0],
100
                                  other['circuit'][-1]) \
101
                    and _compare_endpoints(circuit['circuit'][-1],
102
                                           other['circuit'][0]):
103
                has_return = True
104 1
        if not has_return:
105 1
            content['m_body'] = f"Circuit {circuit['circuit']} has no way back"
106 1
            event.content['message'] = content
107 1
            controller.buffers.app.put(event)
108 1
    return cleaned_circuits
109
110
111
# pylint: disable=too-many-return-statements
112 1
def _compare_endpoints(endpoint1, endpoint2):
113 1
    if endpoint1['dpid'] != endpoint2['dpid']:
114 1
        return False
115 1
    if (
116
        'in_port' not in endpoint1
117
        or 'out_port' not in endpoint2
118
        or endpoint1['in_port'] != endpoint2['out_port']
119
    ):
120 1
        return False
121 1
    if 'in_vlan' in endpoint1 and 'out_vlan' in endpoint2:
122 1
        if endpoint1['in_vlan'] != endpoint2['out_vlan']:
123 1
            return False
124 1
    elif 'in_vlan' in endpoint1 or 'out_vlan' in endpoint2:
125 1
        return False
126 1
    if 'out_vlan' in endpoint1 and 'in_vlan' in endpoint2:
127 1
        if endpoint1['out_vlan'] != endpoint2['in_vlan']:
128 1
            return False
129 1
    elif 'out_vlan' in endpoint1 or 'in_vlan' in endpoint2:
130 1
        return False
131
    return True
132