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

one method for returning all peerings

parent b5d512bc
No related branches found
No related tags found
No related merge requests found
...@@ -211,6 +211,42 @@ def list_interfaces(netconf_config): ...@@ -211,6 +211,42 @@ def list_interfaces(netconf_config):
for u in _units(name.text, i): for u in _units(name.text, i):
yield u yield u
def all_bgp_peers(netconf_config):
def _peering_params(neighbor_node):
info = {'address': neighbor_node.find('name').text}
peer_as = neighbor_node.find('peer-as')
if peer_as is not None:
# lxml usage warning: can't just test `if peer_as:`
info['peer-as'] = int(peer_as.text)
description = neighbor_node.find('description')
if description is not None:
# lxml usage warning: can't just test `if description:`
info['description'] = description.text
return info
for instance in netconf_config.xpath(
'//configuration/routing-instances/instance'):
instance_name = instance.find('name').text
for group in instance.xpath('./protocols/bgp/group'):
group_name = group.find('name').text
for neighbor in group.xpath('./neighbor'):
peer = _peering_params(neighbor)
peer['instance'] = instance_name
peer['group'] = group_name
yield peer
for logical_system in netconf_config.xpath(
'//configuration/logical-systems'):
logical_system_name = logical_system.find('name').text
for group in logical_system.xpath('./protocols/bgp/group'):
group_name = group.find('name').text
for neighbor in group.xpath('./neighbor'):
peer = _peering_params(neighbor)
peer['logical-system'] = logical_system_name
peer['group'] = group_name
yield peer
def list_bgp_routes(netconf_config): def list_bgp_routes(netconf_config):
for r in netconf_config.xpath( for r in netconf_config.xpath(
......
...@@ -169,7 +169,7 @@ def bgp_configs(hostname): ...@@ -169,7 +169,7 @@ def bgp_configs(hostname):
status=404, status=404,
mimetype="text/html") mimetype="text/html")
routes = list(juniper.list_bgp_routes( routes = list(juniper.all_bgp_peers(
etree.XML(netconf_string.decode('utf-8')))) etree.XML(netconf_string.decode('utf-8'))))
if not routes: if not routes:
return Response( return Response(
......
...@@ -35,33 +35,53 @@ def test_interface_list(netconf_doc): ...@@ -35,33 +35,53 @@ def test_interface_list(netconf_doc):
assert interfaces # at least shouldn't be empty assert interfaces # at least shouldn't be empty
def test_bgp_list(netconf_doc): def test_bgp_peering_data(netconf_doc):
schema = { schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"type": "array", "definitions": {
"items": { "instance-peering": {
"type": "object", "type": "object",
"properties": { "properties": {
"name": {"type": "string"}, "instance": {"type": "string"},
"description": {"type": "string"}, "group": {"type": "string"},
"as": { "description": {"type": "string"},
"type": "object", "address": {"type": "string"},
"properties": { "peer-as": {"type": "integer"}
"local": {"type": "integer"}, },
"peer": {"type": "integer"}, # description is not always present, just based on
}, # empirical tests - not a problem
"required": ["local", "peer"], "required": ["instance", "group", "address", "peer-as"],
"additionalProperties": False "additionalProperties": False
}
}, },
"required": ["name", "description", "as"], "logical-system-peering": {
"additionalProperties": False "type": "object",
} "properties": {
"logical-system": {"type": "string"},
"group": {"type": "string"},
"description": {"type": "string"},
"address": {"type": "string"},
"peer-as": {"type": "integer"}
},
# peer-as and/or description are not always present,
# just based on empirical tests - not a problem
"required": ["logical-system", "group", "address"],
"additionalProperties": False
},
"peering": {
"oneOf": [
{"$ref": "#/definitions/instance-peering"},
{"$ref": "#/definitions/logical-system-peering"}
]
}
},
"type": "array",
"items": {"$ref": "#/definitions/peering"}
} }
routes = list(juniper.list_bgp_routes(netconf_doc)) peerings = list(juniper.all_bgp_peers(netconf_doc))
jsonschema.validate(routes, schema) jsonschema.validate(peerings, schema)
assert peerings # there's always at least one
def test_snmp_community_string(mocked_netifaces, netconf_doc): def test_snmp_community_string(mocked_netifaces, netconf_doc):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment