From 8efa55cc5b9483377deff7561503c55a4b7b1a3b Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Wed, 16 Jan 2019 13:54:04 +0000
Subject: [PATCH] Modified the build_network_graph API endpoint to read the
 sr-cluster mappings from a file - temporary solution

---
 src/service/clmcservice/__init__.py                |  6 +-----
 src/service/clmcservice/graphapi/views.py          | 14 ++++++++++++--
 src/service/development.ini                        |  2 +-
 src/service/production.ini                         |  2 +-
 .../resources/GraphAPI/network_clusters.json       |  6 ++++++
 5 files changed, 21 insertions(+), 9 deletions(-)
 create mode 100644 src/service/resources/GraphAPI/network_clusters.json

diff --git a/src/service/clmcservice/__init__.py b/src/service/clmcservice/__init__.py
index c35e2ec..07f8d88 100644
--- a/src/service/clmcservice/__init__.py
+++ b/src/service/clmcservice/__init__.py
@@ -24,7 +24,6 @@
 
 
 # Python standard libs
-from json import load
 from os.path import dirname, abspath
 import os
 
@@ -54,10 +53,7 @@ def main(global_config, **settings):
     settings['influx_port'] = int(settings['influx_port'])  # the influx port setting must be converted to integer instead of a string
     settings['kapacitor_port'] = int(settings['kapacitor_port'])  # the kapacitor port setting must be converted to integer instead of a string
 
-    network_config_file_path = settings["network_configuration_path"]
-    with open(network_config_file_path) as f:
-        network = load(f)
-        settings["network_bandwidth"] = network["bandwidth"]
+    settings["network_bandwidth"] = 104857600  # TODO currently assumed fixed bandwidth across all links
 
     config = Configurator(settings=settings)
 
diff --git a/src/service/clmcservice/graphapi/views.py b/src/service/clmcservice/graphapi/views.py
index c3441af..ca9e3a0 100644
--- a/src/service/clmcservice/graphapi/views.py
+++ b/src/service/clmcservice/graphapi/views.py
@@ -25,12 +25,13 @@
 
 from clmcservice.graphapi.utilities import validate_json_queries_body, validate_graph_url_params, \
     build_network_graph, delete_network_graph, build_temporal_subgraph, delete_temporal_subgraph, validate_graph_rtt_params, RTT_CYPHER_QUERY_TEMPLATE
-from uuid import uuid4
 from influxdb import InfluxDBClient
 from py2neo import Graph
 from pyramid.httpexceptions import HTTPBadRequest, HTTPNotFound, HTTPServiceUnavailable, HTTPNotImplemented
 from pyramid.view import view_defaults, view_config
 from requests import exceptions, get
+from uuid import uuid4
+from json import load
 import logging
 
 
@@ -271,7 +272,16 @@ class GraphAPI(object):
             log.error("Unexpected error: {0}".format(msg))
             raise HTTPNotImplemented("The SDN controller failed to return a valid JSON response when querying for the network topology.")
 
-        clusters = {}  # TODO this mapping should be retrieved somehow
+        # TODO this is a temporary solution - currently the service router to clusters mapping is read from a file (which must be manually prepared beforehand)
+        clusters_file = self.request.registry.settings["network_configuration_path"]
+        try:
+            with open(clusters_file) as fh:
+                clusters = load(fh)
+        except Exception as e:
+            log.error("Unexpected error: {0}".format(e))
+            log.error("No service_router-to-cluster mapping was found while building the network topology.")
+            clusters = {}
+
         # build the network graph and retrieve the number of switch nodes and cluster nodes that were created
         switch_count, clusters_count = build_network_graph(graph, switches, links, clusters)
 
diff --git a/src/service/development.ini b/src/service/development.ini
index cbc930b..ea65782 100644
--- a/src/service/development.ini
+++ b/src/service/development.ini
@@ -15,7 +15,7 @@ pyramid.includes = pyramid_debugtoolbar pyramid_exclog
 exclog.ignore =
 
 
-network_configuration_path = /vagrant/src/service/resources/GraphAPI/network_config.json
+network_configuration_path = /vagrant/src/service/resources/GraphAPI/network_clusters.json
 
 # PostgreSQL connection url
 sqlalchemy.url = postgresql://clmc:clmc_service@localhost:5432/whoamidb
diff --git a/src/service/production.ini b/src/service/production.ini
index 4d96131..083bb91 100644
--- a/src/service/production.ini
+++ b/src/service/production.ini
@@ -15,7 +15,7 @@ pyramid.includes = pyramid_exclog
 exclog.ignore =
 
 
-network_configuration_path = /opt/clmc/src/service/resources/GraphAPI/network_config.json
+network_configuration_path = /opt/clmc/src/service/resources/GraphAPI/network_clusters.json
 
 # PostgreSQL connection url
 sqlalchemy.url = postgresql://clmc:clmc_service@localhost:5432/whoamidb
diff --git a/src/service/resources/GraphAPI/network_clusters.json b/src/service/resources/GraphAPI/network_clusters.json
new file mode 100644
index 0000000..40fe262
--- /dev/null
+++ b/src/service/resources/GraphAPI/network_clusters.json
@@ -0,0 +1,6 @@
+{
+  "172.20.231.11": "20-sr1-cluster1-cluster",
+  "172.20.231.18": "22-sr1-cluster1-cluster",
+  "172.20.231.17": "23-sr1-cluster1-cluster",
+  "172.20.231.2": "24-sr1-cluster1-cluster"
+}
\ No newline at end of file
-- 
GitLab