diff --git a/inventory_provider/db/opsdb.py b/inventory_provider/db/opsdb.py index e569325fde730376ed61569216ca16ca24aec3c4..a53e40656a2b0acc0613a4d0cc42497340325899 100644 --- a/inventory_provider/db/opsdb.py +++ b/inventory_provider/db/opsdb.py @@ -1,31 +1,6 @@ from inventory_provider.db import db -equipment_location_query = """SELECT - e.absid, - e.name AS equipment_name, - p.name AS pop_name, - p.abbreviation AS pop_abbreviation, - p.site_id AS pop_site_id, - p.country, - g.longitude, - g.latitude - FROM - equipment e - INNER JOIN pop p - ON p.absid = e.PTR_pop - INNER JOIN geocoding g - ON g.absid = p.PTR_geocoding - WHERE - e.status != 'terminated' - AND e.status != 'disposed' - ORDER BY - FIELD(e.status, - 'spare', - 'planned', - 'ordered', - 'installed', - 'operational')""" geant_lambda_sql = """SELECT @@ -237,11 +212,44 @@ def get_circuit_hierarchy(connection): # pragma: no cover return r -def get_equipment_location_data(connection): # pragma: no cover +def get_pop_info(connection, equipment_name): + query = """ +SELECT + e.name AS equipment_name, + p.name AS pop_name, + p.abbreviation AS pop_abbreviation, + p.city AS pop_city, + p.country AS pop_country, + g.longitude, + g.latitude, + e.status +FROM equipment e +INNER JOIN pop p ON p.absid = e.PTR_pop +LEFT JOIN geocoding g ON g.absid = p.PTR_geocoding +WHERE (e.status = 'Installed' OR e.status = 'Operational') + AND e.name = %s +ORDER BY FIELD(e.status, 'Operational', 'Installed') +""" + + def _row2rsp(row): + return { + 'equipment-name': row['equipment_name'], + 'status': row['status'], + 'pop': { + 'name': row['pop_name'], + 'city': row['pop_city'], + 'country': row['pop_country'], + 'abbreviation': row['pop_abbreviation'], + 'longitude': row['longitude'], + 'latitude': row['latitude'] + } + } + with db.cursor(connection) as crs: - crs.execute(equipment_location_query) - r = _convert_to_dict(crs) - return r + crs.execute(query, [equipment_name]) + rows = _convert_to_dict(crs) + return list([_row2rsp(r) for r in rows]) + def get_coriant_path(connection, equipment_name, card_id, port_number): @@ -270,6 +278,10 @@ SELECT pop_b.city as pop_city_b, pop_a.country as pop_country_a, pop_b.country as pop_country_b, + g_a.longitude as pop_longitude_a, + g_a.latitude as pop_latitude_a, + g_b.longitude as pop_longitude_b, + g_b.latitude as pop_latitude_b, vcc.port_a, vcc.port_b @@ -280,9 +292,13 @@ SELECT LEFT JOIN equipment_card eqc_b ON eqc_b.absid = vcc.PTR_card_b LEFT JOIN pop pop_a ON pop_a.absid = vcc.PTR_pop_a LEFT JOIN pop pop_b ON pop_b.absid = vcc.PTR_pop_b - """ + LEFT JOIN geocoding g_a ON g_a.absid = pop_a.PTR_geocoding + LEFT JOIN geocoding g_b ON g_b.absid = pop_b.PTR_geocoding +""" def _fields2rsp(row): + + return { 'absid': row['absid'], 'category': row['category'], @@ -300,7 +316,9 @@ SELECT 'name': row['pop_name_a'], 'city': row['pop_city_a'], 'country': row['pop_country_a'], - 'abbreviation': row['pop_abbreviation_a'] + 'abbreviation': row['pop_abbreviation_a'], + 'longitude': row['pop_longitude_a'], + 'latitude': row['pop_latitude_a'] } }, 'b': { @@ -312,7 +330,9 @@ SELECT 'name': row['pop_name_b'], 'city': row['pop_city_b'], 'country': row['pop_country_b'], - 'abbreviation': row['pop_abbreviation_b'] + 'abbreviation': row['pop_abbreviation_b'], + 'longitude': row['pop_longitude_b'], + 'latitude': row['pop_latitude_b'] } } } diff --git a/test/test_opsdb_queries.py b/test/test_opsdb_queries.py index 19f18e5e1b4cd7b70f17b70895ea6d332f93002f..3c1cd39027a75b78eab5a714dd2693ad43ff761c 100644 --- a/test/test_opsdb_queries.py +++ b/test/test_opsdb_queries.py @@ -37,6 +37,52 @@ def connection(db_params): yield c +EQUIPMENT_LOCATION_METADATA = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + + "definitions": { + "pop-info": { + "type": "object", + "properties": { + "name": {"type": "string"}, + "abbreviation": {"type": "string"}, + "country": {"type": "string"}, + "city": {"type": "string"}, + "longitude": {"type": "number"}, + "latitude": {"type": "number"} + }, + "required": [ + "name", + "abbreviation", + "country", + "city", + "longitude", + "latitude" + ], + "additionalProperties": False + }, + "equipment-info": { + "type": "object", + "properties": { + 'equipment-name': {"type": "string"}, + 'status': {"type": "string"}, + 'pop': {"$ref": "#/definitions/pop-info"} + }, + "required": [ + "equipment-name", + "status", + "pop" + ], + "additionalProperties": False + + } + }, + + "type": "array", + "items": {"$ref": "#/definitions/equipment-info"} +} + CORIANT_PATH_METADATA = { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", @@ -49,9 +95,19 @@ CORIANT_PATH_METADATA = { "name": {"type": "string"}, "abbreviation": {"type": "string"}, "country": {"type": "string"}, - "city": {"type": "string"} + "city": {"type": "string"}, + "longitude": {"type": "number"}, + "latitude": {"type": "number"} }, - "required": ["absid", "name", "abbreviation", "country", "city"], + "required": [ + "absid", + "name", + "abbreviation", + "country", + "city", + "longitude", + "latitude" + ], "additionalProperties": False }, "endpoint": { @@ -92,12 +148,23 @@ CORIANT_PATH_METADATA = { } +@pytest.mark.parametrize('equipment', [ +'mx1.cbg.uk.geant.net', +'grv3.lon.uk.geant.net', +'mx1.gen.ch.geant.net' +]) +def test_equipment_location(connection, equipment): + circuit = opsdb.get_pop_info(connection, equipment) + jsonschema.validate(circuit, EQUIPMENT_LOCATION_METADATA) + assert len(circuit) == 1 + + @pytest.mark.parametrize('equipment,card,port', [ ('grv3.lon.uk.geant.net', '1-1', '3'), ('grv3.lon.uk.geant.net', '1-1', '5'), ('grv1.ams.nl.geant.net', '1-1', '1'), ('grv3.lon.uk.geant.net', '1-1', '1'), ]) -def test_query(connection, equipment, card, port): +def test_coriant_path(connection, equipment, card, port): circuit = opsdb.get_coriant_path(connection, equipment, card, port) jsonschema.validate(circuit, CORIANT_PATH_METADATA)