summaryrefslogtreecommitdiff
path: root/src/conf_mode/lldp.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/conf_mode/lldp.py')
-rwxr-xr-xsrc/conf_mode/lldp.py102
1 files changed, 40 insertions, 62 deletions
diff --git a/src/conf_mode/lldp.py b/src/conf_mode/lldp.py
index b72916ab8..ec59c68d0 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,24 @@
# 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.version import get_version_data
from vyos import ConfigError
+from vyos.util import call
-# 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 %}"
-
-"""
+config_file = "/etc/default/lldpd"
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 +41,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,30 +61,31 @@ 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')
options['sonmp'] = config.exists('sonmp')
# start with an unknown version information
- options['description'] = 'unknown'
+ version_data = get_version_data()
+ options['description'] = version_data['version']
options['listen_on'] = []
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 +95,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 +106,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 +129,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 +148,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,10 +210,10 @@ def generate(lldp):
if lldp is None:
return
- with open('/opt/vyatta/etc/version', 'r') as f:
- tmp = f.read()
- lldp['options']['description'] = tmp.split()[1]
-
+ # 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)
# generate listen on interfaces
for intf in lldp['interface_list']:
@@ -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)
@@ -263,10 +241,10 @@ def generate(lldp):
def apply(lldp):
if lldp:
# start/restart lldp service
- os.system('sudo systemctl restart lldpd.service')
+ call('sudo systemctl restart lldpd.service')
else:
# LLDP service has been terminated
- os.system('sudo systemctl stop lldpd.service')
+ call('sudo systemctl stop lldpd.service')
os.unlink(config_file)
os.unlink(vyos_config_file)
@@ -278,5 +256,5 @@ if __name__ == '__main__':
apply(c)
except ConfigError as e:
print(e)
- sys.exit(1)
+ exit(1)