|
1
|
|
|
"""Main module of kytos/telemetry Network Application. |
|
2
|
|
|
|
|
3
|
|
|
Napp to deploy In-band Network Telemetry over Ethernet Virtual Circuits |
|
4
|
|
|
|
|
5
|
|
|
""" |
|
6
|
|
|
|
|
7
|
1 |
|
import asyncio |
|
8
|
1 |
|
import pathlib |
|
9
|
1 |
|
from datetime import datetime |
|
10
|
|
|
|
|
11
|
1 |
|
import napps.kytos.telemetry_int.kytos_api_helper as api |
|
12
|
1 |
|
from napps.kytos.telemetry_int import settings, utils |
|
13
|
1 |
|
from tenacity import RetryError |
|
14
|
|
|
|
|
15
|
1 |
|
from kytos.core import KytosEvent, KytosNApp, log, rest |
|
16
|
1 |
|
from kytos.core.helpers import alisten_to, avalidate_openapi_request, load_spec |
|
17
|
1 |
|
from kytos.core.rest_api import HTTPException, JSONResponse, Request, aget_json_or_400 |
|
18
|
|
|
|
|
19
|
1 |
|
from .exceptions import ( |
|
20
|
|
|
EVCHasINT, |
|
21
|
|
|
EVCHasNoINT, |
|
22
|
|
|
EVCNotFound, |
|
23
|
|
|
FlowsNotFound, |
|
24
|
|
|
ProxyPortNotFound, |
|
25
|
|
|
ProxyPortSameSourceIntraEVC, |
|
26
|
|
|
ProxyPortStatusNotUP, |
|
27
|
|
|
UnrecoverableError, |
|
28
|
|
|
) |
|
29
|
1 |
|
from .managers.int import INTManager |
|
30
|
|
|
|
|
31
|
|
|
# pylint: disable=fixme |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
1 |
|
class Main(KytosNApp): |
|
35
|
|
|
"""Main class of kytos/telemetry NApp. |
|
36
|
|
|
|
|
37
|
|
|
This class is the entry point for this NApp. |
|
38
|
|
|
""" |
|
39
|
|
|
|
|
40
|
1 |
|
spec = load_spec(pathlib.Path(__file__).parent / "openapi.yml") |
|
41
|
|
|
|
|
42
|
1 |
|
def setup(self): |
|
43
|
|
|
"""Replace the '__init__' method for the KytosNApp subclass. |
|
44
|
|
|
|
|
45
|
|
|
The setup method is automatically called by the controller when your |
|
46
|
|
|
application is loaded. |
|
47
|
|
|
|
|
48
|
|
|
So, if you have any setup routine, insert it here. |
|
49
|
|
|
""" |
|
50
|
|
|
|
|
51
|
1 |
|
self.int_manager = INTManager(self.controller) |
|
52
|
1 |
|
self._ofpt_error_lock = asyncio.Lock() |
|
53
|
|
|
|
|
54
|
1 |
|
def execute(self): |
|
55
|
|
|
"""Run after the setup method execution. |
|
56
|
|
|
|
|
57
|
|
|
You can also use this method in loop mode if you add to the above setup |
|
58
|
|
|
method a line like the following example: |
|
59
|
|
|
|
|
60
|
|
|
self.execute_as_loop(30) # 30-second interval. |
|
61
|
|
|
""" |
|
62
|
|
|
|
|
63
|
1 |
|
def shutdown(self): |
|
64
|
|
|
"""Run when your NApp is unloaded. |
|
65
|
|
|
|
|
66
|
|
|
If you have some cleanup procedure, insert it here. |
|
67
|
|
|
""" |
|
68
|
|
|
|
|
69
|
1 |
|
@rest("v1/evc/enable", methods=["POST"]) |
|
70
|
1 |
|
async def enable_telemetry(self, request: Request) -> JSONResponse: |
|
71
|
|
|
"""REST to enable INT flows on EVCs. |
|
72
|
|
|
|
|
73
|
|
|
If a list of evc_ids is empty, it'll enable on non-INT EVCs. |
|
74
|
|
|
""" |
|
75
|
1 |
|
await avalidate_openapi_request(self.spec, request) |
|
76
|
|
|
|
|
77
|
1 |
|
try: |
|
78
|
1 |
|
content = await aget_json_or_400(request) |
|
79
|
1 |
|
evc_ids = content["evc_ids"] |
|
80
|
1 |
|
force = content.get("force", False) |
|
81
|
1 |
|
if not isinstance(force, bool): |
|
82
|
|
|
raise TypeError(f"'force' wrong type: {type(force)} expected bool") |
|
83
|
|
|
except (TypeError, KeyError): |
|
84
|
|
|
raise HTTPException(400, detail=f"Invalid payload: {content}") |
|
85
|
|
|
|
|
86
|
1 |
|
try: |
|
87
|
1 |
|
evcs = ( |
|
88
|
|
|
await api.get_evcs() |
|
89
|
|
|
if len(evc_ids) != 1 |
|
90
|
|
|
else await api.get_evc(evc_ids[0]) |
|
91
|
|
|
) |
|
92
|
|
|
except RetryError as exc: |
|
93
|
|
|
exc_error = str(exc.last_attempt.exception()) |
|
94
|
|
|
log.error(exc_error) |
|
95
|
|
|
raise HTTPException(503, detail=exc_error) |
|
96
|
|
|
|
|
97
|
1 |
|
if evc_ids: |
|
98
|
1 |
|
evcs = {evc_id: evcs.get(evc_id, {}) for evc_id in evc_ids} |
|
99
|
|
|
else: |
|
100
|
|
|
evcs = {k: v for k, v in evcs.items() if not utils.has_int_enabled(v)} |
|
101
|
|
|
if not evcs: |
|
102
|
|
|
# There's no non-INT EVCs to get enabled. |
|
103
|
|
|
return JSONResponse(list(evcs.keys())) |
|
104
|
|
|
|
|
105
|
1 |
|
try: |
|
106
|
|
|
# First, it tries to get and remove the existing INT flows like mef_eline |
|
107
|
1 |
|
stored_flows = await api.get_stored_flows( |
|
108
|
|
|
[ |
|
109
|
|
|
utils.get_cookie(evc_id, settings.INT_COOKIE_PREFIX) |
|
110
|
|
|
for evc_id in evcs |
|
111
|
|
|
] |
|
112
|
|
|
) |
|
113
|
1 |
|
await self.int_manager._remove_int_flows(stored_flows) |
|
114
|
1 |
|
await self.int_manager.enable_int(evcs, force) |
|
115
|
|
|
except (EVCNotFound, FlowsNotFound, ProxyPortNotFound) as exc: |
|
116
|
|
|
raise HTTPException(404, detail=str(exc)) |
|
117
|
|
|
except (EVCHasINT, ProxyPortStatusNotUP, ProxyPortSameSourceIntraEVC) as exc: |
|
118
|
|
|
raise HTTPException(409, detail=str(exc)) |
|
119
|
|
|
except RetryError as exc: |
|
120
|
|
|
exc_error = str(exc.last_attempt.exception()) |
|
121
|
|
|
log.error(exc_error) |
|
122
|
|
|
raise HTTPException(503, detail=exc_error) |
|
123
|
|
|
except UnrecoverableError as exc: |
|
124
|
|
|
exc_error = str(exc) |
|
125
|
|
|
log.error(exc_error) |
|
126
|
|
|
raise HTTPException(500, detail=exc_error) |
|
127
|
|
|
|
|
128
|
1 |
|
return JSONResponse(list(evcs.keys()), status_code=201) |
|
129
|
|
|
|
|
130
|
1 |
|
@rest("v1/evc/disable", methods=["POST"]) |
|
131
|
1 |
|
async def disable_telemetry(self, request: Request) -> JSONResponse: |
|
132
|
|
|
"""REST to disable/remove INT flows for an EVC_ID |
|
133
|
|
|
|
|
134
|
|
|
If a list of evc_ids is empty, it'll disable on all INT EVCs. |
|
135
|
|
|
""" |
|
136
|
1 |
|
await avalidate_openapi_request(self.spec, request) |
|
137
|
|
|
|
|
138
|
1 |
|
try: |
|
139
|
1 |
|
content = await aget_json_or_400(request) |
|
140
|
1 |
|
evc_ids = content["evc_ids"] |
|
141
|
1 |
|
force = content.get("force", False) |
|
142
|
1 |
|
if not isinstance(force, bool): |
|
143
|
|
|
raise TypeError(f"'force' wrong type: {type(force)} expected bool") |
|
144
|
|
|
except (TypeError, KeyError): |
|
145
|
|
|
raise HTTPException(400, detail=f"Invalid payload: {content}") |
|
146
|
|
|
|
|
147
|
1 |
|
try: |
|
148
|
1 |
|
evcs = ( |
|
149
|
|
|
await api.get_evcs() |
|
150
|
|
|
if len(evc_ids) != 1 |
|
151
|
|
|
else await api.get_evc(evc_ids[0]) |
|
152
|
|
|
) |
|
153
|
|
|
except RetryError as exc: |
|
154
|
|
|
exc_error = str(exc.last_attempt.exception()) |
|
155
|
|
|
log.error(exc_error) |
|
156
|
|
|
raise HTTPException(503, detail=exc_error) |
|
157
|
|
|
|
|
158
|
1 |
|
if evc_ids: |
|
159
|
1 |
|
evcs = {evc_id: evcs.get(evc_id, {}) for evc_id in evc_ids} |
|
160
|
|
|
else: |
|
161
|
|
|
evcs = {k: v for k, v in evcs.items() if utils.has_int_enabled(v)} |
|
162
|
|
|
if not evcs: |
|
163
|
|
|
# There's no INT EVCs to get disabled. |
|
164
|
|
|
return JSONResponse(list(evcs.keys())) |
|
165
|
|
|
|
|
166
|
1 |
|
try: |
|
167
|
1 |
|
await self.int_manager.disable_int(evcs, force) |
|
168
|
|
|
except EVCNotFound as exc: |
|
169
|
|
|
raise HTTPException(404, detail=str(exc)) |
|
170
|
|
|
except EVCHasNoINT as exc: |
|
171
|
|
|
raise HTTPException(409, detail=str(exc)) |
|
172
|
|
|
except RetryError as exc: |
|
173
|
|
|
exc_error = str(exc.last_attempt.exception()) |
|
174
|
|
|
log.error(exc_error) |
|
175
|
|
|
raise HTTPException(503, detail=exc_error) |
|
176
|
|
|
except UnrecoverableError as exc: |
|
177
|
|
|
exc_error = str(exc) |
|
178
|
|
|
log.error(exc_error) |
|
179
|
|
|
raise HTTPException(500, detail=exc_error) |
|
180
|
|
|
|
|
181
|
1 |
|
return JSONResponse(list(evcs.keys())) |
|
182
|
|
|
|
|
183
|
1 |
|
@rest("v1/evc") |
|
184
|
1 |
|
async def get_evcs(self, _request: Request) -> JSONResponse: |
|
185
|
|
|
"""REST to return the list of EVCs with INT enabled""" |
|
186
|
1 |
|
try: |
|
187
|
1 |
|
evcs = await api.get_evcs(**{"metadata.telemetry.enabled": "true"}) |
|
188
|
1 |
|
return JSONResponse(evcs) |
|
189
|
|
|
except RetryError as exc: |
|
190
|
|
|
exc_error = str(exc.last_attempt.exception()) |
|
191
|
|
|
log.error(exc_error) |
|
192
|
|
|
raise HTTPException(503, detail=exc_error) |
|
193
|
|
|
except UnrecoverableError as exc: |
|
194
|
|
|
exc_error = str(exc) |
|
195
|
|
|
log.error(exc_error) |
|
196
|
|
|
raise HTTPException(500, detail=exc_error) |
|
197
|
|
|
|
|
198
|
1 |
|
@rest("v1/evc/redeploy", methods=["PATCH"]) |
|
199
|
1 |
|
async def redeploy_telemetry(self, request: Request) -> JSONResponse: |
|
200
|
|
|
"""REST to redeploy INT on EVCs. |
|
201
|
|
|
|
|
202
|
|
|
If a list of evc_ids is empty, it'll redeploy on all INT EVCs. |
|
203
|
|
|
""" |
|
204
|
|
|
await avalidate_openapi_request(self.spec, request) |
|
205
|
|
|
|
|
206
|
|
|
try: |
|
207
|
|
|
content = await aget_json_or_400(request) |
|
208
|
|
|
evc_ids = content["evc_ids"] |
|
209
|
|
|
except (TypeError, KeyError): |
|
210
|
|
|
raise HTTPException(400, detail=f"Invalid payload: {content}") |
|
211
|
|
|
|
|
212
|
|
|
try: |
|
213
|
|
|
evcs = ( |
|
214
|
|
|
await api.get_evcs() |
|
215
|
|
|
if len(evc_ids) != 1 |
|
216
|
|
|
else await api.get_evc(evc_ids[0]) |
|
217
|
|
|
) |
|
218
|
|
|
except RetryError as exc: |
|
219
|
|
|
exc_error = str(exc.last_attempt.exception()) |
|
220
|
|
|
log.error(exc_error) |
|
221
|
|
|
raise HTTPException(503, detail=exc_error) |
|
222
|
|
|
|
|
223
|
|
|
if evc_ids: |
|
224
|
|
|
evcs = {evc_id: evcs.get(evc_id, {}) for evc_id in evc_ids} |
|
225
|
|
|
else: |
|
226
|
|
|
evcs = {k: v for k, v in evcs.items() if utils.has_int_enabled(v)} |
|
227
|
|
|
if not evcs: |
|
228
|
|
|
raise HTTPException(404, detail="There aren't INT EVCs to redeploy") |
|
229
|
|
|
|
|
230
|
|
|
try: |
|
231
|
|
|
await self.int_manager.redeploy_int(evcs) |
|
232
|
|
|
except (EVCNotFound, FlowsNotFound, ProxyPortNotFound) as exc: |
|
233
|
|
|
raise HTTPException(404, detail=str(exc)) |
|
234
|
|
|
except ProxyPortSameSourceIntraEVC as exc: |
|
235
|
|
|
raise HTTPException(409, detail=str(exc)) |
|
236
|
|
|
except RetryError as exc: |
|
237
|
|
|
exc_error = str(exc.last_attempt.exception()) |
|
238
|
|
|
log.error(exc_error) |
|
239
|
|
|
raise HTTPException(503, detail=exc_error) |
|
240
|
|
|
except UnrecoverableError as exc: |
|
241
|
|
|
exc_error = str(exc) |
|
242
|
|
|
log.error(exc_error) |
|
243
|
|
|
raise HTTPException(500, detail=exc_error) |
|
244
|
|
|
|
|
245
|
|
|
return JSONResponse(list(evcs.keys()), status_code=201) |
|
246
|
|
|
|
|
247
|
1 |
|
@rest("v1/sync") |
|
248
|
1 |
|
def sync_flows(self, _request: Request) -> JSONResponse: |
|
249
|
|
|
"""Endpoint to force the telemetry napp to search for INT flows and delete them |
|
250
|
|
|
accordingly to the evc metadata.""" |
|
251
|
|
|
|
|
252
|
|
|
# TODO |
|
253
|
|
|
# for evc_id in get_evcs_ids(): |
|
254
|
|
|
return JSONResponse("TBD") |
|
255
|
|
|
|
|
256
|
1 |
|
@rest("v1/evc/update") |
|
257
|
1 |
|
def update_evc(self, _request: Request) -> JSONResponse: |
|
258
|
|
|
"""If an EVC changed from unidirectional to bidirectional telemetry, |
|
259
|
|
|
make the change.""" |
|
260
|
|
|
return JSONResponse({}) |
|
261
|
|
|
|
|
262
|
1 |
|
@alisten_to("kytos/mef_eline.evcs_loaded") |
|
263
|
1 |
|
async def on_mef_eline_evcs_loaded(self, event: KytosEvent) -> None: |
|
264
|
|
|
"""Handle kytos/mef_eline.evcs_loaded.""" |
|
265
|
1 |
|
self.int_manager.load_uni_src_proxy_ports(event.content) |
|
266
|
|
|
|
|
267
|
1 |
|
@alisten_to("kytos/of_multi_table.enable_table") |
|
268
|
1 |
|
async def on_table_enabled(self, event): |
|
269
|
|
|
"""Handle of_multi_table.enable_table.""" |
|
270
|
1 |
|
table_group = event.content.get("telemetry_int", {}) |
|
271
|
1 |
|
if not table_group: |
|
272
|
|
|
return |
|
273
|
1 |
|
for group in table_group: |
|
274
|
1 |
|
if group not in settings.TABLE_GROUP_ALLOWED: |
|
275
|
1 |
|
log.error( |
|
276
|
|
|
f'The table group "{group}" is not allowed for ' |
|
277
|
|
|
f"telemetry_int. Allowed table groups are " |
|
278
|
|
|
f"{settings.TABLE_GROUP_ALLOWED}" |
|
279
|
|
|
) |
|
280
|
1 |
|
return |
|
281
|
1 |
|
self.int_manager.flow_builder.table_group.update(table_group) |
|
282
|
1 |
|
content = {"group_table": self.int_manager.flow_builder.table_group} |
|
283
|
1 |
|
event_out = KytosEvent(name="kytos/telemetry_int.enable_table", content=content) |
|
284
|
1 |
|
await self.controller.buffers.app.aput(event_out) |
|
285
|
|
|
|
|
286
|
1 |
|
@alisten_to("kytos/mef_eline.deleted") |
|
287
|
1 |
|
async def on_evc_deleted(self, event: KytosEvent) -> None: |
|
288
|
|
|
"""On EVC deleted.""" |
|
289
|
|
|
content = event.content |
|
290
|
|
|
if ( |
|
291
|
|
|
"metadata" in content |
|
292
|
|
|
and "telemetry" in content["metadata"] |
|
293
|
|
|
and content["metadata"]["telemetry"]["enabled"] |
|
294
|
|
|
): |
|
295
|
|
|
evc_id = content["evc_id"] |
|
296
|
|
|
log.info(f"Event mef_eline.deleted on EVC id: {evc_id}") |
|
297
|
|
|
await self.int_manager.disable_int({evc_id: content}, force=True) |
|
298
|
|
|
|
|
299
|
1 |
|
@alisten_to("kytos/topology.link_down") |
|
300
|
1 |
|
async def on_link_down(self, event): |
|
301
|
|
|
"""Handle topology.link_down.""" |
|
302
|
|
|
await self.int_manager.handle_pp_link_down(event.content["link"]) |
|
303
|
|
|
|
|
304
|
1 |
|
@alisten_to("kytos/topology.link_up") |
|
305
|
1 |
|
async def on_link_up(self, event): |
|
306
|
|
|
"""Handle topology.link_up.""" |
|
307
|
|
|
await self.int_manager.handle_pp_link_up(event.content["link"]) |
|
308
|
|
|
|
|
309
|
1 |
|
@alisten_to("kytos/flow_manager.flow.error") |
|
310
|
1 |
|
async def on_flow_mod_error(self, event: KytosEvent): |
|
311
|
|
|
"""On flow mod errors. |
|
312
|
|
|
|
|
313
|
|
|
Only OFPT_ERRORs will be handled, telemetry_int already uses force: true |
|
314
|
|
|
""" |
|
315
|
1 |
|
flow = event.content["flow"] |
|
316
|
1 |
|
if any( |
|
317
|
|
|
( |
|
318
|
|
|
event.content.get("error_exception"), |
|
319
|
|
|
event.content.get("error_command") != "add", |
|
320
|
|
|
flow.cookie >> 56 != settings.INT_COOKIE_PREFIX, |
|
321
|
|
|
) |
|
322
|
|
|
): |
|
323
|
|
|
return |
|
324
|
|
|
|
|
325
|
1 |
|
async with self._ofpt_error_lock: |
|
326
|
1 |
|
evc_id = utils.get_id_from_cookie(flow.cookie) |
|
327
|
1 |
|
evc = await api.get_evc(evc_id, exclude_archived=False) |
|
328
|
1 |
|
if ( |
|
329
|
|
|
not evc |
|
330
|
|
|
or "telemetry" not in evc[evc_id]["metadata"] |
|
331
|
|
|
or "enabled" not in evc[evc_id]["metadata"]["telemetry"] |
|
332
|
|
|
or not evc[evc_id]["metadata"]["telemetry"]["enabled"] |
|
333
|
|
|
): |
|
334
|
|
|
return |
|
335
|
|
|
|
|
336
|
1 |
|
metadata = { |
|
337
|
|
|
"telemetry": { |
|
338
|
|
|
"enabled": False, |
|
339
|
|
|
"status": "DOWN", |
|
340
|
|
|
"status_reason": ["ofpt_error"], |
|
341
|
|
|
"status_updated_at": datetime.utcnow().strftime( |
|
342
|
|
|
"%Y-%m-%dT%H:%M:%S" |
|
343
|
|
|
), |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
1 |
|
log.error( |
|
347
|
|
|
f"Disabling EVC({evc_id}) due to OFPT_ERROR, " |
|
348
|
|
|
f"error_type: {event.content.get('error_type')}, " |
|
349
|
|
|
f"error_code: {event.content.get('error_code')}, " |
|
350
|
|
|
f"flow: {flow.as_dict()} " |
|
351
|
|
|
) |
|
352
|
|
|
|
|
353
|
1 |
|
evcs = {evc_id: {evc_id: evc_id}} |
|
354
|
1 |
|
await self.int_manager.remove_int_flows(evcs, metadata, force=True) |
|
355
|
|
|
|
|
356
|
1 |
|
@alisten_to("kytos/topology.interfaces.metadata.removed") |
|
357
|
1 |
|
async def on_intf_metadata_removed(self, event: KytosEvent) -> None: |
|
358
|
|
|
"""On interface metadata removed.""" |
|
359
|
1 |
|
await self.int_manager.handle_pp_metadata_removed(event.content["interface"]) |
|
360
|
|
|
|
|
361
|
|
|
# Event-driven methods: future |
|
362
|
1 |
|
def listen_for_new_evcs(self): |
|
363
|
|
|
"""Change newly created EVC to INT-enabled EVC based on the metadata field |
|
364
|
|
|
(future)""" |
|
365
|
|
|
pass |
|
366
|
|
|
|
|
367
|
1 |
|
def listen_for_evc_change(self): |
|
368
|
|
|
"""Change newly created EVC to INT-enabled EVC based on the |
|
369
|
|
|
metadata field (future)""" |
|
370
|
|
|
pass |
|
371
|
|
|
|
|
372
|
1 |
|
def listen_for_path_changes(self): |
|
373
|
|
|
"""Change EVC's new path to INT-enabled EVC based on the metadata field |
|
374
|
|
|
when there is a path change. (future)""" |
|
375
|
|
|
pass |
|
376
|
|
|
|
|
377
|
1 |
|
def listen_for_topology_changes(self): |
|
378
|
|
|
"""If the topology changes, make sure it is not the loop ports. |
|
379
|
|
|
If so, update proxy ports""" |
|
380
|
|
|
pass |
|
381
|
|
|
|