forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrr-reload.py
executable file
·2401 lines (2076 loc) · 91.8 KB
/
frr-reload.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
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
# Frr Reloader
# Copyright (C) 2014 Cumulus Networks, Inc.
#
"""
This program
- reads a frr configuration text file
- reads frr's current running configuration via "vtysh -c 'show running'"
- compares the two configs and determines what commands to execute to
synchronize frr's running configuration with the configuation in the
text file
"""
from __future__ import print_function, unicode_literals
import argparse
import logging
import os, os.path
import random
import re
import string
import subprocess
import sys
from collections import OrderedDict
from ipaddress import IPv6Address, ip_network
from pprint import pformat
# Python 3
def iteritems(d):
return iter(d.items())
log = logging.getLogger(__name__)
class VtyshException(Exception):
pass
class Vtysh(object):
def __init__(self, bindir=None, confdir=None, sockdir=None, pathspace=None):
self.bindir = bindir
self.confdir = confdir
self.pathspace = pathspace
self.common_args = [os.path.join(bindir or "", "vtysh")]
if confdir:
self.common_args.extend(["--config_dir", confdir])
if sockdir:
self.common_args.extend(["--vty_socket", sockdir])
if pathspace:
self.common_args.extend(["-N", pathspace])
def _call(self, args, stdin=None, stdout=None, stderr=None):
kwargs = {}
if stdin is not None:
kwargs["stdin"] = stdin
if stdout is not None:
kwargs["stdout"] = stdout
if stderr is not None:
kwargs["stderr"] = stderr
return subprocess.Popen(self.common_args + args, **kwargs)
def _call_cmd(self, command, stdin=None, stdout=None, stderr=None):
if isinstance(command, list):
args = [item for sub in command for item in ["-c", sub]]
else:
args = ["-c", command]
return self._call(args, stdin, stdout, stderr)
def __call__(self, command, stdouts=None):
"""
Call a CLI command (e.g. "show running-config")
Output text is automatically redirected, decoded and returned.
Multiple commands may be passed as list.
"""
proc = self._call_cmd(command, stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
if proc.wait() != 0:
if stdouts is not None:
stdouts.append(stdout.decode("UTF-8"))
raise VtyshException(
'vtysh returned status %d for command "%s"' % (proc.returncode, command)
)
return stdout.decode("UTF-8")
def is_config_available(self):
"""
Return False if no frr daemon is running or some other vtysh session is
in 'configuration terminal' mode which will prevent us from making any
configuration changes.
"""
output = self("configure")
if "configuration is locked" in output.lower():
log.error("vtysh 'configure' returned\n%s\n" % (output))
return False
return True
def exec_file(self, filename):
child = self._call(["-f", filename])
if child.wait() != 0:
raise VtyshException(
"vtysh (exec file) exited with status %d" % (child.returncode)
)
def mark_file(self, filename, stdin=None):
child = self._call(
["-m", "-f", filename],
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
)
try:
stdout, stderr = child.communicate()
except subprocess.TimeoutExpired:
child.kill()
stdout, stderr = child.communicate()
raise VtyshException("vtysh call timed out!")
if child.wait() != 0:
raise VtyshException(
"vtysh (mark file) exited with status %d:\n%s"
% (child.returncode, stderr)
)
return stdout.decode("UTF-8")
def mark_show_run(self, daemon=None):
cmd = "show running-config"
if daemon:
cmd += " %s" % daemon
cmd += " no-header"
show_run = self._call_cmd(cmd, stdout=subprocess.PIPE)
mark = self._call(
["-m", "-f", "-"], stdin=show_run.stdout, stdout=subprocess.PIPE
)
show_run.wait()
stdout, stderr = mark.communicate()
mark.wait()
if show_run.returncode != 0:
raise VtyshException(
"vtysh (show running-config) exited with status %d:"
% (show_run.returncode)
)
if mark.returncode != 0:
raise VtyshException(
"vtysh (mark running-config) exited with status %d" % (mark.returncode)
)
return stdout.decode("UTF-8")
class Context(object):
"""
A Context object represents a section of frr configuration such as:
!
interface swp3
description swp3 -> r8's swp1
ipv6 nd suppress-ra
link-detect
!
or a single line context object such as this:
ip forwarding
"""
def __init__(self, keys, lines):
self.keys = keys
self.lines = lines
# Keep a dictionary of the lines, this is to make it easy to tell if a
# line exists in this Context
self.dlines = OrderedDict()
for ligne in lines:
self.dlines[ligne] = True
def __str__(self):
return str(self.keys) + " : " + str(self.lines)
def add_lines(self, lines):
"""
Add lines to specified context
"""
self.lines.extend(lines)
for ligne in lines:
self.dlines[ligne] = True
def get_normalized_es_id(line):
"""
The es-id or es-sys-mac need to be converted to lower case
"""
sub_strs = ["evpn mh es-id", "evpn mh es-sys-mac"]
for sub_str in sub_strs:
obj = re.match(sub_str + r" (?P<esi>\S*)", line)
if obj:
line = "%s %s" % (sub_str, obj.group("esi").lower())
break
return line
def get_normalized_mac_ip_line(line):
if line.startswith("evpn mh es"):
return get_normalized_es_id(line)
if not "ipv6 add" in line:
return get_normalized_ipv6_line(line)
return line
def get_normalized_interface_vrf(line):
"""
If 'interface <int_name> vrf <vrf_name>' is present in file,
we need to remove the explicit "vrf <vrf_name>"
so that the context information is created
correctly and configurations are matched appropriately.
"""
intf_vrf = re.search(r"interface (\S+) vrf (\S+)", line)
if intf_vrf:
old_line = "vrf %s" % intf_vrf.group(2)
new_line = line.replace(old_line, "").strip()
return new_line
return line
# This dictionary contains a tree of all commands that we know start a
# new multi-line context. All other commands are treated either as
# commands inside a multi-line context or as single-line contexts. This
# dictionary should be updated whenever a new node is added to FRR.
ctx_keywords = {
"router bgp ": {
"address-family ": {
"vni ": {},
},
"vnc defaults": {},
"vnc nve-group ": {},
"vnc l2-group ": {},
"vrf-policy ": {},
"bmp targets ": {},
"segment-routing srv6": {},
},
"router rip": {},
"router ripng": {},
"router isis ": {
"segment-routing srv6": {
"node-msd": {},
},
},
"router openfabric ": {},
"router ospf": {},
"router ospf6": {},
"router eigrp ": {},
"router babel": {},
"router pim": {},
"router pim6": {},
"mpls ldp": {"address-family ": {"interface ": {}}},
"l2vpn ": {"member pseudowire ": {}},
"key chain ": {"key ": {}},
"vrf ": {},
"interface ": {"link-params": {}},
"pseudowire ": {},
"segment-routing": {
"traffic-eng": {
"segment-list ": {},
"policy ": {"candidate-path ": {}},
"pcep": {"pcc": {}, "pce ": {}, "pce-config ": {}},
},
"srv6": {"locators": {"locator ": {}}, "encapsulation": {}},
},
"nexthop-group ": {},
"route-map ": {},
"pbr-map ": {},
"rpki": {},
"bfd": {"peer ": {}, "profile ": {}},
"line vty": {},
}
class Config(object):
"""
A frr configuration is stored in a Config object. A Config object
contains a dictionary of Context objects where the Context keys
('router ospf' for example) are our dictionary key.
"""
def __init__(self, vtysh):
self.lines = []
self.contexts = OrderedDict()
self.vtysh = vtysh
def load_from_file(self, filename):
"""
Read configuration from specified file and slurp it into internal memory
The internal representation has been marked appropriately by passing it
through vtysh with the -m parameter
"""
log.info("Loading Config object from file %s", filename)
file_output = self.vtysh.mark_file(filename)
vrf_context = None
pim_vrfs = []
for line in file_output.split("\n"):
line = line.strip()
# Compress duplicate whitespaces
line = " ".join(line.split())
# Detect when we are within a vrf context for converting legacy PIM commands
if vrf_context:
re_vrf = re.match("^(exit-vrf|exit|end)$", line)
if re_vrf:
vrf_context = None
else:
re_vrf = re.match("^vrf ([a-z]+)$", line)
if re_vrf:
vrf_context = re_vrf.group(1)
# Detect legacy pim commands that need to move under the router pim context
re_pim = re.match(
"^ip(v6)? pim ((ecmp|join|keep|mlag|packets|register|rp|send|spt|ssm).*)$",
line,
)
if re_pim and re_pim.group(2):
router_pim = "router pim"
if re_pim.group(1):
router_pim += "6"
if vrf_context:
router_pim += " vrf " + vrf_context
if vrf_context:
pim_vrfs.append(router_pim)
pim_vrfs.append(re_pim.group(2))
pim_vrfs.append("exit")
line = "# PIM VRF LINE MOVED TO ROUTER PIM"
else:
self.lines.append(router_pim)
self.lines.append(re_pim.group(2))
line = "exit"
re_pim = re.match("^ip(v6)? ((ssmpingd|msdp).*)$", line)
if re_pim and re_pim.group(2):
router_pim = "router pim"
if re_pim.group(1):
router_pim += "6"
if vrf_context:
router_pim += " vrf " + vrf_context
if vrf_context:
pim_vrfs.append(router_pim)
pim_vrfs.append(re_pim.group(2))
pim_vrfs.append("exit")
line = "# PIM VRF LINE MOVED TO ROUTER PIM"
else:
self.lines.append(router_pim)
self.lines.append(re_pim.group(2))
line = "exit"
# Remove 'vrf <vrf_name>' from 'interface <x> vrf <vrf_name>'
if line.startswith("interface ") and "vrf" in line:
line = get_normalized_interface_vrf(line)
if ":" in line:
line = get_normalized_mac_ip_line(line)
# vrf static routes can be added in two ways. The old way is:
#
# "ip route x.x.x.x/x y.y.y.y vrf <vrfname>"
#
# but it's rendered in the configuration as the new way::
#
# vrf <vrf-name>
# ip route x.x.x.x/x y.y.y.y
# exit-vrf
#
# this difference causes frr-reload to not consider them a
# match and delete vrf static routes incorrectly.
# fix the old way to match new "show running" output so a
# proper match is found.
if (
line.startswith("ip route ") or line.startswith("ipv6 route ")
) and " vrf " in line:
newline = line.split(" ")
vrf_index = newline.index("vrf")
vrf_ctx = newline[vrf_index] + " " + newline[vrf_index + 1]
del newline[vrf_index : vrf_index + 2]
newline = " ".join(newline)
self.lines.append(vrf_ctx)
self.lines.append(newline)
self.lines.append("exit-vrf")
line = "end"
self.lines.append(line)
if len(pim_vrfs) > 0:
self.lines.append(pim_vrfs)
self.load_contexts()
def load_from_show_running(self, daemon):
"""
Read running configuration and slurp it into internal memory
The internal representation has been marked appropriately by passing it
through vtysh with the -m parameter
"""
log.info("Loading Config object from vtysh show running")
config_text = self.vtysh.mark_show_run(daemon)
for line in config_text.split("\n"):
line = line.strip()
if (
line == "Building configuration..."
or line == "Current configuration:"
or not line
):
continue
self.lines.append(line)
self.load_contexts()
def get_lines(self):
"""
Return the lines read in from the configuration
"""
return "\n".join(self.lines)
def get_contexts(self):
"""
Return the parsed context as strings for display, log etc.
"""
for _, ctx in sorted(iteritems(self.contexts)):
print(str(ctx))
def save_contexts(self, key, lines):
"""
Save the provided key and lines as a context
"""
if not key:
return
# IP addresses specified in "network" statements, "ip prefix-lists"
# etc. can differ in the host part of the specification the user
# provides and what the running config displays. For example, user can
# specify 11.1.1.1/24, and the running config displays this as
# 11.1.1.0/24. Ensure we don't do a needless operation for such lines.
# IS-IS & OSPFv3 have no "network" support.
re_key_rt = re.match(r"(ip|ipv6)\s+route\s+([A-Fa-f:.0-9/]+)(.*)$", key[0])
if re_key_rt:
addr = re_key_rt.group(2)
if "/" in addr:
try:
newaddr = ip_network(addr, strict=False)
key[0] = "%s route %s/%s%s" % (
re_key_rt.group(1),
str(newaddr.network_address),
newaddr.prefixlen,
re_key_rt.group(3),
)
except ValueError:
pass
re_key_rt = re.match(
r"(ip|ipv6)\s+prefix-list(.*)(permit|deny)\s+([A-Fa-f:.0-9/]+)(.*)$", key[0]
)
if re_key_rt:
addr = re_key_rt.group(4)
if "/" in addr:
try:
network_addr = ip_network(addr, strict=False)
newaddr = "%s/%s" % (
str(network_addr.network_address),
network_addr.prefixlen,
)
except ValueError:
newaddr = addr
else:
newaddr = addr
legestr = re_key_rt.group(5)
re_lege = re.search(r"(.*)le\s+(\d+)\s+ge\s+(\d+)(.*)", legestr)
if re_lege:
legestr = "%sge %s le %s%s" % (
re_lege.group(1),
re_lege.group(3),
re_lege.group(2),
re_lege.group(4),
)
key[0] = "%s prefix-list%s%s %s%s" % (
re_key_rt.group(1),
re_key_rt.group(2),
re_key_rt.group(3),
newaddr,
legestr,
)
if lines and key[0].startswith("router bgp"):
newlines = []
for line in lines:
re_net = re.match(r"network\s+([A-Fa-f:.0-9/]+)(.*)$", line)
if re_net:
addr = re_net.group(1)
if "/" not in addr and key[0].startswith("router bgp"):
# This is most likely an error because with no
# prefixlen, BGP treats the prefixlen as 8
addr = addr + "/8"
try:
network_addr = ip_network(addr, strict=False)
line = "network %s/%s %s" % (
str(network_addr.network_address),
network_addr.prefixlen,
re_net.group(2),
)
newlines.append(line)
except ValueError:
# Really this should be an error. Whats a network
# without an IP Address following it ?
newlines.append(line)
else:
newlines.append(line)
lines = newlines
# More fixups in user specification and what running config shows.
# "null0" in routes must be replaced by Null0.
if (
key[0].startswith("ip route")
or key[0].startswith("ipv6 route")
and "null0" in key[0]
):
key[0] = re.sub(r"\s+null0(\s*$)", " Null0", key[0])
if lines and key[0].startswith("vrf "):
newlines = []
for line in lines:
if line.startswith("ip route ") or line.startswith("ipv6 route "):
if "null0" in line:
line = re.sub(r"\s+null0(\s*$)", " Null0", line)
newlines.append(line)
else:
newlines.append(line)
lines = newlines
if lines:
if tuple(key) not in self.contexts:
ctx = Context(tuple(key), lines)
self.contexts[tuple(key)] = ctx
else:
ctx = self.contexts[tuple(key)]
ctx.add_lines(lines)
else:
if tuple(key) not in self.contexts:
ctx = Context(tuple(key), [])
self.contexts[tuple(key)] = ctx
def load_contexts(self):
"""
Parse the configuration and create contexts for each appropriate block
The end of a context is flagged via the 'end' keyword:
!
interface swp52
ipv6 nd suppress-ra
link-detect
!
end
router bgp 10
bgp router-id 10.0.0.1
bgp log-neighbor-changes
no bgp default ipv4-unicast
neighbor EBGP peer-group
neighbor EBGP advertisement-interval 1
neighbor EBGP timers connect 10
neighbor 2001:40:1:4::6 remote-as 40
neighbor 2001:40:1:8::a remote-as 40
!
end
address-family ipv6
neighbor IBGPv6 activate
neighbor 2001:10::2 peer-group IBGPv6
neighbor 2001:10::3 peer-group IBGPv6
exit-address-family
!
end
router ospf
ospf router-id 10.0.0.1
log-adjacency-changes detail
timers throttle spf 0 50 5000
!
end
The code assumes that its working on the output from the "vtysh -m"
command. That provides the appropriate markers to signify end of
a context. This routine uses that to build the contexts for the
config.
There are single line contexts such as "log file /media/node/zebra.log"
and multi-line contexts such as "router ospf" and subcontexts
within a context such as "address-family" within "router bgp"
In each of these cases, the first line of the context becomes the
key of the context. So "router bgp 10" is the key for the non-address
family part of bgp, "router bgp 10, address-family ipv6 unicast" is
the key for the subcontext and so on.
"""
# stack of context keys
ctx_keys = []
# stack of context keywords
cur_ctx_keywords = [ctx_keywords]
# list of stored commands
cur_ctx_lines = []
for line in self.lines:
if not line:
continue
if line.startswith("!") or line.startswith("#"):
continue
if line.startswith("exit"):
# ignore on top level
if len(ctx_keys) == 0:
continue
# save current context
self.save_contexts(ctx_keys, cur_ctx_lines)
# exit current context
log.debug("LINE %-50s: exit context %-50s", line, ctx_keys)
ctx_keys.pop()
cur_ctx_keywords.pop()
cur_ctx_lines = []
continue
if line.startswith("end"):
# exit all contexts
while len(ctx_keys) > 0:
# save current context
self.save_contexts(ctx_keys, cur_ctx_lines)
# exit current context
log.debug("LINE %-50s: exit context %-50s", line, ctx_keys)
ctx_keys.pop()
cur_ctx_keywords.pop()
cur_ctx_lines = []
continue
new_ctx = False
# check if the line is a context-entering keyword
for k, v in cur_ctx_keywords[-1].items():
if line.startswith(k):
# candidate-path is a special case. It may be a node and
# may be a single-line command. The distinguisher is the
# word "dynamic" or "explicit" at the middle of the line.
# It was perhaps not the best choice by the pathd authors
# but we have what we have.
if k == "candidate-path " and "explicit" in line:
# this is a single-line command
break
# save current context
self.save_contexts(ctx_keys, cur_ctx_lines)
# enter new context
new_ctx = True
ctx_keys.append(line)
cur_ctx_keywords.append(v)
cur_ctx_lines = []
log.debug("LINE %-50s: enter context %-50s", line, ctx_keys)
break
if new_ctx:
continue
if len(ctx_keys) == 0:
log.debug("LINE %-50s: single-line context", line)
self.save_contexts([line], [])
else:
log.debug("LINE %-50s: add to current context %-50s", line, ctx_keys)
cur_ctx_lines.append(line)
# Save the context of the last one
if len(ctx_keys) > 0:
self.save_contexts(ctx_keys, cur_ctx_lines)
def lines_to_config(ctx_keys, line, delete):
"""
Return the command as it would appear in frr.conf
"""
cmd = []
# If there's no `line` and `ctx_keys` length is 1, then it may be a single-line command.
# In this case, we should treat it as a single command in an empty context.
if len(ctx_keys) == 1 and not line:
single = True
for k, v in ctx_keywords.items():
if ctx_keys[0].startswith(k):
single = False
break
if single:
line = ctx_keys[0]
ctx_keys = []
if line:
for i, ctx_key in enumerate(ctx_keys):
cmd.append(" " * i + ctx_key)
line = line.lstrip()
indent = len(ctx_keys) * " "
# There are some commands that are on by default so their "no" form will be
# displayed in the config. "no bgp default ipv4-unicast" is one of these.
# If we need to remove this line we do so by adding "bgp default ipv4-unicast",
# not by doing a "no no bgp default ipv4-unicast"
if delete:
if line.startswith("no "):
cmd.append("%s%s" % (indent, line[3:]))
else:
cmd.append("%sno %s" % (indent, line))
else:
cmd.append(indent + line)
for i in reversed(range(len(ctx_keys))):
cmd.append(" " * i + "exit")
# If line is None then we are typically deleting an entire
# context ('no router ospf' for example)
else:
for i, ctx_key in enumerate(ctx_keys[:-1]):
cmd.append("%s%s" % (" " * i, ctx_key))
# Only put the 'no' on the last sub-context
if delete:
if ctx_keys[-1].startswith("no "):
cmd.append("%s%s" % (" " * (len(ctx_keys) - 1), ctx_keys[-1][3:]))
else:
cmd.append("%sno %s" % (" " * (len(ctx_keys) - 1), ctx_keys[-1]))
else:
cmd.append("%s%s" % (" " * (len(ctx_keys) - 1), ctx_keys[-1]))
cmd.append("%sexit" % (" " * (len(ctx_keys) - 1)))
for i in reversed(range(len(ctx_keys) - 1)):
cmd.append(" " * i + "exit")
return cmd
def get_normalized_ipv6_line(line):
"""
Return a normalized IPv6 line as produced by frr,
with all letters in lower case and trailing and leading
zeros removed, and only the network portion present if
the IPv6 word is a network
"""
norm_line = ""
words = line.split(" ")
for word in words:
if ":" in word:
norm_word = None
if "/" in word:
try:
v6word = ip_network(word, strict=False)
norm_word = "%s/%s" % (
str(v6word.network_address),
v6word.prefixlen,
)
except ValueError:
pass
if not norm_word:
try:
norm_word = "%s" % IPv6Address(word)
except ValueError:
norm_word = word
else:
norm_word = word
norm_line = norm_line + " " + norm_word
return norm_line.strip()
def line_exist(lines, target_ctx_keys, target_line, exact_match=True):
for ctx_keys, line in lines:
if ctx_keys == target_ctx_keys:
if exact_match:
if line == target_line:
return True
else:
if line.startswith(target_line):
return True
return False
def bgp_delete_inst_move_line(lines_to_del):
# Deletion of bgp default inst followed by
# bgp vrf inst leads to issue of default
# instance can not be removed.
# Move the bgp default instance line to end.
bgp_defult_inst = False
bgp_vrf_inst = False
for ctx_keys, line in lines_to_del:
# Find bgp default inst
if (
ctx_keys[0].startswith("router bgp")
and not line
and "vrf" not in ctx_keys[0]
):
bgp_defult_inst = True
# Find bgp vrf inst
if ctx_keys[0].startswith("router bgp") and not line and "vrf" in ctx_keys[0]:
bgp_vrf_inst = True
if bgp_defult_inst and bgp_vrf_inst:
for ctx_keys, line in lines_to_del:
# move bgp default inst to end
if (
ctx_keys[0].startswith("router bgp")
and not line
and "vrf" not in ctx_keys[0]
):
lines_to_del.remove((ctx_keys, line))
lines_to_del.append((ctx_keys, line))
def bgp_delete_nbr_remote_as_line(lines_to_add):
# Handle deletion of neighbor <nbr> remote-as line from
# lines_to_add if the nbr is configured with peer-group and
# peer-group has remote-as config present.
# 'neighbor <nbr> remote-as change on peer is not allowed
# if the peer is part of peer-group and peer-group has
# remote-as config.
pg_dict = dict()
found_pg_cmd = False
# Find all peer-group commands; create dict of each peer-group
# to store assoicated neighbor as value
for ctx_keys, line in lines_to_add:
if (
ctx_keys[0].startswith("router bgp")
and line
and line.startswith("neighbor ")
):
# {'router bgp 65001': {'PG': [], 'PG1': []},
# 'router bgp 65001 vrf vrf1': {'PG': [], 'PG1': []}}
if ctx_keys[0] not in pg_dict:
pg_dict[ctx_keys[0]] = dict()
# find 'neighbor <pg_name> peer-group'
re_pg = re.match(r"neighbor (\S+) peer-group$", line)
if re_pg and re_pg.group(1) not in pg_dict[ctx_keys[0]]:
pg_dict[ctx_keys[0]][re_pg.group(1)] = {
"nbr": list(),
"remoteas": False,
}
found_pg_cmd = True
# Do nothing if there is no any "peer-group"
if found_pg_cmd is False:
return
# Find peer-group with remote-as command, also search neighbor
# associated to peer-group and store into peer-group dict
for ctx_keys, line in lines_to_add:
if (
ctx_keys[0].startswith("router bgp")
and line
and line.startswith("neighbor ")
):
if ctx_keys[0] in pg_dict:
for pg_key in pg_dict[ctx_keys[0]]:
# Find 'neighbor <pg_name> remote-as'
pg_rmtas = r"neighbor %s remote-as (\S+)" % pg_key
re_pg_rmtas = re.search(pg_rmtas, line)
if re_pg_rmtas:
pg_dict[ctx_keys[0]][pg_key]["remoteas"] = True
# Find 'neighbor <peer> [interface] peer-group <pg_name>'
nb_pg = r"neighbor (\S+) peer-group %s$" % pg_key
re_nbr_pg = re.search(nb_pg, line)
if (
re_nbr_pg
and re_nbr_pg.group(1) not in pg_dict[ctx_keys[0]][pg_key]
):
pg_dict[ctx_keys[0]][pg_key]["nbr"].append(re_nbr_pg.group(1))
# Find any neighbor <nbr> remote-as config line check if the nbr
# is in the peer group's list of nbrs. Remove 'neighbor <nbr> remote-as <>'
# from lines_to_add.
lines_to_del_from_add = []
for ctx_keys, line in lines_to_add:
if (
ctx_keys[0].startswith("router bgp")
and line
and line.startswith("neighbor ")
):
nbr_rmtas = r"neighbor (\S+) remote-as.*"
re_nbr_rmtas = re.search(nbr_rmtas, line)
if re_nbr_rmtas and ctx_keys[0] in pg_dict:
for pg in pg_dict[ctx_keys[0]]:
if pg_dict[ctx_keys[0]][pg]["remoteas"] == True:
for nbr in pg_dict[ctx_keys[0]][pg]["nbr"]:
if re_nbr_rmtas.group(1) == nbr:
lines_to_del_from_add.append((ctx_keys, line))
for ctx_keys, line in lines_to_del_from_add:
lines_to_add.remove((ctx_keys, line))
def bgp_remove_neighbor_cfg(lines_to_del, del_nbr_dict):
# This method handles deletion of bgp neighbor configs,
# if there is neighbor to peer-group cmd is in delete list.
# As 'no neighbor .* peer-group' deletes the neighbor,
# subsequent neighbor speciic config line deletion results
# in error.
lines_to_del_to_del = []
for ctx_keys, line in lines_to_del:
if (
ctx_keys[0].startswith("router bgp")
and line
and line.startswith("neighbor ")
):
if ctx_keys[0] in del_nbr_dict:
for nbr in del_nbr_dict[ctx_keys[0]]:
re_nbr_pg = re.search(r"neighbor (\S+) .*peer-group (\S+)", line)
nb_exp = r"neighbor %s .*" % nbr
if not re_nbr_pg:
re_nb = re.search(nb_exp, line)
if re_nb:
lines_to_del_to_del.append((ctx_keys, line))
for ctx_keys, line in lines_to_del_to_del:
lines_to_del.remove((ctx_keys, line))
def bgp_delete_move_lines(lines_to_add, lines_to_del):
# This method handles deletion of bgp peer group config.
# The objective is to delete config lines related to peers
# associated with the peer-group and move the peer-group
# config line to the end of the lines_to_del list.
bgp_delete_nbr_remote_as_line(lines_to_add)
del_dict = dict()
del_nbr_dict = dict()
# Stores the lines to move to the end of the pending list.
lines_to_del_to_del = []
# Stores the lines to move to end of the pending list.
lines_to_del_to_app = []
found_pg_del_cmd = False
# When "neighbor <pg_name> peer-group" under a bgp instance is removed,
# it also deletes the associated peer config. Any config line below no form of
# peer-group related to a peer are errored out as the peer no longer exists.
# To cleanup peer-group and associated peer(s) configs:
# - Remove all the peers config lines from the pending list (lines_to_del list).
# - Move peer-group deletion line to the end of the pending list, to allow
# removal of any of the peer-group specific configs.
#
# Create a dictionary of config context (i.e. router bgp vrf x).
# Under each context node, create a dictionary of a peer-group name.
# Append a peer associated to the peer-group into a list under a peer-group node.
# Remove all of the peer associated config lines from the pending list.
# Append peer-group deletion line to end of the pending list.
#
# Example:
# neighbor underlay peer-group
# neighbor underlay remote-as external
# neighbor underlay advertisement-interval 0
# neighbor underlay timers 3 9
# neighbor underlay timers connect 10