From d3f24620a0d660a54909e174ea99e54196063cd9 Mon Sep 17 00:00:00 2001
From: Bjarke Madsen <bjarke.madsen@geant.org>
Date: Wed, 17 Aug 2022 16:51:39 +0200
Subject: [PATCH] Refactor mapping of ports to SIDs a bit

---
 inventory_provider/db/ims_data.py | 59 ++++++++++++++++++++-----------
 1 file changed, 38 insertions(+), 21 deletions(-)

diff --git a/inventory_provider/db/ims_data.py b/inventory_provider/db/ims_data.py
index d8e214d8..66583ae0 100644
--- a/inventory_provider/db/ims_data.py
+++ b/inventory_provider/db/ims_data.py
@@ -129,9 +129,9 @@ def get_circuit_related_customers(ds: IMS):
 
     return_value = defaultdict(list)
     for ccr in ds.get_filtered_entities(
-                'CircuitCustomerRelation',
-                'circuit.inventoryStatusId== 3',
-                ims.CIRCUIT_CUSTOMER_RELATION['Customer']):
+        'CircuitCustomerRelation',
+        'circuit.inventoryStatusId== 3',
+            ims.CIRCUIT_CUSTOMER_RELATION['Customer']):
         return_value[ccr['circuitid']].append(
             {
                 'id': ccr['customer']['id'],
@@ -273,6 +273,32 @@ def get_port_id_services(ds: IMS):
             }
 
 
+def get_port_sids(ds: IMS):
+    """
+    This function fetches SIDs for external ports that have them defined,
+
+    :param ds: IMS datasource object
+    :returns: Dict mapping external port IDs to the SID assigned to the port
+    """
+    return {
+        p['objectid']: p['value'] for p in ds.get_filtered_entities(
+            'ExtraFieldValue', 'extrafieldid == 3249 | value <> ""',
+            step_count=10000)}
+
+
+def get_internal_port_sids(ds: IMS):
+    """
+    This function fetches SIDs for external ports that have them defined,
+
+    :param ds: IMS datasource object
+    :returns: Dict mapping internal port IDs to the SID assigned to the port
+    """
+    return {
+        p['objectid']: p['value'] for p in ds.get_filtered_entities(
+            'ExtraFieldValue', 'extrafieldid == 3250 | value <> ""',
+            step_count=10000)}
+
+
 def get_port_details(ds: IMS):
     port_nav_props = [
         ims.PORT_PROPERTIES['Node'],
@@ -283,18 +309,15 @@ def get_port_details(ds: IMS):
         ims.PORT_PROPERTIES['Shelf']
     }
 
-    _port_sids = {
-        p['objectid']: p['value'] for p in ds.get_filtered_entities(
-            'ExtraFieldValue', 'extrafieldid == 3249 | value <> ""',
-            step_count=10000)}
-
-    _internal_port_sids = {
-        p['objectid']: p['value'] for p in ds.get_filtered_entities(
-            'ExtraFieldValue', 'extrafieldid == 3250 | value <> ""',
-            step_count=10000)}
-
     # this is here instead of chaining to make debugging easier
     def _process_ports(ports, p_type):
+        _port_sids = {}
+
+        if p_type == 'external':
+            _port_sids = get_port_sids(ds)
+        else:
+            _port_sids = get_internal_port_sids(ds)
+
         for p in ports:
             vendor = None
             interface_name = None
@@ -320,14 +343,8 @@ def get_port_details(ds: IMS):
                 'interface_name': interface_name
             }
 
-            _port_sid = None
-            if p_type == 'internal' and p['id'] in _internal_port_sids:
-                _port_sid = _internal_port_sids[p['id']]
-            elif p_type == 'external' and p['id'] in _port_sids:
-                _port_sid = _port_sids.get(p['id'])
-
-            if _port_sid is not None:
-                data['sid'] = _port_sid
+            if p['id'] in _port_sids:
+                data['sid'] = _port_sids[p['id']]
 
             yield data
 
-- 
GitLab