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
|
#!/usr/bin/env python3
#
# Copyright (C) 2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import jmespath
from copy import deepcopy
from sys import exit
from vyos.config import Config
from vyos.template import render
from vyos import ConfigError, airbag
airbag.enable()
config_file = r'/tmp/bgp.frr'
default_config_data = {
'as_number': ''
}
def get_config():
bgp = deepcopy(default_config_data)
conf = Config()
# this lives in the "nbgp" tree until we switch over
base = ['protocols', 'nbgp']
if not conf.exists(base):
return None
bgp = deepcopy(default_config_data)
# Get full BGP configuration as dictionary - output the configuration for development
#
# vyos@vyos# commit
# [ protocols nbgp 65000 ]
# {'nbgp': {'65000': {'address-family': {'ipv4-unicast': {'aggregate-address': {'1.1.0.0/16': {},
# '2.2.2.0/24': {}}},
# 'ipv6-unicast': {'aggregate-address': {'2001:db8::/32': {}}}},
# 'neighbor': {'192.0.2.1': {'password': 'foo',
# 'remote-as': '100'}}}}}
#
tmp = conf.get_config_dict(base)
# extract base key from dict as this is our AS number
bgp['as_number'] = jmespath.search('nbgp | keys(@) [0]', tmp)
# adjust level of dictionary returned by get_config_dict()
# by using jmesgpath and update dictionary
bgp.update(jmespath.search('nbgp.* | [0]', tmp))
from pprint import pprint
pprint(bgp)
# resulting in e.g.
# vyos@vyos# commit
# [ protocols nbgp 65000 ]
# {'address-family': {'ipv4-unicast': {'aggregate-address': {'1.1.0.0/16': {},
# '2.2.2.0/24': {}}},
# 'ipv6-unicast': {'aggregate-address': {'2001:db8::/32': {}}}},
# 'as_number': '65000',
# 'neighbor': {'192.0.2.1': {'password': 'foo', 'remote-as': '100'}},
# 'timers': {'holdtime': '5'}}
return bgp
def verify(bgp):
# bail out early - looks like removal from running config
if not bgp:
return None
return None
def generate(bgp):
# bail out early - looks like removal from running config
if not bgp:
return None
render(config_file, 'frr/bgp.frr.tmpl', bgp)
return None
def apply(bgp):
return None
if __name__ == '__main__':
try:
c = get_config()
verify(c)
generate(c)
apply(c)
except ConfigError as e:
print(e)
exit(1)
|