Skip to content
Snippets Groups Projects
testing.py 6.46 KiB
Newer Older
import collections
import json
import re

from flask import Blueprint, Response, current_app, jsonify
from lxml import etree

from inventory_provider import juniper
Erik Reid's avatar
Erik Reid committed
from inventory_provider.routes import common
Robert Latta's avatar
Robert Latta committed
from inventory_provider.tasks import worker
Erik Reid's avatar
Erik Reid committed

routes = Blueprint("inventory-data-testing-support-routes", __name__)


@routes.route("flushdb", methods=['GET', 'POST'])
Erik Reid's avatar
Erik Reid committed
def flushdb():
    common.get_redis().flushdb()
    return Response('OK')
Robert Latta's avatar
Robert Latta committed
@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-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_redis()
    routers = []
    for k in r.keys('junosspace:*'):
        info = r.get(k.decode('utf-8'))
        assert info  # sanity: value shouldn't be empty
        info = json.loads(info.decode('utf-8'))
        routers.append(info['address'])
    return jsonify(routers)


@routes.route("opsdb/interfaces")
def get_all_interface_details():
    r = common.get_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_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_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_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_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_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_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_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.list_bgp_routes(
        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_redis()
    ifc_data_string = r.get('snmp-interfaces:' + hostname)
    if not ifc_data_string:
        return Response(
            response="no available info for '%s'" % hostname,
            status=404,
            mimetype="text/html")

    def _ifc_name(ifc):
        if 'v4InterfaceName' in ifc:
            return ifc['v4InterfaceName']
        if 'v6InterfaceName' in ifc:
            return ifc['v6InterfaceName']
        assert False, "sanity failure: no interface name found"

    ifc_data = json.loads(ifc_data_string.decode('utf-8'))
    result = [
        {'index': i['index'], 'name': _ifc_name(i)}
        for i in ifc_data]
    return jsonify(result)


# TODO: remove all alarmsdb i/o, and then remove the following
# @routes.route("/interfaces/status/<hostname>/<path:interface>",
#               methods=['GET', 'POST'])
# @common.require_accepts_json
# def interface_statuses(hostname, interface):
#     r = common.get_redis()
#     status = r.get(
#         'alarmsdb:interface_status:%s:%s'
#         % (hostname, interface))
#     if not status:
#         return Response(
#             response="no available info for {} {}".format(
#               hostname, interface),
#             status=404,
#             mimetype="text/html")
#     return jsonify({"status": status.decode('utf-8')})
#
#
# @routes.route("/services/<hostname>/<path:interface>",
#               methods=['GET', 'POST'])
# @common.require_accepts_json
# def services_for_interface(hostname, interface):
#     r = common.get_redis()
#     services = r.get(
#         'opsdb:interface_services:%s:%s'
#         % (hostname, interface))
#     if not services:
#         return Response(
#             response="no available info for {} {}".format(
#               hostname, interface),
#             status=404,
#             mimetype="text/html")
#     return jsonify(json.loads(services.decode('utf-8')))