diff --git a/inventory_provider/netconf.py b/inventory_provider/netconf.py index 008134136f673709883f065953e801a6c3941634..e20edbeaae61d23218cdbab8335f069e2d45e9ab 100644 --- a/inventory_provider/netconf.py +++ b/inventory_provider/netconf.py @@ -1,19 +1,21 @@ # import os from jnpr.junos import Device -# from inventory_provider import config from lxml import etree -def load_config(hostname, ssh_params): - +def _rpc(hostname, ssh): dev = Device( host=hostname, - user=ssh_params['username'], - ssh_private_key_file=ssh_params['private-key']) + user=ssh['username'], + ssh_private_key_file=ssh['private-key']) dev.open() + return dev.rpc + + +def load_config(hostname, ssh_params): # data = dev.rpc.get_config(options={'format': 'json'}) - return dev.rpc.get_config() + return _rpc(hostname, ssh_params).get_config() def load_interfaces(hostname, ssh_params): @@ -47,11 +49,3 @@ def load_interfaces(hostname, ssh_params): # for i in data.xpath("//configuration/routing-instances/instance/interface"): # print(i.xpath('./name/text()')) - -# 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']) -# with open(os.path.join(TEST_DATA_DIR, "%s-netconf.xml" % r['hostname']), 'w') as f: -# f.write(etree.tostring(netconf, encoding='unicode')) diff --git a/test/data/netconf-config-schema.xml b/test/data/netconf-config-schema.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e18961529dc601e2c0efce7f474ae371777f8c9 Binary files /dev/null and b/test/data/netconf-config-schema.xml differ diff --git a/test/data/update_netconf_config_schema.py b/test/data/update_netconf_config_schema.py new file mode 100644 index 0000000000000000000000000000000000000000..0e4b4e99616e94932e496175fefa4193e4a72633 --- /dev/null +++ b/test/data/update_netconf_config_schema.py @@ -0,0 +1,27 @@ +import os +from lxml import etree +from inventory_provider import netconf + +def load_config_schema(hostname, ssh_params): + return netconf._rpc(hostname, ssh_params).get_xnm_information( + type='xml-schema', + namespace='junos-configuration') + + +if __name__ == "__main__": + from inventory_provider import config + with open("config.json") as f: + params = config.load(f) + # for r in params['routers']: + # netconf = _load_netconf(r['hostname'], params['ssh']) + # with open(os.path.join(TEST_DATA_DIR, "%s-netconf.xml" % r['hostname']), 'w') as f: + # f.write(etree.tostring(netconf, encoding='unicode')) + # x = dev.rpc.get_software_information() + + hostname = 'mx1.ams.nl.geant.net' + # hostname = params['routers'][0]['hostname'] + + OUTPUT_DIRNAME = os.path.dirname(__file__) + schema_doc = load_config_schema(hostname, params['ssh']) + with open(os.path.join(OUTPUT_DIRNAME, 'netconf-config-schema.xml'), 'w') as f: + f.write(etree.tostring(schema_doc, pretty_print=True).decode('utf-8')) \ No newline at end of file diff --git a/test/test_netconf_data.py b/test/test_netconf_data.py new file mode 100644 index 0000000000000000000000000000000000000000..88d3613f05b6fa9775978096d066ec272f3146f3 --- /dev/null +++ b/test/test_netconf_data.py @@ -0,0 +1,36 @@ +import os +from inventory_provider import netconf +from lxml import etree + +TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') + +class MockedJunosRpc(object): + + def __init__(self, hostname): + filename = os.path.join(TEST_DATA_DIR, "%s-netconf.xml" % hostname) + self.config = etree.parse(filename) + + def get_config(self): + return self.config + + +class MockedJunosDevice(object): + + def __init__(self, **kwargs): + self.rpc = MockedJunosRpc(kwargs['host']) + + def open(self): + pass + + +def test_nr1(mocker, data_config): + mocker.patch( + 'inventory_provider.pyez_test.Device', + # 'inventory_provider.pyez_test.jnpr.junos.Device', + MockedJunosDevice) + + import json + for r in data_config['routers']: + print(r['hostname']) + x = netconf.load_interfaces(r['hostname'], data_config['ssh']) + print(json.dumps(list(x), indent=4))