diff --git a/inventory_provider/routes/msr.py b/inventory_provider/routes/msr.py index 9e128ba4c07f3e83c29afdbc4c7df93f7a33533c..cead3b83685f663c2010aa9fa5798edd0ae9c636 100644 --- a/inventory_provider/routes/msr.py +++ b/inventory_provider/routes/msr.py @@ -94,38 +94,32 @@ def access_services(): .. asjson:: inventory_provider.routes.msr.ACCESS_SERVICES_LIST_SCHEMA - :param name: :return: """ - # todo - replace with IMS implementation - return Response( - response='no access services found', - status=404, - mimetype="text/html") - # redis = common.get_current_redis() - # - # def _services(): - # for k in redis.scan_iter('opsdb:access_services:*'): - # service = redis.get(k.decode('utf-8')).decode('utf-8') - # yield json.loads(service) - # - # cache_key = 'classifier-cache:msr:access-services' - # result = redis.get(cache_key) - # - # if result: - # result = json.loads(result.decode('utf-8')) - # else: - # result = list(_services()) - # # cache this data for the next call - # redis.set(cache_key, json.dumps(result).encode('utf-8')) - # - # if not result: - # return Response( - # response='no access services found', - # status=404, - # mimetype="text/html") - # - # return jsonify(result) + redis = common.get_current_redis() + + def _services(): + for k in redis.scan_iter('ims:access_services:*'): + service = redis.get(k.decode('utf-8')).decode('utf-8') + yield json.loads(service) + + cache_key = 'classifier-cache:msr:access-services' + result = redis.get(cache_key) + + if result: + result = json.loads(result.decode('utf-8')) + else: + result = list(_services()) + # cache this data for the next call + redis.set(cache_key, json.dumps(result).encode('utf-8')) + + if not result: + return Response( + response='no access services found', + status=404, + mimetype="text/html") + + return jsonify(result) def _handle_peering_group_request(name, cache_key, group_key_base): diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py index 02f93249d9a24176e15613c80e6a7ecae1278ac5..c87da9388843d1842190874b9c14e39c585eb0b5 100644 --- a/inventory_provider/tasks/worker.py +++ b/inventory_provider/tasks/worker.py @@ -408,7 +408,7 @@ def launch_refresh_cache_all(config): monitor.clear_joblog(get_current_redis(config)) - # first batch of subtasks: refresh cached IMS data + # first batch of subtasks: refresh cached IMS location data subtasks = [ update_neteng_managed_device_list.apply_async(), update_equipment_locations.apply_async(), @@ -431,13 +431,10 @@ def launch_refresh_cache_all(config): @log_task_entry_and_exit def internal_refresh_phase_2(self): # second batch of subtasks: - # alarms db status cache - # juniper netconf & snmp data + # ims circuit information try: subtasks = [ - # update_circuit_hierarchy.apply_async(), - # update_interfaces_to_services.apply_async() update_circuit_hierarchy_and_port_id_services.apply_async() ] @@ -475,12 +472,11 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False): locations = {k: v for k, v in ims_data.get_node_locations(ds1)} tls_names = list(ims_data.get_service_types(ds1)) - # logger.debug(json.dumps(locations['TS1.GEN.CH'], indent=2)) - # return hierarchy = None port_id_details = defaultdict(list) port_id_services = defaultdict(list) interface_services = defaultdict(list) + access_services = {} def _convert_to_bits(value, unit): unit = unit.lower() @@ -494,6 +490,8 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False): def _get_speed(circuit_id): c = hierarchy[circuit_id] + if c['status'] != 'operational': + return 0 pattern = re.compile(r'^(\d+)([a-zA-z]+)$') m = pattern.match(c['speed']) if m: @@ -522,7 +520,6 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False): for x in ims_data.get_port_details(ds2): pd = port_id_details[x['port_id']] pd.append(x) - # port_id_details = list(ims_data.get_port_details(ds2)) logger.debug("Port details complete") def _populate_circuit_info(): @@ -634,6 +631,9 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False): circ['calculated-speed'] = _get_speed(circ['id']) _format_service(circ) + if circ['service_type'].lower() == 'geant ip': + access_services[circ['id']] = circ + interface_services[k].extend(circuits) if use_current: @@ -643,10 +643,16 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False): rp = r.pipeline() for k in r.scan_iter('ims:circuit_hierarchy:*', count=1000): rp.delete(k) + rp.execute() + rp = r.pipeline() for k in r.scan_iter('ims:interface_services:*', count=1000): rp.delete(k) rp.execute() rp = r.pipeline() + for k in r.scan_iter('ims:access_services:*', count=1000): + rp.delete(k) + rp.execute() + rp = r.pipeline() for circ in hierarchy.values(): rp.set(f'ims:circuit_hierarchy:{circ["id"]}', json.dumps([circ])) rp.execute() @@ -656,6 +662,22 @@ def update_circuit_hierarchy_and_port_id_services(self, use_current=False): f'ims:interface_services:{k}', json.dumps(v)) rp.execute() + rp = r.pipeline() + + for v in access_services.values(): + rp.set( + f'ims:access_services:{v["name"]}', + json.dumps({ + 'id': v['id'], + 'name': v['name'], + 'pop_name': v['pop_name'], + 'other_end_pop_name': v['other_end_pop_name'], + 'equipment': v['equipment'], + 'other_end_equipment': v['other_end_equipment'], + 'speed_value': v['calculated-speed'], + 'speed_unit': 'n/a' + })) + rp.execute() @app.task(base=InventoryTask, bind=True, name='update_equipment_locations') diff --git a/test/data/router-info.json b/test/data/router-info.json index e3bed7251440eab498899defb87d58b686c1073a..d8bbbe465c13622414e10a0614b0e74c2c70ba70 100644 Binary files a/test/data/router-info.json and b/test/data/router-info.json differ diff --git a/test/test_msr_routes.py b/test/test_msr_routes.py index d328b847d0e54ca44e6e37b62e5ea443bdb51da5..403321060059c7e5895a388ba8317081f86362db 100644 --- a/test/test_msr_routes.py +++ b/test/test_msr_routes.py @@ -4,7 +4,7 @@ import jsonschema import pytest from inventory_provider.routes.msr import PEERING_LIST_SCHEMA, \ - PEERING_GROUP_LIST_SCHEMA + PEERING_GROUP_LIST_SCHEMA, ACCESS_SERVICES_LIST_SCHEMA DEFAULT_REQUEST_HEADERS = { "Content-type": "application/json", @@ -19,13 +19,12 @@ def test_access_services(client): rv = client.get( '/msr/access-services', headers=DEFAULT_REQUEST_HEADERS) - assert rv.status_code == 404 - # assert rv.status_code == 200 - # assert rv.is_json - # response_data = json.loads(rv.data.decode('utf-8')) - # jsonschema.validate(response_data, ACCESS_SERVICES_LIST_SCHEMA) - # - # assert response_data # test data is non-empty + assert rv.status_code == 200 + assert rv.is_json + response_data = json.loads(rv.data.decode('utf-8')) + jsonschema.validate(response_data, ACCESS_SERVICES_LIST_SCHEMA) + + assert response_data # test data is non-empty def test_logical_system_peerings_all(client):