Skip to content
Snippets Groups Projects
Commit 8dc0ce4e authored by Sam Roberts's avatar Sam Roberts
Browse files

present VRR peerings in endpoint

parent a862f23f
No related branches found
No related tags found
1 merge request!1Feature/reporting 297 msr mdvpn endpoint
...@@ -880,15 +880,37 @@ def bgp_all_peerings(): ...@@ -880,15 +880,37 @@ def bgp_all_peerings():
@routes.route('/mdvpn', methods=['GET', 'POST']) @routes.route('/mdvpn', methods=['GET', 'POST'])
@common.require_accepts_json @common.require_accepts_json
def mdvpn(): def mdvpn():
def _make_bgplu_index(bgplu): def _get_consistent_description(description):
"""
The same interface in VRR peerings can have multiple names.
These names are (currently) the same but with a different local prefix,
with no ordering guaranteed by the redis cache.
As only one description is returned by this endpoint for each
IPv4 address, this serves as a quick and dirty way of merging these
multiple descriptions into one an external user can use to identify
the peering reliably.
:param description: The raw description for a VRR peering
:return: The same description with location prefix removed
"""
# it is incredibly likely this will need revision later down the line
expected_prefixes = [
"MD-VPN-VRR-PARIS-",
"MD-VPN-VRR-LJUBLJANA-"
]
for prefix in expected_prefixes:
if description.startswith(prefix):
return description.replace(prefix, '')
return description
def _make_group_index(group, index_key):
index = {} index = {}
for peering in bgplu: for peering in group:
asn = peering['remote-asn'] key = peering.get(index_key)
if asn in index: if key in index:
peering_list = index[asn] peering_list = index[key]
peering_list.append(peering) peering_list.append(peering)
else: else:
index[asn] = [peering] index[key] = [peering]
return index return index
def _bgplu_peerings(asn, bgplu_index): def _bgplu_peerings(asn, bgplu_index):
...@@ -904,19 +926,44 @@ def mdvpn(): ...@@ -904,19 +926,44 @@ def mdvpn():
peerings.append(formatted_peering) peerings.append(formatted_peering)
return peerings return peerings
def _peerings_for_nren(asn, bgplu_index): def _vpnrr_peerings(asn, vpnrr_index):
peerings = []
if asn in vpnrr_index:
vrr_peering_group = vpnrr_index[asn]
# rearrange into index using ipv4 as key
# this will collect related entries under the same ipv4
ip_index = _make_group_index(vrr_peering_group, 'address')
for ip in ip_index:
ip_details = ip_index[ip] # a list of all info for given ipv4
hostnames = [item['hostname'] for item in ip_details]
description = ip_details[0]['description']
formatted_peering = {
"description": _get_consistent_description(description),
"v4": ip,
"hostname": hostnames
}
peerings.append(formatted_peering)
return peerings
def _peerings_for_nren(asn, bgplu_index, vpnrr_index):
return { return {
"asn": asn, "asn": asn,
"AP": _bgplu_peerings(asn, bgplu_index), "AP": _bgplu_peerings(asn, bgplu_index),
"VRR": [] "VRR": _vpnrr_peerings(asn, vpnrr_index)
} }
r = common.get_current_redis() r = common.get_current_redis()
bgplu = json.loads(r.get('juniper-peerings:group:BGPLU').decode('utf-8')) bgplu = json.loads(r.get('juniper-peerings:group:BGPLU').decode('utf-8'))
bgplu_index = _make_bgplu_index(bgplu) vpnrr = json.loads(r.get('juniper-peerings:group:VPN-RR').decode('utf-8'))
bgplu_index = _make_group_index(bgplu, 'remote-asn')
vpnrr_index = _make_group_index(vpnrr, 'remote-asn')
config = current_app.config['INVENTORY_PROVIDER_CONFIG'] config = current_app.config['INVENTORY_PROVIDER_CONFIG']
nren_asn_map = config['nren-asn-map'] nren_asn_map = config['nren-asn-map']
nren_details = [_peerings_for_nren(int(asn), bgplu_index) for asn in nren_details = [
nren_asn_map] _peerings_for_nren(int(asn),
bgplu_index,
vpnrr_index)
for asn in nren_asn_map]
response = json.dumps(nren_details) response = json.dumps(nren_details)
return Response(response, mimetype='application/json') return Response(response, mimetype='application/json')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment