Code Duplication    Length = 29-29 lines in 2 locations

main.py 2 locations

@@ 374-402 (lines=29) @@
371
        self.notify_topology_update()
372
        return jsonify("Operation successful"), 200
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, interface_disable_id=None, dpid=None):
377
        """Administratively disable interfaces in the topology."""
378
        if dpid is None:
379
            dpid = ":".join(interface_disable_id.split(":")[:-1])
380
        try:
381
            switch = self.controller.switches[dpid]
382
        except KeyError as exc:
383
            return jsonify(f"Switch not found: {exc}"), 404
384
385
        if interface_disable_id:
386
            interface_number = int(interface_disable_id.split(":")[-1])
387
388
            try:
389
                interface = switch.interfaces[interface_number]
390
                self.topo_controller.disable_interface(interface.id)
391
                interface.disable()
392
                self.notify_interface_link_status(interface, "link disabled")
393
            except KeyError:
394
                msg = f"Switch {dpid} interface {interface_number} not found"
395
                return jsonify(msg), 404
396
        else:
397
            for interface in switch.interfaces.copy().values():
398
                interface.disable()
399
                self.notify_interface_link_status(interface, "link disabled")
400
            self.topo_controller.upsert_switch(switch.id, switch.as_dict())
401
        self.notify_topology_update()
402
        return jsonify("Operation successful"), 200
403
404
    @rest('v3/interfaces/<interface_id>/metadata')
405
    def get_interface_metadata(self, interface_id):
@@ 344-372 (lines=29) @@
341
342
        return jsonify({'interfaces': interfaces})
343
344
    @rest('v3/interfaces/switch/<dpid>/enable', methods=['POST'])
345
    @rest('v3/interfaces/<interface_enable_id>/enable', methods=['POST'])
346
    def enable_interface(self, interface_enable_id=None, dpid=None):
347
        """Administratively enable interfaces in the topology."""
348
        if dpid is None:
349
            dpid = ":".join(interface_enable_id.split(":")[:-1])
350
        try:
351
            switch = self.controller.switches[dpid]
352
        except KeyError as exc:
353
            return jsonify(f"Switch not found: {exc}"), 404
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
                return jsonify(msg), 404
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 jsonify("Operation successful"), 200
373
374
    @rest('v3/interfaces/switch/<dpid>/disable', methods=['POST'])
375
    @rest('v3/interfaces/<interface_disable_id>/disable', methods=['POST'])