Code Duplication    Length = 31-31 lines in 2 locations

main.py 2 locations

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