From 1f9f6b6820405b8ed7e9f113368c1757c9e84933 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Wed, 10 Apr 2019 11:48:48 +0200
Subject: [PATCH] added coriant path lookup

---
 inventory_provider/db/opsdb.py | 131 +++++++++++++++++++++++++++++++++
 1 file changed, 131 insertions(+)

diff --git a/inventory_provider/db/opsdb.py b/inventory_provider/db/opsdb.py
index f9d9cf43..de3237f2 100644
--- a/inventory_provider/db/opsdb.py
+++ b/inventory_provider/db/opsdb.py
@@ -164,6 +164,17 @@ retrieve_services_query = """SELECT *
                       'installed',
                       'operational')"""
 
+"""
+SELECT eq_a.name, eq_b.name, eqc_a.card_id, eqc_b.card_id, vcc.port_a, vcc.port_b FROM vcircuitconns vcc
+    LEFT JOIN equipment eq_a ON eq_a.absid = vcc.PTR_equip_a
+    LEFT JOIN equipment eq_b ON eq_b.absid = vcc.PTR_equip_b
+    LEFT JOIN equipment_card eqc_a ON eqc_a.absid = vcc.PTR_card_a
+    LEFT JOIN equipment_card eqc_b ON eqc_b.absid = vcc.PTR_card_b
+WHERE vcc.status = 'Operational'
+"""
+
+
+
 
 def _convert_to_dict(crs):
     return [dict((crs.description[i][0], "" if value is None else value)
@@ -242,3 +253,123 @@ def get_equipment_location_data(connection):  # pragma: no cover
         crs.execute(equipment_location_query)
         r = _convert_to_dict(crs)
     return r
+
+
+def get_coriant_path(connection, equipment_name, card_id, port_number):
+
+    circuit_query = """
+SELECT
+
+    vcc.absid,
+    vcc.category,
+    vcc.circuit_type,
+    vcc.service_type,
+    vcc.peering_type,
+    eq_a.name as equipment_name_a,
+    eq_b.name as equipment_name_b,
+    eqc_a.card_id as card_id_a,
+    eqc_a.card_id as card_id_b,
+    pop_a.name as pop_name_a,
+    pop_b.name as pop_name_b
+
+ FROM vcircuitconns vcc
+    LEFT JOIN equipment eq_a ON eq_a.absid = vcc.PTR_equip_a
+    LEFT JOIN equipment eq_b ON eq_b.absid = vcc.PTR_equip_b
+    LEFT JOIN equipment_card eqc_a ON eqc_a.absid = vcc.PTR_card_a
+    LEFT JOIN equipment_card eqc_b ON eqc_b.absid = vcc.PTR_card_b
+    LEFT JOIN pop pop_a ON pop_a.absid = vcc.PTR_pop_a
+    LEFT JOIN pop pop_b ON pop_b.absid = vcc.PTR_pop_b
+
+WHERE vcc.status = 'Operational'
+    AND (
+        (eq_a.name = %(equipment_name)s
+            AND eqc_a.card_id = %(card_id)s
+            AND vcc.port_a = %(port_number)s )
+        OR
+        (eq_b.name = %(equipment_name)s
+            AND eqc_b.card_id = %(card_id)s
+            AND vcc.port_b = %(port_number)s ))
+
+ORDER BY FIELD(vcc.circuit_type, 'path') DESC
+    """
+
+    parent_query = """
+SELECT
+
+    parent.absid,
+    parent.category,
+    parent.circuit_type,
+    parent.service_type,
+    parent.peering_type,
+    eq_a.name as equipment_name_a,
+    eq_b.name as equipment_name_b,
+    eqc_a.card_id as card_id_a,
+    eqc_a.card_id as card_id_b,
+    pop_a.name as pop_name_a,
+    pop_b.name as pop_name_b
+
+ FROM vcircuitconns parent
+    LEFT JOIN equipment eq_a ON eq_a.absid = parent.PTR_equip_a
+    LEFT JOIN equipment eq_b ON eq_b.absid = parent.PTR_equip_b
+    LEFT JOIN equipment_card eqc_a ON eqc_a.absid = parent.PTR_card_a
+    LEFT JOIN equipment_card eqc_b ON eqc_b.absid = parent.PTR_card_b
+    LEFT JOIN pop pop_a ON pop_a.absid = parent.PTR_pop_a
+    LEFT JOIN pop pop_b ON pop_b.absid = parent.PTR_pop_b
+    JOIN circuit_glue
+        ON circuit_glue.PTR_circuit = parent.absid
+
+WHERE circuit_glue.PTR_component = %s
+AND circuit_type = 'Path'
+    """
+
+    #     for_testing = """
+    # SELECT parent.absid, child.absid,
+    #        parent.circuit_type as parent_circuit_type,
+    #        child.circuit_type as child_circuit_type,
+    #        eq_a.name as equipment_name_a,
+    #        eq_b.name as equipment_name_b,
+    #        eqc_a.card_id as card_id_a,
+    #        eqc_a.card_id as card_id_b,
+    #        child.port_a as port_a,
+    #        child.port_b as port_b,
+    #        pop_a.name as pop_name_a,
+    #        pop_b.name as pop_name_b
+    #
+    # FROM vcircuitconns parent
+    #     RIGHT JOIN circuit_glue
+    #         ON circuit_glue.PTR_circuit = parent.absid
+    #     JOIN vcircuitconns child
+    #         ON circuit_glue.PTR_component = child.absid
+    #
+    # LEFT JOIN equipment eq_a ON eq_a.absid = child.PTR_equip_a
+    # LEFT JOIN equipment eq_b ON eq_b.absid = child.PTR_equip_b
+    # LEFT JOIN equipment_card eqc_a ON eqc_a.absid = child.PTR_card_a
+    # LEFT JOIN equipment_card eqc_b ON eqc_b.absid = child.PTR_card_b
+    # LEFT JOIN pop pop_a ON pop_a.absid = child.PTR_pop_a
+    # LEFT JOIN pop pop_b ON pop_b.absid = child.PTR_pop_b
+    #
+    # WHERE parent.circuit_type = 'Path'
+    # AND child.circuit_type <> 'Path'
+    # AND child.status = 'Operational'
+    # AND (eq_a.name like 'grv%' or eq_b.name like 'grv%')
+    #     """
+
+    args = {
+        'equipment_name': equipment_name,
+        'card_id': card_id,
+        'port_number': port_number
+    }
+
+    with db.cursor(connection) as crs:
+        crs.execute(circuit_query, args)
+        r = _convert_to_dict(crs)
+        if not r:
+            return None
+        circuit = r[0]
+        if circuit['circuit_type'].lower() == 'path':
+            return circuit
+
+        crs.execute(parent_query, [circuit['absid']])
+        r = _convert_to_dict(crs)
+        print("PARENT")
+        return r[0] if r else None
-- 
GitLab