Add bridge-domain support.
Refactor validator main function to avoid 'interface' symbol clash. Add get_mtu() for interfaces, returns the sub-int's MTU or its parent's MTU, defaulting to 1500. Ensure MTU for all bridge-domain members is set to the same value. Ensure all bridge-domain members are L2 (have no LCP, have no address)
This commit is contained in:
@ -12,10 +12,10 @@ try:
|
||||
except ImportError:
|
||||
print("ERROR: install yamale manually: sudo pip install yamale")
|
||||
sys.exit(-2)
|
||||
from validator.loopback import loopback
|
||||
from validator.bondethernet import bondethernet
|
||||
from validator.interface import interface
|
||||
from validator.bridgedomain import bridgedomain
|
||||
from validator.loopback import validate_loopbacks
|
||||
from validator.bondethernet import validate_bondethernets
|
||||
from validator.interface import validate_interfaces
|
||||
from validator.bridgedomain import validate_bridgedomains
|
||||
|
||||
from yamale.validators import DefaultValidators, Validator
|
||||
import ipaddress
|
||||
@ -79,25 +79,25 @@ class Validator(object):
|
||||
|
||||
self.logger.debug("Validating Semantics...")
|
||||
|
||||
rv, msgs = bondethernet(self.args, yaml)
|
||||
rv, msgs = validate_bondethernets(self.args, yaml)
|
||||
if msgs:
|
||||
ret_msgs.extend(msgs)
|
||||
if not rv:
|
||||
ret_rv = False
|
||||
|
||||
rv, msgs = interface(self.args, yaml)
|
||||
rv, msgs = validate_interfaces(self.args, yaml)
|
||||
if msgs:
|
||||
ret_msgs.extend(msgs)
|
||||
if not rv:
|
||||
ret_rv = False
|
||||
|
||||
rv, msgs = loopback(self.args, yaml)
|
||||
rv, msgs = validate_loopbacks(self.args, yaml)
|
||||
if msgs:
|
||||
ret_msgs.extend(msgs)
|
||||
if not rv:
|
||||
ret_rv = False
|
||||
|
||||
rv, msgs = bridgedomain(self.args, yaml)
|
||||
rv, msgs = validate_bridgedomains(self.args, yaml)
|
||||
if msgs:
|
||||
ret_msgs.extend(msgs)
|
||||
if not rv:
|
||||
|
@ -16,7 +16,7 @@ def get_by_name(yaml, ifname):
|
||||
return None
|
||||
|
||||
|
||||
def bondethernet(args, yaml):
|
||||
def validate_bondethernets(args, yaml):
|
||||
result = True
|
||||
msgs = []
|
||||
logger = logging.getLogger('vppcfg.validator')
|
||||
|
@ -1,15 +1,55 @@
|
||||
import logging
|
||||
import validator.interface as interface
|
||||
|
||||
class NullHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
|
||||
def bridgedomain(args, yaml):
|
||||
def get_by_name(yaml, ifname):
|
||||
""" Return the BridgeDomain by name, if it exists. Return None otherwise. """
|
||||
try:
|
||||
if ifname in yaml['bridgedomains']:
|
||||
return yaml['bridgedomains'][ifname]
|
||||
except:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def validate_bridgedomains(args, yaml):
|
||||
result = True
|
||||
msgs = []
|
||||
logger = logging.getLogger('vppcfg.validator')
|
||||
logger.addHandler(NullHandler())
|
||||
|
||||
if not 'bridgedomains' in yaml:
|
||||
return result, msgs
|
||||
|
||||
logger.debug("Validating bridgedomains...")
|
||||
for ifname, iface in yaml['bridgedomains'].items():
|
||||
logger.debug("bridgedomain %s" % iface)
|
||||
bd_mtu = 1500
|
||||
if 'mtu' in iface:
|
||||
bd_mtu = iface['mtu']
|
||||
|
||||
if 'interfaces' in iface:
|
||||
for member in iface['interfaces']:
|
||||
member_iface = interface.get_by_name(yaml, member)
|
||||
if not member_iface:
|
||||
msgs.append("bondethernet %s member %s doesn't exist" % (ifname, member))
|
||||
result = False
|
||||
continue
|
||||
|
||||
if interface.has_lcp(yaml, member):
|
||||
msgs.append("bridgedomain %s member %s has an LCP" % (ifname, member))
|
||||
result = False
|
||||
if interface.has_address(yaml, member):
|
||||
msgs.append("bridgedomain %s member %s has an address" % (ifname, member))
|
||||
result = False
|
||||
member_mtu = interface.get_mtu(yaml, member)
|
||||
if member_mtu != bd_mtu:
|
||||
msgs.append("bridgedomain %s member %s has MTU %d, while bridge has %d" % (ifname, member, member_mtu, bd_mtu))
|
||||
result = False
|
||||
|
||||
|
||||
return result, msgs
|
||||
|
@ -5,6 +5,19 @@ class NullHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
def get_parent_by_name(yaml, ifname):
|
||||
""" Returns the sub-interface's parent, or None if the sub-int doesn't exist. """
|
||||
if not '.' in ifname:
|
||||
return None
|
||||
ifname, subid = ifname.split('.')
|
||||
subid = int(subid)
|
||||
try:
|
||||
iface = yaml['interfaces'][ifname]
|
||||
return iface
|
||||
except:
|
||||
pass
|
||||
return None
|
||||
|
||||
def get_by_name(yaml, ifname):
|
||||
""" Returns the interface or sub-interface by a given name, or None if it does not exist """
|
||||
if '.' in ifname:
|
||||
@ -115,7 +128,31 @@ def valid_encapsulation(yaml, sub_ifname):
|
||||
return True
|
||||
|
||||
|
||||
def interface(args, yaml):
|
||||
def is_l3(yaml, ifname):
|
||||
""" Returns True if the interface exists and has either an LCP or an address """
|
||||
iface = get_by_name(yaml, ifname)
|
||||
if not iface:
|
||||
return False
|
||||
if has_lcp(yaml, ifname):
|
||||
return True
|
||||
if has_address(yaml, ifname):
|
||||
return True
|
||||
return False
|
||||
|
||||
def get_mtu(yaml, ifname):
|
||||
""" Returns MTU of the interface. If it's not set, return the parent's MTU, and
|
||||
return 1500 if no MTU was set on the sub-int or the parent."""
|
||||
iface = get_by_name(yaml, ifname)
|
||||
parent_iface = get_parent_by_name(yaml, ifname)
|
||||
try:
|
||||
return iface['mtu']
|
||||
return parent_iface['mtu']
|
||||
except:
|
||||
pass
|
||||
return 1500
|
||||
|
||||
|
||||
def validate_interfaces(args, yaml):
|
||||
result = True
|
||||
msgs = []
|
||||
logger = logging.getLogger('vppcfg.validator')
|
||||
|
@ -14,7 +14,7 @@ def get_by_name(yaml, ifname):
|
||||
return None
|
||||
|
||||
|
||||
def loopback(args, yaml):
|
||||
def validate_loopbacks(args, yaml):
|
||||
result = True
|
||||
msgs = []
|
||||
logger = logging.getLogger('vppcfg.validator')
|
||||
|
Reference in New Issue
Block a user