Code Duplication    Length = 29-30 lines in 2 locations

main.py 2 locations

@@ 188-217 (lines=30) @@
185
        return jsonify({msg_error:
186
                        error_list}), 409
187
188
    @rest('v3/interfaces/disable', methods=['POST'])
189
    @rest('v3/interfaces/<interface_disable_id>/disable', methods=['POST'])
190
    def disable_interface(self, interface_disable_id=None):
191
        """Administratively disable a list of interfaces in the topology."""
192
        error_list = []  # List of interfaces that were not deactivated.
193
        interface_ids = []
194
        msg_error = "Some interfaces couldn't be found and deactivated: "
195
        if not interface_disable_id:
196
            interface_ids = self._get_data(request)
197
        else:
198
            interface_ids.append(interface_disable_id)
199
        for interface_id in interface_ids:
200
            switch_id = ":".join(interface_id.split(":")[:-1])
201
            interface_number = int(interface_id.split(":")[-1])
202
203
            try:
204
                switch = self.controller.switches[switch_id]
205
            except KeyError as exc:
206
                return jsonify(f"Switch not found: {exc}"), 404
207
208
            try:
209
                switch.interfaces[interface_number].disable()
210
            except KeyError as exc:
211
                error_list.append(f"Switch {switch_id} Interface {exc}")
212
213
        if not error_list:
214
            return jsonify("Operation successful"), 201
215
216
        return jsonify({msg_error:
217
                        error_list}), 409
218
219
    @rest('v3/interfaces/<interface_id>/metadata')
220
    def get_interface_metadata(self, interface_id):
@@ 158-186 (lines=29) @@
155
156
        return jsonify({'interfaces': interfaces})
157
158
    @rest('v3/interfaces/enable', methods=['POST'])
159
    @rest('v3/interfaces/<interface_enable_id>/enable', methods=['POST'])
160
    def enable_interface(self, interface_enable_id=None):
161
        """Administratively enable a list of interfaces in the topology."""
162
        error_list = []  # List of interfaces that were not activated.
163
        interface_ids = []
164
        msg_error = "Some interfaces couldn't be found and activated: "
165
        if not interface_enable_id:
166
            interface_ids = self._get_data(request)
167
        else:
168
            interface_ids.append(interface_enable_id)
169
        for interface_id in interface_ids:
170
            switch_id = ":".join(interface_id.split(":")[:-1])
171
            interface_number = int(interface_id.split(":")[-1])
172
173
            try:
174
                switch = self.controller.switches[switch_id]
175
            except KeyError as exc:
176
                return jsonify(f"Switch not found: {exc}"), 404
177
178
            try:
179
                switch.interfaces[interface_number].enable()
180
            except KeyError as exc:
181
                error_list.append(f"Switch {switch_id} Interface {exc}")
182
183
        if not error_list:
184
            return jsonify("Operation successful"), 201
185
        return jsonify({msg_error:
186
                        error_list}), 409
187
188
    @rest('v3/interfaces/disable', methods=['POST'])
189
    @rest('v3/interfaces/<interface_disable_id>/disable', methods=['POST'])