diff --git a/inventory_provider/routes/ims_classifier.py b/inventory_provider/routes/ims_classifier.py index c57167aa884795a66fa7f1dbdc04e0023fb9cad1..29b3b2e12f7f4098af2c1c3fad001e8e70b19969 100644 --- a/inventory_provider/routes/ims_classifier.py +++ b/inventory_provider/routes/ims_classifier.py @@ -14,7 +14,7 @@ from redis import Redis from inventory_provider.db.ims import IMS_SERVICE_NAMES from inventory_provider.routes import common from inventory_provider.routes.classifier import _asn_group_info, \ - _link_interface_info + _link_interface_info, _ix_peering_info, _vpn_rr_peering_info routes = Blueprint("ims-inventory-data-classifier-support-routes", __name__) @@ -341,48 +341,6 @@ def get_juniper_link_info(source_equipment: str, interface: str) -> Response: return Response(result, mimetype="application/json") -def ix_peering_info(peer_info: dict) -> dict: - """ - TODO: this is probably the least efficient way of doing this - (if it's a problem, pre-compute these lists) - - :param peer_info: an element from ix_public_peer:address - :return: - """ - - result = { - 'peer': peer_info, - 'group': [], - 'router': [] - } - - try: - ipaddress.ip_address(peer_info['name']) - except ValueError: - raise ClassifierProcessingError( - f'unable to parse {peer_info["name"]} as an ip address') - - description = peer_info['description'] - assert description is not None # sanity - - keyword = description.split(' ')[0] # regex needed??? (e.g. tabs???) - - for doc in common.load_json_docs( - config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'], - key_pattern='ix_public_peer:*', - num_threads=10): - - other = doc['value'] - if other['router'] == peer_info['router']: - result['router'].append(other['name']) - - assert other['description'] is not None # sanity: as above... - if other['description'].startswith(keyword): - result['group'].append(other['name']) - - return result - - def find_interfaces(address) -> Iterator: """ TODO: this is probably the least efficient way of doing this @@ -439,23 +397,20 @@ def peer_info(address_str: str) -> Response: 'locations': [], } - info = r.get(f'ix_public_peer:{address_str}') - if info: - info = info.decode('utf-8') - info = json.loads(info) - result['ix-public-peer-info'] = ix_peering_info(info) + ix_peering_info = _ix_peering_info(r, address_str) + if ix_peering_info: + result['ix-public-peer-info'] = ix_peering_info result['locations'].append(build_locations( _location_from_equipment( - get_ims_equipment_name(info['router']), r))) + get_ims_equipment_name( + ix_peering_info['peer']['router']), r))) - info = r.get(f'vpn_rr_peer:{address_str}') - if info: - info = info.decode('utf-8') - info = json.loads(info) - result['vpn-rr-peer-info'] = info + vpn_rr_peering_info = _vpn_rr_peering_info(r, address_str) + if vpn_rr_peering_info: + result['vpn-rr-peer-info'] = vpn_rr_peering_info result['locations'].append(build_locations( _location_from_equipment( - get_ims_equipment_name(info['router']), r))) + get_ims_equipment_name(vpn_rr_peering_info['router']), r))) asn_group_info = _asn_group_info(r, address_str) if asn_group_info: