Code Duplication    Length = 31-31 lines in 2 locations

main.py 2 locations

@@ 380-410 (lines=31) @@
377
        self.notify_topology_update()
378
        return JSONResponse("Operation successful")
379
380
    @rest('v3/interfaces/switch/{dpid}/disable', methods=['POST'])
381
    @rest('v3/interfaces/{interface_disable_id}/disable', methods=['POST'])
382
    def disable_interface(self, request: Request) -> JSONResponse:
383
        """Administratively disable interfaces in the topology."""
384
        interface_disable_id = request.path_params.get("interface_disable_id")
385
        dpid = request.path_params.get("dpid")
386
        if dpid is None:
387
            dpid = ":".join(interface_disable_id.split(":")[:-1])
388
        try:
389
            switch = self.controller.switches[dpid]
390
        except KeyError:
391
            raise HTTPException(404, detail="Switch not found")
392
393
        if interface_disable_id:
394
            interface_number = int(interface_disable_id.split(":")[-1])
395
396
            try:
397
                interface = switch.interfaces[interface_number]
398
                self.topo_controller.disable_interface(interface.id)
399
                interface.disable()
400
                self.notify_interface_link_status(interface, "link disabled")
401
            except KeyError:
402
                msg = f"Switch {dpid} interface {interface_number} not found"
403
                raise HTTPException(404, detail=msg)
404
        else:
405
            for interface in switch.interfaces.copy().values():
406
                interface.disable()
407
                self.notify_interface_link_status(interface, "link disabled")
408
            self.topo_controller.upsert_switch(switch.id, switch.as_dict())
409
        self.notify_topology_update()
410
        return JSONResponse("Operation successful")
411
412
    @rest('v3/interfaces/{interface_id}/metadata')
413
    def get_interface_metadata(self, request: Request) -> JSONResponse:
@@ 348-378 (lines=31) @@
345
346
        return JSONResponse({'interfaces': interfaces})
347
348
    @rest('v3/interfaces/switch/{dpid}/enable', methods=['POST'])
349
    @rest('v3/interfaces/{interface_enable_id}/enable', methods=['POST'])
350
    def enable_interface(self, request: Request) -> JSONResponse:
351
        """Administratively enable interfaces in the topology."""
352
        interface_enable_id = request.path_params.get("interface_enable_id")
353
        dpid = request.path_params.get("dpid")
354
        if dpid is None:
355
            dpid = ":".join(interface_enable_id.split(":")[:-1])
356
        try:
357
            switch = self.controller.switches[dpid]
358
        except KeyError:
359
            raise HTTPException(404, detail="Switch not found")
360
361
        if interface_enable_id:
362
            interface_number = int(interface_enable_id.split(":")[-1])
363
364
            try:
365
                interface = switch.interfaces[interface_number]
366
                self.topo_controller.enable_interface(interface.id)
367
                interface.enable()
368
                self.notify_interface_link_status(interface, "link enabled")
369
            except KeyError:
370
                msg = f"Switch {dpid} interface {interface_number} not found"
371
                raise HTTPException(404, detail=msg)
372
        else:
373
            for interface in switch.interfaces.copy().values():
374
                interface.enable()
375
                self.notify_interface_link_status(interface, "link enabled")
376
            self.topo_controller.upsert_switch(switch.id, switch.as_dict())
377
        self.notify_topology_update()
378
        return JSONResponse("Operation successful")
379
380
    @rest('v3/interfaces/switch/{dpid}/disable', methods=['POST'])
381
    @rest('v3/interfaces/{interface_disable_id}/disable', methods=['POST'])