Skip to content
Snippets Groups Projects
Commit 585842da authored by Erik Reid's avatar Erik Reid
Browse files

moved some parsing into coriant classifier handler

parent c6c54f42
No related branches found
No related tags found
No related merge requests found
import ipaddress import ipaddress
import json import json
import logging
import re import re
from flask import Blueprint, Response, current_app from flask import Blueprint, Response, current_app
...@@ -9,6 +10,8 @@ from inventory_provider.db import opsdb, db ...@@ -9,6 +10,8 @@ from inventory_provider.db import opsdb, db
routes = Blueprint("inventory-data-classifier-support-routes", __name__) routes = Blueprint("inventory-data-classifier-support-routes", __name__)
logger = logging.getLogger(__name__)
class ClassifierRequestError(Exception): class ClassifierRequestError(Exception):
status_code = 500 status_code = 500
...@@ -280,37 +283,46 @@ def get_trap_metadata(source_equipment, interface, circuit_id): ...@@ -280,37 +283,46 @@ def get_trap_metadata(source_equipment, interface, circuit_id):
return Response(result, mimetype="application/json") return Response(result, mimetype="application/json")
@routes.route("/coriant-info/" @routes.route('/coriant-info/<equipment_name>/<path:entity_string>',
"<equipment_name>/<card_id>/<port_number>",
methods=['GET', 'POST']) methods=['GET', 'POST'])
@common.require_accepts_json @common.require_accepts_json
def get_coriant_info(equipment_name, card_id, port_number): def get_coriant_info(equipment_name, entity_string):
r = common.get_redis() r = common.get_redis()
cache_key = 'classifier-cache:coriant:%s:%s:%s' % ( cache_key = 'classifier-cache:coriant:%s:%s' % (
equipment_name, card_id, port_number) equipment_name, entity_string)
result = r.get(cache_key) result = r.get(cache_key)
if result: if result:
result = result.decode('utf-8') result = result.decode('utf-8')
else: else:
m = re.match(r'^(\d+\-\d+)\.(\d+)', entity_string)
if not m:
logger.warning(
'invalid coriant entity string format: %r' % entity_string)
return Response(
response="no available info for '{}' '{}'".format(
equipment_name, entity_string),
status=404,
mimetype="text/html")
result = {
'equipment name': equipment_name,
'card id': m.group(1),
'port number': m.group(2)
}
config = current_app.config['INVENTORY_PROVIDER_CONFIG'] config = current_app.config['INVENTORY_PROVIDER_CONFIG']
with db.connection(config['ops-db']) as cx: with db.connection(config['ops-db']) as cx:
path = opsdb.lookup_coriant_path( path = opsdb.lookup_coriant_path(
cx, equipment_name, card_id, port_number) cx, equipment_name, result['card id'], result['port number'])
if not path: if path:
return Response( result['path'] = path
response="no available info for {} {} {}".format(
equipment_name, card_id, port_number),
status=404,
mimetype="text/html")
result = json.dumps({'path': path})
# cache this data for the next call # cache this data for the next call
r.set(cache_key, result.encode('utf-8')) result = json.dumps(result).encode('utf-8')
r.set(cache_key, result)
return Response(result, mimetype="application/json") return Response(result, mimetype="application/json")
...@@ -278,16 +278,14 @@ def test_coriant_info(client, mocker): ...@@ -278,16 +278,14 @@ def test_coriant_info(client, mocker):
""" """
just check the correct method is called, but mock out all sql access just check the correct method is called, but mock out all sql access
""" """
expected_path = { CONNECTION = 'bogus connection'
'C': 'bogus connection', EQUIPMENT = 'bogus equipment name'
'E': 'bogus equipment name', CARD_ID = '123-456'
'CID': 'bogus card id', PORT_NUMBER = '789'
'P': 'bogus card number'
}
@contextlib.contextmanager @contextlib.contextmanager
def mocked_connection(ignored): def mocked_connection(ignored):
yield expected_path['C'] yield CONNECTION
mocker.patch( mocker.patch(
'inventory_provider.db.db.connection', mocked_connection) 'inventory_provider.db.db.connection', mocked_connection)
...@@ -296,14 +294,28 @@ def test_coriant_info(client, mocker): ...@@ -296,14 +294,28 @@ def test_coriant_info(client, mocker):
lambda a, b, c, d: {'C': a, 'E': b, 'CID': c, 'P': d}) lambda a, b, c, d: {'C': a, 'E': b, 'CID': c, 'P': d})
rv = client.get( rv = client.get(
'/classifier/coriant-info/{E}/{CID}/{P}'.format(**expected_path), '/classifier/coriant-info/{eq}/{cid}.{pn}abc.234 af/23'.format(
eq=EQUIPMENT,
cid=CARD_ID,
pn=PORT_NUMBER),
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
assert rv.is_json assert rv.is_json
response_data = json.loads(rv.data.decode('utf-8')) response_data = json.loads(rv.data.decode('utf-8'))
expected_response = {'path': expected_path} expected_response = {
'equipment name': EQUIPMENT,
'card id': CARD_ID,
'port number': PORT_NUMBER,
'path': {
'C': CONNECTION,
'E': EQUIPMENT,
'CID': CARD_ID,
'P': PORT_NUMBER
}
}
assert response_data == expected_response assert response_data == expected_response
...@@ -323,7 +335,7 @@ def test_coriant_info_not_found(client, mocker): ...@@ -323,7 +335,7 @@ def test_coriant_info_not_found(client, mocker):
lambda a, b, c, d: None) lambda a, b, c, d: None)
rv = client.get( rv = client.get(
'/classifier/coriant-info/aaa/bbb/ccc', '/classifier/coriant-info/aaa/unparseableentitystring',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 404 assert rv.status_code == 404
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment