From 8f7bbaf973a9d3649d6d0ac72adadd8dd62987b4 Mon Sep 17 00:00:00 2001
From: Lluis Gifre <lluis.gifre@cttc.es>
Date: Tue, 27 Jul 2021 18:22:20 +0200
Subject: [PATCH] Move database to common folder, since it will be used also by
 device service, and implement very minor changes.

---
 src/{context => common}/database/__init__.py  |   0
 .../database/inmemory/InMemoryDatabase.py     |   3 +-
 .../database/inmemory/__init__.py             |   0
 .../database/redis/RedisDatabase.py           |   4 +-
 .../database/redis/__init__.py                |   0
 src/context/data/topo_nsfnet.json             | 136 ------------------
 src/context/database/Factory.py               |  31 ----
 src/context/service/ContextService.py         |   2 +-
 .../service/ContextServiceServicerImpl.py     |   3 +-
 src/context/service/__main__.py               |   2 +-
 10 files changed, 8 insertions(+), 173 deletions(-)
 rename src/{context => common}/database/__init__.py (100%)
 rename src/{context => common}/database/inmemory/InMemoryDatabase.py (87%)
 rename src/{context => common}/database/inmemory/__init__.py (100%)
 rename src/{context => common}/database/redis/RedisDatabase.py (97%)
 rename src/{context => common}/database/redis/__init__.py (100%)
 delete mode 100644 src/context/data/topo_nsfnet.json
 delete mode 100644 src/context/database/Factory.py

diff --git a/src/context/database/__init__.py b/src/common/database/__init__.py
similarity index 100%
rename from src/context/database/__init__.py
rename to src/common/database/__init__.py
diff --git a/src/context/database/inmemory/InMemoryDatabase.py b/src/common/database/inmemory/InMemoryDatabase.py
similarity index 87%
rename from src/context/database/inmemory/InMemoryDatabase.py
rename to src/common/database/inmemory/InMemoryDatabase.py
index 13ef3e876..59a2274a9 100644
--- a/src/context/database/inmemory/InMemoryDatabase.py
+++ b/src/common/database/inmemory/InMemoryDatabase.py
@@ -1,7 +1,8 @@
 import logging, json
 
 LOGGER = logging.getLogger(__name__)
-FILEPATH = 'context/data/topo_nsfnet.json'
+
+FILEPATH = 'data/topo_nsfnet.json'
 
 class InMemoryDatabase:
     def __init__(self, filepath=FILEPATH, **parameters):
diff --git a/src/context/database/inmemory/__init__.py b/src/common/database/inmemory/__init__.py
similarity index 100%
rename from src/context/database/inmemory/__init__.py
rename to src/common/database/inmemory/__init__.py
diff --git a/src/context/database/redis/RedisDatabase.py b/src/common/database/redis/RedisDatabase.py
similarity index 97%
rename from src/context/database/redis/RedisDatabase.py
rename to src/common/database/redis/RedisDatabase.py
index 883e1f880..38b0bf34b 100644
--- a/src/context/database/redis/RedisDatabase.py
+++ b/src/common/database/redis/RedisDatabase.py
@@ -10,6 +10,8 @@ MAX_RETRIES = 15
 
 URL_TEMPLATE = 'redis://{host}:{port}/{dbid}'
 
+FILEPATH = 'data/topo_nsfnet.json'
+
 class RedisDatabase:
     def __init__(self, **parameters):
         host = os.environ.get('REDISDB_SERVICE_HOST')
@@ -32,7 +34,7 @@ class RedisDatabase:
     def get_topology(self):
         str_topology = self.handler.get('topology')
         if(str_topology is None):
-            with open('context/data/topo_nsfnet.json', 'r') as f:
+            with open(FILEPATH, 'r') as f:
                 json_topology = json.loads(f.read())
             str_topology = json.dumps(json_topology)
             self.handler.setnx('topology', str_topology)
diff --git a/src/context/database/redis/__init__.py b/src/common/database/redis/__init__.py
similarity index 100%
rename from src/context/database/redis/__init__.py
rename to src/common/database/redis/__init__.py
diff --git a/src/context/data/topo_nsfnet.json b/src/context/data/topo_nsfnet.json
deleted file mode 100644
index f2cbb53a8..000000000
--- a/src/context/data/topo_nsfnet.json
+++ /dev/null
@@ -1,136 +0,0 @@
-{
-    "topoId": {
-        "contextId": {"contextUuid": {"uuid": "default"}},
-        "topoId": {"uuid": "topo0-nsfnet"}
-    },
-    "device" : [
-        {"device_id": {"device_id": {"uuid": "1" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "1" }}, "port_id": {"uuid" : "101"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "2" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "2" }}, "port_id": {"uuid" : "201"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "3" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "3" }}, "port_id": {"uuid" : "301"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "4" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "4" }}, "port_id": {"uuid" : "401"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "5" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "5" }}, "port_id": {"uuid" : "501"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "6" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "6" }}, "port_id": {"uuid" : "601"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "7" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "7" }}, "port_id": {"uuid" : "701"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "8" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "8" }}, "port_id": {"uuid" : "801"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "9" }}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "9" }}, "port_id": {"uuid" : "901"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "10"}}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "10"}}, "port_id": {"uuid" : "1001"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "11"}}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "11"}}, "port_id": {"uuid" : "1101"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "12"}}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "12"}}, "port_id": {"uuid" : "1201"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "13"}}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "13"}}, "port_id": {"uuid" : "1301"}}, "port_type": "LINE"}
-        ]},
-        {"device_id": {"device_id": {"uuid": "14"}}, "device_type": "ROADM", "device_config": {"device_config": ""}, "devOperationalStatus": 1, "endpointList" : [
-            {"port_id": {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "14"}}, "port_id": {"uuid" : "1401"}}, "port_type": "LINE"}
-        ]} 
-    ],
-    "link" : [
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "1" }}, "port_id": {"uuid": "101" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "2" }}, "port_id": {"uuid": "201" }}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "1" }}, "port_id": {"uuid": "101" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "3" }}, "port_id": {"uuid": "301" }}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "1" }}, "port_id": {"uuid": "101" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "8" }}, "port_id": {"uuid": "801" }}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "2" }}, "port_id": {"uuid": "201" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "3" }}, "port_id": {"uuid": "301" }}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "2" }}, "port_id": {"uuid": "201" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "4" }}, "port_id": {"uuid": "401" }}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "3" }}, "port_id": {"uuid": "301" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "6" }}, "port_id": {"uuid": "601" }}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "4" }}, "port_id": {"uuid": "401" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "5" }}, "port_id": {"uuid": "501" }}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "4" }}, "port_id": {"uuid": "401" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "11"}}, "port_id": {"uuid": "1101"}}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "5" }}, "port_id": {"uuid": "501" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "6" }}, "port_id": {"uuid": "601" }}
-        ]},         
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "5" }}, "port_id": {"uuid": "501" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "7" }}, "port_id": {"uuid": "701" }}
-        ]},                                                          
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "6" }}, "port_id": {"uuid": "601" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "10"}}, "port_id": {"uuid": "1001"}}
-        ]},                                                  
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "6" }}, "port_id": {"uuid": "601" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "13"}}, "port_id": {"uuid": "1301"}}
-        ]},                                                  
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "7" }}, "port_id": {"uuid": "701" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "8" }}, "port_id": {"uuid": "801" }}
-        ]},                                                  
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "8" }}, "port_id": {"uuid": "801" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "9" }}, "port_id": {"uuid": "901" }}
-        ]},                                                  
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "9" }}, "port_id": {"uuid": "901" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "10"}}, "port_id": {"uuid": "1001"}}
-        ]}, 
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "9" }}, "port_id": {"uuid": "901" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "12"}}, "port_id": {"uuid": "1201"}}
-        ]},   
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "9" }}, "port_id": {"uuid": "901" }},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "14"}}, "port_id": {"uuid": "1401"}}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "11"}}, "port_id": {"uuid": "1101"}},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "12"}}, "port_id": {"uuid": "1201"}}
-        ]},   
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "11"}}, "port_id": {"uuid": "1101"}},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "14"}}, "port_id": {"uuid": "1401"}}
-        ]},     
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "12"}}, "port_id": {"uuid": "1201"}},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "13"}}, "port_id": {"uuid": "1301"}}
-        ]},
-        {"endpointList" : [
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "13"}}, "port_id": {"uuid": "1301"}},
-            {"topoId": {"topoId": {"uuid": "topo0-nsfnet"}}, "dev_id": {"device_id": {"uuid": "14"}}, "port_id": {"uuid": "1401"}}
-        ]}      
-    ]
-}
diff --git a/src/context/database/Factory.py b/src/context/database/Factory.py
deleted file mode 100644
index 9edd9ac25..000000000
--- a/src/context/database/Factory.py
+++ /dev/null
@@ -1,31 +0,0 @@
-import logging, os
-from enum import Enum
-from .inmemory.InMemoryDatabase import InMemoryDatabase
-from .redis.RedisDatabase import RedisDatabase
-
-LOGGER = logging.getLogger(__name__)
-
-class DatabaseEngineEnum(Enum):
-    INMEMORY = 'inmemory'
-    REDIS = 'redis'
-    #MONGO = 'mongo'
-    #RETHINK = 'rethink'
-    #ETCD = 'etcd'
-
-ENGINES = {
-    DatabaseEngineEnum.INMEMORY.value: InMemoryDatabase,
-    DatabaseEngineEnum.REDIS.value: RedisDatabase,
-    #DatabaseEngineEnum.MONGO.value: MongoDatabase,
-    #DatabaseEngineEnum.RETHINK.value: RethinkDatabase,
-    #DatabaseEngineEnum.ETCD.value: EtcdDatabase,
-}
-
-DEFAULT_DB_ENGINE = DatabaseEngineEnum.INMEMORY
-
-def get_database(engine=None, **parameters):
-    if engine is None: engine = os.environ.get('DB_ENGINE', DEFAULT_DB_ENGINE)
-    if(isinstance(engine, DatabaseEngineEnum)): engine = engine.value
-    LOGGER.info('Selected Database Engine: {}'.format(engine))
-    engine_class = ENGINES.get(engine)
-    if engine_class is None: raise Exception('Unsupported Engine({})'.format(engine))
-    return engine_class(**parameters)
diff --git a/src/context/service/ContextService.py b/src/context/service/ContextService.py
index 49c3fe18f..41c232665 100644
--- a/src/context/service/ContextService.py
+++ b/src/context/service/ContextService.py
@@ -1,4 +1,4 @@
-import grpc, os
+import grpc
 import logging
 from concurrent import futures
 from grpc_health.v1.health import HealthServicer, OVERALL_HEALTH
diff --git a/src/context/service/ContextServiceServicerImpl.py b/src/context/service/ContextServiceServicerImpl.py
index e796f7ad5..599c8b2c4 100644
--- a/src/context/service/ContextServiceServicerImpl.py
+++ b/src/context/service/ContextServiceServicerImpl.py
@@ -1,7 +1,6 @@
 import grpc, logging
 from prometheus_client import Counter, Histogram
-from google.protobuf.json_format import MessageToDict
-from context.proto.context_pb2 import Topology, Empty
+from context.proto.context_pb2 import Topology
 from context.proto.context_pb2_grpc import ContextServiceServicer
 
 LOGGER = logging.getLogger(__name__)
diff --git a/src/context/service/__main__.py b/src/context/service/__main__.py
index 7590608ca..5c20c3cec 100644
--- a/src/context/service/__main__.py
+++ b/src/context/service/__main__.py
@@ -1,6 +1,6 @@
 import logging, os, signal, sys, threading
 from prometheus_client import start_http_server
-from context.database.Factory import get_database
+from common.database.Factory import get_database
 from context.service.ContextService import ContextService
 from context.Config import SERVICE_PORT, MAX_WORKERS, GRACE_PERIOD, LOG_LEVEL, METRICS_PORT
 
-- 
GitLab