Code Duplication    Length = 42-48 lines in 2 locations

main.py 2 locations

@@ 444-491 (lines=48) @@
441
442
        return JSONResponse(status)
443
444
    @rest("v1/evc/disable", methods=["POST"])
445
    def disable_telemetry(self, request: Request) -> JSONResponse:
446
        """REST to disable/remove INT flows for an EVC_ID
447
        Args:
448
            {"evc_ids": [list of evc_ids] }
449
        Returns:
450
            200 if successful
451
            400 is otherwise
452
        """
453
        try:
454
            content = get_json_or_400(request, self.controller.loop)
455
            evc_ids = content["evc_ids"]
456
        except (TypeError, KeyError):
457
            raise HTTPException(400, detail=f"Invalid payload: {content}")
458
459
        status = {}
460
461
        evcs = get_evcs() if len(evc_ids) != 1 else get_evc(evc_ids[0])
462
463
        # TODO extract this and cover proxy port validations too
464
        for evc_id in evc_ids:
465
            if evc_id not in evcs:
466
                raise HTTPException(404, detail=f"EVC {evc_id} doesn't exist")
467
            if not has_int_enabled(evcs[evc_id]):
468
                raise HTTPException(
469
                    400, detail=f"EVC {evc_id} doesn't have INT enabled"
470
                )
471
472
        if not evc_ids:
473
            # Enable telemetry for ALL INT EVCs.
474
            evcs = {k: v for k, v in evcs.items() if has_int_enabled(v)}
475
        else:
476
            evcs = {evc_id: evcs[evc_id] for evc_id in evc_ids}
477
478
        # Process each EVC individually
479
        # TODO dispatch in batch and update metadata in bulk shortly after
480
        for evc_id, evc in evcs.items():
481
            try:
482
                status[evc_id] = self.decommission_int(evc)
483
            except EvcHasNoINT as err_msg:
484
                # Ignore since it is not an issue.
485
                status[evc_id] = err_msg.message
486
            except ErrorBase as err_msg:
487
                # Rollback INT configuration. This error will lead to inconsistency.
488
                # Critical
489
                status[evc_id] = err_msg.message
490
491
        return JSONResponse(status)
492
493
    @rest("v1/evc")
494
    def get_evcs(self, _request: Request) -> JSONResponse:
@@ 401-442 (lines=42) @@
398
399
    # REST methods
400
401
    @rest("v1/evc/enable", methods=["POST"])
402
    def enable_telemetry(self, request: Request) -> JSONResponse:
403
        """REST to enable/create INT flows for one or more EVC_IDs.
404
                  evcs are provided via POST as a list
405
        Args:
406
            {"evc_ids": [list of evc_ids] }
407
408
        Returns:
409
            200 and outcomes for each evc listed.
410
        """
411
412
        try:
413
            content = get_json_or_400(request, self.controller.loop)
414
            evc_ids = content["evc_ids"]
415
        except (TypeError, KeyError):
416
            raise HTTPException(400, detail=f"Invalid payload: {content}")
417
418
        status = {}
419
        evcs = get_evcs() if len(evc_ids) != 1 else get_evc(evc_ids[0])
420
421
        # TODO extract this and cover proxy port validations too
422
        for evc_id in evc_ids:
423
            if evc_id not in evcs:
424
                raise HTTPException(404, detail=f"EVC {evc_id} doesn't exist")
425
            if has_int_enabled(evcs[evc_id]):
426
                raise HTTPException(400, detail=f"EVC {evc_id} already has INT enabled")
427
428
        if not evc_ids:
429
            # Enable telemetry for ALL non INT EVCs.
430
            evcs = {k: v for k, v in evcs.items() if not has_int_enabled(v)}
431
        else:
432
            evcs = {evc_id: evcs[evc_id] for evc_id in evc_ids}
433
434
        # Process each EVC individually
435
        # TODO dispatch in batch and update metadata in bulk shortly after
436
        for evc_id, evc in evcs.items():
437
            try:
438
                status[evc_id] = self.provision_int(evc)
439
            except ErrorBase as err_msg:
440
                status[evc_id] = err_msg.message
441
442
        return JSONResponse(status)
443
444
    @rest("v1/evc/disable", methods=["POST"])
445
    def disable_telemetry(self, request: Request) -> JSONResponse: