import json import re from flask import Blueprint, jsonify, Response from lxml import etree from inventory_provider import juniper from inventory_provider.routes import common routes = Blueprint("inventory-data-query-routes", __name__) @routes.route("/routers", methods=['GET', 'POST']) @common.require_accepts_json def routers(): r = common.get_redis() result = [] for k in r.keys('netconf:*'): m = re.match('^netconf:(.+)$', k.decode('utf-8')) assert m result.append(m.group(1)) return jsonify(result) @routes.route("/interfaces/<hostname>", methods=['GET', 'POST']) @common.require_accepts_json def router_interfaces(hostname): r = common.get_redis() interfaces = [] for k in r.keys('netconf-interfaces:%s:*' % hostname): ifc = r.get(k.decode('utf-8')) if ifc: interfaces.append(json.loads(ifc.decode('utf-8'))) if not interfaces: return Response( response="no available interface info for '%s'" % hostname, status=404, mimetype="text/html") return jsonify(interfaces) @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) @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("/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')))