Skip to content
Snippets Groups Projects
infinera_classifier.py 1.93 KiB
import json

from inventory_provider.routes import common

from flask import Blueprint, Response


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


@routes.errorhandler(500)
def handle_request_error(error):
    return Response(
       response=str(error),
       status=500)


@routes.errorhandler(422)
def handle_request_error(error):
    return Response(
       response=str(error),
       status=422)


@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:infinera:cache:%s:%s' % (source_equipment, interface)
    result = r.get(cache_key)

    if result:
        result = result.decode('utf-8')
    else:
        result = {}

        services = r.get(
            'opsdb:interface_services:%s:%s' % (source_equipment, interface))
        if services:
            result['services'] = json.loads(services.decode('utf=8'))
            geant_lambdas = []
            for s in result['services']:
                gl = r.get('opsdb:geant_lambdas:%s' % s['name'])
                if gl:
                    geant_lambdas.append(json.loads(gl.decode('utf=8')))

            if geant_lambdas:
                result['geant_lambdas'] = geant_lambdas

        ifc_info = r.get(
            'netconf-interfaces:%s:%s' % (source_equipment, interface))
        if ifc_info:
            result['interface'] = json.loads(ifc_info.decode('utf-8'))

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

        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")