Code Duplication    Length = 27-27 lines in 2 locations

main.py 2 locations

@@ 364-390 (lines=27) @@
361
        self.notify_topology_update()
362
        return jsonify("Operation successful"), 200
363
364
    @rest('v3/interfaces/switch/<dpid>/disable', methods=['POST'])
365
    @rest('v3/interfaces/<interface_disable_id>/disable', methods=['POST'])
366
    def disable_interface(self, interface_disable_id=None, dpid=None):
367
        """Administratively disable interfaces in the topology."""
368
        if dpid is None:
369
            dpid = ":".join(interface_disable_id.split(":")[:-1])
370
        try:
371
            switch = self.controller.switches[dpid]
372
        except KeyError as exc:
373
            return jsonify(f"Switch not found: {exc}"), 404
374
375
        if interface_disable_id:
376
            interface_number = int(interface_disable_id.split(":")[-1])
377
378
            try:
379
                interface = switch.interfaces[interface_number]
380
                self.topo_controller.disable_interface(interface.id)
381
                interface.disable()
382
            except KeyError:
383
                msg = f"Switch {dpid} interface {interface_number} not found"
384
                return jsonify(msg), 404
385
        else:
386
            for interface in switch.interfaces.values():
387
                interface.disable()
388
            self.topo_controller.upsert_switch(switch.id, switch.as_dict())
389
        self.notify_topology_update()
390
        return jsonify("Operation successful"), 200
391
392
    @rest('v3/interfaces/<interface_id>/metadata')
393
    def get_interface_metadata(self, interface_id):
@@ 336-362 (lines=27) @@
333
334
        return jsonify({'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, interface_enable_id=None, dpid=None):
339
        """Administratively enable interfaces in the topology."""
340
        if dpid is None:
341
            dpid = ":".join(interface_enable_id.split(":")[:-1])
342
        try:
343
            switch = self.controller.switches[dpid]
344
        except KeyError as exc:
345
            return jsonify(f"Switch not found: {exc}"), 404
346
347
        if interface_enable_id:
348
            interface_number = int(interface_enable_id.split(":")[-1])
349
350
            try:
351
                interface = switch.interfaces[interface_number]
352
                self.topo_controller.enable_interface(interface.id)
353
                interface.enable()
354
            except KeyError:
355
                msg = f"Switch {dpid} interface {interface_number} not found"
356
                return jsonify(msg), 404
357
        else:
358
            for interface in switch.interfaces.values():
359
                interface.enable()
360
            self.topo_controller.upsert_switch(switch.id, switch.as_dict())
361
        self.notify_topology_update()
362
        return jsonify("Operation successful"), 200
363
364
    @rest('v3/interfaces/switch/<dpid>/disable', methods=['POST'])
365
    @rest('v3/interfaces/<interface_disable_id>/disable', methods=['POST'])