summaryrefslogtreecommitdiff
path: root/python/vyos/configdict.py
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-09-04 21:50:46 +0200
committerChristian Poessinger <christian@poessinger.com>2019-09-04 22:03:10 +0200
commit952871200ecee584e7ed1bcb37bdaa06111e3a72 (patch)
treeb8f0d609026746a27cee176f2d961e1cf3227fd6 /python/vyos/configdict.py
parent64d58eda4c1ebaa9d346d65d606d2a75694467ee (diff)
downloadvyos-1x-952871200ecee584e7ed1bcb37bdaa06111e3a72.tar.gz
vyos-1x-952871200ecee584e7ed1bcb37bdaa06111e3a72.zip
Python/configdict: add function vlan_to_dict
A generic function which can parse the VLAN (vif, vif-s, cif-c) nodes in a config session. A dictionary describing the VLAN is returned. A good example will be the interface-bonding.py script used to generate bond interfaces in the system. It is used as follows: if conf.exists('vif'): for vif in conf.list_nodes('vif'): # set config level to vif interface conf.set_level(cfg_base + ' vif ' + vif) bond['vif'].append(vlan_to_dict(conf))
Diffstat (limited to 'python/vyos/configdict.py')
-rw-r--r--python/vyos/configdict.py108
1 files changed, 108 insertions, 0 deletions
diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py
index a723c5322..4bc8863bb 100644
--- a/python/vyos/configdict.py
+++ b/python/vyos/configdict.py
@@ -18,6 +18,7 @@ A library for retrieving value dicts from VyOS configs in a declarative fashion.
"""
+from vyos import ConfigError
def retrieve_config(path_hash, base_path, config):
"""
@@ -86,3 +87,110 @@ def list_diff(first, second):
"""
second = set(second)
return [item for item in first if item not in second]
+
+
+def get_ethertype(ethertype_val):
+ if ethertype_val == '0x88A8':
+ return '802.1ad'
+ elif ethertype_val == '0x8100':
+ return '802.1q'
+ else:
+ raise ConfigError('invalid ethertype "{}"'.format(ethertype_val))
+
+
+def vlan_to_dict(conf):
+ """
+ Common used function which will extract VLAN related information from config
+ and represent the result as Python dictionary.
+
+ Function call's itself recursively if a vif-s/vif-c pair is detected.
+ """
+ vlan = {
+ 'id': conf.get_level().split()[-1], # get the '100' in 'interfaces bonding bond0 vif-s 100'
+ 'address': [],
+ 'address_remove': [],
+ 'description': '',
+ 'dhcp_client_id': '',
+ 'dhcp_hostname': '',
+ 'dhcpv6_prm_only': False,
+ 'dhcpv6_temporary': False,
+ 'disable': False,
+ 'disable_link_detect': 1,
+ 'mac': '',
+ 'mtu': 1500
+ }
+ # retrieve configured interface addresses
+ if conf.exists('address'):
+ vlan['address'] = conf.return_values('address')
+
+ # Determine interface addresses (currently effective) - to determine which
+ # address is no longer valid and needs to be removed from the bond
+ eff_addr = conf.return_effective_values('address')
+ act_addr = conf.return_values('address')
+ vlan['address_remove'] = list_diff(eff_addr, act_addr)
+
+ # retrieve interface description
+ if conf.exists('description'):
+ vlan['description'] = conf.return_value('description')
+
+ # get DHCP client identifier
+ if conf.exists('dhcp-options client-id'):
+ vlan['dhcp_client_id'] = conf.return_value('dhcp-options client-id')
+
+ # DHCP client host name (overrides the system host name)
+ if conf.exists('dhcp-options host-name'):
+ vlan['dhcp_hostname'] = conf.return_value('dhcp-options host-name')
+
+ # DHCPv6 only acquire config parameters, no address
+ if conf.exists('dhcpv6-options parameters-only'):
+ vlan['dhcpv6_prm_only'] = conf.return_value('dhcpv6-options parameters-only')
+
+ # DHCPv6 temporary IPv6 address
+ if conf.exists('dhcpv6-options temporary'):
+ vlan['dhcpv6_temporary'] = conf.return_value('dhcpv6-options temporary')
+
+ # ignore link state changes
+ if conf.exists('disable-link-detect'):
+ vlan['disable_link_detect'] = 2
+
+ # disable bond interface
+ if conf.exists('disable'):
+ vlan['disable'] = True
+
+ # Media Access Control (MAC) address
+ if conf.exists('mac'):
+ vlan['mac'] = conf.return_value('mac')
+
+ # Maximum Transmission Unit (MTU)
+ if conf.exists('mtu'):
+ vlan['mtu'] = int(conf.return_value('mtu'))
+
+ # ethertype is mandatory on vif-s nodes and only exists here!
+ # check if this is a vif-s node at all:
+ if conf.get_level().split()[-2] == 'vif-s':
+ vlan['vif_c'] = []
+ vlan['vif_c_remove'] = []
+
+ # ethertype uses a default of 0x88A8
+ tmp = '0x88A8'
+ if conf.exists('ethertype'):
+ tmp = conf.return_value('ethertype')
+ vlan['ethertype'] = get_ethertype(tmp)
+
+ # get vif-c interfaces (currently effective) - to determine which vif-c
+ # interface is no longer present and needs to be removed
+ eff_intf = conf.list_effective_nodes('vif-c')
+ act_intf = conf.list_nodes('vif-c')
+ vlan['vif_c_remove'] = list_diff(eff_intf, act_intf)
+
+ # check if there is a Q-in-Q vlan customer interface
+ # and call this function recursively
+ if conf.exists('vif-c'):
+ cfg_level = conf.get_level()
+ # add new key (vif-c) to dictionary
+ for vif in conf.list_nodes('vif-c'):
+ # set config level to vif interface
+ conf.set_level(cfg_level + ' vif-c ' + vif)
+ vlan['vif_c'].append(vlan_to_dict(conf))
+
+ return vlan