1
|
|
|
"""Utility functions to be used in this Napp""" |
2
|
|
|
|
3
|
1 |
|
import requests |
4
|
1 |
|
from kytos.core import KytosEvent |
5
|
1 |
|
from napps.amlight.sdntrace_cp import settings |
6
|
|
|
|
7
|
|
|
|
8
|
1 |
|
def get_stored_flows(dpids: list = None, state: str = "installed"): |
9
|
|
|
"""Get stored flows from flow_manager napps.""" |
10
|
1 |
|
api_url = f'{settings.FLOW_MANAGER_URL}/stored_flows' |
11
|
1 |
|
if dpids: |
12
|
|
|
str_dpids = '' |
13
|
|
|
for dpid in dpids: |
14
|
|
|
str_dpids += f'&dpid={dpid}' |
15
|
|
|
api_url += '/?'+str_dpids[1:] |
16
|
1 |
|
if state: |
17
|
1 |
|
char = '&' if dpids else '/?' |
18
|
1 |
|
api_url += char+f'state={state}' |
19
|
1 |
|
result = requests.get(api_url) |
20
|
1 |
|
flows_from_manager = result.json() |
21
|
1 |
|
return flows_from_manager |
22
|
|
|
|
23
|
|
|
|
24
|
1 |
|
def convert_entries(entries): |
25
|
|
|
""" Transform entries dictionary in a plain dictionary suitable for |
26
|
|
|
matching |
27
|
|
|
|
28
|
|
|
:param entries: dict |
29
|
|
|
:return: plain dict |
30
|
|
|
""" |
31
|
1 |
|
new_entries = {} |
32
|
1 |
|
for entry in entries['trace'].values(): |
33
|
1 |
|
for field, value in entry.items(): |
34
|
1 |
|
new_entries[field] = value |
35
|
1 |
|
if 'dl_vlan' in new_entries: |
36
|
1 |
|
new_entries['dl_vlan'] = [new_entries['dl_vlan']] |
37
|
1 |
|
if ('dpid' not in new_entries) or ('in_port' not in new_entries): |
38
|
1 |
|
new_entries = {} |
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 interface.link: |
62
|
1 |
|
if interface == interface.link.endpoint_a: |
63
|
1 |
|
return interface.link.endpoint_b |
64
|
1 |
|
return interface.link.endpoint_a |
65
|
1 |
|
return None |
66
|
|
|
|
67
|
|
|
|
68
|
1 |
|
def _prepare_json(trace_result): |
69
|
|
|
"""Auxiliar function to return the json for REST call.""" |
70
|
1 |
|
result = [] |
71
|
1 |
|
for trace_step in trace_result: |
72
|
1 |
|
result.append(trace_step['in']) |
73
|
1 |
|
if result: |
74
|
1 |
|
result[-1]["out"] = trace_result[-1].get("out") |
75
|
1 |
|
return result |
76
|
|
|
|
77
|
|
|
|
78
|
1 |
|
def prepare_json(trace_result): |
79
|
|
|
"""Prepare return json for REST call.""" |
80
|
1 |
|
result = [] |
81
|
1 |
|
if trace_result and isinstance(trace_result[0], list): |
82
|
1 |
|
for trace in trace_result: |
83
|
1 |
|
result.append(_prepare_json(trace)) |
84
|
|
|
else: |
85
|
1 |
|
result = _prepare_json(trace_result) |
86
|
1 |
|
return {'result': result} |
87
|
|
|
|
88
|
|
|
|
89
|
1 |
|
def format_result(trace): |
90
|
|
|
"""Format the result for automate circuit finding""" |
91
|
1 |
|
result = [] |
92
|
1 |
|
for step in trace: |
93
|
1 |
|
new_result = {'dpid': step['in']['dpid'], |
94
|
|
|
'in_port': step['in']['port']} |
95
|
1 |
|
if 'out' in step: |
96
|
1 |
|
new_result.update({'out_port': step['out']['port']}) |
97
|
1 |
|
if 'vlan' in step['out']: |
98
|
1 |
|
new_result.update({'out_vlan': step['out']['vlan']}) |
99
|
1 |
|
if 'vlan' in step['in']: |
100
|
1 |
|
new_result.update({'in_vlan': step['in']['vlan']}) |
101
|
1 |
|
result.append(new_result) |
102
|
1 |
|
return result |
103
|
|
|
|
104
|
|
|
|
105
|
1 |
|
def clean_circuits(circuits, controller): |
106
|
|
|
"""Remove sub-circuits.""" |
107
|
1 |
|
cleaned_circuits = [] |
108
|
1 |
|
event = KytosEvent(name='amlight/kytos_courier.slack_send') |
109
|
1 |
|
content = { |
110
|
|
|
'channel': settings.SLACK_CHANNEL, |
111
|
|
|
'source': 'amlight/sdntrace_cp' |
112
|
|
|
} |
113
|
1 |
|
for circuit in circuits: |
114
|
1 |
|
sub = False |
115
|
1 |
|
for other in circuits: |
116
|
1 |
|
if circuit['circuit'] == other['circuit']: |
117
|
1 |
|
continue |
118
|
1 |
|
sub = True |
119
|
1 |
|
for step in circuit['circuit']: |
120
|
1 |
|
if step not in other['circuit']: |
121
|
1 |
|
sub = False |
122
|
1 |
|
break |
123
|
1 |
|
if sub: |
124
|
1 |
|
break |
125
|
1 |
|
if not sub: |
126
|
1 |
|
cleaned_circuits.append(circuit) |
127
|
|
|
|
128
|
1 |
|
for circuit in cleaned_circuits: |
129
|
1 |
|
has_return = False |
130
|
1 |
|
for other in cleaned_circuits: |
131
|
1 |
|
if _compare_endpoints(circuit['circuit'][0], |
132
|
|
|
other['circuit'][-1]) \ |
133
|
|
|
and _compare_endpoints(circuit['circuit'][-1], |
134
|
|
|
other['circuit'][0]): |
135
|
|
|
has_return = True |
136
|
1 |
|
if not has_return: |
137
|
1 |
|
content['m_body'] = f"Circuit {circuit['circuit']} has no way back" |
138
|
1 |
|
event.content['message'] = content |
139
|
1 |
|
controller.buffers.app.put(event) |
140
|
1 |
|
return cleaned_circuits |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
# pylint: disable=too-many-return-statements |
144
|
1 |
|
def _compare_endpoints(endpoint1, endpoint2): |
145
|
1 |
|
if endpoint1['dpid'] != endpoint2['dpid']: |
146
|
1 |
|
return False |
147
|
1 |
|
if ( |
148
|
|
|
'in_port' not in endpoint1 |
149
|
|
|
or 'out_port' not in endpoint2 |
150
|
|
|
or endpoint1['in_port'] != endpoint2['out_port'] |
151
|
|
|
): |
152
|
1 |
|
return False |
153
|
1 |
|
if 'in_vlan' in endpoint1 and 'out_vlan' in endpoint2: |
154
|
1 |
|
if endpoint1['in_vlan'] != endpoint2['out_vlan']: |
155
|
1 |
|
return False |
156
|
1 |
|
elif 'in_vlan' in endpoint1 or 'out_vlan' in endpoint2: |
157
|
1 |
|
return False |
158
|
1 |
|
if 'out_vlan' in endpoint1 and 'in_vlan' in endpoint2: |
159
|
1 |
|
if endpoint1['out_vlan'] != endpoint2['in_vlan']: |
160
|
1 |
|
return False |
161
|
1 |
|
elif 'out_vlan' in endpoint1 or 'in_vlan' in endpoint2: |
162
|
1 |
|
return False |
163
|
|
|
return True |
164
|
|
|
|