Initial commit
This commit is contained in:
77
validator/__init__.py
Normal file
77
validator/__init__.py
Normal file
@ -0,0 +1,77 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import (
|
||||
absolute_import,
|
||||
division,
|
||||
print_function,
|
||||
)
|
||||
|
||||
import logging
|
||||
try:
|
||||
import yamale
|
||||
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
|
||||
|
||||
class NullHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
|
||||
class Validator(object):
|
||||
def __init__(self, args):
|
||||
self.logger = logging.getLogger('vppcfg.validator')
|
||||
self.logger.addHandler(NullHandler())
|
||||
|
||||
self.args = args
|
||||
|
||||
def validate(self, yaml):
|
||||
ret_rv = True
|
||||
ret_msgs = []
|
||||
if self.args.schema:
|
||||
try:
|
||||
self.logger.info("Validating against schema %s" % self.args.schema)
|
||||
schema = yamale.make_schema(self.args.schema)
|
||||
data = yamale.make_data(content=str(yaml))
|
||||
yamale.validate(schema, data)
|
||||
self.logger.debug("Schema correctly validated by yamale")
|
||||
except ValueError as e:
|
||||
ret_rv = False
|
||||
for result in e.results:
|
||||
for error in result.errors:
|
||||
ret_msgs.extend(['yamale: %s' % error])
|
||||
return ret_rv, ret_msgs
|
||||
else:
|
||||
self.logger.warning("Schema validation disabled")
|
||||
|
||||
self.logger.debug("Validating Semantics...")
|
||||
|
||||
rv, msgs = bondethernet(self.args, yaml)
|
||||
if msgs:
|
||||
ret_msgs.extend(msgs)
|
||||
if not rv:
|
||||
ret_rv = False
|
||||
|
||||
rv, msgs = interface(self.args, yaml)
|
||||
if msgs:
|
||||
ret_msgs.extend(msgs)
|
||||
if not rv:
|
||||
ret_rv = False
|
||||
|
||||
rv, msgs = loopback(self.args, yaml)
|
||||
if msgs:
|
||||
ret_msgs.extend(msgs)
|
||||
if not rv:
|
||||
ret_rv = False
|
||||
|
||||
rv, msgs = bridgedomain(self.args, yaml)
|
||||
if msgs:
|
||||
ret_msgs.extend(msgs)
|
||||
if not rv:
|
||||
ret_rv = False
|
||||
|
||||
return ret_rv, ret_msgs
|
44
validator/bondethernet.py
Normal file
44
validator/bondethernet.py
Normal file
@ -0,0 +1,44 @@
|
||||
import logging
|
||||
import validator.interface as interface
|
||||
|
||||
class NullHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
|
||||
def exists(yaml, ifname):
|
||||
""" Return True if the BondEthernet exists """
|
||||
try:
|
||||
if ifname in yaml['bondethernets']:
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def bondethernet(args, yaml):
|
||||
result = True
|
||||
msgs = []
|
||||
logger = logging.getLogger('vppcfg.validator')
|
||||
logger.addHandler(NullHandler())
|
||||
|
||||
if not 'bondethernets' in yaml:
|
||||
return result, msgs
|
||||
|
||||
for ifname, iface in yaml['bondethernets'].items():
|
||||
logger.debug("bondethernet %s: %s" % (ifname, iface))
|
||||
for member in iface['interfaces']:
|
||||
if not interface.exists(yaml, member):
|
||||
msgs.append("bondethernet %s member %s doesn't exist" % (ifname, member))
|
||||
result = False
|
||||
continue
|
||||
|
||||
if interface.has_sub(yaml, member):
|
||||
msgs.append("bondethernet %s member %s has sub-interface(s)" % (ifname, member))
|
||||
result = False
|
||||
if interface.has_lcp(yaml, member):
|
||||
msgs.append("bondethernet %s member %s has an LCP" % (ifname, member))
|
||||
result = False
|
||||
if interface.has_address(yaml, member):
|
||||
msgs.append("bondethernet %s member %s has address(es)" % (ifname, member))
|
||||
result = False
|
||||
return result, msgs
|
15
validator/bridgedomain.py
Normal file
15
validator/bridgedomain.py
Normal file
@ -0,0 +1,15 @@
|
||||
import logging
|
||||
|
||||
class NullHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
|
||||
def bridgedomain(args, yaml):
|
||||
result = True
|
||||
msgs = []
|
||||
logger = logging.getLogger('vppcfg.validator')
|
||||
logger.addHandler(NullHandler())
|
||||
|
||||
logger.debug("Validating bridgedomains...")
|
||||
return result, msgs
|
149
validator/interface.py
Normal file
149
validator/interface.py
Normal file
@ -0,0 +1,149 @@
|
||||
import logging
|
||||
import validator.bondethernet as bondethernet
|
||||
|
||||
class NullHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
def has_sub(yaml, ifname):
|
||||
""" Returns True if this interface has sub-interfaces """
|
||||
if not 'interfaces' in yaml:
|
||||
return False
|
||||
|
||||
if ifname in yaml['interfaces']:
|
||||
iface = yaml['interfaces'][ifname]
|
||||
if 'sub-interfaces' in iface and len(iface['sub-interfaces']) > 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
def has_address(yaml, ifname):
|
||||
""" Returns True if this interface or sub-interface has one or more addresses"""
|
||||
if not 'interfaces' in yaml:
|
||||
return False
|
||||
|
||||
if '.' in ifname:
|
||||
ifname, subid = ifname.split('.')
|
||||
subid = int(subid)
|
||||
try:
|
||||
if len(yaml['interfaces'][ifname]['sub-interfaces'][subid]['addresses']) > 0:
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
try:
|
||||
if len(yaml['interfaces'][ifname]['addresses']) > 0:
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def is_bond_member(yaml, ifname):
|
||||
""" Returns True if this interface is a member of a BondEthernet """
|
||||
if not 'bondethernets' in yaml:
|
||||
return False
|
||||
|
||||
for bond, iface in yaml['bondethernets'].items():
|
||||
if not 'interfaces' in iface:
|
||||
continue
|
||||
if ifname in iface['interfaces']:
|
||||
return True
|
||||
return False
|
||||
|
||||
def has_lcp(yaml, ifname):
|
||||
""" Returns True if this interface or sub-interface has an LCP"""
|
||||
if not 'interfaces' in yaml:
|
||||
return False
|
||||
|
||||
if '.' in ifname:
|
||||
ifname, subid = ifname.split('.')
|
||||
subid = int(subid)
|
||||
try:
|
||||
if len(yaml['interfaces'][ifname]['sub-interfaces'][subid]['lcp']) > 0:
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
try:
|
||||
if len(yaml['interfaces'][ifname]['lcp']) > 0:
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def exists(yaml, ifname):
|
||||
""" Returns true if ifname exists as a phy or sub-int """
|
||||
try:
|
||||
if ifname in yaml['interfaces']:
|
||||
return True
|
||||
if '.' in ifname:
|
||||
ifname, subid = ifname.split('.')
|
||||
subid = int(subid)
|
||||
if subid in yaml['interfaces'][ifname]['sub-interfaces']:
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def valid_encapsulation(yaml, sub_ifname):
|
||||
try:
|
||||
ifname, subid = sub_ifname.split('.')
|
||||
subid = int(subid)
|
||||
sub_iface = yaml['interfaces'][ifname]['sub-interfaces'][subid]
|
||||
except:
|
||||
return False
|
||||
|
||||
if not 'encapsulation' in sub_iface:
|
||||
return True
|
||||
|
||||
encap = sub_iface['encapsulation']
|
||||
if 'dot1ad' in encap and 'dot1q' in encap:
|
||||
return False
|
||||
if 'inner-dot1q' in encap and not ('dot1ad' in encap or 'dot1q' in encap):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def interface(args, yaml):
|
||||
result = True
|
||||
msgs = []
|
||||
logger = logging.getLogger('vppcfg.validator')
|
||||
logger.addHandler(NullHandler())
|
||||
|
||||
if not 'interfaces' in yaml:
|
||||
return result, msgs
|
||||
|
||||
for ifname, iface in yaml['interfaces'].items():
|
||||
logger.debug("interface %s" % iface)
|
||||
if ifname.startswith("BondEthernet") and not bondethernet.exists(yaml, ifname):
|
||||
msgs.append("interface %s does not exist in bondethernets" % ifname)
|
||||
result = False
|
||||
continue
|
||||
|
||||
iface_lcp = has_lcp(yaml, ifname)
|
||||
iface_address = has_address(yaml, ifname)
|
||||
|
||||
if iface_address and not iface_lcp:
|
||||
msgs.append("interface %s has adddress(es) but no LCP" % ifname)
|
||||
result = False
|
||||
|
||||
if has_sub(yaml, ifname):
|
||||
for sub_id, sub_iface in yaml['interfaces'][ifname]['sub-interfaces'].items():
|
||||
sub_ifname = "%s.%d" % (ifname, sub_id)
|
||||
logger.debug("sub-interface %s" % sub_iface)
|
||||
if has_lcp(yaml, sub_ifname):
|
||||
if not iface_lcp:
|
||||
msgs.append("sub-interface %s has LCP but %s does not have LCP" % (sub_ifname, ifname))
|
||||
result = False
|
||||
if has_address(yaml, sub_ifname):
|
||||
## The sub_iface lcp is not required: it can be derived from the iface_lcp, which has to be set
|
||||
if not iface_lcp:
|
||||
msgs.append("sub-interface %s has address(es) but %s does not have LCP" % (sub_ifname, ifname))
|
||||
result = False
|
||||
if not valid_encapsulation(yaml, sub_ifname):
|
||||
msgs.append("sub-interface %s has invalid encapsulation" % (sub_ifname))
|
||||
result = False
|
||||
|
||||
return result, msgs
|
15
validator/loopback.py
Normal file
15
validator/loopback.py
Normal file
@ -0,0 +1,15 @@
|
||||
import logging
|
||||
|
||||
class NullHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
|
||||
def loopback(args, yaml):
|
||||
result = True
|
||||
msgs = []
|
||||
logger = logging.getLogger('vppcfg.validator')
|
||||
logger.addHandler(NullHandler())
|
||||
|
||||
logger.debug("Validating loopbacks...")
|
||||
return result, msgs
|
Reference in New Issue
Block a user