Passed
Pull Request — master (#96)
by
unknown
02:58
created

build.utils.match_field_dl_vlan()   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
"""Utility functions to be used in this Napp"""
2
3 1
import ipaddress
4
5 1
import requests
6 1
from kytos.core import KytosEvent
7 1
from napps.amlight.sdntrace_cp import settings
8
9
10 1
def get_stored_flows(dpids: list = None, state: str = "installed"):
11
    """Get stored flows from flow_manager napps."""
12 1
    api_url = f'{settings.FLOW_MANAGER_URL}/stored_flows'
13 1
    if dpids:
14
        str_dpids = ''
15
        for dpid in dpids:
16
            str_dpids += f'&dpid={dpid}'
17
        api_url += '/?'+str_dpids[1:]
18 1
    if state:
19 1
        char = '&' if dpids else '/?'
20 1
        api_url += char+f'state={state}'
21 1
    result = requests.get(api_url)
22 1
    flows_from_manager = result.json()
23 1
    return flows_from_manager
24
25
26 1
def convert_entries(entries):
27
    """ Transform entries dictionary in a plain dictionary suitable for
28
        matching
29
30
    :param entries: dict
31
    :return: plain dict
32
    """
33 1
    new_entries = {}
34 1
    for entry in entries['trace'].values():
35 1
        for field, value in entry.items():
36 1
            new_entries[field] = value
37 1
    if 'dl_vlan' in new_entries:
38 1
        new_entries['dl_vlan'] = [new_entries['dl_vlan']]
39 1
    return new_entries
40
41
42 1
def convert_list_entries(entries):
43
    """ Transform a list of entries dictionary in a list
44
    of plain dictionary suitable for matching
45
    :param entries: list(dict)
46
    :return: list(plain dict)
47
    """
48 1
    new_entries = []
49 1
    for entry in entries:
50 1
        new_entry = convert_entries(entry)
51 1
        if new_entry:
52 1
            new_entries.append(new_entry)
53 1
    return new_entries
54
55
56 1
def find_endpoint(switch, port):
57
    """ Find where switch/port is connected. If it is another switch,
58
    returns the interface it is connected to, otherwise returns None """
59
60 1
    interface = switch.get_interface_by_port_no(port)
61 1
    if not interface:
62
        return None
63 1
    if interface and interface.link:
64 1
        if interface == interface.link.endpoint_a:
65 1
            return {'endpoint': interface.link.endpoint_b}
66 1
        return {'endpoint': interface.link.endpoint_a}
67 1
    return {'endpoint': None}
68
69
70 1
def _prepare_json(trace_result):
71
    """Auxiliar function to return the json for REST call."""
72 1
    result = []
73 1
    for trace_step in trace_result:
74 1
        result.append(trace_step['in'])
75 1
    if result:
76 1
        result[-1]["out"] = trace_result[-1].get("out")
77 1
    return result
78
79
80 1
def prepare_json(trace_result):
81
    """Prepare return json for REST call."""
82 1
    result = []
83 1
    if trace_result and isinstance(trace_result[0], list):
84 1
        for trace in trace_result:
85 1
            result.append(_prepare_json(trace))
86
    else:
87 1
        result = _prepare_json(trace_result)
88 1
    return {'result': result}
89
90
91 1
def format_result(trace):
92
    """Format the result for automate circuit finding"""
93 1
    result = []
94 1
    for step in trace:
95 1
        new_result = {'dpid': step['in']['dpid'],
96
                      'in_port': step['in']['port']}
97 1
        if 'out' in step:
98 1
            new_result.update({'out_port': step['out']['port']})
99 1
            if 'vlan' in step['out']:
100 1
                new_result.update({'out_vlan': step['out']['vlan']})
101 1
        if 'vlan' in step['in']:
102 1
            new_result.update({'in_vlan': step['in']['vlan']})
103 1
        result.append(new_result)
104 1
    return result
105
106
107 1
def clean_circuits(circuits, controller):
108
    """Remove sub-circuits."""
109 1
    cleaned_circuits = []
110 1
    event = KytosEvent(name='amlight/kytos_courier.slack_send')
111 1
    content = {
112
        'channel': settings.SLACK_CHANNEL,
113
        'source': 'amlight/sdntrace_cp'
114
    }
115 1
    for circuit in circuits:
116 1
        sub = False
117 1
        for other in circuits:
118 1
            if circuit['circuit'] == other['circuit']:
119 1
                continue
120 1
            sub = True
121 1
            for step in circuit['circuit']:
122 1
                if step not in other['circuit']:
123 1
                    sub = False
124 1
                    break
125 1
            if sub:
126 1
                break
127 1
        if not sub:
128 1
            cleaned_circuits.append(circuit)
129
130 1
    for circuit in cleaned_circuits:
131 1
        has_return = False
132 1
        for other in cleaned_circuits:
133 1
            if _compare_endpoints(circuit['circuit'][0],
134
                                  other['circuit'][-1]) \
135
                    and _compare_endpoints(circuit['circuit'][-1],
136
                                           other['circuit'][0]):
137
                has_return = True
138 1
        if not has_return:
139 1
            content['m_body'] = f"Circuit {circuit['circuit']} has no way back"
140 1
            event.content['message'] = content
141 1
            controller.buffers.app.put(event)
142 1
    return cleaned_circuits
143
144
145
# pylint: disable=too-many-return-statements
146 1
def _compare_endpoints(endpoint1, endpoint2):
147 1
    if endpoint1['dpid'] != endpoint2['dpid']:
148 1
        return False
149 1
    if (
150
        'in_port' not in endpoint1
151
        or 'out_port' not in endpoint2
152
        or endpoint1['in_port'] != endpoint2['out_port']
153
    ):
154 1
        return False
155 1
    if 'in_vlan' in endpoint1 and 'out_vlan' in endpoint2:
156 1
        if endpoint1['in_vlan'] != endpoint2['out_vlan']:
157 1
            return False
158 1
    elif 'in_vlan' in endpoint1 or 'out_vlan' in endpoint2:
159 1
        return False
160 1
    if 'out_vlan' in endpoint1 and 'in_vlan' in endpoint2:
161 1
        if endpoint1['out_vlan'] != endpoint2['in_vlan']:
162 1
            return False
163 1
    elif 'out_vlan' in endpoint1 or 'in_vlan' in endpoint2:
164 1
        return False
165 1
    return True
166
167
168 1
def convert_vlan(value):
169
    """Auxiliar function to calculate dl_vlan"""
170 1
    if isinstance(value, int):
171 1
        return value, 4095
172 1
    value, mask = map(int, value.split('/'))
173 1
    return value, mask
174
175
176 1
def match_field_dl_vlan(value, field_flow):
177
    """ Verify match in dl_vlan.
178
    value only takes an int in range [1,4095].
179
    0 is not allowed for value. """
180 1
    if not value:
181 1
        return field_flow == 0
182 1
    value = value[-1]
183 1
    value_flow, mask_flow = convert_vlan(field_flow)
184 1
    return value & (mask_flow & 4095) == value_flow & (mask_flow & 4095)
185
186
187 1
def match_field_ip(field, field_flow):
188
    "Verify match in ip fields"
189
    packet_ip = int(ipaddress.ip_address(field))
190
    return packet_ip & field_flow.netmask == field_flow.address
191