Code Duplication    Length = 31-31 lines in 2 locations

main.py 2 locations

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