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

roll back to version that downloads

parent f49a7945
Branches
No related tags found
No related merge requests found
...@@ -6,15 +6,14 @@ from lxml import etree ...@@ -6,15 +6,14 @@ from lxml import etree
from inventory_provider import config from inventory_provider import config
SCHEMA_FILENAME = os.path.realpath(os.path.join( SCHEMA_FILENAME = os.path.realpath(os.path.join(
os.path.dirname(__file__), os.path.dirname(__file__),
"data", "data",
"juniper-netconf.xsd" "juniper-netconf.xsd"
)) ))
def _load_netconf(hostname, ssh_params):
def _load_netconf(hostname, ssh_params):
dev = Device( dev = Device(
host=hostname, host=hostname,
user=ssh_params['username'], user=ssh_params['username'],
...@@ -23,108 +22,10 @@ def _load_netconf(hostname, ssh_params): ...@@ -23,108 +22,10 @@ def _load_netconf(hostname, ssh_params):
print(dev.facts) print(dev.facts)
return dev.rpc.get_config() 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__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.INFO)
TEST_DATA_DIR = os.path.dirname(__file__) TEST_DATA_DIR = os.path.dirname(__file__)
CONFIG_FILENAME = "config.json" CONFIG_FILENAME = "config.json"
...@@ -133,38 +34,10 @@ if __name__ == "__main__": ...@@ -133,38 +34,10 @@ if __name__ == "__main__":
with open("config.json") as f: with open("config.json") as f:
params = config.load(f) params = config.load(f)
for r in params['routers']: for r in params['routers']:
# netconf = _load_netconf(r['hostname'], params['ssh']) logging.debug("router: " + r['hostname'])
# filename = os.path.join( netconf = _load_netconf(r['hostname'], params['ssh'])
# 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( filename = os.path.join(
TEST_DATA_DIR, TEST_DATA_DIR,
"%s-netconf.xml" % r['hostname']) "%s-netconf.xml" % r['hostname'])
with open(filename, 'w') as f:
# schema_doc = etree.parse(SCHEMA_FILENAME) f.write(etree.tostring(netconf, encoding='unicode'))
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment