From f224e8f539a45421cf9a754bb2f9a1903daf67fa Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Thu, 18 Jul 2019 15:09:26 +0200
Subject: [PATCH] reverse subnet db calculation

---
 inventory_provider/tasks/worker.py | 37 +++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py
index 3c4ffff2..ff507dda 100644
--- a/inventory_provider/tasks/worker.py
+++ b/inventory_provider/tasks/worker.py
@@ -464,9 +464,10 @@ def launch_refresh_cache_all(config):
     subtasks = [
         update_equipment_locations.apply_async(),
     ]
-    for hostname in _derive_router_hostnames(config):
-        logger.debug(
-            'queueing router refresh jobs for %r' % hostname)
+    # for hostname in _derive_router_hostnames(config):
+    hostnames = _derive_router_hostnames(config)
+    for hostname in hostnames[0:2]:
+        logger.debug('queueing router refresh jobs for %r' % hostname)
         subtasks.append(reload_router_config.apply_async(args=[hostname]))
 
     pending_task_ids = [x.id for x in subtasks]
@@ -511,6 +512,36 @@ def refresh_finalizer(self, pending_task_ids_json):
     task_ids = json.loads(pending_task_ids_json)
     assert jsonschema.validate(task_ids, input_schema)
     _wait_for_tasks(task_ids)
+    _build_subnet_db()
+
+    logger.debug('<<< refresh_finalizer')
+
+
+def _build_subnet_db():
+
+    r = get_next_redis(InventoryTask.config)
+
+    subnets = {}
+    for k in r.scan_iter('reverse_interface_addresses:*'):
+        info = r.get(k.decode('utf-8')).decode('utf-8')
+        info = json.loads(info)
+        entry = subnets.setdefault('subnet', [])
+        entry.append({
+            'interface name': info['interface name'],
+            'router': info['router']
+        })
+
+    for k, v in subnets.items():
+        r.set('subnets:' + k, json.dumps(v))
+
+
+def refresh_interface_address_lookups(hostname, netconf):
+    _refresh_peers(
+        hostname,
+        'reverse_interface_addresses',
+        juniper.interface_addresses(netconf))
+
+
 
 
 def check_task_status(task_id):
-- 
GitLab