Add USE_FAKE_SDL to mock the data storage layer
[ric-plt/a1.git] / a1 / data.py
index 45fe5de..cd44962 100644 (file)
@@ -1,6 +1,3 @@
-"""
-Represents A1s database and database access functions.
-"""
 # ==================================================================================
 #       Copyright (c) 2019-2020 Nokia
 #       Copyright (c) 2018-2020 AT&T Intellectual Property.
@@ -17,6 +14,10 @@ Represents A1s database and database access functions.
 #   See the License for the specific language governing permissions and
 #   limitations under the License.
 # ==================================================================================
+"""
+Represents A1s database and database access functions.
+"""
+import distutils.util
 import os
 import time
 from threading import Thread
@@ -24,10 +25,10 @@ from mdclogpy import Logger
 from ricxappframe.xapp_sdl import SDLWrapper
 from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound, PolicyTypeAlreadyExists, CantDeleteNonEmptyType
 
-
 # constants
 INSTANCE_DELETE_NO_RESP_TTL = int(os.environ.get("INSTANCE_DELETE_NO_RESP_TTL", 5))
 INSTANCE_DELETE_RESP_TTL = int(os.environ.get("INSTANCE_DELETE_RESP_TTL", 5))
+USE_FAKE_SDL = bool(distutils.util.strtobool(os.environ.get("USE_FAKE_SDL", "False")))
 A1NS = "A1m_ns"
 TYPE_PREFIX = "a1.policy_type."
 INSTANCE_PREFIX = "a1.policy_instance."
@@ -36,7 +37,9 @@ HANDLER_PREFIX = "a1.policy_handler."
 
 
 mdc_logger = Logger(name=__name__)
-SDL = SDLWrapper()
+if USE_FAKE_SDL:
+    mdc_logger.debug("Using fake SDL")
+SDL = SDLWrapper(use_fake_sdl=USE_FAKE_SDL)
 
 # Internal helpers
 
@@ -166,6 +169,8 @@ def store_policy_type(policy_type_id, body):
     key = _generate_type_key(policy_type_id)
     if SDL.get(A1NS, key) is not None:
         raise PolicyTypeAlreadyExists()
+    # overwrite ID in body to enforce consistency
+    body['policy_type_id'] = policy_type_id
     SDL.set(A1NS, key, body)