You need to sign in or sign up before continuing.
-
Robert Latta authoredRobert Latta authored
testing.py 6.44 KiB
import collections
import json
import re
from flask import Blueprint, Response, jsonify, current_app
from lxml import etree
from inventory_provider import juniper
from inventory_provider.routes import common
from inventory_provider.tasks import worker, ims_worker
from inventory_provider.tasks import common as worker_common
routes = Blueprint("inventory-data-testing-support-routes", __name__)
@routes.route("flushdb", methods=['GET', 'POST'])
def flushdb():
common.get_current_redis().flushdb()
return Response('OK')
# IMS routes
@routes.route("update-circuit-hierarchy-ims", methods=['GET', 'POST'])
def update_circuit_hierarchy_ims():
ims_worker.update_circuit_hierarchy_ims.delay(use_current=True)
return Response('OK')
@routes.route("update-equipment-locations-ims", methods=['GET', 'POST'])
def update_equipment_locations_ims():
ims_worker.update_equipment_locations_ims.delay(use_current=True)
return Response('OK')
@routes.route("update-lg-routers-ims", methods=['GET', 'POST'])
def update_lg_routers_ims():
ims_worker.update_lg_routers_ims.delay(use_current=True)
return Response('OK')
# End of IMS routes
@routes.route("update-interfaces-to-services", methods=['GET', 'POST'])
def update_interfaces_to_services():
worker.update_interfaces_to_services.delay()
return Response('OK')
@routes.route("update-geant-lambdas", methods=['GET', 'POST'])
def update_geant_lambdas():
worker.update_geant_lambdas.delay()
return Response('OK')
@routes.route("update-fibre-spans", methods=['GET', 'POST'])
def update_fibre_spans():
worker.update_fibre_spans.delay()
return Response('OK')
@routes.route("update-service-hierarchy")
def update_service_hierarchy():
worker.update_circuit_hierarchy.delay()
return Response('OK')
@routes.route("update-equipment-locations", methods=['GET', 'POST'])
def update_equipment_location():
worker.update_equipment_locations.delay()
return Response('OK')
@routes.route("update-interface-statuses", methods=['GET', 'POST'])
def update_interface_statuses():
worker.update_interface_statuses.delay()
return Response('OK')
@routes.route("juniper-server-addresses", methods=['GET', 'POST'])
@common.require_accepts_json
def juniper_addresses():
# TODO: this route (and corant, infinera routes) can be removed
r = common.get_current_redis()
routers = r.get('netdash')
assert routers # sanity: value shouldn't be empty
routers = json.loads(routers.decode('utf-8'))
return jsonify(routers)
@routes.route("opsdb/interfaces")
def get_all_interface_details():
r = common.get_current_redis()
result = collections.defaultdict(list)
for k in r.keys('opsdb:interface_services:*'):
m = re.match(
r'^opsdb:interface_services:([^:]+):(.*)$',
k.decode('utf-8'))
assert m
result[m.group(1)].append(m.group(2))
return jsonify(result)
@routes.route("opsdb/interfaces/<equipment_name>")
def get_interface_details_for_equipment(equipment_name):
r = common.get_current_redis()
result = []
for k in r.keys('opsdb:interface_services:%s:*' % equipment_name):
m = re.match(
r'^opsdb:interface_services:%s:(.*)$' % equipment_name,
k.decode('utf-8'))
assert m
result.append(m.group(1))
return jsonify(result)
@routes.route("opsdb/interfaces/<equipment_name>/<path:interface>")
def get_interface_details(equipment_name, interface):
r = common.get_current_redis()
key = 'opsdb:interface_services:%s:%s' % (equipment_name, interface)
# TODO: handle None (return 404)
return jsonify(json.loads(r.get(key).decode('utf-8')))
@routes.route("opsdb/equipment-location")
def get_all_equipment_locations():
r = common.get_current_redis()
result = {}
for k in r.keys('opsdb:location:*'):
k = k.decode('utf-8')
m = re.match(r'^opsdb:location:(.*)$', k)
assert m
result[m.group(1)] = json.loads(r.get(k).decode('utf-8'))
return jsonify(result)
@routes.route("opsdb/equipment-location/<path:equipment_name>")
def get_equipment_location(equipment_name):
r = common.get_current_redis()
result = r.get('opsdb:location:' + equipment_name)
# TODO: handle None (return 404)
return jsonify(json.loads(result.decode('utf-8')))
@routes.route("opsdb/circuit-hierarchy/children/<int:parent_id>")
def get_children(parent_id):
r = common.get_current_redis()
result = r.get('opsdb:services:children:%d' % parent_id)
# TODO: handle None (return 404)
return jsonify(json.loads(result.decode('utf-8')))
@routes.route("opsdb/circuit-hierarchy/parents/<int:child_id>")
def get_parents(child_id):
r = common.get_current_redis()
result = r.get('opsdb:services:parents:%d' % child_id)
# TODO: handle None (return 404)
return jsonify(json.loads(result.decode('utf-8')))
@routes.route("bgp/<hostname>", methods=['GET', 'POST'])
@common.require_accepts_json
def bgp_configs(hostname):
r = common.get_current_redis()
netconf_string = r.get('netconf:' + hostname)
if not netconf_string:
return Response(
response="no available info for '%s'" % hostname,
status=404,
mimetype="text/html")
routes = list(juniper.all_bgp_peers(
etree.XML(netconf_string.decode('utf-8'))))
if not routes:
return Response(
response="no interfaces found for '%s'" % hostname,
status=404,
mimetype="text/html")
return jsonify(routes)
@routes.route("snmp/<hostname>", methods=['GET', 'POST'])
@common.require_accepts_json
def snmp_ids(hostname):
r = common.get_next_redis()
ifc_data_string = r.get('snmp-interfaces:' + hostname)
ifc_data = json.loads(ifc_data_string.decode('utf-8'))
return jsonify(ifc_data)
@routes.route("netconf/<hostname>", methods=['GET', 'POST'])
def get_netconf(hostname):
config = current_app.config["INVENTORY_PROVIDER_CONFIG"]
try:
netconf_doc = juniper.load_config(
hostname, config["ssh"], validate=False)
msg = etree.tostring(netconf_doc, encoding='unicode')
except (ConnectionError, juniper.NetconfHandlingError) as e:
msg = f'error loading netconf data from {hostname}\n{e}'
return msg
@routes.route("latchdb", methods=['GET', 'POST'])
def latch_db():
config = current_app.config["INVENTORY_PROVIDER_CONFIG"]
worker_common.latch_db(config)
r = worker_common.get_current_redis(config)
return jsonify(worker_common.get_latch(r))