Skip to content
Snippets Groups Projects
Commit 04b3a824 authored by Robert Latta's avatar Robert Latta
Browse files

changed _load_interfaces to use cached interface service info

parent f71fe002
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,8 @@ from distutils.util import strtobool
from flask import Blueprint, Response, current_app, request
from inventory_provider import juniper
from inventory_provider.routes import common
from inventory_provider.routes.classifier import get_ims_equipment_name
from inventory_provider.routes.classifier import get_ims_equipment_name, \
get_ims_interface
logger = logging.getLogger(__name__)
routes = Blueprint('poller-support-routes', __name__)
......@@ -109,19 +110,9 @@ def _load_services(hostname=None):
if hostname:
hostname = get_ims_equipment_name(hostname)
key_pattern = f'ims:interface_port_ids:{hostname}:*' \
if hostname else 'ims:interface_port_ids:*'
port_id_interfaces = {}
interface_services = {}
def _get_port_id_services(s):
nonlocal port_id_interfaces
for d in s:
k = d['key']
v = d['value']
port_id_interfaces[v['port_id']] = k.split(':')[-2:]
yield f'ims:port_id_services:{v["port_id"]}'
result = dict()
key_pattern = f'ims:interface_services:{hostname}:*' \
if hostname else 'ims:interface_services:*'
def _filter_and_format_services(_services):
included_service_ids = set()
......@@ -137,25 +128,24 @@ def _load_services(hostname=None):
'status': s['status']
}
if_ports = common.load_json_docs(
for doc in common.load_json_docs(
config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'],
key_pattern=key_pattern,
num_threads=20)
for r in common.load_json_docs(
config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'],
key_pattern=_get_port_id_services(if_ports),
num_threads=20):
services = r['value']
if services:
h, i = port_id_interfaces[services[0]['port_a_id']]
ifs_h = interface_services.get(h, {})
interface_services[h] = ifs_h
ifs_h_i = ifs_h.get(i, [])
ifs_h_i.extend(list(_filter_and_format_services(services)))
ifs_h[i] = ifs_h_i
return interface_services
m = re.match(r'^ims:interface_services:([^:]+):(.+)', doc['key'])
if not m:
logger.warning(f'can\'t parse redis service key {doc["key"]}')
# there are some weird records (dtn*, dp1*)
continue
router = m.group(1)
interface = m.group(2)
result.setdefault(router, dict())
result[router][interface] = \
list(_filter_and_format_services(doc['value']))
return result
def _load_interfaces(hostname):
......@@ -199,9 +189,12 @@ def _load_poller_interfaces(hostname=None):
base_ifc = ifc['name'].split('.')[0]
ifc['bundle-parents'] = router_bundle.get(base_ifc, [])
router_services = services.get(ifc['router'], None)
router_services = services.get(
get_ims_equipment_name(ifc['router']), None)
if router_services:
ifc['circuits'] = router_services.get(ifc['name'], [])
ifc['circuits'] = router_services.get(
get_ims_interface(ifc['name']), []
)
yield ifc
......
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