diff --git a/inventory_provider/routes/poller.py b/inventory_provider/routes/poller.py index 5a5863604dc44650bea40262d5be1bb2a4360226..bc84ad21a5d9ba08783d855d0fcae7c637a057a7 100644 --- a/inventory_provider/routes/poller.py +++ b/inventory_provider/routes/poller.py @@ -157,42 +157,25 @@ GWS_DIRECT_DATA_SCHEMA = { 'type': 'string', 'pattern': r'^(\d+\.)*\d+$' }, - 'counters': { + 'counter': { 'type': 'object', 'properties': { - 'discards_in': {'$ref': '#/definitions/oid'}, - 'discards_out': {'$ref': '#/definitions/oid'}, - 'errors_in': {'$ref': '#/definitions/oid'}, - 'errors_out': {'$ref': '#/definitions/oid'}, - 'traffic_in': {'$ref': '#/definitions/oid'}, - 'traffic_out': {'$ref': '#/definitions/oid'}, - }, - 'additionalProperties': False - }, - 'interface': { - 'type': 'object', - 'properties': { - 'tag': {'type': 'string'}, - 'counters': {'$ref': '#/definitions/counters'} - }, - 'required': ['tag', 'counters'], - 'additionalProperties': False - }, - 'host': { - 'type': 'object', - 'properties': { - 'hostname': {'type': 'string'}, - 'community': {'type': 'string'}, - 'interfaces': { - 'type': 'array', - 'items': {'$ref': '#/definitions/interface'}, - 'minItems': 1 - } + 'field': { + 'enum': [ + 'discards_in', + 'discards_out', + 'errors_in', + 'errors_out', + 'traffic_in', + 'traffic_out' + ] + }, + 'oid': {'$ref': '#/definitions/oid'} }, - 'required': ['hostname', 'community', 'interfaces'], + 'required': ['field', 'oid'], 'additionalProperties': False }, - 'nren-isp': { + 'interface-counters': { 'type': 'object', 'properties': { 'nren': {'type': 'string'}, @@ -200,19 +183,23 @@ GWS_DIRECT_DATA_SCHEMA = { 'type': 'string', 'enum': ['Cogent', 'Telia', 'Century Link'] }, - 'hosts': { + 'hostname': {'type': 'string'}, + 'community': {'type': 'string'}, + 'tag': {'type': 'string'}, + 'counters': { 'type': 'array', - 'items': {'$ref': '#/definitions/host'}, + 'items': {'$ref': '#/definitions/counter'}, 'minItems': 1 } }, - 'required': ['nren', 'isp', 'hosts'], + 'required': [ + 'nren', 'isp', 'hostname', 'community', 'tag', 'counters'], 'additionalProperties': False } }, 'type': 'array', - 'items': {'$ref': '#/definitions/nren-isp'} + 'items': {'$ref': '#/definitions/interface-counters'} } @@ -640,5 +627,36 @@ def gws_direct(): :return: """ - config_params = current_app.config['INVENTORY_PROVIDER_CONFIG'] - return jsonify(config_params['gws-direct']) + + cache_key = f'classifier-cache:gws-direct' + r = common.get_current_redis() + + result = r.get(cache_key) + if result: + result = result.decode('utf-8') + else: + + def _interfaces(): + config_params = current_app.config['INVENTORY_PROVIDER_CONFIG'] + for nren_isp in config_params['gws-direct']: + for host in nren_isp['hosts']: + for ifc in host['interfaces']: + + + yield { + 'nren': nren_isp['nren'], + 'isp': nren_isp['isp'], + 'hostname': host['hostname'], + 'community': host['community'], + 'tag': ifc['tag'], + 'counters': [ + {'field': k, 'oid': v} + for k, v in ifc['counters'].items()] + } + + result = json.dumps(list(_interfaces())) + + # cache this data for the next call + r.set(cache_key, result.encode('utf-8')) + + return Response(result, mimetype="application/json") diff --git a/test/test_general_poller_routes.py b/test/test_general_poller_routes.py index 73b6d81c0389a2cc8997b7810ee4b620e3fe08ae..7a7807c6053c9522a6fdaba7c2b176a0f5c51f2c 100644 --- a/test/test_general_poller_routes.py +++ b/test/test_general_poller_routes.py @@ -60,6 +60,5 @@ def test_gws_direct(client): assert rv.status_code == 200 assert rv.is_json response_data = json.loads(rv.data.decode('utf-8')) - jsonschema.validate( - response_data, poller.GWS_DIRECT_DATA_SCHEMA) + jsonschema.validate(response_data, poller.GWS_DIRECT_DATA_SCHEMA) assert response_data, "the subscription list shouldn't be empty"