summaryrefslogtreecommitdiff
path: root/src/conf_mode/lldp.py
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-04-05 19:43:11 +0200
committerChristian Poessinger <christian@poessinger.com>2020-04-05 19:43:11 +0200
commit013a9369bf2d7b2ea67eb4880c9e97a7ddba1c51 (patch)
treed5c968be57a06cc9a6efb6d3b262c2817025ced4 /src/conf_mode/lldp.py
parent870676f17138677c3504386c35db2a966291895c (diff)
downloadvyos-1x-013a9369bf2d7b2ea67eb4880c9e97a7ddba1c51.tar.gz
vyos-1x-013a9369bf2d7b2ea67eb4880c9e97a7ddba1c51.zip
lldp: T2230: move inlined templates to dedicated files
Diffstat (limited to 'src/conf_mode/lldp.py')
-rwxr-xr-xsrc/conf_mode/lldp.py90
1 files changed, 34 insertions, 56 deletions
diff --git a/src/conf_mode/lldp.py b/src/conf_mode/lldp.py
index b72916ab8..dd0b1ba0b 100755
--- a/src/conf_mode/lldp.py
+++ b/src/conf_mode/lldp.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
-# Copyright (C) 2017-2019 VyOS maintainers and contributors
+# Copyright (C) 2017-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
@@ -14,47 +14,21 @@
# 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 re
-import sys
import os
-import jinja2
+import re
from copy import deepcopy
+from jinja2 import FileSystemLoader, Environment
+from sys import exit
+
from vyos.config import Config
from vyos.validate import is_addr_assigned,is_loopback_addr
+from vyos.defaults import directories as vyos_data_dir
from vyos import ConfigError
-# Please be careful if you edit the template.
config_file = "/etc/default/lldpd"
-lldp_tmpl = """
-### Autogenerated by lldp.py ###
-DAEMON_ARGS="-M 4{% if options.snmp %} -x{% endif %}{% if options.cdp %} -c{% endif %}{% if options.edp %} -e{% endif %}{% if options.fdp %} -f{% endif %}{% if options.sonmp %} -s{% endif %}"
-
-"""
-
vyos_config_file = "/etc/lldpd.d/01-vyos.conf"
-vyos_tmpl = """
-### Autogenerated by lldp.py ###
-
-configure system platform VyOS
-configure system description "VyOS {{ options.description }}"
-{% if options.listen_on -%}
-configure system interface pattern "{{ ( options.listen_on | select('equalto','all') | map('replace','all','*') | list + options.listen_on | select('equalto','!all') | map('replace','!all','!*') | list + options.listen_on | reject('equalto','all') | reject('equalto','!all') | list ) | unique | join(",") }}"
-{%- endif %}
-{% if options.mgmt_addr -%}
-configure system ip management pattern {{ options.mgmt_addr | join(",") }}
-{%- endif %}
-{%- for loc in location -%}
-{%- if loc.elin %}
-configure ports {{ loc.name }} med location elin "{{ loc.elin }}"
-{%- endif %}
-{%- if loc.coordinate_based %}
-configure ports {{ loc.name }} med location coordinate {% if loc.coordinate_based.latitude %}latitude {{ loc.coordinate_based.latitude }}{% endif %} {% if loc.coordinate_based.longitude %}longitude {{ loc.coordinate_based.longitude }}{% endif %} {% if loc.coordinate_based.altitude %}altitude {{ loc.coordinate_based.altitude }} m{% endif %} {% if loc.coordinate_based.datum %}datum {{ loc.coordinate_based.datum }}{% endif %}
-{%- endif %}
-
-
-{% endfor %}
-"""
+base = ['service', 'lldp']
default_config_data = {
"options": '',
@@ -64,7 +38,7 @@ default_config_data = {
def get_options(config):
options = {}
- config.set_level('service lldp')
+ config.set_level(base)
options['listen_vlan'] = config.exists('listen-vlan')
options['mgmt_addr'] = []
@@ -84,9 +58,9 @@ def get_options(config):
if snmp:
config.set_level('')
options["sys_snmp"] = config.exists('service snmp')
- config.set_level('service lldp')
+ config.set_level(base)
- config.set_level('service lldp legacy-protocols')
+ config.set_level(base + ['legacy-protocols'])
options['cdp'] = config.exists('cdp')
options['edp'] = config.exists('edp')
options['fdp'] = config.exists('fdp')
@@ -99,15 +73,15 @@ def get_options(config):
return options
def get_interface_list(config):
- config.set_level('service lldp')
- intfs_names = config.list_nodes('interface')
+ config.set_level(base)
+ intfs_names = config.list_nodes(['interface'])
if len(intfs_names) < 0:
return 0
interface_list = []
for name in intfs_names:
- config.set_level('service lldp interface {0}'.format(name))
- disable = config.exists('disable')
+ config.set_level(base + ['interface', name])
+ disable = config.exists(['disable'])
intf = {
'name': name,
'disable': disable
@@ -117,10 +91,10 @@ def get_interface_list(config):
def get_location_intf(config, name):
- path = 'service lldp interface {0}'.format(name)
+ path = base + ['interface', name]
config.set_level(path)
- config.set_level('{} location'.format(path))
+ config.set_level(path + ['location'])
elin = ''
coordinate_based = {}
@@ -128,18 +102,18 @@ def get_location_intf(config, name):
elin = config.return_value('elin')
if config.exists('coordinate-based'):
- config.set_level('{} location coordinate-based'.format(path))
+ config.set_level(path + ['location', 'coordinate-based'])
- coordinate_based['latitude'] = config.return_value('latitude')
- coordinate_based['longitude'] = config.return_value('longitude')
+ coordinate_based['latitude'] = config.return_value(['latitude'])
+ coordinate_based['longitude'] = config.return_value(['longitude'])
coordinate_based['altitude'] = '0'
- if config.exists('altitude'):
- coordinate_based['altitude'] = config.return_value('altitude')
+ if config.exists(['altitude']):
+ coordinate_based['altitude'] = config.return_value(['altitude'])
coordinate_based['datum'] = 'WGS84'
- if config.exists('datum'):
- coordinate_based['datum'] = config.return_value('datum')
+ if config.exists(['datum']):
+ coordinate_based['datum'] = config.return_value(['datum'])
intf = {
'name': name,
@@ -151,8 +125,8 @@ def get_location_intf(config, name):
def get_location(config):
- config.set_level('service lldp')
- intfs_names = config.list_nodes('interface')
+ config.set_level(base)
+ intfs_names = config.list_nodes(['interface'])
if len(intfs_names) < 0:
return 0
@@ -170,7 +144,7 @@ def get_location(config):
def get_config():
lldp = deepcopy(default_config_data)
conf = Config()
- if not conf.exists('service lldp'):
+ if not conf.exists(base):
return None
else:
lldp['options'] = get_options(conf)
@@ -232,11 +206,15 @@ def generate(lldp):
if lldp is None:
return
+ # Prepare Jinja2 template loader from files
+ tmpl_path = os.path.join(vyos_data_dir['data'], 'templates', 'lldp')
+ fs_loader = FileSystemLoader(tmpl_path)
+ env = Environment(loader=fs_loader)
+
with open('/opt/vyatta/etc/version', 'r') as f:
tmp = f.read()
lldp['options']['description'] = tmp.split()[1]
-
# generate listen on interfaces
for intf in lldp['interface_list']:
tmp = ''
@@ -248,13 +226,13 @@ def generate(lldp):
lldp['options']['listen_on'].append(tmp)
# generate /etc/default/lldpd
- tmpl = jinja2.Template(lldp_tmpl)
+ tmpl = env.get_template('lldpd.tmpl')
config_text = tmpl.render(lldp)
with open(config_file, 'w') as f:
f.write(config_text)
# generate /etc/lldpd.d/01-vyos.conf
- tmpl = jinja2.Template(vyos_tmpl)
+ tmpl = env.get_template('vyos.conf.tmpl')
config_text = tmpl.render(lldp)
with open(vyos_config_file, 'w') as f:
f.write(config_text)
@@ -278,5 +256,5 @@ if __name__ == '__main__':
apply(c)
except ConfigError as e:
print(e)
- sys.exit(1)
+ exit(1)