-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathconfigure.py
executable file
·1830 lines (1649 loc) · 62.1 KB
/
configure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Common configure functions for bgp"""
# Python
import logging
import re
import jinja2
# Genie
from genie.utils.timeout import Timeout
# Utils
from genie.libs.sdk.apis.iosxe.running_config.get import (
get_running_config_section_dict,
)
# Steps
from pyats.aetest.steps import Steps
# Unicon
from unicon.core.errors import SubCommandFailure
log = logging.getLogger(__name__)
def configure_bgp_import_path_selection(
device, bgp_as, address_family, vrf, selection_type
):
""" Configures import path selection on BGP router
Args:
device('obj'): device to configure
bgp_as('str'): bgp id
address_family:('str'): address family
vrf('str'): vrf name
type('str'): type of selection to configure
Returns:
N/A
Raises:
SubCommandFailure: Failed executing command
"""
log.info('Configuring "import path selection {}"'.format(selection_type))
try:
device.configure(
"router bgp {bgp_as}\n"
"address-family {address_family} vrf {vrf}\n"
"import path selection {selection_type}".format(
bgp_as=bgp_as,
address_family=address_family,
vrf=vrf,
selection_type=selection_type,
)
)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure import path selection on BGP router {bgp_as}"\
.format(bgp_as=bgp_as
)
)
def configure_bgp_router_id(device, bgp_as, router_id):
""" Configures router-id on BGP router
Args:
device('obj'): device to configure on
bgp_as('str'): bgp_as to configure
router_id('str'): router_id of device
Return:
N/A
Raises:
SubCommandFailure: Failed executing command
"""
log.info(
"Configuring router BGP on {hostname}\n"
" -local AS number: {bgp_as}\n"
" -bgp router-id: {router_id}".format(
hostname=device.hostname, bgp_as=bgp_as, router_id=router_id
)
)
try:
device.configure(
"router bgp {bgp_as}\n"
"bgp router-id {router_id}\n".format(
bgp_as=bgp_as, router_id=router_id
)
)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure router-id {router_id} on "
"BGP router {bgp_as}".format(router_id=router_id, bgp_as=bgp_as)
)
def configure_bgp_neighbor(
device,
bgp_as,
neighbor_as,
neighbor_address,
source_interface=None,
ebgp=None,
address_family=None,
vrf=None
):
""" Configures bgp neighbor on bgp router
Args:
device('obj'): device to configure on
bgp_as('str'): bgp_as to configure
neighbor_as('str'): neighbor_as to configure
neighbor_address('str'): address of neighbor
source_interface('str',optional): used to configure update-source on neighbor ( Default is None )
ebgp('str',optional): used to configure ebgp-mulithop ( Default is None )
address_family('str',optional): address family ( Default is None )
vrf('str',optional): vrf to configure address_family with ( Default is None )
Returns:
N/A
Raises:
SubCommandFailure: Failed executing command
"""
log_msg = (
"Configuring BGP on {hostname}\n"
" -local AS number: {bgp_as}\n"
" -remote AS number: {neighbor_as}\n"
" -neighbor: {neighbor_address}".format(
hostname=device.hostname,
bgp_as=bgp_as,
neighbor_as=neighbor_as,
neighbor_address=neighbor_address,
)
)
cmd = []
cmd.append("router bgp {bgp_as}".format(bgp_as=bgp_as))
if address_family:
cmd2 = f"address-family {address_family}"
if vrf:
cmd2 += f" vrf {vrf}"
cmd.append(cmd2)
cmd.append("neighbor {neighbor_address} remote-as {neighbor_as}".format(
neighbor_address=neighbor_address,
neighbor_as=neighbor_as))
if source_interface:
log_msg += "\n -update-source: {}".format(source_interface)
cmd.append("neighbor {neighbor_address} update-source {source_interface}"\
.format(neighbor_address=neighbor_address,
source_interface=source_interface,
))
if ebgp:
log_msg += "\n -ebgp-multihop: {}".format(ebgp)
cmd.append("neighbor {neighbor_address} ebgp-multihop {ebgp}".format(
neighbor_address=neighbor_address, ebgp=ebgp
))
log.info(log_msg)
try:
device.configure(cmd)
except SubCommandFailure as e:
raise SubCommandFailure(
"Could not configure bgp neighbor {neighbor_as} "
"on router {bgp_as}.\nError: {e}".format(neighbor_as=neighbor_as, bgp_as=bgp_as, e=e)
)
def configure_bgp_soo_on_inbound_from_neighbor(
device, soo_rt, bgp_as, vrf, neighbor_address
):
""" Configures extended community SoO on inbound from neighbor using soo_rt
Args:
device('obj'): device to execute on
soo_rt('str'): route to configure SoO with
bgp_as('str'): what router bgp to configure on
vrf('str'): what vrf to configure on
neighbor_address('str'): what neighbor to configure on
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
log.info(
"Configuring extended community SoO with {} value on the "
"inbound from {}".format(soo_rt, neighbor_address)
)
try:
device.configure(
"route-map setsoo permit 10\n"
"set extcommunity soo {}".format(soo_rt)
)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure extended community SoO {}".format(soo_rt)
)
try:
device.configure(
"router bgp {}\n"
"address-family ipv4 vrf {}\n"
"neighbor {} route-map "
"setsoo in".format(bgp_as, vrf, neighbor_address)
)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure SoO in from neighbor {}".format(
neighbor_address
)
)
def configure_prefix_list_prefix_list_to_bgp_neighbor(
device, bgp_as, address_family, vrf, prefix_list=None
):
""" Configure prefix list to bgp neighbor
Args:
device ('obj'): Device object
bgp_as (str): bgp AS number
vrf ('str'): vrf name
address_family ('str'): address family
prefix_list ('list'): A list of dictionaries following below format:
[{
'neighbor': neighbor address,
'prefix_list': prefix,
'direction': direction
}]
ex.)
[
{
'neighbor': '192.168.1.4',
'prefix_list': 'in'
'direction': 'in'
},
{
'neighbor': '192.168.1.5',
'prefix_list': 'out'
'direction': 'out'
}
]
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
TypeError: prefix_list is not a list
"""
config = []
config.append("router bgp {bgp_as}".format(bgp_as=bgp_as))
config.append(
"\naddress-family {address_family} vrf {vrf}".format(
address_family=address_family, vrf=vrf
)
)
if not isinstance(prefix_list, list):
raise TypeError("prefix_list must be a list")
for pf in prefix_list:
config.append(
"\nneighbor {neighbor} prefix-list {prefix_list} {direction}"\
.format(neighbor=pf["neighbor"],
prefix_list=pf["prefix_list"],
direction=pf["direction"],
)
)
try:
device.configure("".join(config))
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure prefix-liston router bgp "
"{bgp_as}".format(bgp_as=bgp_as)
)
def configure_maximum_prefix_to_bgp_neighbor(
device, bgp_as, address_family, vrf, maximum_prefix=None
):
""" Configure maximum prefix to router bgp neighbor
Args:
device ('obj'): Device object
bgp_as (str): bgp AS number
vrf ('str'): vrf name
address_family ('str'): address family
maximum_prefix ('list'): A list of dictionaries following below format:
[{
'neighbor': neighbor address,
'maximum_prefix': maximum prefix number
}]
ex.)
[
{
'neighbor': '192.168.1.6,
'maximum_prefix': 5,
}
]
Returns:
None
Raises:
SubCommandFailure: Failed executing configure commands
TypeError: maximum_prefix is not a list
"""
config = []
config.append("router bgp {bgp_as}".format(bgp_as=bgp_as))
config.append(
"\naddress-family {address_family} vrf {vrf}".format(
address_family=address_family, vrf=vrf
)
)
if not isinstance(maximum_prefix, list):
raise ValueError("prefix_list must be a list")
for pf in maximum_prefix:
config.append(
"\nneighbor {neighbor} maximum-prefix {maximum_prefix}".format(
neighbor=pf["neighbor"], maximum_prefix=pf["maximum_prefix"]
)
)
try:
device.configure("".join(config))
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure maximum prefixes on bgp "
"router {bgp_as}".format(bgp_as=bgp_as)
)
def configure_route_map_route_map_to_bgp_neighbor(
device,
bgp_as,
address_family="",
route_map=None,
vrf="",
vrf_address_family="",
):
""" Configure route map to bgp neighbors
Args:
device ('obj'): Device object
bgp_as ('int'): BGP AS number
address_family ('str'): address family
vrf ('str'): vrf name
vrf_address_family ('str'): address family for vrf
route_map ('list'): route map list which contains dictionary
dictionary contains following 5 keys:
neighbor ('str'): neighbor value
route_map ('str'): route-map name
direction ('str'): direction type
ex.)
[
{
'neighbor': '192.168.60.10',
'route_map': 'community_test_out',
'direction': 'out'
},
{
'neighbor': '192.168.60.11',
'route_map': 'community_test_out',
'direction': 'out'
},
{
'neighbor': '192.168.6.10',
'route_map': 'community_test_in',
'direction': 'in'
},
]
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
TypeError: route_map is not a list
"""
# router bgp 65109
# address-family vpnv4
# neighbor 192.168.36.119 route-map community_test_out out
# neighbor 192.168.36.120 route-map community_test_out out
# address-family ipv4 vrf
# neighbor 192.168.10.253 route-map community_test_in in
if route_map is None:
route_map = []
config = []
config.append("router bgp {bgp_as}\n".format(bgp_as=bgp_as))
if address_family:
config.append(
"address-family {address_family}\n".format(
address_family=address_family
)
)
if route_map:
if not isinstance(route_map, list):
raise TypeError("route_map must be a list")
for routemap in route_map:
direction = routemap["direction"]
if direction == "in" and vrf and vrf_address_family:
config.append(
"address-family {vrf_address_family} vrf {vrf}\n".format(
vrf_address_family=vrf_address_family, vrf=vrf
)
)
config.append(
"neighbor {neighbor} route-map {route_map_name} "
"{route_map_direction}\n".format(
neighbor=routemap["neighbor"],
route_map_name=routemap["route_map"],
route_map_direction=direction,
)
)
try:
device.configure("".join(config))
except SubCommandFailure as e:
raise SubCommandFailure(
"Failed to configure route map to bgp neighbors"
)
def configure_bgp_neighbor_activate(
device, address_family, bgp_as, neighbor_address, steps=Steps(),
peer_policy=None, vrf=None):
""" Activate bgp neighbor on bgp router
Args:
device ('obj') : Device to be configured
bgp_as ('str') : Bgp Id to be added to configuration
neighbor_address ('str') : Address of neighbor to be added to configuration
address_family ('str') : Address family to be configured
steps('obj') : Context manager steps
peer_policy('str') : peer policy to be configured
vrf ('str') : vrf name
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
with steps.start(
"Configure device {dev}".format(dev=device.name), continue_=True
) as step:
cnfg=[f"router bgp {bgp_as}"]
if vrf:
cnfg.append(f"address-family {address_family} vrf {vrf}")
else:
cnfg.append(f"address-family {address_family}")
cnfg.append(f"neighbor {neighbor_address} activate")
if peer_policy:
cnfg.append(
"neighbor {neighbor_address} inherit peer-policy {peer_policy}"\
.format(neighbor_address=neighbor_address,
peer_policy=peer_policy))
try:
device.configure(cnfg)
except SubCommandFailure as e:
raise SubCommandFailure(
"Could not ativate bgp neighbor on bgp "
"router {bgp_as}".format(bgp_as=bgp_as)
)
def configure_inherit_peer_session(
device, bgp_as, neighbor_address, peer_policy=None
):
""" configure inherit peer session under bgp
Args:
device ('obj') : Device to be configured
bgp_as ('str') : Bgp Id to be added to configuration
neighbor_address ('str') : Address of neighbor to be added to configuration
peer_policy('str') : peer policy to be configured
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
try:
cnfg=["router bgp {bgp_as}".format(bgp_as=bgp_as)]
cnfg.append(
"neighbor {neighbor_address} inherit peer-session {peer_policy}"\
.format(neighbor_address=neighbor_address,
peer_policy=peer_policy))
device.configure(cnfg)
except SubCommandFailure as e:
raise SubCommandFailure(
"Could not inherit peer session in "
"router {bgp_as}".format(bgp_as=bgp_as)
)
def configure_bgp_l2vpn_neighbor_activate(
device, address_family, bgp_as, neighbor_address,
address_family_modifier="", community=""
):
""" Activate bgp neighbor on bgp router
Args:
device ('obj') : Device to be configured
bgp_as ('str') : Bgp Id to be added to configuration
neighbor_address ('str') : Address of neighbor to be added to configuration
address_family ('str') : Address family to be configured
address_family_modifier ('str') : the endpoint provisioning information to be distributed
to BGP peers.
community('str') : Specifies the communities attribute to be sent to a BGP neighbor.
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
log.info("configure l2vpn vpls address-family on router bgp {bgp_as}"
.format(bgp_as=bgp_as))
try:
device.configure([
"router bgp {bgp_as}".format(bgp_as=bgp_as),
"address-family {address_family} {address_family_modifier}".format(
address_family=address_family,
address_family_modifier=address_family_modifier),
"neighbor {neighbor_address} activate".format(
neighbor_address=neighbor_address),
"neighbor {neighbor_address} send-community {community}".format(
neighbor_address=neighbor_address, community=community)
])
except SubCommandFailure as e:
raise SubCommandFailure(
"Could not ativate l2vpn bgp neighbor on bgp "
"router {bgp_as}. Error:{e}".format(bgp_as=bgp_as, e=e)
)
def configure_shut_bgp_neighbors(
device, bgp_as, neighbors=None, address_family=None, vrf=None,
noshut=False):
""" Configures shut/enable on bgp neighbors if provided otherwise the ones found in running config
Args:
device ('obj'): device under test
bgp_as ('int'): router bgp_as to configure on
address_family ('str'): address_family to configure under
vrf ('str'): vrf to configure under
neighbors ('list'): List of neighbors to shut/enable
noshut ('bool'): does the opposite of shut if True
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
ValueError: Some information is missing
"""
search = None
if noshut:
if neighbors:
log.info("Enabling router bgp neighbors {}".format(neighbors))
elif address_family and vrf:
log.info(
"Enabling router bgp neighbors under address_family {} and vrf "
"{}".format(address_family, vrf)
)
search = "address-family {} vrf {}".format(address_family, vrf)
elif address_family:
log.info(
"Enabling router bgp neighbors under address_family {}".format(
address_family
)
)
search = "address-family {}".format(address_family)
else:
if neighbors:
log.info("Shutting down router bgp neighbors {}".format(neighbors))
elif address_family and vrf:
log.info(
"Shutting down router bgp neighbors under address_family {} and"
" vrf {}".format(address_family, vrf)
)
search = "address-family {} vrf {}".format(address_family, vrf)
elif address_family:
log.info(
"Shutting down router bgp neighbors under address_family {}"\
.format(address_family)
)
search = "address-family {}".format(address_family)
p1_active_neighbor = re.compile(
r"^neighbor +(?P<neighbor>[\d\.]+) +activate"
)
p2_shutdown_neighbor = re.compile(
r"^neighbor +(?P<neighbor>[\d\.]+) +shutdown"
)
p3_neighbor = re.compile(r"^neighbor +(?P<neighbor>[\d\.]+)")
cmd = "router bgp {}\n".format(bgp_as)
if neighbors:
if noshut:
if vrf:
cmd += "address-family {} vrf {}\n".format(address_family, vrf)
for neighbor in neighbors:
cmd += "no neighbor {} shutdown".format(neighbor)
else:
if vrf:
cmd += "address-family {} vrf {}\n".format(address_family, vrf)
for neighbor in neighbors:
cmd += "neighbor {} shutdown".format(neighbor)
try:
device.configure(cmd)
except SubCommandFailure:
if noshut:
raise SubCommandFailure("Could not enable bgp neighbors")
else:
raise SubCommandFailure("Could not shut bgp neighbors")
else:
already_shut = []
config_dict = get_running_config_section_dict(
device, "router bgp"
)
if config_dict:
for sub_level in config_dict.get(
"router bgp {}".format(bgp_as), {}
):
# Following if/else block is used for neighbors under 'router bgp id' level
if noshut:
m = p2_shutdown_neighbor.match(sub_level)
if m:
cmd += "no neighbor {} shutdown\n".format(
m.groupdict()["neighbor"]
)
else:
m = p3_neighbor.match(sub_level)
if m:
if m.groupdict()["neighbor"] not in already_shut:
already_shut.append(m.groupdict()["neighbor"])
cmd += "neighbor {} shutdown\n".format(
m.groupdict()["neighbor"]
)
# Following if block is used for neighbors under address_family level
if search and search in sub_level:
# enter address-family
cmd += sub_level + "\n"
# shut / no shut neighbor
for command in config_dict["router bgp {}".format(bgp_as)][
sub_level
]:
if noshut:
m = p2_shutdown_neighbor.match(command)
if m:
cmd += "no neighbor {} shutdown\n".format(
m.groupdict()["neighbor"]
)
else:
m = p1_active_neighbor.match(command)
if m:
cmd += "neighbor {} shutdown\n".format(
m.groupdict()["neighbor"]
)
# exit address-family
cmd += "exit-address-family\n"
if "neighbor" in cmd:
try:
device.configure(cmd)
except SubCommandFailure:
if noshut:
raise SubCommandFailure(
"Could not enable bgp neighbors"
)
else:
raise SubCommandFailure("Could not shut bgp neighbors")
else:
if vrf:
raise ValueError(
"No neighbors found in running config "
"under {} address_family and {} vrf.".format(
address_family, vrf
)
)
else:
raise ValueError(
"No neighbors found in running config "
"under {} address_family.".format(address_family)
)
else:
raise ValueError("No running configuration under router bgp.")
def configure_no_shut_bgp_neighbors(
device, bgp_as, neighbors=None, address_family=None, vrf=None
):
""" Enables bgp neighbors if provided otherwise it enabled the ones found in running config
Args:
device ('obj'): device under test
bgp_id ('int'): router bgp_id to configure on
address_family ('str'): address_family to configure under
vrf ('str'): vrf to configure under
neighbors('list'): Libs with BGP neighbors
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
ValueError: Some information is missing
"""
try:
configure_shut_bgp_neighbors(
device, bgp_as, neighbors, address_family, vrf, noshut=True
)
except SubCommandFailure as e:
raise SubCommandFailure(e)
except ValueError as e:
raise ValueError(str(e))
def configure_bgp_neighbor_remote_as(
device, bgp_as, vrf, neighbor_as, neighbor_address, address_family
):
""" Configure destination in vrf
Args:
device ('obj'): Device object
bgp_as ('str'): Router bgp
vrf ('str'): Vrf name
neighbor_as ('str'): Destination
neighbor_address ('str'): Neighbor address
address_family ('str'): Address family
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
try:
device.configure(
[
"router bgp {as_n}".format(as_n=bgp_as),
"address-family {address_family} vrf {vrf}".format(
vrf=vrf, address_family=address_family
),
"neighbor {neighbor} remote-as {neighbor_as}".format(
neighbor=neighbor_address, neighbor_as=neighbor_as
),
]
)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure destination {dest} on "
"on device {dev}".format(dest=neighbor_as, dev=device.name)
)
def remove_bgp_configuration(device, bgp_as):
""" Remove bgp configuration
Args:
device ('obj'): Device object
bgp_as ('str'): Router bgp
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
try:
device.configure("no router bgp {as_n}".format(as_n=bgp_as))
except SubCommandFailure:
raise SubCommandFailure(
"Could not remove BGP router {bgp_as} "
"configuration from device {dev}".format(
bgp_as=bgp_as, dev=device.name
)
)
def configure_bgp_neighbor_as_override(
device, vrf, bgp_as, address_family, neighbor_address
):
""" Configure AS override in VRF
Args:
device ('obj'): Device object
bgp_as ('str'): Router bgp
vrf ('str'): Vrf name
neighbor_address ('str'): Neighbor address
address_family ('str'): Address family
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
try:
device.configure(
[
"router bgp {as_n}".format(as_n=bgp_as),
"address-family {address_family} vrf {vrf}".format(
vrf=vrf, address_family=address_family
),
"neighbor {neighbor} as-override".format(
neighbor=neighbor_address
),
]
)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure as-override on {dev} "
"for vrf {vrf}".format(vrf=vrf, dev=device.name)
)
def configure_bgp_additional_paths(device, bgp_as):
""" Configure additional_paths on bgp router
Args:
device ('obj'): device to use
bgp_as ('int'): bgp router to configure
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
log.info("Configuring bgp router {} with additional paths".format(bgp_as))
try:
device.configure(
[
"router bgp {}".format(bgp_as),
"bgp additional-paths select all",
"bgp additional-paths send receive",
"bgp additional-paths install",
]
)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure additional paths on bgp "
"router {}".format(bgp_as)
)
def configure_bgp_advertise_additional_paths(device, bgp_as, neighbor):
""" Configures advertisement for additional paths
Args:
device ('obj'): device to configure
bgp_as ('str'): router bgp number
neighbor ('str'): neighbor to advertise to
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
log.info(
"Configuring bgp router {} with advertisement of additional-paths"\
.format(bgp_as)
)
try:
device.configure(
[
"router bgp {}".format(bgp_as),
"neighbor {} advertise additional-paths all".format(neighbor),
]
)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure advertisement for "
"additional paths on bgp router {}".format(bgp_as)
)
def configure_bgp_address_advertisement(
device, bgp_as, address_family, ip_address, mask
):
""" Configure address advertisement on router bgp
Args:
device ('obj'): device to use
bgp_as ('int'): bgp router to configure
address_family ('str'): address family to configure under
ip_address ('str'): ip address
mask ('str'): mask
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
log.info(
"Enabling address advertisement for ip: {} mask {}".format(
ip_address, mask
)
)
cmd = []
cmd.append("router bgp {}".format(bgp_as))
cmd.append("address-family {}".format(address_family))
if address_family == 'ipv4':
cmd.append("network {} mask {}".format(ip_address, mask))
else:
cmd.append("network {}".format(ip_address))
try:
device.configure(cmd)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure address advertisement on "
"router bgp {bgp_as}".format(bgp_as)
)
def configure_redistribute_connected(device, bgp_as, address_family, vrf=None):
""" configure redistribute connected in bgp
Args:
device ('obj'): device to use
bgp_as ('str'): bgp as number
address_family ('str'): address family under bgp
vrf ('str'): vrf in address_family default to None
Returns:
None
Raises:
SubCommandFailure: Failed configuring redistribute
connected under bgp address_family
"""
log.info(
"configure redistribute connected under bgp {}".format(bgp_as)
)
confg = ["router bgp {}".format(bgp_as)]
if vrf:
confg.append("address-family {address_family} vrf {vrf}".format(
address_family=address_family, vrf=vrf))
else:
confg.append("address-family {address_family}".format(
address_family=address_family))
confg.append("redistribute connected")
try:
device.configure(confg)
except SubCommandFailure:
raise SubCommandFailure(
"Could not configure redistribute connected under bgp {bgp_as}"\
.format(bgp_as=bgp_as)
)
def configure_bgp_address_family_attributes(
device, bgp_as, address_family, neighbor, send_label=False,
route_reflector_client=False, next_hop_self_all=False,
next_hop_unchanged=False
):
""" configure attributes for bgp
Args:
device ('obj'): device to use
bgp_as ('int'): bgp router to configure
address_family ('str'): address family to configure under
neighbor ('str'): neighbor address to send label
send_label (`boolean`) : send MPLS labels with the routes if true
route_reflector_client (`boolean`) : sets a device as routing information exchange server if true
next-hop-self all (`boolean`) : sets a device as routing information exchange server if true
Returns:
N/A
Raises:
SubCommandFailure: Failed executing configure commands
"""
try:
confg = ["router bgp {}".format(bgp_as)]
confg.append("address-family {}".format(address_family))
if send_label:
confg.append("neighbor {} send-label".format(neighbor))
if route_reflector_client:
confg.append("neighbor {} route-reflector-client".format(neighbor))
if next_hop_self_all:
confg.append("neighbor {} next-hop-self all".format(neighbor))
if next_hop_unchanged:
confg.append("neighbor {} next-hop-unchanged".format(neighbor))
device.configure(confg)
except SubCommandFailure as e:
raise SubCommandFailure(
"Could not configure bgp attribute Error: {e} "
"router bgp {bgp_as}".format(e=e, bgp_as=bgp_as)
)
def configure_no_bgp_default(
device, bgp_as, ipv4_unicast=False, route_target_filter=False
):
""" configure no bgp default attributes under bgp