Skip to content
Snippets Groups Projects
Commit 9c382d79 authored by Robert Latta's avatar Robert Latta
Browse files

Removed redundant routes and related files e.g. storage/*

parent 647cfcc2
Branches
Tags
No related merge requests found
......@@ -26,9 +26,6 @@ def create_app():
from inventory_provider.routes import opsdb
app.register_blueprint(opsdb.routes, url_prefix='/opsdb')
from inventory_provider.routes import alarmsdb
app.register_blueprint(alarmsdb.routes, url_prefix='/alarmsdb')
from inventory_provider.routes import classifier
app.register_blueprint(classifier.routes, url_prefix='/classifier')
......
import functools
import json
from flask import Blueprint, request, Response, current_app
from inventory_provider import alarmsdb, db
routes = Blueprint("inventory-alarmsdb-query-routes", __name__)
def require_accepts_json(f):
"""
used as a route handler decorator to return an error
unless the request allows responses with type "application/json"
:param f: the function to be decorated
:return: the decorated function
"""
@functools.wraps(f)
def decorated_function(*args, **kwargs):
# TODO: use best_match to disallow */* ...?
if not request.accept_mimetypes.accept_json:
return Response(
response="response will be json",
status=406,
mimetype="text/html")
return f(*args, **kwargs)
return decorated_function
@routes.route("/interface-status", methods=['GET', 'POST'])
@require_accepts_json
def get_interface_status():
config = current_app.config['INVENTORY_PROVIDER_CONFIG']
equipment = request.args.get("equipment")
interface = request.args.get("interface")
with db.connection(config['alarms-db']) as connection:
result = {"status": alarmsdb.get_last_known_interface_status(
connection, equipment, interface)}
return Response(
json.dumps(result),
mimetype="application/json")
......@@ -7,7 +7,6 @@ from lxml import etree
import redis
from inventory_provider import db, juniper
from inventory_provider.storage import external_inventory
routes = Blueprint("inventory-data-query-routes", __name__)
......@@ -170,7 +169,7 @@ def bgp_configs(hostname):
@require_accepts_json
def interface_statuses(hostname, interface):
r = db.get_redis()
result = r.hget(external_inventory.interface_status_key,
result = r.hget("interface_statuses",
"{}::{}".format(hostname, interface))
if not result:
return Response(
......
import json
from collections import defaultdict
from flask import current_app
from inventory_provider import alarmsdb, db
services_key = "inv_services"
interfaces_key = "inv_interfaces"
equipment_locations_key = "inv_eq_locations"
service_child_to_parents_key = "inv_service_child_to_parents"
service_parent_to_children_key = "inv_service_parent_to_children"
interface_status_key = "service_status"
def update_services_to_monitor(services):
r = db.get_redis()
relevant_types = ("path", "service", "l2circuit")
r.delete(services_key)
for service in services:
if service["circuit_type"].lower() in relevant_types:
r.hset(services_key, service["id"], json.dumps(service))
def update_interfaces_to_services(services):
r = db.get_redis()
config = current_app.config["INVENTORY_PROVIDER_CONFIG"]
with db.connection(config["alarms-db"]) as cnx:
with db.cursor(cnx) as csr:
mapped_interfaces = defaultdict(list)
r.delete(interfaces_key)
for service in services:
key = "{}::{}".format(
service["equipment"],
service["interface_name"]
)
mapped_interfaces[key].append(service)
if not r.hexists(interface_status_key, key):
r.hset(interface_status_key, key,
alarmsdb.get_last_known_interface_status(
csr,
service["equipment"],
service["interface_name"]
))
for key, value in mapped_interfaces.items():
r.hset(interfaces_key, key, json.dumps(value))
def update_service_hierarchy(records):
r = db.get_redis()
children_to_parents = defaultdict(list)
parents_to_children = defaultdict(list)
for relation in records:
parent_id = relation["parent_circuit_id"]
child_id = relation["child_circuit_id"]
parents_to_children[parent_id].append(relation)
children_to_parents[child_id].append(relation)
r.delete(service_child_to_parents_key)
for child, parents in children_to_parents.items():
r.hset(service_child_to_parents_key, child, json.dumps(parents))
r.delete(service_parent_to_children_key)
for parent, children in parents_to_children.items():
r.hset(service_parent_to_children_key, parent, json.dumps(children))
def update_equipment_locations(equipment_location_data):
r = db.get_redis()
r.delete(equipment_locations_key)
for ld in equipment_location_data:
r.hset(equipment_locations_key, ld["equipment_name"], json.dumps(ld))
from inventory_provider.storage import external_inventory
def test_update_services_to_monitor(mocker):
mocked_redis = mocker.patch(
"inventory_provider.storage.external_inventory.db.get_redis")
mocked_hset = mocked_redis.return_value.hset
services = [
{"circuit_type": "path", "id": "test_id_0", "other": "stuff"},
{"circuit_type": "service", "id": "test_id_1", "other": "stuff"},
{"circuit_type": "l2circuit", "id": "test_id_2", "other": "stuff"},
{"circuit_type": "spam", "id": "test_id_4", "other": "stuff"}
]
external_inventory.update_services_to_monitor(services)
mocked_hset.assert_any_call(
external_inventory.services_key, "test_id_0",
"{\"circuit_type\": \"path\","
" \"id\": \"test_id_0\","
" \"other\": \"stuff\"}")
mocked_hset.assert_any_call(
external_inventory.services_key, "test_id_1",
"{\"circuit_type\": \"service\","
" \"id\": \"test_id_1\","
" \"other\": \"stuff\"}")
mocked_hset.assert_any_call(
external_inventory.services_key, "test_id_2",
"{\"circuit_type\": \"l2circuit\","
" \"id\": \"test_id_2\","
" \"other\": \"stuff\"}")
assert mocked_hset.call_count == 3
def test_update_interfaces_to_services(mocker):
mocked_redis = mocker.patch(
"inventory_provider.storage.external_inventory.db.get_redis")
mocker.patch(
"inventory_provider.storage.external_inventory.current_app")
mocker.patch(
"inventory_provider.storage.external_inventory.db.connection")
mocker.patch(
"inventory_provider.storage.external_inventory.db.cursor")
mocked_hset = mocked_redis.return_value.hset
services = [
{"equipment": "eq_0", "interface_name": "if_0"},
{"equipment": "eq_1", "interface_name": "if_1"},
{"equipment": "eq_2", "interface_name": "if_2"},
{"equipment": "eq_3", "interface_name": "if_3"},
{"equipment": "eq_3", "interface_name": "if_3", "extra": "stuff"},
{"equipment": "eq_4", "interface_name": "if_4"}
]
unique_keys = set(s["equipment"] + "::" + s["interface_name"]
for s in services)
external_inventory.update_interfaces_to_services(services)
assert mocked_hset.call_count == len(unique_keys)
mocked_hset.assert_any_call(external_inventory.interfaces_key,
"eq_2::if_2",
"[{\"equipment\": \"eq_2\","
" \"interface_name\": \"if_2\"}]")
mocked_hset.assert_any_call(external_inventory.interfaces_key,
"eq_3::if_3",
"[{\"equipment\": \"eq_3\","
" \"interface_name\": \"if_3\"},"
" {\"equipment\": \"eq_3\","
" \"interface_name\": \"if_3\","
" \"extra\": \"stuff\"}]")
def test_update_service_hierarchy(mocker):
mocked_redis = mocker.patch(
"inventory_provider.storage.external_inventory.db.get_redis")
mocked_hset = mocked_redis.return_value.hset
data = [
{"parent_circuit": "PC_1",
"parent_circuit_id": "1001",
"parent_circuit_status": "operational",
"child_circuit": "CC_1",
"child_circuit_id": "2001",
"segment_group": 1},
{"parent_circuit": "PC_2",
"parent_circuit_id": "1002",
"parent_circuit_status": "operational",
"child_circuit": "CC_1",
"child_circuit_id": "2001",
"segment_group": 1},
{"parent_circuit": "PC_3",
"parent_circuit_id": "1003",
"parent_circuit_status": "operational",
"child_circuit": "CC_2",
"child_circuit_id": "2002",
"segment_group": 1},
{"parent_circuit": "PC_3",
"parent_circuit_id": "1003",
"parent_circuit_status": "operational",
"child_circuit": "CC_3",
"child_circuit_id": "2003",
"segment_group": 1}
]
external_inventory.update_service_hierarchy(data)
u_parent_keys = set([d["parent_circuit_id"] for d in data])
u_child_keys = set([d["child_circuit_id"] for d in data])
assert mocked_hset.call_count == len(u_parent_keys) + len(u_child_keys)
mocked_hset.assert_any_call(
external_inventory.service_child_to_parents_key,
"2001",
"[{\"parent_circuit\": \"PC_1\","
" \"parent_circuit_id\": \"1001\","
" \"parent_circuit_status\": \"operational\","
" \"child_circuit\": \"CC_1\","
" \"child_circuit_id\": \"2001\","
" \"segment_group\": 1},"
" {\"parent_circuit\": \"PC_2\","
" \"parent_circuit_id\": \"1002\","
" \"parent_circuit_status\": \"operational\","
" \"child_circuit\": \"CC_1\","
" \"child_circuit_id\": \"2001\","
" \"segment_group\": 1}]"
)
mocked_hset.assert_any_call(
external_inventory.service_child_to_parents_key,
"2002",
"[{\"parent_circuit\": \"PC_3\","
" \"parent_circuit_id\": \"1003\","
" \"parent_circuit_status\": \"operational\","
" \"child_circuit\": \"CC_2\","
" \"child_circuit_id\": \"2002\","
" \"segment_group\": 1}]"
)
mocked_hset.assert_any_call(
external_inventory.service_parent_to_children_key,
"1003",
"[{\"parent_circuit\": \"PC_3\","
" \"parent_circuit_id\": \"1003\","
" \"parent_circuit_status\": \"operational\","
" \"child_circuit\": \"CC_2\","
" \"child_circuit_id\": \"2002\","
" \"segment_group\": 1},"
" {\"parent_circuit\": \"PC_3\","
" \"parent_circuit_id\": \"1003\","
" \"parent_circuit_status\": \"operational\","
" \"child_circuit\": \"CC_3\","
" \"child_circuit_id\": \"2003\","
" \"segment_group\": 1}]"
)
mocked_hset.assert_any_call(
external_inventory.service_parent_to_children_key,
"1002",
"[{\"parent_circuit\": \"PC_2\","
" \"parent_circuit_id\": \"1002\","
" \"parent_circuit_status\": \"operational\","
" \"child_circuit\": \"CC_1\","
" \"child_circuit_id\": \"2001\","
" \"segment_group\": 1}]"
)
import json
import jsonschema
DEFAULT_REQUEST_HEADERS = {
"Content-type": "application/json",
"Accept": ["application/json"]
}
def test_get_interface_status(mocker, client):
mocked_conn = mocker.patch('inventory_provider.routes.alarmsdb'
'.db.connection')
mocked_conn.return_value.__enter__.return_value = None
mocked_inteface_status = mocker.patch(
'inventory_provider.routes.alarmsdb.'
'alarmsdb.get_last_known_interface_status')
mocked_inteface_status.return_value = "up"
rv = client.get(
'/alarmsdb/interface-status?'
'equipment=mx1.lon.uk.geant.net&interface=xe-1/2/2',
headers=DEFAULT_REQUEST_HEADERS)
interfaces_list_schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"status": {
"type": "string",
}
}
}
response = json.loads(rv.data.decode("utf-8"))
jsonschema.validate(response, interfaces_list_schema)
assert response == {"status": "up"}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment