From c2599401a8fd0a8a565be1ee547a3982f6bc411a Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Mon, 24 Dec 2018 16:45:04 +0100
Subject: [PATCH] use netconf data for bgp route

---
 inventory_provider/routes/data.py |  27 +++----
 test/test_data_routes.py          | 129 ++++++++++++------------------
 2 files changed, 65 insertions(+), 91 deletions(-)

diff --git a/inventory_provider/routes/data.py b/inventory_provider/routes/data.py
index 9e27f2d2..766a48d3 100644
--- a/inventory_provider/routes/data.py
+++ b/inventory_provider/routes/data.py
@@ -141,23 +141,22 @@ def bgp_configs(hostname):
     r = redis.StrictRedis(
         host=redis_config["hostname"],
         port=redis_config["port"])
-    bgp_data_string = r.hget(hostname, 'bgp')
-    if not bgp_data_string:
+
+    netconf_string = r.hget(hostname, 'netconf')
+    if not netconf_string:
         return Response(
-            response="no available bgp info for '%s'" % hostname,
+            response="no available info for '%s'" % hostname,
+            status=404,
+            mimetype="text/html")
+
+    routes = list(netconf.list_bgp_routes(
+        etree.XML(netconf_string.decode('utf-8'))))
+    if not routes:
+        return Response(
+            response="no interfaces found for '%s'" % hostname,
             status=404,
             mimetype="text/html")
 
-    def _interfaces(s):
-        for ifc in json.loads(s):
-            yield {
-                "description": ifc["description"][0]["data"],
-                "as": {
-                    "peer": ifc["peer-as"][0]["data"],
-                    "local": ifc["local-as"][0]["as-number"][0]["data"]
-                }
-            }
-    interfaces = list(_interfaces(bgp_data_string.decode('utf-8')))
     return Response(
-        json.dumps(interfaces),
+        json.dumps(routes),
         mimetype="application/json")
diff --git a/test/test_data_routes.py b/test/test_data_routes.py
index 508134a8..bc575f1f 100644
--- a/test/test_data_routes.py
+++ b/test/test_data_routes.py
@@ -85,27 +85,6 @@ def client_with_mocked_data(mocker, client):
     return client
 
 
-# def _routers(client):
-#     routers_list_schema = {
-#         "$schema": "http://json-schema.org/draft-07/schema#",
-#         "type": "array",
-#         "items": {"type": "string"}
-#     }
-#
-#     rv = client.post(
-#         "data/routers",
-#         headers=DEFAULT_REQUEST_HEADERS)
-#     assert rv.status_code == 200
-#
-#     response = json.loads(rv.data.decode("utf-8"))
-#     jsonschema.validate(response, routers_list_schema)
-#     return response
-#
-#
-# def test_routers_list(client_with_mocked_data):
-#     assert _routers(client_with_mocked_data)
-
-
 def test_router_interfaces(router, client_with_mocked_data):
 
     interfaces_list_schema = {
@@ -156,62 +135,58 @@ def test_snmp_ids(router, client_with_mocked_data):
     assert response  # at least shouldn't be empty
 
 
-# def test_router_bgp_route(client_with_mocked_data):
-#
-#     bgp_list_schema = {
-#         "$schema": "http://json-schema.org/draft-07/schema#",
-#         "type": "array",
-#         "items": {
-#             "type": "object",
-#             "properties": {
-#                 "description": {"type": "string"},
-#                 "as": {
-#                     "type": "object",
-#                     "properties": {
-#                         "peer": {
-#                             "type": "string",
-#                             "pattern": r'^\d+$'
-#                         },
-#                         "local": {
-#                             "type": "string",
-#                             "pattern": r'^\d+$'
-#                         },
-#                     },
-#                     "required": ["peer", "local"],
-#                     "additionalProperties": False
-#                 },
-#             },
-#             "required": ["description", "as"],
-#             "additionalProperties": False
-#         }
-#     }
-#
-#     routers_with_bpg_configs = [
-#         "mx1.mil2.it.geant.net",
-#         "mx1.vie.at.geant.net",
-#         "mx1.fra.de.geant.net",
-#         "mx1.ams.nl.geant.net",
-#         "mx1.pra.cz.geant.net",
-#         "mx1.dub.ie.geant.net",
-#         "mx1.mad.es.geant.net",
-#         "mx1.gen.ch.geant.net",
-#         "mx1.mar.fr.geant.net",
-#         "mx1.lon.uk.geant.net"
-#     ]
-#
-#     for router in _routers(client_with_mocked_data):
-#
-#         if router not in routers_with_bpg_configs:
-#             continue
-#
-#         rv = client_with_mocked_data.post(
-#             "/data/bgp/" + router,
-#             headers=DEFAULT_REQUEST_HEADERS)
-#
-#         response = json.loads(rv.data.decode("utf-8"))
-#         jsonschema.validate(response, bgp_list_schema)
-#         assert response  # at least shouldn't be empty
-#
+def test_router_bgp_routes(router, client_with_mocked_data):
+
+    ROUTERS_WITH_BGP_CONFIG = [
+        "mx1.bud.hu.geant.net",
+        "mx1.pra.cz.geant.net",
+        "mx1.lon.uk.geant.net",
+        "mx1.vie.at.geant.net",
+        "mx1.ams.nl.geant.net",
+        "mx1.fra.de.geant.net",
+        "mx1.gen.ch.geant.net",
+        "mx1.mil2.it.geant.net",
+        "mx1.mad.es.geant.net",
+        "mx1.dub.ie.geant.net",
+        "mx1.mar.fr.geant.net"
+    ]
+
+    if router not in ROUTERS_WITH_BGP_CONFIG:
+        pytest.skip('%s is not expected to have bgp peers' % router)
+        return
+
+    bgp_list_schema = {
+        "$schema": "http://json-schema.org/draft-07/schema#",
+        "type": "array",
+        "items": {
+            "type": "object",
+            "properties": {
+                "name": {"type": "string"},
+                "description": {"type": "string"},
+                "as": {
+                    "type": "object",
+                    "properties": {
+                        "peer": {"type": "integer"},
+                        "local": {"type": "integer"}
+                    },
+                    "required": ["peer", "local"],
+                    "additionalProperties": False
+                },
+            },
+            "required": ["description", "as", "name"],
+            "additionalProperties": False
+        }
+    }
+
+    rv = client_with_mocked_data.post(
+        "/data/bgp/" + router,
+        headers=DEFAULT_REQUEST_HEADERS)
+
+    assert rv.status_code == 200
+    response = json.loads(rv.data.decode("utf-8"))
+    jsonschema.validate(response, bgp_list_schema)
+    assert response  # at least shouldn't be empty
+
 #
 # def test_router_debug_data_route(client_with_mocked_data):
 #     debug_data_schema = {
-- 
GitLab