| Total Complexity | 182 |
| Total Lines | 1039 |
| Duplicated Lines | 3.85 % |
| Coverage | 93.25% |
| 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 | 1 | """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 | from threading import Lock |
|
| 9 | |||
| 10 | 1 | from pydantic import ValidationError |
|
| 11 | |||
| 12 | 1 | from kytos.core import KytosNApp, log, rest |
|
| 13 | 1 | from kytos.core.helpers import (alisten_to, listen_to, load_spec, |
|
| 14 | validate_openapi) |
||
| 15 | 1 | from kytos.core.interface import TAG, UNI |
|
| 16 | 1 | from kytos.core.link import Link |
|
| 17 | 1 | from kytos.core.rest_api import (HTTPException, JSONResponse, Request, |
|
| 18 | get_json_or_400) |
||
| 19 | 1 | from napps.kytos.mef_eline import controllers, settings |
|
| 20 | 1 | from napps.kytos.mef_eline.exceptions import DisabledSwitch, InvalidPath |
|
| 21 | 1 | from napps.kytos.mef_eline.models import (EVC, DynamicPathManager, EVCDeploy, |
|
| 22 | Path) |
||
| 23 | 1 | from napps.kytos.mef_eline.scheduler import CircuitSchedule, Scheduler |
|
| 24 | 1 | from napps.kytos.mef_eline.utils import (aemit_event, check_disabled_component, |
|
| 25 | emit_event, map_evc_event_content) |
||
| 26 | |||
| 27 | |||
| 28 | # pylint: disable=too-many-public-methods |
||
| 29 | 1 | class Main(KytosNApp): |
|
| 30 | """Main class of amlight/mef_eline NApp. |
||
| 31 | |||
| 32 | This class is the entry point for this napp. |
||
| 33 | """ |
||
| 34 | |||
| 35 | 1 | spec = load_spec(pathlib.Path(__file__).parent / "openapi.yml") |
|
| 36 | |||
| 37 | 1 | def setup(self): |
|
| 38 | """Replace the '__init__' method for the KytosNApp subclass. |
||
| 39 | |||
| 40 | The setup method is automatically called by the controller when your |
||
| 41 | application is loaded. |
||
| 42 | |||
| 43 | So, if you have any setup routine, insert it here. |
||
| 44 | """ |
||
| 45 | # object used to scheduler circuit events |
||
| 46 | 1 | self.sched = Scheduler() |
|
| 47 | |||
| 48 | # object to save and load circuits |
||
| 49 | 1 | self.mongo_controller = self.get_eline_controller() |
|
| 50 | 1 | self.mongo_controller.bootstrap_indexes() |
|
| 51 | |||
| 52 | # set the controller that will manager the dynamic paths |
||
| 53 | 1 | DynamicPathManager.set_controller(self.controller) |
|
| 54 | |||
| 55 | # dictionary of EVCs created. It acts as a circuit buffer. |
||
| 56 | # Every create/update/delete must be synced to mongodb. |
||
| 57 | 1 | self.circuits = {} |
|
| 58 | |||
| 59 | 1 | self.table_group = {"epl": 0, "evpl": 0} |
|
| 60 | 1 | self._lock = Lock() |
|
| 61 | 1 | self.execute_as_loop(settings.DEPLOY_EVCS_INTERVAL) |
|
| 62 | |||
| 63 | 1 | self.load_all_evcs() |
|
| 64 | 1 | self._topology_updated_at = None |
|
| 65 | |||
| 66 | 1 | def get_evcs_by_svc_level(self) -> list: |
|
| 67 | """Get circuits sorted by desc service level and asc creation_time. |
||
| 68 | |||
| 69 | In the future, as more ops are offloaded it should be get from the DB. |
||
| 70 | """ |
||
| 71 | 1 | return sorted(self.circuits.values(), |
|
| 72 | key=lambda x: (-x.service_level, x.creation_time)) |
||
| 73 | |||
| 74 | 1 | @staticmethod |
|
| 75 | 1 | def get_eline_controller(): |
|
| 76 | """Return the ELineController instance.""" |
||
| 77 | return controllers.ELineController() |
||
| 78 | |||
| 79 | 1 | def execute(self): |
|
| 80 | """Execute once when the napp is running.""" |
||
| 81 | 1 | if self._lock.locked(): |
|
| 82 | 1 | return |
|
| 83 | 1 | log.debug("Starting consistency routine") |
|
| 84 | 1 | with self._lock: |
|
| 85 | 1 | self.execute_consistency() |
|
| 86 | 1 | log.debug("Finished consistency routine") |
|
| 87 | |||
| 88 | 1 | def should_be_checked(self, circuit): |
|
| 89 | "Verify if the circuit meets the necessary conditions to be checked" |
||
| 90 | # pylint: disable=too-many-boolean-expressions |
||
| 91 | 1 | if ( |
|
| 92 | circuit.is_enabled() |
||
| 93 | and not circuit.is_active() |
||
| 94 | and not circuit.lock.locked() |
||
| 95 | and not circuit.has_recent_removed_flow() |
||
| 96 | and not circuit.is_recent_updated() |
||
| 97 | and circuit.are_unis_active(self.controller.switches) |
||
| 98 | # if a inter-switch EVC does not have current_path, it does not |
||
| 99 | # make sense to run sdntrace on it |
||
| 100 | and (circuit.is_intra_switch() or circuit.current_path) |
||
| 101 | ): |
||
| 102 | 1 | return True |
|
| 103 | return False |
||
| 104 | |||
| 105 | 1 | def execute_consistency(self): |
|
| 106 | """Execute consistency routine.""" |
||
| 107 | 1 | circuits_to_check = [] |
|
| 108 | 1 | stored_circuits = self.mongo_controller.get_circuits()['circuits'] |
|
| 109 | 1 | for circuit in self.get_evcs_by_svc_level(): |
|
| 110 | 1 | stored_circuits.pop(circuit.id, None) |
|
| 111 | 1 | if self.should_be_checked(circuit): |
|
| 112 | 1 | circuits_to_check.append(circuit) |
|
| 113 | 1 | circuits_checked = EVCDeploy.check_list_traces(circuits_to_check) |
|
| 114 | 1 | for circuit in circuits_to_check: |
|
| 115 | 1 | is_checked = circuits_checked.get(circuit.id) |
|
| 116 | 1 | if is_checked: |
|
| 117 | 1 | circuit.execution_rounds = 0 |
|
| 118 | 1 | log.info(f"{circuit} enabled but inactive - activating") |
|
| 119 | 1 | with circuit.lock: |
|
| 120 | 1 | circuit.activate() |
|
| 121 | 1 | circuit.sync() |
|
| 122 | else: |
||
| 123 | 1 | circuit.execution_rounds += 1 |
|
| 124 | 1 | if circuit.execution_rounds > settings.WAIT_FOR_OLD_PATH: |
|
| 125 | 1 | log.info(f"{circuit} enabled but inactive - redeploy") |
|
| 126 | 1 | with circuit.lock: |
|
| 127 | 1 | circuit.deploy() |
|
| 128 | 1 | for circuit_id in stored_circuits: |
|
| 129 | 1 | log.info(f"EVC found in mongodb but unloaded {circuit_id}") |
|
| 130 | 1 | self._load_evc(stored_circuits[circuit_id]) |
|
| 131 | |||
| 132 | 1 | def shutdown(self): |
|
| 133 | """Execute when your napp is unloaded. |
||
| 134 | |||
| 135 | If you have some cleanup procedure, insert it here. |
||
| 136 | """ |
||
| 137 | |||
| 138 | 1 | @rest("/v2/evc/", methods=["GET"]) |
|
| 139 | 1 | def list_circuits(self, request: Request) -> JSONResponse: |
|
| 140 | """Endpoint to return circuits stored. |
||
| 141 | |||
| 142 | archive query arg if defined (not null) will be filtered |
||
| 143 | accordingly, by default only non archived evcs will be listed |
||
| 144 | """ |
||
| 145 | 1 | log.debug("list_circuits /v2/evc") |
|
| 146 | 1 | args = request.query_params |
|
| 147 | 1 | archived = args.get("archived", "false").lower() |
|
| 148 | 1 | args = {k: v for k, v in args.items() if k not in {"archived"}} |
|
| 149 | 1 | circuits = self.mongo_controller.get_circuits(archived=archived, |
|
| 150 | metadata=args) |
||
| 151 | 1 | circuits = circuits['circuits'] |
|
| 152 | 1 | return JSONResponse(circuits) |
|
| 153 | |||
| 154 | 1 | @rest("/v2/evc/schedule", methods=["GET"]) |
|
| 155 | 1 | def list_schedules(self, _request: Request) -> JSONResponse: |
|
| 156 | """Endpoint to return all schedules stored for all circuits. |
||
| 157 | |||
| 158 | Return a JSON with the following template: |
||
| 159 | [{"schedule_id": <schedule_id>, |
||
| 160 | "circuit_id": <circuit_id>, |
||
| 161 | "schedule": <schedule object>}] |
||
| 162 | """ |
||
| 163 | 1 | log.debug("list_schedules /v2/evc/schedule") |
|
| 164 | 1 | circuits = self.mongo_controller.get_circuits()['circuits'].values() |
|
| 165 | 1 | if not circuits: |
|
| 166 | 1 | result = {} |
|
| 167 | 1 | status = 200 |
|
| 168 | 1 | return JSONResponse(result, status_code=status) |
|
| 169 | |||
| 170 | 1 | result = [] |
|
| 171 | 1 | status = 200 |
|
| 172 | 1 | for circuit in circuits: |
|
| 173 | 1 | circuit_scheduler = circuit.get("circuit_scheduler") |
|
| 174 | 1 | if circuit_scheduler: |
|
| 175 | 1 | for scheduler in circuit_scheduler: |
|
| 176 | 1 | value = { |
|
| 177 | "schedule_id": scheduler.get("id"), |
||
| 178 | "circuit_id": circuit.get("id"), |
||
| 179 | "schedule": scheduler, |
||
| 180 | } |
||
| 181 | 1 | result.append(value) |
|
| 182 | |||
| 183 | 1 | log.debug("list_schedules result %s %s", result, status) |
|
| 184 | 1 | return JSONResponse(result, status_code=status) |
|
| 185 | |||
| 186 | 1 | @rest("/v2/evc/{circuit_id}", methods=["GET"]) |
|
| 187 | 1 | def get_circuit(self, request: Request) -> JSONResponse: |
|
| 188 | """Endpoint to return a circuit based on id.""" |
||
| 189 | 1 | circuit_id = request.path_params["circuit_id"] |
|
| 190 | 1 | log.debug("get_circuit /v2/evc/%s", circuit_id) |
|
| 191 | 1 | circuit = self.mongo_controller.get_circuit(circuit_id) |
|
| 192 | 1 | if not circuit: |
|
| 193 | 1 | result = f"circuit_id {circuit_id} not found" |
|
| 194 | 1 | log.debug("get_circuit result %s %s", result, 404) |
|
| 195 | 1 | raise HTTPException(404, detail=result) |
|
| 196 | 1 | status = 200 |
|
| 197 | 1 | log.debug("get_circuit result %s %s", circuit, status) |
|
| 198 | 1 | return JSONResponse(circuit, status_code=status) |
|
| 199 | |||
| 200 | 1 | @rest("/v2/evc/", methods=["POST"]) |
|
| 201 | 1 | @validate_openapi(spec) |
|
| 202 | 1 | def create_circuit(self, request: Request) -> JSONResponse: |
|
| 203 | """Try to create a new circuit. |
||
| 204 | |||
| 205 | Firstly, for EVPL: E-Line NApp verifies if UNI_A's requested C-VID and |
||
| 206 | UNI_Z's requested C-VID are available from the interfaces' pools. This |
||
| 207 | is checked when creating the UNI object. |
||
| 208 | |||
| 209 | Then, E-Line NApp requests a primary and a backup path to the |
||
| 210 | Pathfinder NApp using the attributes primary_links and backup_links |
||
| 211 | submitted via REST |
||
| 212 | |||
| 213 | # For each link composing paths in #3: |
||
| 214 | # - E-Line NApp requests a S-VID available from the link VLAN pool. |
||
| 215 | # - Using the S-VID obtained, generate abstract flow entries to be |
||
| 216 | # sent to FlowManager |
||
| 217 | |||
| 218 | Push abstract flow entries to FlowManager and FlowManager pushes |
||
| 219 | OpenFlow entries to datapaths |
||
| 220 | |||
| 221 | E-Line NApp generates an event to notify all Kytos NApps of a new EVC |
||
| 222 | creation |
||
| 223 | |||
| 224 | Finnaly, notify user of the status of its request. |
||
| 225 | """ |
||
| 226 | # Try to create the circuit object |
||
| 227 | 1 | log.debug("create_circuit /v2/evc/") |
|
| 228 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
| 229 | |||
| 230 | 1 | try: |
|
| 231 | 1 | evc = self._evc_from_dict(data) |
|
| 232 | 1 | except ValueError as exception: |
|
| 233 | 1 | log.debug("create_circuit result %s %s", exception, 400) |
|
| 234 | 1 | raise HTTPException(400, detail=str(exception)) from exception |
|
| 235 | |||
| 236 | 1 | try: |
|
| 237 | 1 | check_disabled_component(evc.uni_a, evc.uni_z) |
|
| 238 | 1 | except DisabledSwitch as exception: |
|
| 239 | 1 | log.debug("create_circuit result %s %s", exception, 409) |
|
| 240 | 1 | raise HTTPException( |
|
| 241 | 409, |
||
| 242 | detail=f"Path is not valid: {exception}" |
||
| 243 | ) from exception |
||
| 244 | |||
| 245 | 1 | if evc.primary_path: |
|
| 246 | try: |
||
| 247 | evc.primary_path.is_valid( |
||
| 248 | evc.uni_a.interface.switch, |
||
| 249 | evc.uni_z.interface.switch, |
||
| 250 | bool(evc.circuit_scheduler), |
||
| 251 | ) |
||
| 252 | except InvalidPath as exception: |
||
| 253 | raise HTTPException( |
||
| 254 | 400, |
||
| 255 | detail=f"primary_path is not valid: {exception}" |
||
| 256 | ) from exception |
||
| 257 | 1 | if evc.backup_path: |
|
| 258 | try: |
||
| 259 | evc.backup_path.is_valid( |
||
| 260 | evc.uni_a.interface.switch, |
||
| 261 | evc.uni_z.interface.switch, |
||
| 262 | bool(evc.circuit_scheduler), |
||
| 263 | ) |
||
| 264 | except InvalidPath as exception: |
||
| 265 | raise HTTPException( |
||
| 266 | 400, |
||
| 267 | detail=f"backup_path is not valid: {exception}" |
||
| 268 | ) from exception |
||
| 269 | |||
| 270 | # verify duplicated evc |
||
| 271 | 1 | if self._is_duplicated_evc(evc): |
|
| 272 | 1 | result = "The EVC already exists." |
|
| 273 | 1 | log.debug("create_circuit result %s %s", result, 409) |
|
| 274 | 1 | raise HTTPException(409, detail=result) |
|
| 275 | |||
| 276 | 1 | try: |
|
| 277 | 1 | evc._validate_has_primary_or_dynamic() |
|
| 278 | 1 | except ValueError as exception: |
|
| 279 | 1 | raise HTTPException(400, detail=str(exception)) from exception |
|
| 280 | |||
| 281 | # save circuit |
||
| 282 | 1 | try: |
|
| 283 | 1 | evc.sync() |
|
| 284 | except ValidationError as exception: |
||
| 285 | raise HTTPException(400, detail=str(exception)) from exception |
||
| 286 | |||
| 287 | # store circuit in dictionary |
||
| 288 | 1 | self.circuits[evc.id] = evc |
|
| 289 | |||
| 290 | # Schedule the circuit deploy |
||
| 291 | 1 | self.sched.add(evc) |
|
| 292 | |||
| 293 | # Circuit has no schedule, deploy now |
||
| 294 | 1 | if not evc.circuit_scheduler: |
|
| 295 | 1 | with evc.lock: |
|
| 296 | 1 | evc.deploy() |
|
| 297 | |||
| 298 | # Notify users |
||
| 299 | 1 | result = {"circuit_id": evc.id} |
|
| 300 | 1 | status = 201 |
|
| 301 | 1 | log.debug("create_circuit result %s %s", result, status) |
|
| 302 | 1 | emit_event(self.controller, name="created", |
|
| 303 | content=map_evc_event_content(evc)) |
||
| 304 | 1 | return JSONResponse(result, status_code=status) |
|
| 305 | |||
| 306 | 1 | @listen_to('kytos/flow_manager.flow.removed') |
|
| 307 | 1 | def on_flow_delete(self, event): |
|
| 308 | """Capture delete messages to keep track when flows got removed.""" |
||
| 309 | self.handle_flow_delete(event) |
||
| 310 | |||
| 311 | 1 | def handle_flow_delete(self, event): |
|
| 312 | """Keep track when the EVC got flows removed by deriving its cookie.""" |
||
| 313 | 1 | flow = event.content["flow"] |
|
| 314 | 1 | evc = self.circuits.get(EVC.get_id_from_cookie(flow.cookie)) |
|
| 315 | 1 | if evc: |
|
| 316 | 1 | log.debug("Flow removed in EVC %s", evc.id) |
|
| 317 | 1 | evc.set_flow_removed_at() |
|
| 318 | |||
| 319 | 1 | @rest("/v2/evc/{circuit_id}", methods=["PATCH"]) |
|
| 320 | 1 | @validate_openapi(spec) |
|
| 321 | 1 | def update(self, request: Request) -> JSONResponse: |
|
| 322 | """Update a circuit based on payload. |
||
| 323 | |||
| 324 | The EVC attributes (creation_time, active, current_path, |
||
| 325 | failover_path, _id, archived) can't be updated. |
||
| 326 | """ |
||
| 327 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
| 328 | 1 | circuit_id = request.path_params["circuit_id"] |
|
| 329 | 1 | log.debug("update /v2/evc/%s", circuit_id) |
|
| 330 | 1 | try: |
|
| 331 | 1 | evc = self.circuits[circuit_id] |
|
| 332 | 1 | except KeyError: |
|
| 333 | 1 | result = f"circuit_id {circuit_id} not found" |
|
| 334 | 1 | log.debug("update result %s %s", result, 404) |
|
| 335 | 1 | raise HTTPException(404, detail=result) from KeyError |
|
| 336 | |||
| 337 | 1 | if evc.archived: |
|
| 338 | 1 | result = "Can't update archived EVC" |
|
| 339 | 1 | log.debug("update result %s %s", result, 409) |
|
| 340 | 1 | raise HTTPException(409, detail=result) |
|
| 341 | |||
| 342 | 1 | try: |
|
| 343 | 1 | enable, redeploy = evc.update( |
|
| 344 | **self._evc_dict_with_instances(data) |
||
| 345 | ) |
||
| 346 | 1 | except ValidationError as exception: |
|
| 347 | raise HTTPException(400, detail=str(exception)) from exception |
||
| 348 | 1 | except ValueError as exception: |
|
| 349 | 1 | log.error(exception) |
|
| 350 | 1 | log.debug("update result %s %s", exception, 400) |
|
| 351 | 1 | raise HTTPException(400, detail=str(exception)) from exception |
|
| 352 | 1 | except DisabledSwitch as exception: |
|
| 353 | 1 | log.debug("update result %s %s", exception, 409) |
|
| 354 | 1 | raise HTTPException( |
|
| 355 | 409, |
||
| 356 | detail=f"Path is not valid: {exception}" |
||
| 357 | ) from exception |
||
| 358 | |||
| 359 | 1 | if evc.is_active(): |
|
| 360 | if enable is False: # disable if active |
||
| 361 | with evc.lock: |
||
| 362 | evc.remove() |
||
| 363 | elif redeploy is not None: # redeploy if active |
||
| 364 | with evc.lock: |
||
| 365 | evc.remove() |
||
| 366 | evc.deploy() |
||
| 367 | else: |
||
| 368 | 1 | if enable is True: # enable if inactive |
|
| 369 | 1 | with evc.lock: |
|
| 370 | 1 | evc.deploy() |
|
| 371 | 1 | result = {evc.id: evc.as_dict()} |
|
| 372 | 1 | status = 200 |
|
| 373 | |||
| 374 | 1 | log.debug("update result %s %s", result, status) |
|
| 375 | 1 | emit_event(self.controller, "updated", |
|
| 376 | content=map_evc_event_content(evc, **data)) |
||
| 377 | 1 | return JSONResponse(result, status_code=status) |
|
| 378 | |||
| 379 | 1 | @rest("/v2/evc/{circuit_id}", methods=["DELETE"]) |
|
| 380 | 1 | def delete_circuit(self, request: Request) -> JSONResponse: |
|
| 381 | """Remove a circuit. |
||
| 382 | |||
| 383 | First, the flows are removed from the switches, and then the EVC is |
||
| 384 | disabled. |
||
| 385 | """ |
||
| 386 | 1 | circuit_id = request.path_params["circuit_id"] |
|
| 387 | 1 | log.debug("delete_circuit /v2/evc/%s", circuit_id) |
|
| 388 | 1 | try: |
|
| 389 | 1 | evc = self.circuits[circuit_id] |
|
| 390 | 1 | except KeyError: |
|
| 391 | 1 | result = f"circuit_id {circuit_id} not found" |
|
| 392 | 1 | log.debug("delete_circuit result %s %s", result, 404) |
|
| 393 | 1 | raise HTTPException(404, detail=result) from KeyError |
|
| 394 | |||
| 395 | 1 | if evc.archived: |
|
| 396 | 1 | result = f"Circuit {circuit_id} already removed" |
|
| 397 | 1 | log.debug("delete_circuit result %s %s", result, 404) |
|
| 398 | 1 | raise HTTPException(404, detail=result) |
|
| 399 | |||
| 400 | 1 | log.info("Removing %s", evc) |
|
| 401 | 1 | with evc.lock: |
|
| 402 | 1 | evc.remove_current_flows() |
|
| 403 | 1 | evc.remove_failover_flows(sync=False) |
|
| 404 | 1 | evc.deactivate() |
|
| 405 | 1 | evc.disable() |
|
| 406 | 1 | self.sched.remove(evc) |
|
| 407 | 1 | evc.archive() |
|
| 408 | 1 | evc.sync() |
|
| 409 | 1 | log.info("EVC removed. %s", evc) |
|
| 410 | 1 | result = {"response": f"Circuit {circuit_id} removed"} |
|
| 411 | 1 | status = 200 |
|
| 412 | |||
| 413 | 1 | log.debug("delete_circuit result %s %s", result, status) |
|
| 414 | 1 | emit_event(self.controller, "deleted", |
|
| 415 | content=map_evc_event_content(evc)) |
||
| 416 | 1 | return JSONResponse(result, status_code=status) |
|
| 417 | |||
| 418 | 1 | @rest("v2/evc/{circuit_id}/metadata", methods=["GET"]) |
|
| 419 | 1 | def get_metadata(self, request: Request) -> JSONResponse: |
|
| 420 | """Get metadata from an EVC.""" |
||
| 421 | 1 | circuit_id = request.path_params["circuit_id"] |
|
| 422 | 1 | try: |
|
| 423 | 1 | return ( |
|
| 424 | JSONResponse({"metadata": self.circuits[circuit_id].metadata}) |
||
| 425 | ) |
||
| 426 | except KeyError as error: |
||
| 427 | raise HTTPException( |
||
| 428 | 404, |
||
| 429 | detail=f"circuit_id {circuit_id} not found." |
||
| 430 | ) from error |
||
| 431 | |||
| 432 | 1 | View Code Duplication | @rest("v2/evc/metadata", methods=["POST"]) |
|
|
|||
| 433 | 1 | @validate_openapi(spec) |
|
| 434 | 1 | def bulk_add_metadata(self, request: Request) -> JSONResponse: |
|
| 435 | """Add metadata to a bulk of EVCs.""" |
||
| 436 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
| 437 | 1 | circuit_ids = data.pop("circuit_ids") |
|
| 438 | |||
| 439 | 1 | self.mongo_controller.update_evcs(circuit_ids, data, "add") |
|
| 440 | |||
| 441 | 1 | fail_evcs = [] |
|
| 442 | 1 | for _id in circuit_ids: |
|
| 443 | 1 | try: |
|
| 444 | 1 | evc = self.circuits[_id] |
|
| 445 | 1 | evc.extend_metadata(data) |
|
| 446 | 1 | except KeyError: |
|
| 447 | 1 | fail_evcs.append(_id) |
|
| 448 | |||
| 449 | 1 | if fail_evcs: |
|
| 450 | 1 | raise HTTPException(404, detail=fail_evcs) |
|
| 451 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
| 452 | |||
| 453 | 1 | @rest("v2/evc/{circuit_id}/metadata", methods=["POST"]) |
|
| 454 | 1 | @validate_openapi(spec) |
|
| 455 | 1 | def add_metadata(self, request: Request) -> JSONResponse: |
|
| 456 | """Add metadata to an EVC.""" |
||
| 457 | 1 | circuit_id = request.path_params["circuit_id"] |
|
| 458 | 1 | metadata = get_json_or_400(request, self.controller.loop) |
|
| 459 | 1 | if not isinstance(metadata, dict): |
|
| 460 | raise HTTPException(400, "Invalid metadata value: {metadata}") |
||
| 461 | 1 | try: |
|
| 462 | 1 | evc = self.circuits[circuit_id] |
|
| 463 | 1 | except KeyError as error: |
|
| 464 | 1 | raise HTTPException( |
|
| 465 | 404, |
||
| 466 | detail=f"circuit_id {circuit_id} not found." |
||
| 467 | ) from error |
||
| 468 | |||
| 469 | 1 | evc.extend_metadata(metadata) |
|
| 470 | 1 | evc.sync() |
|
| 471 | 1 | return JSONResponse("Operation successful", status_code=201) |
|
| 472 | |||
| 473 | 1 | View Code Duplication | @rest("v2/evc/metadata/{key}", methods=["DELETE"]) |
| 474 | 1 | @validate_openapi(spec) |
|
| 475 | 1 | def bulk_delete_metadata(self, request: Request) -> JSONResponse: |
|
| 476 | """Delete metada from a bulk of EVCs""" |
||
| 477 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
| 478 | 1 | key = request.path_params["key"] |
|
| 479 | 1 | circuit_ids = data.pop("circuit_ids") |
|
| 480 | 1 | self.mongo_controller.update_evcs(circuit_ids, {key: ""}, "del") |
|
| 481 | |||
| 482 | 1 | fail_evcs = [] |
|
| 483 | 1 | for _id in circuit_ids: |
|
| 484 | 1 | try: |
|
| 485 | 1 | evc = self.circuits[_id] |
|
| 486 | 1 | evc.remove_metadata(key) |
|
| 487 | except KeyError: |
||
| 488 | fail_evcs.append(_id) |
||
| 489 | |||
| 490 | 1 | if fail_evcs: |
|
| 491 | raise HTTPException(404, detail=fail_evcs) |
||
| 492 | 1 | return JSONResponse("Operation successful") |
|
| 493 | |||
| 494 | 1 | @rest("v2/evc/{circuit_id}/metadata/{key}", methods=["DELETE"]) |
|
| 495 | 1 | def delete_metadata(self, request: Request) -> JSONResponse: |
|
| 496 | """Delete metadata from an EVC.""" |
||
| 497 | 1 | circuit_id = request.path_params["circuit_id"] |
|
| 498 | 1 | key = request.path_params["key"] |
|
| 499 | 1 | try: |
|
| 500 | 1 | evc = self.circuits[circuit_id] |
|
| 501 | 1 | except KeyError as error: |
|
| 502 | 1 | raise HTTPException( |
|
| 503 | 404, |
||
| 504 | detail=f"circuit_id {circuit_id} not found." |
||
| 505 | ) from error |
||
| 506 | |||
| 507 | 1 | evc.remove_metadata(key) |
|
| 508 | 1 | evc.sync() |
|
| 509 | 1 | return JSONResponse("Operation successful") |
|
| 510 | |||
| 511 | 1 | @rest("/v2/evc/{circuit_id}/redeploy", methods=["PATCH"]) |
|
| 512 | 1 | def redeploy(self, request: Request) -> JSONResponse: |
|
| 513 | """Endpoint to force the redeployment of an EVC.""" |
||
| 514 | 1 | circuit_id = request.path_params["circuit_id"] |
|
| 515 | 1 | log.debug("redeploy /v2/evc/%s/redeploy", circuit_id) |
|
| 516 | 1 | try: |
|
| 517 | 1 | evc = self.circuits[circuit_id] |
|
| 518 | 1 | except KeyError: |
|
| 519 | 1 | raise HTTPException( |
|
| 520 | 404, |
||
| 521 | detail=f"circuit_id {circuit_id} not found" |
||
| 522 | ) from KeyError |
||
| 523 | 1 | if evc.is_enabled(): |
|
| 524 | 1 | with evc.lock: |
|
| 525 | 1 | evc.remove_current_flows() |
|
| 526 | 1 | evc.deploy() |
|
| 527 | 1 | result = {"response": f"Circuit {circuit_id} redeploy received."} |
|
| 528 | 1 | status = 202 |
|
| 529 | else: |
||
| 530 | 1 | result = {"response": f"Circuit {circuit_id} is disabled."} |
|
| 531 | 1 | status = 409 |
|
| 532 | |||
| 533 | 1 | return JSONResponse(result, status_code=status) |
|
| 534 | |||
| 535 | 1 | @rest("/v2/evc/schedule/", methods=["POST"]) |
|
| 536 | 1 | @validate_openapi(spec) |
|
| 537 | 1 | def create_schedule(self, request: Request) -> JSONResponse: |
|
| 538 | """ |
||
| 539 | Create a new schedule for a given circuit. |
||
| 540 | |||
| 541 | This service do no check if there are conflicts with another schedule. |
||
| 542 | Payload example: |
||
| 543 | { |
||
| 544 | "circuit_id":"aa:bb:cc", |
||
| 545 | "schedule": { |
||
| 546 | "date": "2019-08-07T14:52:10.967Z", |
||
| 547 | "interval": "string", |
||
| 548 | "frequency": "1 * * * *", |
||
| 549 | "action": "create" |
||
| 550 | } |
||
| 551 | } |
||
| 552 | """ |
||
| 553 | 1 | log.debug("create_schedule /v2/evc/schedule/") |
|
| 554 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
| 555 | 1 | circuit_id = data["circuit_id"] |
|
| 556 | 1 | schedule_data = data["schedule"] |
|
| 557 | |||
| 558 | # Get EVC from circuits buffer |
||
| 559 | 1 | circuits = self._get_circuits_buffer() |
|
| 560 | |||
| 561 | # get the circuit |
||
| 562 | 1 | evc = circuits.get(circuit_id) |
|
| 563 | |||
| 564 | # get the circuit |
||
| 565 | 1 | if not evc: |
|
| 566 | 1 | result = f"circuit_id {circuit_id} not found" |
|
| 567 | 1 | log.debug("create_schedule result %s %s", result, 404) |
|
| 568 | 1 | raise HTTPException(404, detail=result) |
|
| 569 | # Can not modify circuits deleted and archived |
||
| 570 | 1 | if evc.archived: |
|
| 571 | 1 | result = f"Circuit {circuit_id} is archived. Update is forbidden." |
|
| 572 | 1 | log.debug("create_schedule result %s %s", result, 409) |
|
| 573 | 1 | raise HTTPException(409, detail=result) |
|
| 574 | |||
| 575 | # new schedule from dict |
||
| 576 | 1 | new_schedule = CircuitSchedule.from_dict(schedule_data) |
|
| 577 | |||
| 578 | # If there is no schedule, create the list |
||
| 579 | 1 | if not evc.circuit_scheduler: |
|
| 580 | 1 | evc.circuit_scheduler = [] |
|
| 581 | |||
| 582 | # Add the new schedule |
||
| 583 | 1 | evc.circuit_scheduler.append(new_schedule) |
|
| 584 | |||
| 585 | # Add schedule job |
||
| 586 | 1 | self.sched.add_circuit_job(evc, new_schedule) |
|
| 587 | |||
| 588 | # save circuit to mongodb |
||
| 589 | 1 | evc.sync() |
|
| 590 | |||
| 591 | 1 | result = new_schedule.as_dict() |
|
| 592 | 1 | status = 201 |
|
| 593 | |||
| 594 | 1 | log.debug("create_schedule result %s %s", result, status) |
|
| 595 | 1 | return JSONResponse(result, status_code=status) |
|
| 596 | |||
| 597 | 1 | @rest("/v2/evc/schedule/{schedule_id}", methods=["PATCH"]) |
|
| 598 | 1 | @validate_openapi(spec) |
|
| 599 | 1 | def update_schedule(self, request: Request) -> JSONResponse: |
|
| 600 | """Update a schedule. |
||
| 601 | |||
| 602 | Change all attributes from the given schedule from a EVC circuit. |
||
| 603 | The schedule ID is preserved as default. |
||
| 604 | Payload example: |
||
| 605 | { |
||
| 606 | "date": "2019-08-07T14:52:10.967Z", |
||
| 607 | "interval": "string", |
||
| 608 | "frequency": "1 * * *", |
||
| 609 | "action": "create" |
||
| 610 | } |
||
| 611 | """ |
||
| 612 | 1 | data = get_json_or_400(request, self.controller.loop) |
|
| 613 | 1 | schedule_id = request.path_params["schedule_id"] |
|
| 614 | 1 | log.debug("update_schedule /v2/evc/schedule/%s", schedule_id) |
|
| 615 | |||
| 616 | # Try to find a circuit schedule |
||
| 617 | 1 | evc, found_schedule = self._find_evc_by_schedule_id(schedule_id) |
|
| 618 | |||
| 619 | # Can not modify circuits deleted and archived |
||
| 620 | 1 | if not found_schedule: |
|
| 621 | 1 | result = f"schedule_id {schedule_id} not found" |
|
| 622 | 1 | log.debug("update_schedule result %s %s", result, 404) |
|
| 623 | 1 | raise HTTPException(404, detail=result) |
|
| 624 | 1 | if evc.archived: |
|
| 625 | 1 | result = f"Circuit {evc.id} is archived. Update is forbidden." |
|
| 626 | 1 | log.debug("update_schedule result %s %s", result, 409) |
|
| 627 | 1 | raise HTTPException(409, detail=result) |
|
| 628 | |||
| 629 | 1 | new_schedule = CircuitSchedule.from_dict(data) |
|
| 630 | 1 | new_schedule.id = found_schedule.id |
|
| 631 | # Remove the old schedule |
||
| 632 | 1 | evc.circuit_scheduler.remove(found_schedule) |
|
| 633 | # Append the modified schedule |
||
| 634 | 1 | evc.circuit_scheduler.append(new_schedule) |
|
| 635 | |||
| 636 | # Cancel all schedule jobs |
||
| 637 | 1 | self.sched.cancel_job(found_schedule.id) |
|
| 638 | # Add the new circuit schedule |
||
| 639 | 1 | self.sched.add_circuit_job(evc, new_schedule) |
|
| 640 | # Save EVC to mongodb |
||
| 641 | 1 | evc.sync() |
|
| 642 | |||
| 643 | 1 | result = new_schedule.as_dict() |
|
| 644 | 1 | status = 200 |
|
| 645 | |||
| 646 | 1 | log.debug("update_schedule result %s %s", result, status) |
|
| 647 | 1 | return JSONResponse(result, status_code=status) |
|
| 648 | |||
| 649 | 1 | @rest("/v2/evc/schedule/{schedule_id}", methods=["DELETE"]) |
|
| 650 | 1 | def delete_schedule(self, request: Request) -> JSONResponse: |
|
| 651 | """Remove a circuit schedule. |
||
| 652 | |||
| 653 | Remove the Schedule from EVC. |
||
| 654 | Remove the Schedule from cron job. |
||
| 655 | Save the EVC to the Storehouse. |
||
| 656 | """ |
||
| 657 | 1 | schedule_id = request.path_params["schedule_id"] |
|
| 658 | 1 | log.debug("delete_schedule /v2/evc/schedule/%s", schedule_id) |
|
| 659 | 1 | evc, found_schedule = self._find_evc_by_schedule_id(schedule_id) |
|
| 660 | |||
| 661 | # Can not modify circuits deleted and archived |
||
| 662 | 1 | if not found_schedule: |
|
| 663 | 1 | result = f"schedule_id {schedule_id} not found" |
|
| 664 | 1 | log.debug("delete_schedule result %s %s", result, 404) |
|
| 665 | 1 | raise HTTPException(404, detail=result) |
|
| 666 | |||
| 667 | 1 | if evc.archived: |
|
| 668 | 1 | result = f"Circuit {evc.id} is archived. Update is forbidden." |
|
| 669 | 1 | log.debug("delete_schedule result %s %s", result, 409) |
|
| 670 | 1 | raise HTTPException(409, detail=result) |
|
| 671 | |||
| 672 | # Remove the old schedule |
||
| 673 | 1 | evc.circuit_scheduler.remove(found_schedule) |
|
| 674 | |||
| 675 | # Cancel all schedule jobs |
||
| 676 | 1 | self.sched.cancel_job(found_schedule.id) |
|
| 677 | # Save EVC to mongodb |
||
| 678 | 1 | evc.sync() |
|
| 679 | |||
| 680 | 1 | result = "Schedule removed" |
|
| 681 | 1 | status = 200 |
|
| 682 | |||
| 683 | 1 | log.debug("delete_schedule result %s %s", result, status) |
|
| 684 | 1 | return JSONResponse(result, status_code=status) |
|
| 685 | |||
| 686 | 1 | def _is_duplicated_evc(self, evc): |
|
| 687 | """Verify if the circuit given is duplicated with the stored evcs. |
||
| 688 | |||
| 689 | Args: |
||
| 690 | evc (EVC): circuit to be analysed. |
||
| 691 | |||
| 692 | Returns: |
||
| 693 | boolean: True if the circuit is duplicated, otherwise False. |
||
| 694 | |||
| 695 | """ |
||
| 696 | 1 | for circuit in tuple(self.circuits.values()): |
|
| 697 | 1 | if not circuit.archived and circuit.shares_uni(evc): |
|
| 698 | 1 | return True |
|
| 699 | 1 | return False |
|
| 700 | |||
| 701 | 1 | @listen_to("kytos/topology.link_up") |
|
| 702 | 1 | def on_link_up(self, event): |
|
| 703 | """Change circuit when link is up or end_maintenance.""" |
||
| 704 | self.handle_link_up(event) |
||
| 705 | |||
| 706 | 1 | def handle_link_up(self, event): |
|
| 707 | """Change circuit when link is up or end_maintenance.""" |
||
| 708 | 1 | log.info("Event handle_link_up %s", event.content["link"]) |
|
| 709 | 1 | for evc in self.get_evcs_by_svc_level(): |
|
| 710 | 1 | if evc.is_enabled() and not evc.archived: |
|
| 711 | 1 | with evc.lock: |
|
| 712 | 1 | evc.handle_link_up(event.content["link"]) |
|
| 713 | |||
| 714 | 1 | @listen_to("kytos/topology.updated") |
|
| 715 | 1 | def on_topology_update(self, event): |
|
| 716 | """Capture topology update event""" |
||
| 717 | self.handle_topology_update(event) |
||
| 718 | |||
| 719 | 1 | def handle_topology_update(self, event): |
|
| 720 | """Handle topology update""" |
||
| 721 | 1 | with self._lock: |
|
| 722 | 1 | if ( |
|
| 723 | self._topology_updated_at |
||
| 724 | and self._topology_updated_at > event.timestamp |
||
| 725 | ): |
||
| 726 | return |
||
| 727 | 1 | self._topology_updated_at = event.timestamp |
|
| 728 | 1 | for evc in self.get_evcs_by_svc_level(): |
|
| 729 | 1 | if evc.is_enabled() and not evc.archived: |
|
| 730 | 1 | with evc.lock: |
|
| 731 | 1 | evc.handle_topology_update( |
|
| 732 | event.content["topology"].switches |
||
| 733 | ) |
||
| 734 | |||
| 735 | 1 | @listen_to("kytos/topology.link_down") |
|
| 736 | 1 | def on_link_down(self, event): |
|
| 737 | """Change circuit when link is down or under_mantenance.""" |
||
| 738 | self.handle_link_down(event) |
||
| 739 | |||
| 740 | 1 | def handle_link_down(self, event): |
|
| 741 | """Change circuit when link is down or under_mantenance.""" |
||
| 742 | 1 | link = event.content["link"] |
|
| 743 | 1 | log.info("Event handle_link_down %s", link) |
|
| 744 | 1 | switch_flows = {} |
|
| 745 | 1 | evcs_with_failover = [] |
|
| 746 | 1 | evcs_normal = [] |
|
| 747 | 1 | check_failover = [] |
|
| 748 | 1 | for evc in self.get_evcs_by_svc_level(): |
|
| 749 | 1 | if evc.is_affected_by_link(link): |
|
| 750 | # if there is no failover path, handles link down the |
||
| 751 | # tradditional way |
||
| 752 | 1 | if ( |
|
| 753 | not getattr(evc, 'failover_path', None) or |
||
| 754 | evc.is_failover_path_affected_by_link(link) |
||
| 755 | ): |
||
| 756 | 1 | evcs_normal.append(evc) |
|
| 757 | 1 | continue |
|
| 758 | 1 | for dpid, flows in evc.get_failover_flows().items(): |
|
| 759 | 1 | switch_flows.setdefault(dpid, []) |
|
| 760 | 1 | switch_flows[dpid].extend(flows) |
|
| 761 | 1 | evcs_with_failover.append(evc) |
|
| 762 | else: |
||
| 763 | 1 | check_failover.append(evc) |
|
| 764 | |||
| 765 | 1 | while switch_flows: |
|
| 766 | 1 | offset = settings.BATCH_SIZE or None |
|
| 767 | 1 | switches = list(switch_flows.keys()) |
|
| 768 | 1 | for dpid in switches: |
|
| 769 | 1 | emit_event( |
|
| 770 | self.controller, |
||
| 771 | context="kytos.flow_manager", |
||
| 772 | name="flows.install", |
||
| 773 | content={ |
||
| 774 | "dpid": dpid, |
||
| 775 | "flow_dict": {"flows": switch_flows[dpid][:offset]}, |
||
| 776 | } |
||
| 777 | ) |
||
| 778 | 1 | if offset is None or offset >= len(switch_flows[dpid]): |
|
| 779 | 1 | del switch_flows[dpid] |
|
| 780 | 1 | continue |
|
| 781 | 1 | switch_flows[dpid] = switch_flows[dpid][offset:] |
|
| 782 | 1 | time.sleep(settings.BATCH_INTERVAL) |
|
| 783 | |||
| 784 | 1 | for evc in evcs_with_failover: |
|
| 785 | 1 | with evc.lock: |
|
| 786 | 1 | old_path = evc.current_path |
|
| 787 | 1 | evc.current_path = evc.failover_path |
|
| 788 | 1 | evc.failover_path = old_path |
|
| 789 | 1 | evc.sync() |
|
| 790 | 1 | emit_event(self.controller, "redeployed_link_down", |
|
| 791 | content=map_evc_event_content(evc)) |
||
| 792 | 1 | log.info( |
|
| 793 | f"{evc} redeployed with failover due to link down {link.id}" |
||
| 794 | ) |
||
| 795 | |||
| 796 | 1 | for evc in evcs_normal: |
|
| 797 | 1 | emit_event( |
|
| 798 | self.controller, |
||
| 799 | "evc_affected_by_link_down", |
||
| 800 | content={"link_id": link.id} | map_evc_event_content(evc) |
||
| 801 | ) |
||
| 802 | |||
| 803 | # After handling the hot path, check if new failover paths are needed. |
||
| 804 | # Note that EVCs affected by link down will generate a KytosEvent for |
||
| 805 | # deployed|redeployed, which will trigger the failover path setup. |
||
| 806 | # Thus, we just need to further check the check_failover list |
||
| 807 | 1 | for evc in check_failover: |
|
| 808 | 1 | if evc.is_failover_path_affected_by_link(link): |
|
| 809 | 1 | evc.setup_failover_path() |
|
| 810 | |||
| 811 | 1 | @listen_to("kytos/mef_eline.evc_affected_by_link_down") |
|
| 812 | 1 | def on_evc_affected_by_link_down(self, event): |
|
| 813 | """Change circuit when link is down or under_mantenance.""" |
||
| 814 | self.handle_evc_affected_by_link_down(event) |
||
| 815 | |||
| 816 | 1 | def handle_evc_affected_by_link_down(self, event): |
|
| 817 | """Change circuit when link is down or under_mantenance.""" |
||
| 818 | 1 | evc = self.circuits.get(event.content["evc_id"]) |
|
| 819 | 1 | link_id = event.content['link_id'] |
|
| 820 | 1 | if not evc: |
|
| 821 | 1 | return |
|
| 822 | 1 | with evc.lock: |
|
| 823 | 1 | result = evc.handle_link_down() |
|
| 824 | 1 | event_name = "error_redeploy_link_down" |
|
| 825 | 1 | if result: |
|
| 826 | 1 | log.info(f"{evc} redeployed due to link down {link_id}") |
|
| 827 | 1 | event_name = "redeployed_link_down" |
|
| 828 | 1 | emit_event(self.controller, event_name, |
|
| 829 | content=map_evc_event_content(evc)) |
||
| 830 | |||
| 831 | 1 | @listen_to("kytos/mef_eline.(redeployed_link_(up|down)|deployed)") |
|
| 832 | 1 | def on_evc_deployed(self, event): |
|
| 833 | """Handle EVC deployed|redeployed_link_down.""" |
||
| 834 | self.handle_evc_deployed(event) |
||
| 835 | |||
| 836 | 1 | def handle_evc_deployed(self, event): |
|
| 837 | """Setup failover path on evc deployed.""" |
||
| 838 | 1 | evc = self.circuits.get(event.content["evc_id"]) |
|
| 839 | 1 | if not evc: |
|
| 840 | 1 | return |
|
| 841 | 1 | with evc.lock: |
|
| 842 | 1 | evc.setup_failover_path() |
|
| 843 | |||
| 844 | 1 | @listen_to("kytos/topology.topology_loaded") |
|
| 845 | 1 | def on_topology_loaded(self, event): # pylint: disable=unused-argument |
|
| 846 | """Load EVCs once the topology is available.""" |
||
| 847 | self.load_all_evcs() |
||
| 848 | |||
| 849 | 1 | def load_all_evcs(self): |
|
| 850 | """Try to load all EVCs on startup.""" |
||
| 851 | 1 | circuits = self.mongo_controller.get_circuits()['circuits'].items() |
|
| 852 | 1 | for circuit_id, circuit in circuits: |
|
| 853 | 1 | if circuit_id not in self.circuits: |
|
| 854 | 1 | self._load_evc(circuit) |
|
| 855 | |||
| 856 | 1 | def _load_evc(self, circuit_dict): |
|
| 857 | """Load one EVC from mongodb to memory.""" |
||
| 858 | 1 | try: |
|
| 859 | 1 | evc = self._evc_from_dict(circuit_dict) |
|
| 860 | 1 | except ValueError as exception: |
|
| 861 | 1 | log.error( |
|
| 862 | f"Could not load EVC: dict={circuit_dict} error={exception}" |
||
| 863 | ) |
||
| 864 | 1 | return None |
|
| 865 | |||
| 866 | 1 | if evc.archived: |
|
| 867 | 1 | return None |
|
| 868 | 1 | evc.deactivate() |
|
| 869 | 1 | evc.sync() |
|
| 870 | 1 | self.circuits.setdefault(evc.id, evc) |
|
| 871 | 1 | self.sched.add(evc) |
|
| 872 | 1 | return evc |
|
| 873 | |||
| 874 | 1 | @listen_to("kytos/flow_manager.flow.error") |
|
| 875 | 1 | def on_flow_mod_error(self, event): |
|
| 876 | """Handle flow mod errors related to an EVC.""" |
||
| 877 | self.handle_flow_mod_error(event) |
||
| 878 | |||
| 879 | 1 | def handle_flow_mod_error(self, event): |
|
| 880 | """Handle flow mod errors related to an EVC.""" |
||
| 881 | 1 | flow = event.content["flow"] |
|
| 882 | 1 | command = event.content.get("error_command") |
|
| 883 | 1 | if command != "add": |
|
| 884 | return |
||
| 885 | 1 | evc = self.circuits.get(EVC.get_id_from_cookie(flow.cookie)) |
|
| 886 | 1 | if evc: |
|
| 887 | 1 | evc.remove_current_flows() |
|
| 888 | |||
| 889 | 1 | def _evc_dict_with_instances(self, evc_dict): |
|
| 890 | """Convert some dict values to instance of EVC classes. |
||
| 891 | |||
| 892 | This method will convert: [UNI, Link] |
||
| 893 | """ |
||
| 894 | 1 | data = evc_dict.copy() # Do not modify the original dict |
|
| 895 | 1 | for attribute, value in data.items(): |
|
| 896 | # Get multiple attributes. |
||
| 897 | # Ex: uni_a, uni_z |
||
| 898 | 1 | if "uni" in attribute: |
|
| 899 | 1 | try: |
|
| 900 | 1 | data[attribute] = self._uni_from_dict(value) |
|
| 901 | 1 | except ValueError as exception: |
|
| 902 | 1 | result = "Error creating UNI: Invalid value" |
|
| 903 | 1 | raise ValueError(result) from exception |
|
| 904 | |||
| 905 | 1 | if attribute == "circuit_scheduler": |
|
| 906 | 1 | data[attribute] = [] |
|
| 907 | 1 | for schedule in value: |
|
| 908 | 1 | data[attribute].append(CircuitSchedule.from_dict(schedule)) |
|
| 909 | |||
| 910 | # Get multiple attributes. |
||
| 911 | # Ex: primary_links, |
||
| 912 | # backup_links, |
||
| 913 | # current_links_cache, |
||
| 914 | # primary_links_cache, |
||
| 915 | # backup_links_cache |
||
| 916 | 1 | if "links" in attribute: |
|
| 917 | 1 | data[attribute] = [ |
|
| 918 | self._link_from_dict(link) for link in value |
||
| 919 | ] |
||
| 920 | |||
| 921 | # Ex: current_path, |
||
| 922 | # primary_path, |
||
| 923 | # backup_path |
||
| 924 | 1 | if "path" in attribute and attribute != "dynamic_backup_path": |
|
| 925 | 1 | data[attribute] = Path( |
|
| 926 | [self._link_from_dict(link) for link in value] |
||
| 927 | ) |
||
| 928 | |||
| 929 | 1 | return data |
|
| 930 | |||
| 931 | 1 | def _evc_from_dict(self, evc_dict): |
|
| 932 | 1 | data = self._evc_dict_with_instances(evc_dict) |
|
| 933 | 1 | data["table_group"] = self.table_group |
|
| 934 | 1 | return EVC(self.controller, **data) |
|
| 935 | |||
| 936 | 1 | def _uni_from_dict(self, uni_dict): |
|
| 937 | """Return a UNI object from python dict.""" |
||
| 938 | 1 | if uni_dict is None: |
|
| 939 | 1 | return False |
|
| 940 | |||
| 941 | 1 | interface_id = uni_dict.get("interface_id") |
|
| 942 | 1 | interface = self.controller.get_interface_by_id(interface_id) |
|
| 943 | 1 | if interface is None: |
|
| 944 | 1 | result = ( |
|
| 945 | "Error creating UNI:" |
||
| 946 | + f"Could not instantiate interface {interface_id}" |
||
| 947 | ) |
||
| 948 | 1 | raise ValueError(result) from ValueError |
|
| 949 | |||
| 950 | 1 | tag_dict = uni_dict.get("tag", None) |
|
| 951 | 1 | if tag_dict: |
|
| 952 | 1 | tag = TAG.from_dict(tag_dict) |
|
| 953 | else: |
||
| 954 | 1 | tag = None |
|
| 955 | 1 | uni = UNI(interface, tag) |
|
| 956 | |||
| 957 | 1 | return uni |
|
| 958 | |||
| 959 | 1 | def _link_from_dict(self, link_dict): |
|
| 960 | """Return a Link object from python dict.""" |
||
| 961 | 1 | id_a = link_dict.get("endpoint_a").get("id") |
|
| 962 | 1 | id_b = link_dict.get("endpoint_b").get("id") |
|
| 963 | |||
| 964 | 1 | endpoint_a = self.controller.get_interface_by_id(id_a) |
|
| 965 | 1 | endpoint_b = self.controller.get_interface_by_id(id_b) |
|
| 966 | 1 | if not endpoint_a: |
|
| 967 | 1 | error_msg = f"Could not get interface endpoint_a id {id_a}" |
|
| 968 | 1 | raise ValueError(error_msg) |
|
| 969 | 1 | if not endpoint_b: |
|
| 970 | error_msg = f"Could not get interface endpoint_b id {id_b}" |
||
| 971 | raise ValueError(error_msg) |
||
| 972 | |||
| 973 | 1 | link = Link(endpoint_a, endpoint_b) |
|
| 974 | 1 | if "metadata" in link_dict: |
|
| 975 | 1 | link.extend_metadata(link_dict.get("metadata")) |
|
| 976 | |||
| 977 | 1 | s_vlan = link.get_metadata("s_vlan") |
|
| 978 | 1 | if s_vlan: |
|
| 979 | 1 | tag = TAG.from_dict(s_vlan) |
|
| 980 | 1 | if tag is False: |
|
| 981 | error_msg = f"Could not instantiate tag from dict {s_vlan}" |
||
| 982 | raise ValueError(error_msg) |
||
| 983 | 1 | link.update_metadata("s_vlan", tag) |
|
| 984 | 1 | return link |
|
| 985 | |||
| 986 | 1 | def _find_evc_by_schedule_id(self, schedule_id): |
|
| 987 | """ |
||
| 988 | Find an EVC and CircuitSchedule based on schedule_id. |
||
| 989 | |||
| 990 | :param schedule_id: Schedule ID |
||
| 991 | :return: EVC and Schedule |
||
| 992 | """ |
||
| 993 | 1 | circuits = self._get_circuits_buffer() |
|
| 994 | 1 | found_schedule = None |
|
| 995 | 1 | evc = None |
|
| 996 | |||
| 997 | # pylint: disable=unused-variable |
||
| 998 | 1 | for c_id, circuit in circuits.items(): |
|
| 999 | 1 | for schedule in circuit.circuit_scheduler: |
|
| 1000 | 1 | if schedule.id == schedule_id: |
|
| 1001 | 1 | found_schedule = schedule |
|
| 1002 | 1 | evc = circuit |
|
| 1003 | 1 | break |
|
| 1004 | 1 | if found_schedule: |
|
| 1005 | 1 | break |
|
| 1006 | 1 | return evc, found_schedule |
|
| 1007 | |||
| 1008 | 1 | def _get_circuits_buffer(self): |
|
| 1009 | """ |
||
| 1010 | Return the circuit buffer. |
||
| 1011 | |||
| 1012 | If the buffer is empty, try to load data from mongodb. |
||
| 1013 | """ |
||
| 1014 | 1 | if not self.circuits: |
|
| 1015 | # Load circuits from mongodb to buffer |
||
| 1016 | 1 | circuits = self.mongo_controller.get_circuits()['circuits'] |
|
| 1017 | 1 | for c_id, circuit in circuits.items(): |
|
| 1018 | 1 | evc = self._evc_from_dict(circuit) |
|
| 1019 | 1 | self.circuits[c_id] = evc |
|
| 1020 | 1 | return self.circuits |
|
| 1021 | |||
| 1022 | # pylint: disable=attribute-defined-outside-init |
||
| 1023 | 1 | @alisten_to("kytos/of_multi_table.enable_table") |
|
| 1024 | 1 | async def on_table_enabled(self, event): |
|
| 1025 | """Handle a recently table enabled.""" |
||
| 1026 | 1 | table_group = event.content.get("mef_eline", None) |
|
| 1027 | 1 | if not table_group: |
|
| 1028 | return |
||
| 1029 | 1 | for group in table_group: |
|
| 1030 | 1 | if group not in settings.TABLE_GROUP_ALLOWED: |
|
| 1031 | 1 | log.error(f'The table group "{group}" is not allowed for ' |
|
| 1032 | f'mef_eline. Allowed table groups are ' |
||
| 1033 | f'{settings.TABLE_GROUP_ALLOWED}') |
||
| 1034 | 1 | return |
|
| 1035 | 1 | self.table_group.update(table_group) |
|
| 1036 | 1 | content = {"group_table": self.table_group} |
|
| 1037 | 1 | name = "kytos/mef_eline.enable_table" |
|
| 1038 | await aemit_event(self.controller, name, content) |
||
| 1039 |