Skip to content
Snippets Groups Projects
classifier.py 2.87 KiB
import json

from flask import Blueprint, Response, current_app, jsonify

from inventory_provider.routes import common

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


@routes.route("/infinera-dna-addresses", methods=['GET', 'POST'])
@common.require_accepts_json
def infinera_addresses():
    infinera_config = current_app.config[
        "INVENTORY_PROVIDER_CONFIG"]["infinera-dna"]
    return jsonify([dna['address'] for dna in infinera_config])


@routes.route("/coriant-tnms-addresses", methods=['GET', 'POST'])
@common.require_accepts_json
def coriant_addresses():
    coriant_config = current_app.config[
        "INVENTORY_PROVIDER_CONFIG"]["coriant-tnms"]
    return jsonify([tnms['address'] for tnms in coriant_config])


@routes.route("/juniper-server-addresses", methods=['GET', 'POST'])
@common.require_accepts_json
def juniper_addresses():
    # TODO: definitive way of returning these addresses
    # TODO: probably this (and corant, infinera routes) can be removed
    routers = current_app.config[
        "INVENTORY_PROVIDER_CONFIG"]["routers"]
    return jsonify([r['hostname'] for r in routers])


@routes.route("/trap-metadata/<source_equipment>/<path:interface>",
              methods=['GET', 'POST'])
@common.require_accepts_json
def get_trap_metadata(source_equipment, interface):
    r = common.get_redis()

    cache_key = 'classifier:cache:%s:%s' % (source_equipment, interface)
    result = r.get(cache_key)

    if result:
        result = result.decode('utf-8')
    else:
        # todo - Change this to a call to the yet-to-be-created one source of all
        #        relevant information
        # This could be different calls dependant on vendor, in which case we may
        # need to check the trap type, or it could be a case of a key check for
        # each possible data source
        interface_info = r.get(
            'opsdb:interface_services:%s:%s' % (source_equipment, interface))

        if not interface_info:
            return Response(
                response="no available info for {} {}".format(
                    source_equipment, interface),
                status=404,
                mimetype="text/html")

        interface_info = json.loads(interface_info.decode('utf-8'))

        # todo - refactor once structure of new source is decided, currently this
        #        is just a list of services
        vendor = interface_info[0]['manufacturer']
        hostname = interface_info[0]['equipment']
        # TODO: don't copy unecessary data into response (let client do this)
        result = {
            "vendor": vendor,
            "equipment-name": hostname,
            "interface-name": interface,
            "services": interface_info
        }
        result = json.dumps(result)
        # cache this data for the next call
        r.set(cache_key, result.encode('utf-8'))

    return Response(result, mimetype="application/json")