Total Complexity | 235 |
Total Lines | 1353 |
Duplicated Lines | 3.1 % |
Coverage | 89.19% |
Changes | 0 |
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:
Complex classes like build.main 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 | # pylint: disable=protected-access, too-many-lines |
||
2 | """Main module of kytos/mef_eline Kytos Network Application. |
||
3 | |||
4 | NApp to provision circuits from user request. |
||
5 | """ |
||
6 | 1 | import pathlib |
|
7 | 1 | import time |
|
8 | 1 | import traceback |
|
9 | 1 | from collections import defaultdict |
|
10 | 1 | from contextlib import ExitStack |
|
11 | 1 | from copy import deepcopy |
|
12 | 1 | from threading import Lock |
|
13 | 1 | from typing import Optional |
|
14 | |||
15 | 1 | from pydantic import ValidationError |
|
16 | |||
17 | 1 | from kytos.core import KytosNApp, log, rest |
|
18 | 1 | from kytos.core.events import KytosEvent |
|
19 | 1 | from kytos.core.exceptions import KytosTagError |
|
20 | 1 | from kytos.core.helpers import (alisten_to, listen_to, load_spec, now, |
|
21 | validate_openapi) |
||
22 | 1 | from kytos.core.interface import TAG, UNI, TAGRange |
|
23 | 1 | from kytos.core.link import Link |
|
24 | 1 | from kytos.core.rest_api import (HTTPException, JSONResponse, Request, |
|
25 | get_json_or_400) |
||
26 | 1 | from kytos.core.tag_ranges import get_tag_ranges |
|
27 | 1 | from napps.kytos.mef_eline import controllers, settings |
|
28 | 1 | from napps.kytos.mef_eline.exceptions import (DisabledSwitch, |
|
29 | DuplicatedNoTagUNI, |
||
30 | FlowModException, InvalidPath) |
||
31 | 1 | from napps.kytos.mef_eline.models import (EVC, DynamicPathManager, EVCDeploy, |
|
32 | Path) |
||
33 | 1 | from napps.kytos.mef_eline.scheduler import CircuitSchedule, Scheduler |
|
34 | 1 | from napps.kytos.mef_eline.utils import (aemit_event, check_disabled_component, |
|
35 | emit_event, get_vlan_tags_and_masks, |
||
36 | map_evc_event_content, |
||
37 | merge_flow_dicts, prepare_delete_flow, |
||
38 | send_flow_mods_http) |
||
39 | |||
40 | |||
41 | # pylint: disable=too-many-public-methods |
||
42 | 1 | class Main(KytosNApp): |
|
43 | """Main class of amlight/mef_eline NApp. |
||
44 | |||
45 | This class is the entry point for this napp. |
||
46 | """ |
||
47 | |||
48 | 1 | spec = load_spec(pathlib.Path(__file__).parent / "openapi.yml") |
|
49 | |||
50 | 1 | def setup(self): |
|
51 | """Replace the '__init__' method for the KytosNApp subclass. |
||
52 | |||
53 | The setup method is automatically called by the controller when your |
||
54 | application is loaded. |
||
55 | |||
56 | So, if you have any setup routine, insert it here. |
||
57 | """ |
||
58 | # object used to scheduler circuit events |
||
59 | 1 | self.sched = Scheduler() |
|
60 | |||
61 | # object to save and load circuits |
||
62 | 1 | self.mongo_controller = self.get_eline_controller() |
|
63 | 1 | self.mongo_controller.bootstrap_indexes() |
|
64 | |||
65 | # set the controller that will manager the dynamic paths |
||
66 | 1 | DynamicPathManager.set_controller(self.controller) |
|
67 | |||
68 | # dictionary of EVCs created. It acts as a circuit buffer. |
||
69 | # Every create/update/delete must be synced to mongodb. |
||
70 | 1 | self.circuits = dict[str, EVC]() |
|
71 | |||
72 | 1 | self._intf_events = defaultdict(dict) |
|
73 | 1 | self._lock_interfaces = defaultdict(Lock) |
|
74 | 1 | self.table_group = {"epl": 0, "evpl": 0} |
|
75 | 1 | self._lock = Lock() |
|
76 | 1 | self.multi_evc_lock = Lock() |
|
77 | 1 | self.execute_as_loop(settings.DEPLOY_EVCS_INTERVAL) |
|
78 | |||
79 | 1 | self.load_all_evcs() |
|
80 | 1 | self._topology_updated_at = None |
|
81 | |||
82 | 1 | def get_evcs_by_svc_level(self, enable_filter: bool = True) -> list[EVC]: |
|
83 | """Get circuits sorted by desc service level and asc creation_time. |
||
84 | |||
85 | In the future, as more ops are offloaded it should be get from the DB. |
||
86 | """ |
||
87 | 1 | if enable_filter: |
|
88 | 1 | return sorted( |
|
89 | [circuit for circuit in self.circuits.values() |
||
90 | if circuit.is_enabled()], |
||
91 | key=lambda x: (-x.service_level, x.creation_time), |
||
92 | ) |
||
93 | 1 | return sorted(self.circuits.values(), |
|
94 | key=lambda x: (-x.service_level, x.creation_time)) |
||
95 | |||
96 | 1 | @staticmethod |
|
97 | 1 | def get_eline_controller(): |
|
98 | """Return the ELineController instance.""" |
||
99 | return controllers.ELineController() |
||
100 | |||
101 | 1 | def execute(self): |
|
102 | """Execute once when the napp is running.""" |
||
103 | 1 | if self._lock.locked(): |
|
104 | 1 | return |
|
105 | 1 | log.debug("Starting consistency routine") |
|
106 | 1 | with self._lock: |
|
107 | 1 | self.execute_consistency() |
|
108 | 1 | log.debug("Finished consistency routine") |
|
109 | |||
110 | 1 | def should_be_checked(self, circuit): |
|
111 | "Verify if the circuit meets the necessary conditions to be checked" |
||
112 | # pylint: disable=too-many-boolean-expressions |
||
113 | 1 | if ( |
|
114 | circuit.is_enabled() |
||
115 | and not circuit.is_active() |
||
116 | and not circuit.lock.locked() |
||
117 | and not circuit.has_recent_removed_flow() |
||
118 | and not circuit.is_recent_updated() |
||
119 | and circuit.are_unis_active() |
||
120 | # if a inter-switch EVC does not have current_path, it does not |
||
121 | # make sense to run sdntrace on it |
||
122 | and (circuit.is_intra_switch() or circuit.current_path) |
||
123 | ): |
||
124 | 1 | return True |
|
125 | return False |
||
126 | |||
127 | 1 | def execute_consistency(self): |
|
128 | """Execute consistency routine.""" |
||
129 | 1 | circuits_to_check = [] |
|
130 | 1 | for circuit in self.get_evcs_by_svc_level(enable_filter=False): |
|
131 | 1 | if self.should_be_checked(circuit): |
|
132 | 1 | circuits_to_check.append(circuit) |
|
133 | 1 | circuit.try_setup_failover_path(warn_if_not_path=False) |
|
134 | 1 | circuits_checked = EVCDeploy.check_list_traces(circuits_to_check) |
|
135 | 1 | for circuit in circuits_to_check: |
|
136 | 1 | is_checked = circuits_checked.get(circuit.id) |
|
137 | 1 | if is_checked: |
|
138 | 1 | circuit.execution_rounds = 0 |
|
139 | 1 | log.info(f"{circuit} enabled but inactive - activating") |
|
140 | 1 | with circuit.lock: |
|
141 | 1 | circuit.activate() |
|
142 | 1 | circuit.sync() |
|
143 | else: |
||
144 | 1 | circuit.execution_rounds += 1 |
|
145 | 1 | if circuit.execution_rounds > settings.WAIT_FOR_OLD_PATH: |
|
146 | 1 | log.info(f"{circuit} enabled but inactive - redeploy") |
|
147 | 1 | with circuit.lock: |
|
148 | 1 | circuit.deploy() |
|
149 | |||
150 | 1 | def shutdown(self): |
|
151 | """Execute when your napp is unloaded. |
||
152 | |||
153 | If you have some cleanup procedure, insert it here. |
||
154 | """ |
||
155 | |||
156 | 1 | @rest("/v2/evc/", methods=["GET"]) |
|
157 | 1 | def list_circuits(self, request: Request) -> JSONResponse: |
|
158 | """Endpoint to return circuits stored. |
||
159 | |||
160 | archive query arg if defined (not null) will be filtered |
||
161 | accordingly, by default only non archived evcs will be listed |
||
162 | """ |
||
163 | 1 | log.debug("list_circuits /v2/evc") |
|
164 | 1 | args = request.query_params |
|
165 | 1 | archived = args.get("archived", "false").lower() |
|
166 | 1 | args = {k: v for k, v in args.items() if k not in {"archived"}} |
|
167 | 1 | circuits = self.mongo_controller.get_circuits(archived=archived, |
|
168 | metadata=args) |
||
169 | 1 | circuits = circuits['circuits'] |
|
170 | 1 | return JSONResponse(circuits) |
|
171 | |||
172 | 1 | @rest("/v2/evc/schedule", methods=["GET"]) |
|
173 | 1 | def list_schedules(self, _request: Request) -> JSONResponse: |
|
174 | """Endpoint to return all schedules stored for all circuits. |
||
175 | |||
176 | Return a JSON with the following template: |
||
177 | [{"schedule_id": <schedule_id>, |
||
178 | "circuit_id": <circuit_id>, |
||
179 | "schedule": <schedule object>}] |
||
180 | """ |
||
181 | 1 | log.debug("list_schedules /v2/evc/schedule") |
|
182 | 1 | circuits = self.mongo_controller.get_circuits()['circuits'].values() |
|
183 | 1 | if not circuits: |
|
184 | 1 | result = {} |
|
185 | 1 | status = 200 |
|
186 | 1 | return JSONResponse(result, status_code=status) |
|
187 | |||
188 | 1 | result = [] |
|
189 | 1 | status = 200 |
|
190 | 1 | for circuit in circuits: |
|
191 | 1 | circuit_scheduler = circuit.get("circuit_scheduler") |
|
192 | 1 | if circuit_scheduler: |
|
193 | 1 | for scheduler in circuit_scheduler: |
|
194 | 1 | value = { |
|
195 | "schedule_id": scheduler.get("id"), |
||
196 | "circuit_id": circuit.get("id"), |
||
197 | "schedule": scheduler, |
||
198 | } |
||
199 | 1 | result.append(value) |
|
200 | |||
201 | 1 | log.debug("list_schedules result %s %s", result, status) |
|
202 | 1 | return JSONResponse(result, status_code=status) |
|
203 | |||
204 | 1 | @rest("/v2/evc/{circuit_id}", methods=["GET"]) |
|
205 | 1 | def get_circuit(self, request: Request) -> JSONResponse: |
|
206 | """Endpoint to return a circuit based on id.""" |
||
207 | 1 | circuit_id = request.path_params["circuit_id"] |
|
208 | 1 | log.debug("get_circuit /v2/evc/%s", circuit_id) |
|
209 | 1 | circuit = self.mongo_controller.get_circuit(circuit_id) |
|
210 | 1 | if not circuit: |
|
211 | 1 | result = f"circuit_id {circuit_id} not found" |
|
212 | 1 | log.debug("get_circuit result %s %s", result, 404) |
|
213 | 1 | raise HTTPException(404, detail=result) |
|
214 | 1 | status = 200 |
|
215 | 1 | log.debug("get_circuit result %s %s", circuit, status) |
|
216 | 1 | return JSONResponse(circuit, status_code=status) |
|
217 | |||
218 | # pylint: disable=too-many-branches, too-many-statements |
||
219 | 1 | @rest("/v2/evc/", methods=["POST"]) |
|
220 | 1 | @validate_openapi(spec) |
|
221 | 1 | def create_circuit(self, request: Request) -> JSONResponse: |
|
222 | """Try to create a new circuit. |
||
223 | |||
224 | Firstly, for EVPL: E-Line NApp verifies if UNI_A's requested C-VID and |
||
225 | UNI_Z's requested C-VID are available from the interfaces' pools. This |
||
226 | is checked when creating the UNI object. |
||
227 | |||
228 | Then, E-Line NApp requests a primary and a backup path to the |
||
229 | Pathfinder NApp using the attributes primary_links and backup_links |
||
230 | submitted via REST |
||
231 | |||
232 | # For each link composing paths in #3: |
||
233 | # - E-Line NApp requests a S-VID available from the link VLAN pool. |
||
234 | # - Using the S-VID obtained, generate abstract flow entries to be |
||
235 | # sent to FlowManager |
||
236 | |||
237 | Push abstract flow entries to FlowManager and FlowManager pushes |
||
238 | OpenFlow entries to datapaths |
||
239 | |||
240 | E-Line NApp generates an event to notify all Kytos NApps of a new EVC |
||
241 | creation |
||
242 | |||
243 | Finnaly, notify user of the status of its request. |
||
244 | """ |
||
245 | # Try to create the circuit object |
||
246 | 1 | log.debug("create_circuit /v2/evc/") |
|
247 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
248 | |||
249 | 1 | try: |
|
250 | 1 | evc = self._evc_from_dict(data) |
|
251 | 1 | except (ValueError, KytosTagError) as exception: |
|
252 | 1 | log.debug("create_circuit result %s %s", exception, 400) |
|
253 | 1 | raise HTTPException(400, detail=str(exception)) from exception |
|
254 | 1 | try: |
|
255 | 1 | check_disabled_component(evc.uni_a, evc.uni_z) |
|
256 | 1 | except DisabledSwitch as exception: |
|
257 | 1 | log.debug("create_circuit result %s %s", exception, 409) |
|
258 | 1 | raise HTTPException( |
|
259 | 409, |
||
260 | detail=f"Path is not valid: {exception}" |
||
261 | ) from exception |
||
262 | |||
263 | 1 | if evc.primary_path: |
|
264 | 1 | try: |
|
265 | 1 | evc.primary_path.is_valid( |
|
266 | evc.uni_a.interface.switch, |
||
267 | evc.uni_z.interface.switch, |
||
268 | bool(evc.circuit_scheduler), |
||
269 | ) |
||
270 | 1 | except InvalidPath as exception: |
|
271 | 1 | raise HTTPException( |
|
272 | 400, |
||
273 | detail=f"primary_path is not valid: {exception}" |
||
274 | ) from exception |
||
275 | 1 | if evc.backup_path: |
|
276 | 1 | try: |
|
277 | 1 | evc.backup_path.is_valid( |
|
278 | evc.uni_a.interface.switch, |
||
279 | evc.uni_z.interface.switch, |
||
280 | bool(evc.circuit_scheduler), |
||
281 | ) |
||
282 | 1 | except InvalidPath as exception: |
|
283 | 1 | raise HTTPException( |
|
284 | 400, |
||
285 | detail=f"backup_path is not valid: {exception}" |
||
286 | ) from exception |
||
287 | |||
288 | 1 | if not evc._tag_lists_equal(): |
|
289 | 1 | detail = "UNI_A and UNI_Z tag lists should be the same." |
|
290 | 1 | raise HTTPException(400, detail=detail) |
|
291 | |||
292 | 1 | try: |
|
293 | 1 | evc._validate_has_primary_or_dynamic() |
|
294 | 1 | except ValueError as exception: |
|
295 | 1 | raise HTTPException(400, detail=str(exception)) from exception |
|
296 | |||
297 | 1 | try: |
|
298 | 1 | self._check_no_tag_duplication(evc.id, evc.uni_a, evc.uni_z) |
|
299 | except DuplicatedNoTagUNI as exception: |
||
300 | log.debug("create_circuit result %s %s", exception, 409) |
||
301 | raise HTTPException(409, detail=str(exception)) from exception |
||
302 | |||
303 | 1 | try: |
|
304 | 1 | self._use_uni_tags(evc) |
|
305 | 1 | except KytosTagError as exception: |
|
306 | 1 | raise HTTPException(400, detail=str(exception)) from exception |
|
307 | |||
308 | # save circuit |
||
309 | 1 | try: |
|
310 | 1 | evc.sync() |
|
311 | except ValidationError as exception: |
||
312 | raise HTTPException(400, detail=str(exception)) from exception |
||
313 | |||
314 | # store circuit in dictionary |
||
315 | 1 | self.circuits[evc.id] = evc |
|
316 | |||
317 | # Schedule the circuit deploy |
||
318 | 1 | self.sched.add(evc) |
|
319 | |||
320 | # Circuit has no schedule, deploy now |
||
321 | 1 | deployed = False |
|
322 | 1 | if not evc.circuit_scheduler: |
|
323 | 1 | with evc.lock: |
|
324 | 1 | deployed = evc.deploy() |
|
325 | |||
326 | # Notify users |
||
327 | 1 | result = {"circuit_id": evc.id, "deployed": deployed} |
|
328 | 1 | status = 201 |
|
329 | 1 | log.debug("create_circuit result %s %s", result, status) |
|
330 | 1 | emit_event(self.controller, name="created", |
|
331 | content=map_evc_event_content(evc)) |
||
332 | 1 | return JSONResponse(result, status_code=status) |
|
333 | |||
334 | 1 | @staticmethod |
|
335 | 1 | def _use_uni_tags(evc): |
|
336 | 1 | uni_a = evc.uni_a |
|
337 | 1 | evc._use_uni_vlan(uni_a) |
|
338 | 1 | try: |
|
339 | 1 | uni_z = evc.uni_z |
|
340 | 1 | evc._use_uni_vlan(uni_z) |
|
341 | 1 | except KytosTagError as err: |
|
342 | 1 | evc.make_uni_vlan_available(uni_a) |
|
343 | 1 | raise err |
|
344 | |||
345 | 1 | @listen_to('kytos/flow_manager.flow.removed') |
|
346 | 1 | def on_flow_delete(self, event): |
|
347 | """Capture delete messages to keep track when flows got removed.""" |
||
348 | self.handle_flow_delete(event) |
||
349 | |||
350 | 1 | def handle_flow_delete(self, event): |
|
351 | """Keep track when the EVC got flows removed by deriving its cookie.""" |
||
352 | 1 | flow = event.content["flow"] |
|
353 | 1 | evc = self.circuits.get(EVC.get_id_from_cookie(flow.cookie)) |
|
354 | 1 | if evc: |
|
355 | 1 | log.debug("Flow removed in EVC %s", evc.id) |
|
356 | 1 | evc.set_flow_removed_at() |
|
357 | |||
358 | 1 | @rest("/v2/evc/{circuit_id}", methods=["PATCH"]) |
|
359 | 1 | @validate_openapi(spec) |
|
360 | 1 | def update(self, request: Request) -> JSONResponse: |
|
361 | """Update a circuit based on payload. |
||
362 | |||
363 | The EVC attributes (creation_time, active, current_path, |
||
364 | failover_path, _id, archived) can't be updated. |
||
365 | """ |
||
366 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
367 | 1 | circuit_id = request.path_params["circuit_id"] |
|
368 | 1 | log.debug("update /v2/evc/%s", circuit_id) |
|
369 | 1 | try: |
|
370 | 1 | evc = self.circuits[circuit_id] |
|
371 | 1 | except KeyError: |
|
372 | 1 | result = f"circuit_id {circuit_id} not found" |
|
373 | 1 | log.debug("update result %s %s", result, 404) |
|
374 | 1 | raise HTTPException(404, detail=result) from KeyError |
|
375 | |||
376 | 1 | try: |
|
377 | 1 | updated_data = self._evc_dict_with_instances(data) |
|
378 | 1 | self._check_no_tag_duplication( |
|
379 | circuit_id, updated_data.get("uni_a"), |
||
380 | updated_data.get("uni_z") |
||
381 | ) |
||
382 | 1 | enable, redeploy = evc.update(**updated_data) |
|
383 | 1 | except (ValueError, KytosTagError, ValidationError) as exception: |
|
384 | 1 | log.debug("update result %s %s", exception, 400) |
|
385 | 1 | raise HTTPException(400, detail=str(exception)) from exception |
|
386 | 1 | except DuplicatedNoTagUNI as exception: |
|
387 | log.debug("update result %s %s", exception, 409) |
||
388 | raise HTTPException(409, detail=str(exception)) from exception |
||
389 | 1 | except DisabledSwitch as exception: |
|
390 | 1 | log.debug("update result %s %s", exception, 409) |
|
391 | 1 | raise HTTPException( |
|
392 | 409, |
||
393 | detail=f"Path is not valid: {exception}" |
||
394 | ) from exception |
||
395 | 1 | redeployed = False |
|
396 | 1 | if evc.is_active(): |
|
397 | if enable is False: # disable if active |
||
398 | with evc.lock: |
||
399 | evc.remove() |
||
400 | elif redeploy is not None: # redeploy if active |
||
401 | with evc.lock: |
||
402 | evc.remove() |
||
403 | redeployed = evc.deploy() |
||
404 | else: |
||
405 | 1 | if enable is True: # enable if inactive |
|
406 | 1 | with evc.lock: |
|
407 | 1 | redeployed = evc.deploy() |
|
408 | 1 | elif evc.is_enabled() and redeploy: |
|
409 | 1 | with evc.lock: |
|
410 | 1 | evc.remove() |
|
411 | 1 | redeployed = evc.deploy() |
|
412 | 1 | result = {evc.id: evc.as_dict(), 'redeployed': redeployed} |
|
413 | 1 | status = 200 |
|
414 | |||
415 | 1 | log.debug("update result %s %s", result, status) |
|
416 | 1 | emit_event(self.controller, "updated", |
|
417 | content=map_evc_event_content(evc, **data)) |
||
418 | 1 | return JSONResponse(result, status_code=status) |
|
419 | |||
420 | 1 | @rest("/v2/evc/{circuit_id}", methods=["DELETE"]) |
|
421 | 1 | def delete_circuit(self, request: Request) -> JSONResponse: |
|
422 | """Remove a circuit. |
||
423 | |||
424 | First, the flows are removed from the switches, and then the EVC is |
||
425 | disabled. |
||
426 | """ |
||
427 | 1 | circuit_id = request.path_params["circuit_id"] |
|
428 | 1 | log.debug("delete_circuit /v2/evc/%s", circuit_id) |
|
429 | 1 | try: |
|
430 | 1 | evc = self.circuits.pop(circuit_id) |
|
431 | 1 | except KeyError: |
|
432 | 1 | result = f"circuit_id {circuit_id} not found" |
|
433 | 1 | log.debug("delete_circuit result %s %s", result, 404) |
|
434 | 1 | raise HTTPException(404, detail=result) from KeyError |
|
435 | 1 | log.info("Removing %s", evc) |
|
436 | |||
437 | 1 | with evc.lock: |
|
438 | 1 | if not evc.archived: |
|
439 | 1 | evc.deactivate() |
|
440 | 1 | evc.disable() |
|
441 | 1 | self.sched.remove(evc) |
|
442 | 1 | evc.remove_current_flows(sync=False) |
|
443 | 1 | evc.remove_failover_flows(sync=False) |
|
444 | 1 | evc.archive() |
|
445 | 1 | evc.remove_uni_tags() |
|
446 | 1 | evc.sync() |
|
447 | 1 | emit_event( |
|
448 | self.controller, "deleted", |
||
449 | content=map_evc_event_content(evc) |
||
450 | ) |
||
451 | |||
452 | 1 | log.info("EVC removed. %s", evc) |
|
453 | 1 | result = {"response": f"Circuit {circuit_id} removed"} |
|
454 | 1 | status = 200 |
|
455 | 1 | log.debug("delete_circuit result %s %s", result, status) |
|
456 | |||
457 | 1 | return JSONResponse(result, status_code=status) |
|
458 | |||
459 | 1 | @rest("/v2/evc/{circuit_id}/metadata", methods=["GET"]) |
|
460 | 1 | def get_metadata(self, request: Request) -> JSONResponse: |
|
461 | """Get metadata from an EVC.""" |
||
462 | 1 | circuit_id = request.path_params["circuit_id"] |
|
463 | 1 | try: |
|
464 | 1 | return ( |
|
465 | JSONResponse({"metadata": self.circuits[circuit_id].metadata}) |
||
466 | ) |
||
467 | except KeyError as error: |
||
468 | raise HTTPException( |
||
469 | 404, |
||
470 | detail=f"circuit_id {circuit_id} not found." |
||
471 | ) from error |
||
472 | |||
473 | 1 | View Code Duplication | @rest("/v2/evc/metadata", methods=["POST"]) |
|
|||
474 | 1 | @validate_openapi(spec) |
|
475 | 1 | def bulk_add_metadata(self, request: Request) -> JSONResponse: |
|
476 | """Add metadata to a bulk of EVCs.""" |
||
477 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
478 | 1 | circuit_ids = data.pop("circuit_ids") |
|
479 | |||
480 | 1 | self.mongo_controller.update_evcs_metadata(circuit_ids, data, "add") |
|
481 | |||
482 | 1 | fail_evcs = [] |
|
483 | 1 | for _id in circuit_ids: |
|
484 | 1 | try: |
|
485 | 1 | evc = self.circuits[_id] |
|
486 | 1 | evc.extend_metadata(data) |
|
487 | 1 | except KeyError: |
|
488 | 1 | fail_evcs.append(_id) |
|
489 | |||
490 | 1 | if fail_evcs: |
|
491 | 1 | raise HTTPException(404, detail=fail_evcs) |
|
492 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
493 | |||
494 | 1 | @rest("/v2/evc/{circuit_id}/metadata", methods=["POST"]) |
|
495 | 1 | @validate_openapi(spec) |
|
496 | 1 | def add_metadata(self, request: Request) -> JSONResponse: |
|
497 | """Add metadata to an EVC.""" |
||
498 | 1 | circuit_id = request.path_params["circuit_id"] |
|
499 | 1 | metadata = get_json_or_400(request, self.controller.loop) |
|
500 | 1 | if not isinstance(metadata, dict): |
|
501 | raise HTTPException(400, f"Invalid metadata value: {metadata}") |
||
502 | 1 | try: |
|
503 | 1 | evc = self.circuits[circuit_id] |
|
504 | 1 | except KeyError as error: |
|
505 | 1 | raise HTTPException( |
|
506 | 404, |
||
507 | detail=f"circuit_id {circuit_id} not found." |
||
508 | ) from error |
||
509 | |||
510 | 1 | evc.extend_metadata(metadata) |
|
511 | 1 | evc.sync() |
|
512 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
513 | |||
514 | 1 | View Code Duplication | @rest("/v2/evc/metadata/{key}", methods=["DELETE"]) |
515 | 1 | @validate_openapi(spec) |
|
516 | 1 | def bulk_delete_metadata(self, request: Request) -> JSONResponse: |
|
517 | """Delete metada from a bulk of EVCs""" |
||
518 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
519 | 1 | key = request.path_params["key"] |
|
520 | 1 | circuit_ids = data.pop("circuit_ids") |
|
521 | 1 | self.mongo_controller.update_evcs_metadata( |
|
522 | circuit_ids, {key: ""}, "del" |
||
523 | ) |
||
524 | |||
525 | 1 | fail_evcs = [] |
|
526 | 1 | for _id in circuit_ids: |
|
527 | 1 | try: |
|
528 | 1 | evc = self.circuits[_id] |
|
529 | 1 | evc.remove_metadata(key) |
|
530 | 1 | except KeyError: |
|
531 | 1 | fail_evcs.append(_id) |
|
532 | |||
533 | 1 | if fail_evcs: |
|
534 | 1 | raise HTTPException(404, detail=fail_evcs) |
|
535 | 1 | return JSONResponse("Operation successful") |
|
536 | |||
537 | 1 | @rest("/v2/evc/{circuit_id}/metadata/{key}", methods=["DELETE"]) |
|
538 | 1 | def delete_metadata(self, request: Request) -> JSONResponse: |
|
539 | """Delete metadata from an EVC.""" |
||
540 | 1 | circuit_id = request.path_params["circuit_id"] |
|
541 | 1 | key = request.path_params["key"] |
|
542 | 1 | try: |
|
543 | 1 | evc = self.circuits[circuit_id] |
|
544 | 1 | except KeyError as error: |
|
545 | 1 | raise HTTPException( |
|
546 | 404, |
||
547 | detail=f"circuit_id {circuit_id} not found." |
||
548 | ) from error |
||
549 | |||
550 | 1 | evc.remove_metadata(key) |
|
551 | 1 | evc.sync() |
|
552 | 1 | return JSONResponse("Operation successful") |
|
553 | |||
554 | 1 | @rest("/v2/evc/{circuit_id}/redeploy", methods=["PATCH"]) |
|
555 | 1 | def redeploy(self, request: Request) -> JSONResponse: |
|
556 | """Endpoint to force the redeployment of an EVC.""" |
||
557 | 1 | circuit_id = request.path_params["circuit_id"] |
|
558 | 1 | try_avoid_same_s_vlan = request.query_params.get( |
|
559 | "try_avoid_same_s_vlan", "true" |
||
560 | ) |
||
561 | 1 | try_avoid_same_s_vlan = try_avoid_same_s_vlan.lower() |
|
562 | 1 | if try_avoid_same_s_vlan not in {"true", "false"}: |
|
563 | msg = "Parameter try_avoid_same_s_vlan has an invalid value." |
||
564 | raise HTTPException(400, detail=msg) |
||
565 | 1 | log.debug("redeploy /v2/evc/%s/redeploy", circuit_id) |
|
566 | 1 | try: |
|
567 | 1 | evc = self.circuits[circuit_id] |
|
568 | 1 | except KeyError: |
|
569 | 1 | raise HTTPException( |
|
570 | 404, |
||
571 | detail=f"circuit_id {circuit_id} not found" |
||
572 | ) from KeyError |
||
573 | 1 | deployed = False |
|
574 | 1 | if evc.is_enabled(): |
|
575 | 1 | with evc.lock: |
|
576 | 1 | path_dict = evc.remove_current_flows( |
|
577 | sync=False, |
||
578 | return_path=try_avoid_same_s_vlan == "true" |
||
579 | ) |
||
580 | 1 | evc.remove_failover_flows(sync=True) |
|
581 | 1 | deployed = evc.deploy(path_dict) |
|
582 | 1 | if deployed: |
|
583 | 1 | result = {"response": f"Circuit {circuit_id} redeploy received."} |
|
584 | 1 | status = 202 |
|
585 | else: |
||
586 | 1 | result = { |
|
587 | "response": f"Circuit {circuit_id} is disabled." |
||
588 | } |
||
589 | 1 | status = 409 |
|
590 | |||
591 | 1 | return JSONResponse(result, status_code=status) |
|
592 | |||
593 | 1 | @rest("/v2/evc/schedule/", methods=["POST"]) |
|
594 | 1 | @validate_openapi(spec) |
|
595 | 1 | def create_schedule(self, request: Request) -> JSONResponse: |
|
596 | """ |
||
597 | Create a new schedule for a given circuit. |
||
598 | |||
599 | This service do no check if there are conflicts with another schedule. |
||
600 | Payload example: |
||
601 | { |
||
602 | "circuit_id":"aa:bb:cc", |
||
603 | "schedule": { |
||
604 | "date": "2019-08-07T14:52:10.967Z", |
||
605 | "interval": "string", |
||
606 | "frequency": "1 * * * *", |
||
607 | "action": "create" |
||
608 | } |
||
609 | } |
||
610 | """ |
||
611 | 1 | log.debug("create_schedule /v2/evc/schedule/") |
|
612 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
613 | 1 | circuit_id = data["circuit_id"] |
|
614 | 1 | schedule_data = data["schedule"] |
|
615 | |||
616 | # Get EVC from circuits buffer |
||
617 | 1 | circuits = self._get_circuits_buffer() |
|
618 | |||
619 | # get the circuit |
||
620 | 1 | evc = circuits.get(circuit_id) |
|
621 | |||
622 | # get the circuit |
||
623 | 1 | if not evc: |
|
624 | 1 | result = f"circuit_id {circuit_id} not found" |
|
625 | 1 | log.debug("create_schedule result %s %s", result, 404) |
|
626 | 1 | raise HTTPException(404, detail=result) |
|
627 | |||
628 | # new schedule from dict |
||
629 | 1 | new_schedule = CircuitSchedule.from_dict(schedule_data) |
|
630 | |||
631 | # If there is no schedule, create the list |
||
632 | 1 | if not evc.circuit_scheduler: |
|
633 | 1 | evc.circuit_scheduler = [] |
|
634 | |||
635 | # Add the new schedule |
||
636 | 1 | evc.circuit_scheduler.append(new_schedule) |
|
637 | |||
638 | # Add schedule job |
||
639 | 1 | self.sched.add_circuit_job(evc, new_schedule) |
|
640 | |||
641 | # save circuit to mongodb |
||
642 | 1 | evc.sync() |
|
643 | |||
644 | 1 | result = new_schedule.as_dict() |
|
645 | 1 | status = 201 |
|
646 | |||
647 | 1 | log.debug("create_schedule result %s %s", result, status) |
|
648 | 1 | return JSONResponse(result, status_code=status) |
|
649 | |||
650 | 1 | @rest("/v2/evc/schedule/{schedule_id}", methods=["PATCH"]) |
|
651 | 1 | @validate_openapi(spec) |
|
652 | 1 | def update_schedule(self, request: Request) -> JSONResponse: |
|
653 | """Update a schedule. |
||
654 | |||
655 | Change all attributes from the given schedule from a EVC circuit. |
||
656 | The schedule ID is preserved as default. |
||
657 | Payload example: |
||
658 | { |
||
659 | "date": "2019-08-07T14:52:10.967Z", |
||
660 | "interval": "string", |
||
661 | "frequency": "1 * * *", |
||
662 | "action": "create" |
||
663 | } |
||
664 | """ |
||
665 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
666 | 1 | schedule_id = request.path_params["schedule_id"] |
|
667 | 1 | log.debug("update_schedule /v2/evc/schedule/%s", schedule_id) |
|
668 | |||
669 | # Try to find a circuit schedule |
||
670 | 1 | evc, found_schedule = self._find_evc_by_schedule_id(schedule_id) |
|
671 | |||
672 | # Can not modify circuits deleted and archived |
||
673 | 1 | if not found_schedule: |
|
674 | 1 | result = f"schedule_id {schedule_id} not found" |
|
675 | 1 | log.debug("update_schedule result %s %s", result, 404) |
|
676 | 1 | raise HTTPException(404, detail=result) |
|
677 | |||
678 | 1 | new_schedule = CircuitSchedule.from_dict(data) |
|
679 | 1 | new_schedule.id = found_schedule.id |
|
680 | # Remove the old schedule |
||
681 | 1 | evc.circuit_scheduler.remove(found_schedule) |
|
682 | # Append the modified schedule |
||
683 | 1 | evc.circuit_scheduler.append(new_schedule) |
|
684 | |||
685 | # Cancel all schedule jobs |
||
686 | 1 | self.sched.cancel_job(found_schedule.id) |
|
687 | # Add the new circuit schedule |
||
688 | 1 | self.sched.add_circuit_job(evc, new_schedule) |
|
689 | # Save EVC to mongodb |
||
690 | 1 | evc.sync() |
|
691 | |||
692 | 1 | result = new_schedule.as_dict() |
|
693 | 1 | status = 200 |
|
694 | |||
695 | 1 | log.debug("update_schedule result %s %s", result, status) |
|
696 | 1 | return JSONResponse(result, status_code=status) |
|
697 | |||
698 | 1 | @rest("/v2/evc/schedule/{schedule_id}", methods=["DELETE"]) |
|
699 | 1 | def delete_schedule(self, request: Request) -> JSONResponse: |
|
700 | """Remove a circuit schedule. |
||
701 | |||
702 | Remove the Schedule from EVC. |
||
703 | Remove the Schedule from cron job. |
||
704 | Save the EVC to the Storehouse. |
||
705 | """ |
||
706 | 1 | schedule_id = request.path_params["schedule_id"] |
|
707 | 1 | log.debug("delete_schedule /v2/evc/schedule/%s", schedule_id) |
|
708 | 1 | evc, found_schedule = self._find_evc_by_schedule_id(schedule_id) |
|
709 | |||
710 | # Can not modify circuits deleted and archived |
||
711 | 1 | if not found_schedule: |
|
712 | 1 | result = f"schedule_id {schedule_id} not found" |
|
713 | 1 | log.debug("delete_schedule result %s %s", result, 404) |
|
714 | 1 | raise HTTPException(404, detail=result) |
|
715 | |||
716 | # Remove the old schedule |
||
717 | 1 | evc.circuit_scheduler.remove(found_schedule) |
|
718 | |||
719 | # Cancel all schedule jobs |
||
720 | 1 | self.sched.cancel_job(found_schedule.id) |
|
721 | # Save EVC to mongodb |
||
722 | 1 | evc.sync() |
|
723 | |||
724 | 1 | result = "Schedule removed" |
|
725 | 1 | status = 200 |
|
726 | |||
727 | 1 | log.debug("delete_schedule result %s %s", result, status) |
|
728 | 1 | return JSONResponse(result, status_code=status) |
|
729 | |||
730 | 1 | def _check_no_tag_duplication( |
|
731 | self, |
||
732 | evc_id: str, |
||
733 | uni_a: Optional[UNI] = None, |
||
734 | uni_z: Optional[UNI] = None |
||
735 | ): |
||
736 | """Check if the given EVC has UNIs with no tag and if these are |
||
737 | duplicated. Raise DuplicatedNoTagUNI if duplication is found. |
||
738 | Args: |
||
739 | evc (dict): EVC to be analyzed. |
||
740 | """ |
||
741 | |||
742 | # No UNIs |
||
743 | 1 | if not (uni_a or uni_z): |
|
744 | 1 | return |
|
745 | |||
746 | 1 | if (not (uni_a and not uni_a.user_tag) and |
|
747 | not (uni_z and not uni_z.user_tag)): |
||
748 | 1 | return |
|
749 | 1 | for circuit in self.circuits.copy().values(): |
|
750 | 1 | if (not circuit.archived and circuit._id != evc_id): |
|
751 | 1 | if uni_a and uni_a.user_tag is None: |
|
752 | 1 | circuit.check_no_tag_duplicate(uni_a) |
|
753 | 1 | if uni_z and uni_z.user_tag is None: |
|
754 | 1 | circuit.check_no_tag_duplicate(uni_z) |
|
755 | |||
756 | 1 | @listen_to("kytos/topology.link_up") |
|
757 | 1 | def on_link_up(self, event): |
|
758 | """Change circuit when link is up or end_maintenance.""" |
||
759 | self.handle_link_up(event) |
||
760 | |||
761 | 1 | def handle_link_up(self, event): |
|
762 | """Change circuit when link is up or end_maintenance.""" |
||
763 | 1 | log.info("Event handle_link_up %s", event.content["link"]) |
|
764 | 1 | for evc in self.get_evcs_by_svc_level(): |
|
765 | 1 | if evc.is_enabled() and not evc.archived: |
|
766 | 1 | with evc.lock: |
|
767 | 1 | evc.handle_link_up(event.content["link"]) |
|
768 | |||
769 | # Possibly replace this with interruptions? |
||
770 | 1 | @listen_to( |
|
771 | '.*.switch.interface.(link_up|link_down|created|deleted)' |
||
772 | ) |
||
773 | 1 | def on_interface_link_change(self, event: KytosEvent): |
|
774 | """ |
||
775 | Handler for interface link_up and link_down events. |
||
776 | """ |
||
777 | self.handle_on_interface_link_change(event) |
||
778 | |||
779 | 1 | def handle_on_interface_link_change(self, event: KytosEvent): |
|
780 | """ |
||
781 | Handler to sort interface events {link_(up, down), create, deleted} |
||
782 | |||
783 | To avoid multiple database updated (link flap): |
||
784 | Every interface is identfied and processed in parallel. |
||
785 | Once an interface event is received a time is started. |
||
786 | While time is running self._intf_events will be updated. |
||
787 | After time has passed last received event will be processed. |
||
788 | """ |
||
789 | 1 | iface = event.content.get("interface") |
|
790 | 1 | with self._lock_interfaces[iface.id]: |
|
791 | 1 | _now = event.timestamp |
|
792 | # Return out of order events |
||
793 | 1 | if ( |
|
794 | iface.id in self._intf_events |
||
795 | and self._intf_events[iface.id]["event"].timestamp > _now |
||
796 | ): |
||
797 | 1 | return |
|
798 | 1 | self._intf_events[iface.id].update({"event": event}) |
|
799 | 1 | if "last_acquired" in self._intf_events[iface.id]: |
|
800 | 1 | return |
|
801 | 1 | self._intf_events[iface.id].update({"last_acquired": now()}) |
|
802 | 1 | time.sleep(settings.UNI_STATE_CHANGE_DELAY) |
|
803 | 1 | with self._lock_interfaces[iface.id]: |
|
804 | 1 | event = self._intf_events[iface.id]["event"] |
|
805 | 1 | self._intf_events[iface.id].pop('last_acquired', None) |
|
806 | 1 | _, _, event_type = event.name.rpartition('.') |
|
807 | 1 | if event_type in ('link_up', 'created'): |
|
808 | 1 | self.handle_interface_link_up(iface) |
|
809 | 1 | elif event_type in ('link_down', 'deleted'): |
|
810 | 1 | self.handle_interface_link_down(iface) |
|
811 | |||
812 | 1 | def handle_interface_link_up(self, interface): |
|
813 | """ |
||
814 | Handler for interface link_up events |
||
815 | """ |
||
816 | log.info("Event handle_interface_link_up %s", interface) |
||
817 | for evc in self.get_evcs_by_svc_level(): |
||
818 | with evc.lock: |
||
819 | evc.handle_interface_link_up( |
||
820 | interface |
||
821 | ) |
||
822 | |||
823 | 1 | def handle_interface_link_down(self, interface): |
|
824 | """ |
||
825 | Handler for interface link_down events |
||
826 | """ |
||
827 | log.info("Event handle_interface_link_down %s", interface) |
||
828 | for evc in self.get_evcs_by_svc_level(): |
||
829 | with evc.lock: |
||
830 | evc.handle_interface_link_down( |
||
831 | interface |
||
832 | ) |
||
833 | |||
834 | 1 | @listen_to("kytos/topology.link_down", pool="dynamic_single") |
|
835 | 1 | def on_link_down(self, event): |
|
836 | """Change circuit when link is down or under_mantenance.""" |
||
837 | self.handle_link_down(event) |
||
838 | |||
839 | 1 | def prepare_swap_to_failover_flow(self, evc: EVC): |
|
840 | """Prepare an evc for switching to failover.""" |
||
841 | install_flows = {} |
||
842 | try: |
||
843 | install_flows = evc.get_failover_flows() |
||
844 | # pylint: disable=broad-except |
||
845 | except Exception: |
||
846 | err = traceback.format_exc().replace("\n", ", ") |
||
847 | log.error( |
||
848 | "Ignore Failover path for " |
||
849 | f"{evc} due to error: {err}" |
||
850 | ) |
||
851 | return install_flows |
||
852 | |||
853 | 1 | def prepare_swap_to_failover_event(self, evc: EVC, install_flow): |
|
854 | """Prepare event contents for swap to failover.""" |
||
855 | return map_evc_event_content( |
||
856 | evc, |
||
857 | flows=deepcopy(install_flow) |
||
858 | ) |
||
859 | |||
860 | 1 | def execute_swap_to_failover( |
|
861 | self, |
||
862 | evcs: list[EVC] |
||
863 | ) -> tuple[list[EVC], list[EVC]]: |
||
864 | """Process changes needed to commit a swap to failover.""" |
||
865 | 1 | event_contents = {} |
|
866 | 1 | install_flows = {} |
|
867 | 1 | swapped_evcs = list[EVC]() |
|
868 | 1 | not_swapped_evcs = list[EVC]() |
|
869 | |||
870 | 1 | for evc in evcs: |
|
871 | 1 | install_flow = self.prepare_swap_to_failover_flow(evc) |
|
872 | 1 | if install_flow: |
|
873 | 1 | install_flows = merge_flow_dicts(install_flows, install_flow) |
|
874 | 1 | event_contents[evc.id] =\ |
|
875 | self.prepare_swap_to_failover_event(evc, install_flow) |
||
876 | 1 | swapped_evcs.append(evc) |
|
877 | else: |
||
878 | 1 | not_swapped_evcs.append(evc) |
|
879 | |||
880 | 1 | try: |
|
881 | 1 | send_flow_mods_http( |
|
882 | install_flows, |
||
883 | "install" |
||
884 | ) |
||
885 | 1 | for evc in swapped_evcs: |
|
886 | 1 | temp_path = evc.current_path |
|
887 | 1 | evc.current_path = evc.failover_path |
|
888 | 1 | evc.failover_path = temp_path |
|
889 | 1 | emit_event( |
|
890 | self.controller, "failover_link_down", |
||
891 | content=deepcopy(event_contents) |
||
892 | ) |
||
893 | 1 | return swapped_evcs, not_swapped_evcs |
|
894 | 1 | except FlowModException as exc: |
|
895 | 1 | log.error(f"Fail to install failover flows for {evcs}: {exc}") |
|
896 | 1 | return [], [*swapped_evcs, *not_swapped_evcs] |
|
897 | |||
898 | 1 | def prepare_clear_failover_flow(self, evc: EVC): |
|
899 | """Prepare an evc for clearing the old path.""" |
||
900 | del_flows = {} |
||
901 | try: |
||
902 | del_flows = prepare_delete_flow( |
||
903 | merge_flow_dicts( |
||
904 | evc._prepare_uni_flows(evc.failover_path, skip_in=True), |
||
905 | evc._prepare_nni_flows(evc.failover_path) |
||
906 | ) |
||
907 | ) |
||
908 | # pylint: disable=broad-except |
||
909 | except Exception: |
||
910 | err = traceback.format_exc().replace("\n", ", ") |
||
911 | log.error(f"Fail to remove {evc} old_path: {err}") |
||
912 | return del_flows |
||
913 | |||
914 | 1 | def prepare_clear_failover_event(self, evc: EVC, delete_flow): |
|
915 | """Prepare event contents for clearing failover.""" |
||
916 | return map_evc_event_content( |
||
917 | evc, |
||
918 | current_path=evc.current_path.as_dict(), |
||
919 | removed_flows=deepcopy(delete_flow) |
||
920 | ) |
||
921 | |||
922 | 1 | def execute_clear_failover( |
|
923 | self, |
||
924 | evcs: list[EVC] |
||
925 | ) -> tuple[list[EVC], list[EVC]]: |
||
926 | """Process changes needed to commit clearing the failover path""" |
||
927 | 1 | event_contents = {} |
|
928 | 1 | delete_flows = {} |
|
929 | 1 | cleared_evcs = list[EVC]() |
|
930 | 1 | not_cleared_evcs = list[EVC]() |
|
931 | |||
932 | 1 | for evc in evcs: |
|
933 | 1 | delete_flow = self.prepare_clear_failover_flow(evc) |
|
934 | 1 | if delete_flow: |
|
935 | 1 | delete_flows = merge_flow_dicts(delete_flows, delete_flow) |
|
936 | 1 | event_contents[evc.id] =\ |
|
937 | self.prepare_clear_failover_event(evc, delete_flow) |
||
938 | 1 | cleared_evcs.append(evc) |
|
939 | else: |
||
940 | 1 | not_cleared_evcs.append(evc) |
|
941 | |||
942 | 1 | try: |
|
943 | 1 | send_flow_mods_http( |
|
944 | delete_flows, |
||
945 | 'delete' |
||
946 | ) |
||
947 | 1 | for evc in cleared_evcs: |
|
948 | 1 | evc.failover_path.make_vlans_available(self.controller) |
|
949 | 1 | evc.failover_path = Path([]) |
|
950 | 1 | emit_event( |
|
951 | self.controller, |
||
952 | "failover_old_path", |
||
953 | content=event_contents |
||
954 | ) |
||
955 | 1 | return cleared_evcs, not_cleared_evcs |
|
956 | 1 | except FlowModException as exc: |
|
957 | 1 | log.error(f"Failed to delete failover flows for {evcs}: {exc}") |
|
958 | 1 | return [], [*cleared_evcs, *not_cleared_evcs] |
|
959 | |||
960 | 1 | def prepare_undeploy_flow(self, evc: EVC): |
|
961 | """Prepare an evc for undeploying.""" |
||
962 | del_flows = {} |
||
963 | try: |
||
964 | del_flows = prepare_delete_flow( |
||
965 | merge_flow_dicts( |
||
966 | evc._prepare_uni_flows(evc.current_path, skip_in=True), |
||
967 | evc._prepare_nni_flows(evc.current_path), |
||
968 | evc._prepare_nni_flows(evc.failover_path) |
||
969 | ) |
||
970 | ) |
||
971 | # pylint: disable=broad-except |
||
972 | except Exception: |
||
973 | err = traceback.format_exc().replace("\n", ", ") |
||
974 | log.error(f"Fail to undeploy {evc}: {err}") |
||
975 | return del_flows |
||
976 | |||
977 | 1 | def execute_undeploy(self, evcs: list[EVC]): |
|
978 | """Process changes needed to commit an undeploy""" |
||
979 | 1 | delete_flows = {} |
|
980 | 1 | undeploy_evcs = list[EVC]() |
|
981 | 1 | not_undeploy_evcs = list[EVC]() |
|
982 | |||
983 | 1 | for evc in evcs: |
|
984 | 1 | delete_flow = self.prepare_undeploy_flow(evc) |
|
985 | 1 | if delete_flow: |
|
986 | 1 | delete_flows = merge_flow_dicts(delete_flows, delete_flow) |
|
987 | 1 | undeploy_evcs.append(evc) |
|
988 | else: |
||
989 | 1 | not_undeploy_evcs.append(evc) |
|
990 | |||
991 | 1 | try: |
|
992 | 1 | send_flow_mods_http( |
|
993 | delete_flows, |
||
994 | 'delete' |
||
995 | ) |
||
996 | |||
997 | 1 | for evc in undeploy_evcs: |
|
998 | 1 | evc.current_path.make_vlans_available(self.controller) |
|
999 | 1 | evc.failover_path.make_vlans_available(self.controller) |
|
1000 | 1 | evc.current_path = Path([]) |
|
1001 | 1 | evc.failover_path = Path([]) |
|
1002 | 1 | evc.deactivate() |
|
1003 | 1 | emit_event( |
|
1004 | self.controller, |
||
1005 | "need_redeploy", |
||
1006 | content={"evc_id": evc.id} |
||
1007 | ) |
||
1008 | 1 | log.info(f"{evc} scheduled for redeploy") |
|
1009 | 1 | return undeploy_evcs, not_undeploy_evcs |
|
1010 | 1 | except FlowModException as exc: |
|
1011 | 1 | log.error( |
|
1012 | f"Failed to delete flows before redeploy for {evcs}: {exc}" |
||
1013 | ) |
||
1014 | 1 | return [], [*undeploy_evcs, *not_undeploy_evcs] |
|
1015 | |||
1016 | 1 | def handle_link_down(self, event): |
|
1017 | """Change circuit when link is down or under_mantenance.""" |
||
1018 | 1 | link = event.content["link"] |
|
1019 | 1 | log.info("Event handle_link_down %s", link) |
|
1020 | |||
1021 | 1 | with ExitStack() as exit_stack: |
|
1022 | 1 | exit_stack.enter_context(self.multi_evc_lock) |
|
1023 | 1 | swap_to_failover = list[EVC]() |
|
1024 | 1 | undeploy = list[EVC]() |
|
1025 | 1 | clear_failover = list[EVC]() |
|
1026 | 1 | evcs_to_update = dict[str, EVC]() |
|
1027 | |||
1028 | 1 | for evc in self.get_evcs_by_svc_level(): |
|
1029 | 1 | with ExitStack() as sub_stack: |
|
1030 | 1 | sub_stack.enter_context(evc.lock) |
|
1031 | 1 | if all(( |
|
1032 | evc.is_affected_by_link(link), |
||
1033 | evc.failover_path, |
||
1034 | not evc.is_failover_path_affected_by_link(link) |
||
1035 | )): |
||
1036 | 1 | swap_to_failover.append(evc) |
|
1037 | 1 | elif all(( |
|
1038 | evc.is_affected_by_link(link), |
||
1039 | not evc.failover_path or |
||
1040 | evc.is_failover_path_affected_by_link(link) |
||
1041 | )): |
||
1042 | 1 | undeploy.append(evc) |
|
1043 | 1 | elif all(( |
|
1044 | not evc.is_affected_by_link(link), |
||
1045 | evc.failover_path, |
||
1046 | evc.is_failover_path_affected_by_link(link) |
||
1047 | )): |
||
1048 | 1 | clear_failover.append(evc) |
|
1049 | else: |
||
1050 | continue |
||
1051 | |||
1052 | 1 | exit_stack.push(sub_stack.pop_all()) |
|
1053 | |||
1054 | # Swap from current path to failover path |
||
1055 | |||
1056 | 1 | if swap_to_failover: |
|
1057 | 1 | success, failure = self.execute_swap_to_failover( |
|
1058 | swap_to_failover |
||
1059 | ) |
||
1060 | |||
1061 | 1 | clear_failover.extend(success) |
|
1062 | |||
1063 | 1 | evcs_to_update.update((evc.id, evc) for evc in success) |
|
1064 | |||
1065 | 1 | undeploy.extend(failure) |
|
1066 | |||
1067 | # Clear out failover path |
||
1068 | |||
1069 | 1 | if clear_failover: |
|
1070 | 1 | success, failure = self.execute_clear_failover(clear_failover) |
|
1071 | |||
1072 | 1 | evcs_to_update.update((evc.id, evc) for evc in success) |
|
1073 | |||
1074 | 1 | undeploy.extend(failure) |
|
1075 | |||
1076 | # Undeploy the evc, schedule a redeploy |
||
1077 | |||
1078 | 1 | if undeploy: |
|
1079 | 1 | success, failure = self.execute_undeploy(undeploy) |
|
1080 | |||
1081 | 1 | evcs_to_update.update((evc.id, evc) for evc in success) |
|
1082 | |||
1083 | 1 | if failure: |
|
1084 | 1 | log.error(f"Failed to handle_link_down for {failure}") |
|
1085 | |||
1086 | # Push update to DB |
||
1087 | |||
1088 | 1 | if evcs_to_update: |
|
1089 | 1 | self.mongo_controller.update_evcs( |
|
1090 | [evc.as_dict() for evc in evcs_to_update.values()] |
||
1091 | ) |
||
1092 | |||
1093 | 1 | @listen_to("kytos/mef_eline.need_redeploy") |
|
1094 | 1 | def on_evc_need_redeploy(self, event): |
|
1095 | """Redeploy evcs that need to be redeployed.""" |
||
1096 | self.handle_evc_need_redeploy(event) |
||
1097 | |||
1098 | 1 | def handle_evc_need_redeploy(self, event): |
|
1099 | """Redeploy evcs that need to be redeployed.""" |
||
1100 | evc = self.circuits.get(event.content["evc_id"]) |
||
1101 | if evc is None: |
||
1102 | return |
||
1103 | with evc.lock: |
||
1104 | if not evc.is_enabled() or evc.is_active(): |
||
1105 | return |
||
1106 | result = evc.deploy_to_path() |
||
1107 | event_name = "error_redeploy_link_down" |
||
1108 | if result: |
||
1109 | log.info(f"{evc} redeployed") |
||
1110 | event_name = "redeployed_link_down" |
||
1111 | emit_event(self.controller, event_name, |
||
1112 | content=map_evc_event_content(evc)) |
||
1113 | |||
1114 | 1 | @listen_to("kytos/mef_eline.evc_affected_by_link_down") |
|
1115 | 1 | def on_evc_affected_by_link_down(self, event): |
|
1116 | """Change circuit when link is down or under_mantenance.""" |
||
1117 | self.handle_evc_affected_by_link_down(event) |
||
1118 | |||
1119 | 1 | def handle_evc_affected_by_link_down(self, event): |
|
1120 | """Change circuit when link is down or under_mantenance.""" |
||
1121 | 1 | evc = self.circuits.get(event.content["evc_id"]) |
|
1122 | 1 | link = event.content['link'] |
|
1123 | 1 | if not evc: |
|
1124 | 1 | return |
|
1125 | 1 | with evc.lock: |
|
1126 | 1 | if not evc.is_affected_by_link(link): |
|
1127 | return |
||
1128 | 1 | result = evc.handle_link_down() |
|
1129 | 1 | event_name = "error_redeploy_link_down" |
|
1130 | 1 | if result: |
|
1131 | 1 | log.info(f"{evc} redeployed due to link down {link.id}") |
|
1132 | 1 | event_name = "redeployed_link_down" |
|
1133 | 1 | emit_event(self.controller, event_name, |
|
1134 | content=map_evc_event_content(evc)) |
||
1135 | |||
1136 | 1 | @listen_to("kytos/mef_eline.(redeployed_link_(up|down)|deployed)") |
|
1137 | 1 | def on_evc_deployed(self, event): |
|
1138 | """Handle EVC deployed|redeployed_link_down.""" |
||
1139 | self.handle_evc_deployed(event) |
||
1140 | |||
1141 | 1 | def handle_evc_deployed(self, event): |
|
1142 | """Setup failover path on evc deployed.""" |
||
1143 | evc = self.circuits.get(event.content["evc_id"]) |
||
1144 | if not evc: |
||
1145 | return |
||
1146 | evc.try_setup_failover_path() |
||
1147 | |||
1148 | 1 | @listen_to("kytos/topology.topology_loaded") |
|
1149 | 1 | def on_topology_loaded(self, event): # pylint: disable=unused-argument |
|
1150 | """Load EVCs once the topology is available.""" |
||
1151 | self.load_all_evcs() |
||
1152 | |||
1153 | 1 | def load_all_evcs(self): |
|
1154 | """Try to load all EVCs on startup.""" |
||
1155 | 1 | circuits = self.mongo_controller.get_circuits()['circuits'].items() |
|
1156 | 1 | for circuit_id, circuit in circuits: |
|
1157 | 1 | if circuit_id not in self.circuits: |
|
1158 | 1 | self._load_evc(circuit) |
|
1159 | 1 | emit_event(self.controller, "evcs_loaded", content=dict(circuits), |
|
1160 | timeout=1) |
||
1161 | |||
1162 | 1 | def _load_evc(self, circuit_dict): |
|
1163 | """Load one EVC from mongodb to memory.""" |
||
1164 | 1 | try: |
|
1165 | 1 | evc = self._evc_from_dict(circuit_dict) |
|
1166 | 1 | except (ValueError, KytosTagError) as exception: |
|
1167 | 1 | log.error( |
|
1168 | f"Could not load EVC: dict={circuit_dict} error={exception}" |
||
1169 | ) |
||
1170 | 1 | return None |
|
1171 | 1 | if evc.archived: |
|
1172 | 1 | return None |
|
1173 | |||
1174 | 1 | self.circuits.setdefault(evc.id, evc) |
|
1175 | 1 | self.sched.add(evc) |
|
1176 | 1 | return evc |
|
1177 | |||
1178 | 1 | @listen_to("kytos/flow_manager.flow.error") |
|
1179 | 1 | def on_flow_mod_error(self, event): |
|
1180 | """Handle flow mod errors related to an EVC.""" |
||
1181 | self.handle_flow_mod_error(event) |
||
1182 | |||
1183 | 1 | def handle_flow_mod_error(self, event): |
|
1184 | """Handle flow mod errors related to an EVC.""" |
||
1185 | 1 | flow = event.content["flow"] |
|
1186 | 1 | command = event.content.get("error_command") |
|
1187 | 1 | if command != "add": |
|
1188 | return |
||
1189 | 1 | evc = self.circuits.get(EVC.get_id_from_cookie(flow.cookie)) |
|
1190 | 1 | if evc: |
|
1191 | 1 | with evc.lock: |
|
1192 | 1 | evc.remove_current_flows(sync=False) |
|
1193 | 1 | evc.remove_failover_flows(sync=True) |
|
1194 | |||
1195 | 1 | def _evc_dict_with_instances(self, evc_dict): |
|
1196 | """Convert some dict values to instance of EVC classes. |
||
1197 | |||
1198 | This method will convert: [UNI, Link] |
||
1199 | """ |
||
1200 | 1 | data = evc_dict.copy() # Do not modify the original dict |
|
1201 | 1 | for attribute, value in data.items(): |
|
1202 | # Get multiple attributes. |
||
1203 | # Ex: uni_a, uni_z |
||
1204 | 1 | if "uni" in attribute: |
|
1205 | 1 | try: |
|
1206 | 1 | data[attribute] = self._uni_from_dict(value) |
|
1207 | 1 | except ValueError as exception: |
|
1208 | 1 | result = "Error creating UNI: Invalid value" |
|
1209 | 1 | raise ValueError(result) from exception |
|
1210 | |||
1211 | 1 | if attribute == "circuit_scheduler": |
|
1212 | 1 | data[attribute] = [] |
|
1213 | 1 | for schedule in value: |
|
1214 | 1 | data[attribute].append(CircuitSchedule.from_dict(schedule)) |
|
1215 | |||
1216 | # Get multiple attributes. |
||
1217 | # Ex: primary_links, |
||
1218 | # backup_links, |
||
1219 | # current_links_cache, |
||
1220 | # primary_links_cache, |
||
1221 | # backup_links_cache |
||
1222 | 1 | if "links" in attribute: |
|
1223 | 1 | data[attribute] = [ |
|
1224 | self._link_from_dict(link, attribute) for link in value |
||
1225 | ] |
||
1226 | |||
1227 | # Ex: current_path, |
||
1228 | # primary_path, |
||
1229 | # backup_path |
||
1230 | 1 | if "path" in attribute and attribute != "dynamic_backup_path": |
|
1231 | 1 | data[attribute] = Path( |
|
1232 | [self._link_from_dict(link, attribute) for link in value] |
||
1233 | ) |
||
1234 | |||
1235 | 1 | return data |
|
1236 | |||
1237 | 1 | def _evc_from_dict(self, evc_dict): |
|
1238 | 1 | data = self._evc_dict_with_instances(evc_dict) |
|
1239 | 1 | data["table_group"] = self.table_group |
|
1240 | 1 | return EVC(self.controller, **data) |
|
1241 | |||
1242 | 1 | def _uni_from_dict(self, uni_dict): |
|
1243 | """Return a UNI object from python dict.""" |
||
1244 | 1 | if uni_dict is None: |
|
1245 | 1 | return False |
|
1246 | |||
1247 | 1 | interface_id = uni_dict.get("interface_id") |
|
1248 | 1 | interface = self.controller.get_interface_by_id(interface_id) |
|
1249 | 1 | if interface is None: |
|
1250 | 1 | result = ( |
|
1251 | "Error creating UNI:" |
||
1252 | + f"Could not instantiate interface {interface_id}" |
||
1253 | ) |
||
1254 | 1 | raise ValueError(result) from ValueError |
|
1255 | 1 | tag_convert = {1: "vlan"} |
|
1256 | 1 | tag_dict = uni_dict.get("tag", None) |
|
1257 | 1 | if tag_dict: |
|
1258 | 1 | tag_type = tag_dict.get("tag_type") |
|
1259 | 1 | tag_type = tag_convert.get(tag_type, tag_type) |
|
1260 | 1 | tag_value = tag_dict.get("value") |
|
1261 | 1 | if isinstance(tag_value, list): |
|
1262 | 1 | tag_value = get_tag_ranges(tag_value) |
|
1263 | 1 | mask_list = get_vlan_tags_and_masks(tag_value) |
|
1264 | 1 | tag = TAGRange(tag_type, tag_value, mask_list) |
|
1265 | else: |
||
1266 | 1 | tag = TAG(tag_type, tag_value) |
|
1267 | else: |
||
1268 | 1 | tag = None |
|
1269 | 1 | uni = UNI(interface, tag) |
|
1270 | 1 | return uni |
|
1271 | |||
1272 | 1 | def _link_from_dict(self, link_dict: dict, attribute: str) -> Link: |
|
1273 | """Return a Link object from python dict.""" |
||
1274 | 1 | id_a = link_dict.get("endpoint_a").get("id") |
|
1275 | 1 | id_b = link_dict.get("endpoint_b").get("id") |
|
1276 | |||
1277 | 1 | endpoint_a = self.controller.get_interface_by_id(id_a) |
|
1278 | 1 | endpoint_b = self.controller.get_interface_by_id(id_b) |
|
1279 | 1 | if not endpoint_a: |
|
1280 | 1 | error_msg = f"Could not get interface endpoint_a id {id_a}" |
|
1281 | 1 | raise ValueError(error_msg) |
|
1282 | 1 | if not endpoint_b: |
|
1283 | error_msg = f"Could not get interface endpoint_b id {id_b}" |
||
1284 | raise ValueError(error_msg) |
||
1285 | |||
1286 | 1 | link = Link(endpoint_a, endpoint_b) |
|
1287 | 1 | allowed_paths = {"current_path", "failover_path"} |
|
1288 | 1 | if "metadata" in link_dict and attribute in allowed_paths: |
|
1289 | 1 | link.extend_metadata(link_dict.get("metadata")) |
|
1290 | |||
1291 | 1 | s_vlan = link.get_metadata("s_vlan") |
|
1292 | 1 | if s_vlan: |
|
1293 | 1 | tag = TAG.from_dict(s_vlan) |
|
1294 | 1 | if tag is False: |
|
1295 | error_msg = f"Could not instantiate tag from dict {s_vlan}" |
||
1296 | raise ValueError(error_msg) |
||
1297 | 1 | link.update_metadata("s_vlan", tag) |
|
1298 | 1 | return link |
|
1299 | |||
1300 | 1 | def _find_evc_by_schedule_id(self, schedule_id): |
|
1301 | """ |
||
1302 | Find an EVC and CircuitSchedule based on schedule_id. |
||
1303 | |||
1304 | :param schedule_id: Schedule ID |
||
1305 | :return: EVC and Schedule |
||
1306 | """ |
||
1307 | 1 | circuits = self._get_circuits_buffer() |
|
1308 | 1 | found_schedule = None |
|
1309 | 1 | evc = None |
|
1310 | |||
1311 | # pylint: disable=unused-variable |
||
1312 | 1 | for c_id, circuit in circuits.items(): |
|
1313 | 1 | for schedule in circuit.circuit_scheduler: |
|
1314 | 1 | if schedule.id == schedule_id: |
|
1315 | 1 | found_schedule = schedule |
|
1316 | 1 | evc = circuit |
|
1317 | 1 | break |
|
1318 | 1 | if found_schedule: |
|
1319 | 1 | break |
|
1320 | 1 | return evc, found_schedule |
|
1321 | |||
1322 | 1 | def _get_circuits_buffer(self): |
|
1323 | """ |
||
1324 | Return the circuit buffer. |
||
1325 | |||
1326 | If the buffer is empty, try to load data from mongodb. |
||
1327 | """ |
||
1328 | 1 | if not self.circuits: |
|
1329 | # Load circuits from mongodb to buffer |
||
1330 | 1 | circuits = self.mongo_controller.get_circuits()['circuits'] |
|
1331 | 1 | for c_id, circuit in circuits.items(): |
|
1332 | 1 | evc = self._evc_from_dict(circuit) |
|
1333 | 1 | self.circuits[c_id] = evc |
|
1334 | 1 | return self.circuits |
|
1335 | |||
1336 | # pylint: disable=attribute-defined-outside-init |
||
1337 | 1 | @alisten_to("kytos/of_multi_table.enable_table") |
|
1338 | 1 | async def on_table_enabled(self, event): |
|
1339 | """Handle a recently table enabled.""" |
||
1340 | 1 | table_group = event.content.get("mef_eline", None) |
|
1341 | 1 | if not table_group: |
|
1342 | 1 | return |
|
1343 | 1 | for group in table_group: |
|
1344 | 1 | if group not in settings.TABLE_GROUP_ALLOWED: |
|
1345 | 1 | log.error(f'The table group "{group}" is not allowed for ' |
|
1346 | f'mef_eline. Allowed table groups are ' |
||
1347 | f'{settings.TABLE_GROUP_ALLOWED}') |
||
1348 | 1 | return |
|
1349 | 1 | self.table_group.update(table_group) |
|
1350 | 1 | content = {"group_table": self.table_group} |
|
1351 | 1 | name = "kytos/mef_eline.enable_table" |
|
1352 | await aemit_event(self.controller, name, content) |
||
1353 |