Code Duplication    Length = 31-31 lines in 2 locations

main.py 2 locations

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