X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=a1%2Fdata.py;h=436912f8a45e9bdf6ee83e5efe16b90167c6dce4;hb=d7858cf453b4f74cd4dad0e13106218ea9a56785;hp=45fe5de204e27f52d529fb8a9c671b6f367ac072;hpb=45f8f98f0e344cc253d709df23bf27311adff0bb;p=ric-plt%2Fa1.git diff --git a/a1/data.py b/a1/data.py index 45fe5de..436912f 100644 --- a/a1/data.py +++ b/a1/data.py @@ -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,17 +14,21 @@ 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 from mdclogpy import Logger from ricxappframe.xapp_sdl import SDLWrapper -from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound, PolicyTypeAlreadyExists, CantDeleteNonEmptyType - +from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound, PolicyTypeAlreadyExists, PolicyTypeIdMismatch, 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 @@ -81,7 +84,7 @@ def _type_is_valid(policy_type_id): check that a type is valid """ if SDL.get(A1NS, _generate_type_key(policy_type_id)) is None: - raise PolicyTypeNotFound() + raise PolicyTypeNotFound(policy_type_id) def _instance_is_valid(policy_type_id, policy_instance_id): @@ -90,7 +93,7 @@ def _instance_is_valid(policy_type_id, policy_instance_id): """ _type_is_valid(policy_type_id) if SDL.get(A1NS, _generate_instance_key(policy_type_id, policy_instance_id)) is None: - raise PolicyInstanceNotFound + raise PolicyInstanceNotFound(policy_type_id) def _get_statuses(policy_type_id, policy_instance_id): @@ -163,9 +166,11 @@ def store_policy_type(policy_type_id, body): """ store a policy type if it doesn't already exist """ + if policy_type_id != body['policy_type_id']: + raise PolicyTypeIdMismatch("{0} vs. {1}".format(policy_type_id, body['policy_type_id'])) key = _generate_type_key(policy_type_id) if SDL.get(A1NS, key) is not None: - raise PolicyTypeAlreadyExists() + raise PolicyTypeAlreadyExists(policy_type_id) SDL.set(A1NS, key, body) @@ -177,7 +182,7 @@ def delete_policy_type(policy_type_id): if pil == []: # empty, can delete SDL.delete(A1NS, _generate_type_key(policy_type_id)) else: - raise CantDeleteNonEmptyType() + raise CantDeleteNonEmptyType(policy_type_id) def get_policy_type(policy_type_id): @@ -199,8 +204,10 @@ def store_policy_instance(policy_type_id, policy_instance_id, instance): creation_timestamp = time.time() # store the instance + operation = "CREATE" key = _generate_instance_key(policy_type_id, policy_instance_id) if SDL.get(A1NS, key) is not None: + operation = "UPDATE" # Reset the statuses because this is a new policy instance, even if it was overwritten _clear_handlers(policy_type_id, policy_instance_id) # delete all the handlers SDL.set(A1NS, key, instance) @@ -208,6 +215,8 @@ def store_policy_instance(policy_type_id, policy_instance_id, instance): metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id) SDL.set(A1NS, metadata_key, {"created_at": creation_timestamp, "has_been_deleted": False}) + return operation + def get_policy_instance(policy_type_id, policy_instance_id): """