diff --git a/test/data/update_netconf_data.py b/test/data/update_netconf_data.py index e6c766ee58d8fb252cc7357e79ae68101235c6d8..72850761f0da5b4f5aa086d9ade40a6bd05638ec 100644 --- a/test/data/update_netconf_data.py +++ b/test/data/update_netconf_data.py @@ -6,6 +6,12 @@ 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): dev = Device( @@ -28,17 +34,80 @@ def _load_netconf(hostname, ssh_params): # print(i.xpath('./name/text()')) # + +SCHEMA = """<?xml version="1.0" encoding="UTF-8" ?> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> + + <xs:complexType name="generic-sequence"> + <xs:sequence> + <xs:any processContents="skip" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + <xs:anyAttribute processContents="skip" /> + </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" type="generic-sequence" /> + </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> +""" if __name__ == "__main__": TEST_DATA_DIR = os.path.dirname(__file__) CONFIG_FILENAME = "config.json" + SCHEMA_FILENAME = os.path.join(TEST_DATA_DIR, "juniper-netconf.xsd") with open("config.json") as f: params = config.load(f) for r in params['routers']: - netconf = _load_netconf(r['hostname'], params['ssh']) + # 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')) + 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')) + + # schema_doc = etree.parse(SCHEMA_FILENAME) + schema_doc = etree.XML(SCHEMA.encode('utf-8')) + schema = etree.XMLSchema(schema_doc) + parser = etree.XMLParser(schema=schema) + + print(filename) + etree.parse(filename, parser)