diff --git a/test/data/update_netconf_data.py b/test/data/update_netconf_data.py index ad1fce60233935788633f62331754a8d9849e82f..e438654bdbdc887efbd5a470567523be7202823c 100644 --- a/test/data/update_netconf_data.py +++ b/test/data/update_netconf_data.py @@ -6,15 +6,14 @@ from lxml import etree from inventory_provider import config - SCHEMA_FILENAME = os.path.realpath(os.path.join( os.path.dirname(__file__), "data", "juniper-netconf.xsd" )) -def _load_netconf(hostname, ssh_params): +def _load_netconf(hostname, ssh_params): dev = Device( host=hostname, user=ssh_params['username'], @@ -23,108 +22,10 @@ def _load_netconf(hostname, ssh_params): print(dev.facts) return dev.rpc.get_config() - # print(etree.tostring(data, encoding='unicode')) - # - # # for i in data.xpath('//configuration/groups/interfaces/interface'): - # for i in data.xpath('//configuration/interfaces/interface'): - # # print(i) - # print(i.xpath('./name/text()')) - # - # print("-----routing-instances-----") - # for i in data.xpath("//configuration/routing-instances/instance/interface"): - # print(i.xpath('./name/text()')) - # - - -CONFIG_SCHEMA = """<?xml version="1.1" encoding="UTF-8" ?> -<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> - - <xs:complexType name="generic-sequence"> - <xs:sequence> - <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/> - </xs:sequence> - <xs:anyAttribute processContents="skip" /> - </xs:complexType> - - <!-- NOTE: 'unit' content isn't validated --> - <xs:complexType name="juniper-interface"> - <xs:sequence> - <xs:choice minOccurs="1" maxOccurs="unbounded"> - <xs:element name="name" minOccurs="1" maxOccurs="1" type="xs:string" /> - <xs:element name="description" minOccurs="0" maxOccurs="1" type="xs:string" /> - <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded" /> - </xs:choice> - </xs:sequence> - <xs:attribute name="inactive" type="xs:string" /> - </xs:complexType> - - <xs:element name="configuration"> - <xs:complexType> - <xs:sequence> - <xs:choice minOccurs="1" maxOccurs="unbounded"> - <xs:element name="version" minOccurs="0" type="xs:string" /> - <xs:element name="groups" minOccurs="0" type="generic-sequence" /> - <xs:element name="apply-groups" minOccurs="0" type="xs:string" /> - <xs:element name="system" minOccurs="0" type="generic-sequence" /> - <xs:element name="logical-systems" minOccurs="0" type="generic-sequence" /> - <xs:element name="chassis" minOccurs="0" type="generic-sequence" /> - <xs:element name="services" minOccurs="0" type="generic-sequence" /> - <xs:element name="interfaces" minOccurs="0"> - <xs:complexType> - <xs:sequence> - <xs:choice minOccurs="1" maxOccurs="unbounded"> - <xs:element name="apply-groups" minOccurs="0" type="xs:string" /> - <xs:element name="interface" minOccurs="1" maxOccurs="unbounded" type="juniper-interface" /> - </xs:choice> - </xs:sequence> - </xs:complexType> - </xs:element> - <xs:element name="snmp" minOccurs="0" type="generic-sequence" /> - <xs:element name="forwarding-options" minOccurs="0" type="generic-sequence" /> - <xs:element name="routing-options" minOccurs="0" type="generic-sequence" /> - <xs:element name="protocols" minOccurs="0" type="generic-sequence" /> - <xs:element name="policy-options" minOccurs="0" type="generic-sequence" /> - <xs:element name="class-of-service" minOccurs="0" type="generic-sequence" /> - <xs:element name="firewall" minOccurs="0" type="generic-sequence" /> - <xs:element name="routing-instances" minOccurs="0" type="generic-sequence" /> - <xs:element name="bridge-domains" minOccurs="0" type="generic-sequence" /> - </xs:choice> - </xs:sequence> - <xs:attribute name="changed-seconds" type="xs:string" /> - <xs:attribute name="changed-localtime" type="xs:string" /> - </xs:complexType> - </xs:element> - -</xs:schema> -""" - -UNIT_SCHEMA = """<?xml version="1.1" encoding="UTF-8" ?> -<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> - - <xs:complexType name="generic-sequence"> - <xs:sequence> - <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/> - </xs:sequence> - <xs:anyAttribute processContents="skip" /> - </xs:complexType> - - <xs:element name="unit"> - <xs:complexType> - <xs:sequence> - <xs:element name="name" minOccurs="1" maxOccurs="1" type="xs:int" /> - <xs:element name="description" minOccurs="0" maxOccurs="1" type="xs:string" /> - <xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/> - </xs:sequence> - <xs:attribute name="inactive" type="xs:string" /> - </xs:complexType> - </xs:element> - -</xs:schema> -""" if __name__ == "__main__": - logging.basicConfig(level=logging.DEBUG) + logging.basicConfig(level=logging.INFO) TEST_DATA_DIR = os.path.dirname(__file__) CONFIG_FILENAME = "config.json" @@ -133,38 +34,10 @@ if __name__ == "__main__": with open("config.json") as f: params = config.load(f) for r in params['routers']: - # netconf = _load_netconf(r['hostname'], params['ssh']) - # filename = os.path.join( - # TEST_DATA_DIR, - # "%s-netconf.xml" % r['hostname']) - # with open(filename, 'w') as f: - # f.write(etree.tostring(netconf, encoding='unicode')) - + logging.debug("router: " + r['hostname']) + netconf = _load_netconf(r['hostname'], params['ssh']) filename = os.path.join( TEST_DATA_DIR, "%s-netconf.xml" % r['hostname']) - - # schema_doc = etree.parse(SCHEMA_FILENAME) - schema_doc = etree.XML(CONFIG_SCHEMA.encode('utf-8')) - schema = etree.XMLSchema(schema_doc) - parser = etree.XMLParser(schema=schema) - - logging.debug("FILENAME: %s" % filename) - try: - config_doc = etree.parse(filename, parser) - except etree.XMLSyntaxError as ex: - logging.debug(ex) - for e in parser.error_log: - logging.debug("%d.%d: %s" % (e.line, e.column, e.message)) - continue - - - - schema_doc = etree.XML(UNIT_SCHEMA.encode('utf-8')) - schema = etree.XMLSchema(schema_doc) - parser = etree.XMLParser(schema=schema) - - # for i in data.xpath('//configuration/groups/interfaces/interface'): - for i in config_doc.xpath('//configuration/interfaces/interface'): - for u in i.xpath('./unit'): - schema.assertValid(u) + with open(filename, 'w') as f: + f.write(etree.tostring(netconf, encoding='unicode'))