From 679c63201691d4fceb2a56e50332f8a8fc05f562 Mon Sep 17 00:00:00 2001
From: Robert Latta <robert.latta@geant.org>
Date: Fri, 6 Sep 2019 09:48:37 +0000
Subject: [PATCH] Added other-end info for Infinera; Updated test to check
 other-end info

---
 inventory_provider/db/opsdb.py | 19 +++++++++++++++++++
 test/test_opsdb.py             | 20 +++++++++++++++++---
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/inventory_provider/db/opsdb.py b/inventory_provider/db/opsdb.py
index ee0aabc8..a9d4d6ea 100644
--- a/inventory_provider/db/opsdb.py
+++ b/inventory_provider/db/opsdb.py
@@ -165,6 +165,25 @@ def _infinera_field_update(record):
         record["interface_name"] += "-" + record["port"]
     record["interface_name"] = record["interface_name"] \
         .replace("--", "-").upper()
+
+    #
+
+    equipment_parts = record["other_end_equipment"].rsplit("-", 1)
+    card_parts = record["other_end_card_id"].split("-", 1)
+    record["other_end_interface_name"] = ""
+    record["other_end_equipment"] = equipment_parts[0]
+    try:
+        record["other_end_interface_name"] = equipment_parts[1] + "-"
+    except IndexError:
+        pass  # Nothing to see here
+    try:
+        record["other_end_interface_name"] += card_parts[1]
+    except IndexError:
+        record["other_end_interface_name"] += card_parts[0]
+    if record["other_end_port"] is not None and record["other_end_port"] != "":
+        record["other_end_interface_name"] += "-" + record["other_end_port"]
+    record["other_end_interface_name"] = record["other_end_interface_name"] \
+        .replace("--", "-").upper()
     return record
 
 
diff --git a/test/test_opsdb.py b/test/test_opsdb.py
index cf46fd99..f86eb672 100644
--- a/test/test_opsdb.py
+++ b/test/test_opsdb.py
@@ -25,28 +25,42 @@ def test_infinera_field_update():
     i = {
         "equipment": "AMS01-DTNX10-1-1",
         "card_id": "tim-b-5-7",
-        "port": "1"
+        "port": "1",
+        "other_end_equipment": "LON01-DTNX10-1-1",
+        "other_end_card_id": "tim-b-5-8",
+        "other_end_port": "2"
          }
     r = inventory_provider.db.opsdb._infinera_field_update(i)
     assert r["equipment"] == "AMS01-DTNX10-1"
     assert r["interface_name"] == "1-B-5-7-1"
+    assert r["other_end_equipment"] == "LON01-DTNX10-1"
+    assert r["other_end_interface_name"] == "1-B-5-8-2"
 
     i = {
         "equipment": "BUD01_CX_01",
         "card_id": "tim-1/2",
-        "port": "1"
+        "port": "1",
+        "other_end_equipment": "LON01_CX_01",
+        "other_end_card_id": "tim-2/3",
+        "other_end_port": "4"
          }
     r = inventory_provider.db.opsdb._infinera_field_update(i)
     assert r["equipment"] == "BUD01_CX_01"
     assert r["interface_name"] == "1/2-1"
+    assert r["other_end_equipment"] == "LON01_CX_01"
+    assert r["other_end_interface_name"] == "2/3-4"
 
     i = {
         "equipment": "irrelevant",
         "card_id": "tim_1/2",
-        "port": "1"
+        "port": "1",
+        "other_end_equipment": "irrelevant",
+        "other_end_card_id": "tim_2/3",
+        "other_end_port": "4"
          }
     r = inventory_provider.db.opsdb._infinera_field_update(i)
     assert r["interface_name"] == "TIM_1/2-1"
+    assert r["other_end_interface_name"] == "TIM_2/3-4"
 
 
 def test_juniper_field_update():
-- 
GitLab