Code Duplication    Length = 31-31 lines in 2 locations

main.py 2 locations

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