Code Duplication    Length = 40-41 lines in 4 locations

lib/exabgp/reactor/api/command/announce.py 4 locations

@@ 25-64 (lines=40) @@
22
    pass
23
24
25
@Command.register('text', 'announce route')
26
def announce_route(self, reactor, service, line):
27
    def callback():
28
        try:
29
            descriptions, command = extract_neighbors(line)
30
            peers = match_neighbors(reactor.peers(), descriptions)
31
            if not peers:
32
                self.log_failure('no neighbor matching the command : %s' % command)
33
                reactor.processes.answer_error(service)
34
                yield True
35
                return
36
37
            changes = self.api_route(command)
38
            if not changes:
39
                self.log_failure('command could not parse route in : %s' % command)
40
                reactor.processes.answer_error(service)
41
                yield True
42
                return
43
44
            for change in changes:
45
                if not ParseStaticRoute.check(change):
46
                    self.log_message(
47
                        'invalid route for %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive())
48
                    )
49
                    continue
50
                change.nlri.action = OUT.ANNOUNCE
51
                reactor.configuration.inject_change(peers, change)
52
                self.log_message(
53
                    'route added to %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive())
54
                )
55
                yield False
56
57
            reactor.processes.answer_done(service)
58
        except ValueError:
59
            self.log_failure('issue parsing the route')
60
            reactor.processes.answer_error(service)
61
            yield True
62
        except IndexError:
63
            self.log_failure('issue parsing the route')
64
            reactor.processes.answer_error(service)
65
            yield True
66
67
    reactor.asynchronous.schedule(service, line, callback())
@@ 168-208 (lines=41) @@
165
    return True
166
167
168
@Command.register('text', 'withdraw vpls')
169
def withdraw_vpls(self, reactor, service, line):
170
    def callback():
171
        try:
172
            descriptions, command = extract_neighbors(line)
173
            peers = match_neighbors(reactor.peers(), descriptions)
174
            if not peers:
175
                self.log_failure('no neighbor matching the command : %s' % command)
176
                reactor.processes.answer_error(service)
177
                yield True
178
                return
179
180
            changes = self.api_vpls(command)
181
182
            if not changes:
183
                self.log_failure('command could not parse vpls in : %s' % command)
184
                reactor.processes.answer_error(service)
185
                yield True
186
                return
187
188
            for change in changes:
189
                change.nlri.action = OUT.WITHDRAW
190
                if reactor.configuration.inject_change(peers, change):
191
                    self.log_message(
192
                        'vpls removed from %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive())
193
                    )
194
                    yield False
195
                else:
196
                    self.log_failure(
197
                        'vpls not found on %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive())
198
                    )
199
                    yield False
200
201
            reactor.processes.answer_done(service)
202
        except ValueError:
203
            self.log_failure('issue parsing the vpls')
204
            reactor.processes.answer_error(service)
205
            yield True
206
        except IndexError:
207
            self.log_failure('issue parsing the vpls')
208
            reactor.processes.answer_error(service)
209
            yield True
210
211
    reactor.asynchronous.schedule(service, line, callback())
@@ 344-383 (lines=40) @@
341
    return True
342
343
344
@Command.register('text', 'withdraw flow')
345
def withdraw_flow(self, reactor, service, line):
346
    def callback():
347
        try:
348
            descriptions, command = extract_neighbors(line)
349
            peers = match_neighbors(reactor.peers(), descriptions)
350
            if not peers:
351
                self.log_failure('no neighbor matching the command : %s' % command)
352
                reactor.processes.answer_error(service)
353
                yield True
354
                return
355
356
            changes = self.api_flow(command)
357
358
            if not changes:
359
                self.log_failure('command could not parse flow in : %s' % command)
360
                reactor.processes.answer_error(service)
361
                yield True
362
                return
363
364
            for change in changes:
365
                change.nlri.action = OUT.WITHDRAW
366
                if reactor.configuration.inject_change(peers, change):
367
                    self.log_message(
368
                        'flow removed from %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive())
369
                    )
370
                else:
371
                    self.log_failure(
372
                        'flow not found on %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive())
373
                    )
374
                yield False
375
376
            reactor.processes.answer_done(service)
377
        except ValueError:
378
            self.log_failure('issue parsing the flow')
379
            reactor.processes.answer_error(service)
380
            yield True
381
        except IndexError:
382
            self.log_failure('issue parsing the flow')
383
            reactor.processes.answer_error(service)
384
            yield True
385
386
    reactor.asynchronous.schedule(service, line, callback())
@@ 257-296 (lines=40) @@
254
    return True
255
256
257
@Command.register('text', 'withdraw attributes')
258
def withdraw_attribute(self, reactor, service, line):
259
    def callback():
260
        try:
261
            descriptions, command = extract_neighbors(line)
262
            peers = match_neighbors(reactor.peers(), descriptions)
263
            if not peers:
264
                self.log_failure('no neighbor matching the command : %s' % command)
265
                reactor.processes.answer_error(service)
266
                yield True
267
                return
268
269
            changes = self.api_attributes(command, peers)
270
            if not changes:
271
                self.log_failure('command could not parse route in : %s' % command)
272
                reactor.processes.answer_error(service)
273
                yield True
274
                return
275
276
            for change in changes:
277
                change.nlri.action = OUT.WITHDRAW
278
                if reactor.configuration.inject_change(peers, change):
279
                    self.log_message(
280
                        'route removed from %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive())
281
                    )
282
                    yield False
283
                else:
284
                    self.log_failure(
285
                        'route not found on %s : %s' % (', '.join(peers) if peers else 'all peers', change.extensive())
286
                    )
287
                    yield False
288
289
            reactor.processes.answer_done(service)
290
        except ValueError:
291
            self.log_failure('issue parsing the route')
292
            reactor.processes.answer_error(service)
293
            yield True
294
        except IndexError:
295
            self.log_failure('issue parsing the route')
296
            reactor.processes.answer_error(service)
297
            yield True
298
299
    reactor.asynchronous.schedule(service, line, callback())