Code Duplication    Length = 27-27 lines in 2 locations

main.py 2 locations

@@ 374-400 (lines=27) @@
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
            except KeyError:
393
                msg = f"Switch {dpid} interface {interface_number} not found"
394
                return jsonify(msg), 404
395
        else:
396
            for interface in switch.interfaces.values():
397
                interface.disable()
398
            self.topo_controller.upsert_switch(switch.id, switch.as_dict())
399
        self.notify_topology_update()
400
        return jsonify("Operation successful"), 200
401
402
    @rest('v3/interfaces/<interface_id>/metadata')
403
    def get_interface_metadata(self, interface_id):
@@ 346-372 (lines=27) @@
343
344
        return jsonify({'interfaces': interfaces})
345
346
    @rest('v3/interfaces/switch/<dpid>/enable', methods=['POST'])
347
    @rest('v3/interfaces/<interface_enable_id>/enable', methods=['POST'])
348
    def enable_interface(self, interface_enable_id=None, dpid=None):
349
        """Administratively enable interfaces in the topology."""
350
        if dpid is None:
351
            dpid = ":".join(interface_enable_id.split(":")[:-1])
352
        try:
353
            switch = self.controller.switches[dpid]
354
        except KeyError as exc:
355
            return jsonify(f"Switch not found: {exc}"), 404
356
357
        if interface_enable_id:
358
            interface_number = int(interface_enable_id.split(":")[-1])
359
360
            try:
361
                interface = switch.interfaces[interface_number]
362
                self.topo_controller.enable_interface(interface.id)
363
                interface.enable()
364
            except KeyError:
365
                msg = f"Switch {dpid} interface {interface_number} not found"
366
                return jsonify(msg), 404
367
        else:
368
            for interface in switch.interfaces.values():
369
                interface.enable()
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'])