From 101c0336cbacd108128c6bf28836fd5658611dc2 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Mon, 24 Dec 2018 16:24:40 +0100 Subject: [PATCH] parse bgp info from netconf data --- inventory_provider/netconf.py | 21 +++++++++++++++++ test/test_netconf_data.py | 43 ++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/inventory_provider/netconf.py b/inventory_provider/netconf.py index a0d7a1f5..9588dc57 100644 --- a/inventory_provider/netconf.py +++ b/inventory_provider/netconf.py @@ -157,3 +157,24 @@ def list_interfaces(netconf_config): unit_info['name'] = "%s.%s" % (info['name'], unit_info['name']) yield unit_info + +def list_bgp_routes(netconf_config): + for r in netconf_config.xpath( + '//configuration/routing-instances/' + 'instance[name/text()="IAS"]/protocols/bgp/' + 'group[starts-with(name/text(), "GEANT-IX")]/' + 'neighbor'): + name = r.find('name') + description = r.find('description') + local_as = r.find('local-as') + if local_as: + local_as = local_as.find('as-number') + peer_as = r.find('peer-as') + yield { + 'name': name.text, + 'description': description.text, + 'as': { + 'local': int(local_as.text), + 'peer': int(peer_as.text) + } + } diff --git a/test/test_netconf_data.py b/test/test_netconf_data.py index 26984a3a..38d65e2d 100644 --- a/test/test_netconf_data.py +++ b/test/test_netconf_data.py @@ -35,7 +35,28 @@ def netconf_doc(mocker, router, data_config): return netconf.load_config(router, data_config['ssh']) -def test_query_doc_and_validate(netconf_doc): +# def test_interface_list(netconf_doc): +# +# schema = { +# "$schema": "http://json-schema.org/draft-07/schema#", +# "type": "array", +# "items": { +# "type": "object", +# "properties": { +# "name": {"type": "string"}, +# "description": {"type": "string"} +# }, +# "required": ["name", "description"], +# "additionalProperties": False +# } +# } +# +# interfaces = list(netconf.list_interfaces(netconf_doc)) +# jsonschema.validate(interfaces, schema) +# assert interfaces # at least shouldn't be empty + + +def test_bgp_list(netconf_doc): schema = { "$schema": "http://json-schema.org/draft-07/schema#", @@ -44,16 +65,22 @@ def test_query_doc_and_validate(netconf_doc): "type": "object", "properties": { "name": {"type": "string"}, - "description": {"type": "string"} + "description": {"type": "string"}, + "as": { + "type": "object", + "properties": { + "local": {"type": "integer"}, + "peer": {"type": "integer"}, + }, + "required": ["local", "peer"], + "additionalProperties": False + } }, - "required": ["name", "description"], + "required": ["name", "description", "as"], "additionalProperties": False } } - interfaces = list(netconf.list_interfaces(netconf_doc)) - jsonschema.validate(interfaces, schema) - assert interfaces # at least shouldn't be empty - - + routes = list(netconf.list_bgp_routes(netconf_doc)) + jsonschema.validate(routes, schema) -- GitLab