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

switched poller route to use IMS data

parent 1aa3e9ee
No related branches found
No related tags found
No related merge requests found
import json import json
import logging import logging
import re import re
from distutils.util import strtobool
from flask import Blueprint, Response, current_app from flask import Blueprint, Response, current_app, request
from inventory_provider import juniper from inventory_provider import juniper
from inventory_provider.routes import common from inventory_provider.routes import common
from inventory_provider.routes.classifier import get_ims_equipment_name
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
routes = Blueprint('poller-support-routes', __name__) routes = Blueprint('poller-support-routes', __name__)
...@@ -104,35 +106,56 @@ def _load_interface_bundles(hostname=None): ...@@ -104,35 +106,56 @@ def _load_interface_bundles(hostname=None):
def _load_services(hostname=None): def _load_services(hostname=None):
result = dict() if hostname:
key_pattern = f'opsdb:interface_services:{hostname}:*' \ hostname = get_ims_equipment_name(hostname)
if hostname else 'opsdb:interface_services:*'
key_pattern = f'ims:interface_port_ids:{hostname}:*' \
def _service_params(full_service_info): if hostname else 'ims:interface_port_ids:*'
return {
'id': full_service_info['id'], port_id_interfaces = {}
'name': full_service_info['name'], interface_services = {}
'type': full_service_info['service_type'],
'status': full_service_info['status'] 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"]}'
def _filter_and_format_services(_services):
included_service_ids = set()
for s in _services:
if s['id'] in included_service_ids:
continue
if s['circuit_type'] == 'service':
included_service_ids.add(s['id'])
yield {
'id': s['id'],
'name': s['name'],
'type': s['service_type'],
'status': s['status']
}
for doc in common.load_json_docs( if_ports = common.load_json_docs(
config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'], config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'],
key_pattern=key_pattern, key_pattern=key_pattern,
num_threads=20): num_threads=20)
m = re.match(r'^opsdb: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) for r in common.load_json_docs(
interface = m.group(2) config_params=current_app.config['INVENTORY_PROVIDER_CONFIG'],
result.setdefault(router, dict()) key_pattern=_get_port_id_services(if_ports),
result[router][interface] = [_service_params(s) for s in doc['value']] 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 result return interface_services
def _load_interfaces(hostname): def _load_interfaces(hostname):
...@@ -159,7 +182,6 @@ def _load_interfaces(hostname): ...@@ -159,7 +182,6 @@ def _load_interfaces(hostname):
def _load_poller_interfaces(hostname=None): def _load_poller_interfaces(hostname=None):
snmp_indexes = _load_snmp_indexes(hostname) snmp_indexes = _load_snmp_indexes(hostname)
bundles = _load_interface_bundles(hostname) bundles = _load_interface_bundles(hostname)
services = _load_services(hostname) services = _load_services(hostname)
...@@ -207,10 +229,18 @@ def interfaces(hostname=None): ...@@ -207,10 +229,18 @@ def interfaces(hostname=None):
cache_key = f'classifier-cache:poller-interfaces:{hostname}' \ cache_key = f'classifier-cache:poller-interfaces:{hostname}' \
if hostname else 'classifier-cache:poller-interfaces:all' if hostname else 'classifier-cache:poller-interfaces:all'
r = common.get_current_redis() r = common.get_current_redis()
result = r.get(cache_key) ignore_cache = request.args.get('ignore-cache', default='false', type=str)
try:
ignore_cache = strtobool(ignore_cache)
except ValueError:
ignore_cache = False
if ignore_cache:
result = False
else:
result = r.get(cache_key)
if result: if result:
result = result.decode('utf-8') result = result.decode('utf-8')
else: else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment