Code Duplication    Length = 31-31 lines in 2 locations

main.py 2 locations

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