summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-09-22 18:35:44 +0200
committerChristian Poessinger <christian@poessinger.com>2020-09-22 18:35:44 +0200
commitd28a6a516d449ede788816574c35061fbf7d6485 (patch)
tree58df84e463c2f516ffc0d9c4ef4fedd67c019849 /python
parente31dfd9f5542b0572e3ece89bdc347679b08aa72 (diff)
downloadvyos-1x-d28a6a516d449ede788816574c35061fbf7d6485.tar.gz
vyos-1x-d28a6a516d449ede788816574c35061fbf7d6485.zip
ifconfig: T2653: move is_member() from vyos.vylidate to vyos.configdict
Diffstat (limited to 'python')
-rw-r--r--python/vyos/configdict.py54
-rw-r--r--python/vyos/validate.py41
2 files changed, 47 insertions, 48 deletions
diff --git a/python/vyos/configdict.py b/python/vyos/configdict.py
index ef1b452a8..4a4a767f3 100644
--- a/python/vyos/configdict.py
+++ b/python/vyos/configdict.py
@@ -21,8 +21,9 @@ import os
from copy import deepcopy
from vyos.util import vyos_dict_search
-from vyos.validate import is_member
from vyos.xml import defaults
+from vyos.xml import is_tag
+from vyos.xml import is_leaf
from vyos import ConfigError
def retrieve_config(path_hash, base_path, config):
@@ -186,6 +187,47 @@ def T2665_set_dhcpv6pd_defaults(config_dict):
return config_dict
+def is_member(conf, interface, intftype=None):
+ """
+ Checks if passed interface is member of other interface of specified type.
+ intftype is optional, if not passed it will search all known types
+ (currently bridge and bonding)
+
+ Returns:
+ None -> Interface is not a member
+ interface name -> Interface is a member of this interface
+ False -> interface type cannot have members
+ """
+ ret_val = None
+ intftypes = ['bonding', 'bridge']
+ if intftype not in intftypes + [None]:
+ raise ValueError((
+ f'unknown interface type "{intftype}" or it cannot '
+ f'have member interfaces'))
+
+ intftype = intftypes if intftype == None else [intftype]
+
+ # set config level to root
+ old_level = conf.get_level()
+ conf.set_level([])
+
+ for it in intftype:
+ base = ['interfaces', it]
+ for intf in conf.list_nodes(base):
+ memberintf = base + [intf, 'member', 'interface']
+ if is_tag(memberintf):
+ if interface in conf.list_nodes(memberintf):
+ ret_val = intf
+ break
+ elif is_leaf(memberintf):
+ if ( conf.exists(memberintf) and
+ interface in conf.return_values(memberintf) ):
+ ret_val = intf
+ break
+
+ old_level = conf.set_level(old_level)
+ return ret_val
+
def get_interface_dict(config, base, ifname=''):
"""
Common utility function to retrieve and mandgle the interfaces available
@@ -236,17 +278,15 @@ def get_interface_dict(config, base, ifname=''):
# Check if we are a member of a bridge device
bridge = is_member(config, ifname, 'bridge')
- if bridge:
- dict.update({'is_bridge_member' : bridge})
+ if bridge: dict.update({'is_bridge_member' : bridge})
# Check if we are a member of a bond device
bond = is_member(config, ifname, 'bonding')
- if bond:
- dict.update({'is_bond_member' : bond})
+ if bond: dict.update({'is_bond_member' : bond})
+
mac = leaf_node_changed(config, ['mac'])
- if mac:
- dict.update({'mac_old' : mac})
+ if mac: dict.update({'mac_old' : mac})
eui64 = leaf_node_changed(config, ['ipv6', 'address', 'eui64'])
if eui64:
diff --git a/python/vyos/validate.py b/python/vyos/validate.py
index ceeb6888a..691cf3c8e 100644
--- a/python/vyos/validate.py
+++ b/python/vyos/validate.py
@@ -19,7 +19,6 @@ import netifaces
import ipaddress
from vyos.util import cmd
-from vyos import xml
# Important note when you are adding new validation functions:
#
@@ -267,46 +266,6 @@ def assert_mac(m):
raise ValueError(f'{m} is a VRRP MAC address')
-def is_member(conf, interface, intftype=None):
- """
- Checks if passed interface is member of other interface of specified type.
- intftype is optional, if not passed it will search all known types
- (currently bridge and bonding)
-
- Returns:
- None -> Interface is not a member
- interface name -> Interface is a member of this interface
- False -> interface type cannot have members
- """
- ret_val = None
- if intftype not in ['bonding', 'bridge', None]:
- raise ValueError((
- f'unknown interface type "{intftype}" or it cannot '
- f'have member interfaces'))
-
- intftype = ['bonding', 'bridge'] if intftype == None else [intftype]
-
- # set config level to root
- old_level = conf.get_level()
- conf.set_level([])
-
- for it in intftype:
- base = ['interfaces', it]
- for intf in conf.list_nodes(base):
- memberintf = base + [intf, 'member', 'interface']
- if xml.is_tag(memberintf):
- if interface in conf.list_nodes(memberintf):
- ret_val = intf
- break
- elif xml.is_leaf(memberintf):
- if ( conf.exists(memberintf) and
- interface in conf.return_values(memberintf) ):
- ret_val = intf
- break
-
- old_level = conf.set_level(old_level)
- return ret_val
-
def has_address_configured(conf, intf):
"""
Checks if interface has an address configured.