Skip to content
Snippets Groups Projects
Commit 261f2484 authored by Erik Reid's avatar Erik Reid
Browse files

handle netconf validation errors

parent 66200c45
Branches
Tags
No related merge requests found
...@@ -108,6 +108,10 @@ UNIT_SCHEMA = """<?xml version="1.1" encoding="UTF-8" ?> ...@@ -108,6 +108,10 @@ UNIT_SCHEMA = """<?xml version="1.1" encoding="UTF-8" ?>
""" # noqa: E501 """ # noqa: E501
class NetconfHandlingError(Exception):
pass
def _rpc(hostname, ssh): def _rpc(hostname, ssh):
dev = Device( dev = Device(
host=hostname, host=hostname,
...@@ -121,14 +125,22 @@ def _rpc(hostname, ssh): ...@@ -121,14 +125,22 @@ def _rpc(hostname, ssh):
def validate_netconf_config(config_doc): def validate_netconf_config(config_doc):
"""
:param config_doc:
:return:
:raises: NetconfHandlingError in case of validation errors
"""
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _validate(schema, doc): def _validate(schema, doc):
if schema.validate(doc): if schema.validate(doc):
return return
messages = []
for e in schema.error_log: for e in schema.error_log:
logger.error("%d.%d: %s" % (e.line, e.column, e.message)) msg = f'{e.line}.{e.column}: {e.message}'
assert False messages.append(msg)
logger.error(msg)
raise NetconfHandlingError('\n'.join(messages))
schema_doc = etree.XML(CONFIG_SCHEMA.encode('utf-8')) schema_doc = etree.XML(CONFIG_SCHEMA.encode('utf-8'))
config_schema = etree.XMLSchema(schema_doc) config_schema = etree.XMLSchema(schema_doc)
...@@ -151,6 +163,7 @@ def load_config(hostname, ssh_params): ...@@ -151,6 +163,7 @@ def load_config(hostname, ssh_params):
:param hostname: router hostname :param hostname: router hostname
:param ssh_params: 'ssh' config element(cf. config.py:CONFIG_SCHEMA) :param ssh_params: 'ssh' config element(cf. config.py:CONFIG_SCHEMA)
:return: :return:
:raises: NetconfHandlingError from validate_netconf_config
""" """
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.info("capturing netconf data for '%s'" % hostname) logger.info("capturing netconf data for '%s'" % hostname)
......
...@@ -157,7 +157,7 @@ def netconf_refresh_config(self, hostname): ...@@ -157,7 +157,7 @@ def netconf_refresh_config(self, hostname):
netconf_doc = juniper.load_config( netconf_doc = juniper.load_config(
hostname, InventoryTask.config["ssh"]) hostname, InventoryTask.config["ssh"])
netconf_str = etree.tostring(netconf_doc, encoding='unicode') netconf_str = etree.tostring(netconf_doc, encoding='unicode')
except ConnectionError: except (ConnectionError, juniper.NetconfHandlingError):
msg = f'error loading netconf data from {hostname}' msg = f'error loading netconf data from {hostname}'
logger.exception(msg) logger.exception(msg)
self.log_warning(msg) self.log_warning(msg)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment