X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=a1%2Fdata.py;h=cd44962bd574c48643e400dd6caceda63a4b0dad;hb=refs%2Fchanges%2F37%2F3537%2F1;hp=d737680ef226f264d9c76c7893ec3624e210ce33;hpb=0e3bc64f93a316e49bbdeb047e7592840207231d;p=ric-plt%2Fa1.git diff --git a/a1/data.py b/a1/data.py index d737680..cd44962 100644 --- a/a1/data.py +++ b/a1/data.py @@ -1,11 +1,3 @@ -""" -Represents A1s database and database access functions. -In the future, this may change to use a different backend, possibly dramatically. -Hopefully, the access functions are a good api so nothing else has to change when this happens - -For now, the database is in memory. -We use dict data structures (KV) with the expectation of having to move this into Redis -""" # ================================================================================== # Copyright (c) 2019-2020 Nokia # Copyright (c) 2018-2020 AT&T Intellectual Property. @@ -22,68 +14,33 @@ We use dict data structures (KV) with the expectation of having to move this int # 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 -import msgpack from mdclogpy import Logger - -from ricsdl.syncstorage import SyncStorage - +from ricxappframe.xapp_sdl import SDLWrapper from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound, PolicyTypeAlreadyExists, CantDeleteNonEmptyType -mdc_logger = Logger(name=__name__) - - +# 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" - - -class SDLWrapper: - """ - This is a wrapper around the expected SDL Python interface. - The usage of POLICY_DATA will be replaced with SDL when SDL for python is available. - The eventual SDL API is expected to be very close to what is here. - - We use msgpack for binary (de)serialization: https://msgpack.org/index.html - """ - - def __init__(self): - self.sdl = SyncStorage() - - def set(self, key, value): - """set a key""" - self.sdl.set(A1NS, {key: msgpack.packb(value, use_bin_type=True)}) - - def get(self, key): - """get a key""" - ret_dict = self.sdl.get(A1NS, {key}) - if key in ret_dict: - return msgpack.unpackb(ret_dict[key], raw=False) - - return None - - def find_and_get(self, prefix): - """get all k v pairs that start with prefix""" - # note: SDL "*" usage is inconsistent with real python regex, where it would be ".*" - ret_dict = self.sdl.find_and_get(A1NS, "{0}*".format(prefix)) - return {k: msgpack.unpackb(v, raw=False) for k, v in ret_dict.items()} - - def delete(self, key): - """ delete a key""" - self.sdl.remove(A1NS, {key}) - - -SDL = SDLWrapper() - TYPE_PREFIX = "a1.policy_type." INSTANCE_PREFIX = "a1.policy_instance." METADATA_PREFIX = "a1.policy_inst_metadata." HANDLER_PREFIX = "a1.policy_handler." +mdc_logger = Logger(name=__name__) +if USE_FAKE_SDL: + mdc_logger.debug("Using fake SDL") +SDL = SDLWrapper(use_fake_sdl=USE_FAKE_SDL) + # Internal helpers @@ -126,7 +83,7 @@ def _type_is_valid(policy_type_id): """ check that a type is valid """ - if SDL.get(_generate_type_key(policy_type_id)) is None: + if SDL.get(A1NS, _generate_type_key(policy_type_id)) is None: raise PolicyTypeNotFound() @@ -135,7 +92,7 @@ def _instance_is_valid(policy_type_id, policy_instance_id): check that an instance is valid """ _type_is_valid(policy_type_id) - if SDL.get(_generate_instance_key(policy_type_id, policy_instance_id)) is None: + if SDL.get(A1NS, _generate_instance_key(policy_type_id, policy_instance_id)) is None: raise PolicyInstanceNotFound @@ -145,7 +102,7 @@ def _get_statuses(policy_type_id, policy_instance_id): """ _instance_is_valid(policy_type_id, policy_instance_id) prefixes_for_handler = "{0}{1}.{2}.".format(HANDLER_PREFIX, policy_type_id, policy_instance_id) - return list(SDL.find_and_get(prefixes_for_handler).values()) + return list(SDL.find_and_get(A1NS, prefixes_for_handler).values()) def _get_instance_list(policy_type_id): @@ -154,7 +111,7 @@ def _get_instance_list(policy_type_id): """ _type_is_valid(policy_type_id) prefixes_for_type = "{0}{1}.".format(INSTANCE_PREFIX, policy_type_id) - instancekeys = SDL.find_and_get(prefixes_for_type).keys() + instancekeys = SDL.find_and_get(A1NS, prefixes_for_type).keys() return [k.split(prefixes_for_type)[1] for k in instancekeys] @@ -163,9 +120,9 @@ def _clear_handlers(policy_type_id, policy_instance_id): delete all the handlers for a policy instance """ all_handlers_pref = _generate_handler_prefix(policy_type_id, policy_instance_id) - keys = SDL.find_and_get(all_handlers_pref) + keys = SDL.find_and_get(A1NS, all_handlers_pref) for k in keys: - SDL.delete(k) + SDL.delete(A1NS, k) def _get_metadata(policy_type_id, policy_instance_id): @@ -174,7 +131,7 @@ def _get_metadata(policy_type_id, policy_instance_id): """ _instance_is_valid(policy_type_id, policy_instance_id) metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id) - return SDL.get(metadata_key) + return SDL.get(A1NS, metadata_key) def _delete_after(policy_type_id, policy_instance_id, ttl): @@ -188,8 +145,8 @@ def _delete_after(policy_type_id, policy_instance_id, ttl): # ready to delete _clear_handlers(policy_type_id, policy_instance_id) # delete all the handlers - SDL.delete(_generate_instance_key(policy_type_id, policy_instance_id)) # delete instance - SDL.delete(_generate_instance_metadata_key(policy_type_id, policy_instance_id)) # delete instance metadata + SDL.delete(A1NS, _generate_instance_key(policy_type_id, policy_instance_id)) # delete instance + SDL.delete(A1NS, _generate_instance_metadata_key(policy_type_id, policy_instance_id)) # delete instance metadata mdc_logger.debug("type {0} instance {1} deleted".format(policy_type_id, policy_instance_id)) @@ -200,7 +157,7 @@ def get_type_list(): """ retrieve all type ids """ - typekeys = SDL.find_and_get(TYPE_PREFIX).keys() + typekeys = SDL.find_and_get(A1NS, TYPE_PREFIX).keys() # policy types are ints but they get butchered to strings in the KV return [int(k.split(TYPE_PREFIX)[1]) for k in typekeys] @@ -210,9 +167,11 @@ def store_policy_type(policy_type_id, body): store a policy type if it doesn't already exist """ key = _generate_type_key(policy_type_id) - if SDL.get(key) is not None: + if SDL.get(A1NS, key) is not None: raise PolicyTypeAlreadyExists() - SDL.set(key, body) + # overwrite ID in body to enforce consistency + body['policy_type_id'] = policy_type_id + SDL.set(A1NS, key, body) def delete_policy_type(policy_type_id): @@ -221,7 +180,7 @@ def delete_policy_type(policy_type_id): """ pil = get_instance_list(policy_type_id) if pil == []: # empty, can delete - SDL.delete(_generate_type_key(policy_type_id)) + SDL.delete(A1NS, _generate_type_key(policy_type_id)) else: raise CantDeleteNonEmptyType() @@ -231,7 +190,7 @@ def get_policy_type(policy_type_id): retrieve a type """ _type_is_valid(policy_type_id) - return SDL.get(_generate_type_key(policy_type_id)) + return SDL.get(A1NS, _generate_type_key(policy_type_id)) # Instances @@ -246,13 +205,13 @@ def store_policy_instance(policy_type_id, policy_instance_id, instance): # store the instance key = _generate_instance_key(policy_type_id, policy_instance_id) - if SDL.get(key) is not None: + if SDL.get(A1NS, key) is not None: # 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(key, instance) + SDL.set(A1NS, key, instance) metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id) - SDL.set(metadata_key, {"created_at": creation_timestamp, "has_been_deleted": False}) + SDL.set(A1NS, metadata_key, {"created_at": creation_timestamp, "has_been_deleted": False}) def get_policy_instance(policy_type_id, policy_instance_id): @@ -260,7 +219,7 @@ def get_policy_instance(policy_type_id, policy_instance_id): Retrieve a policy instance """ _instance_is_valid(policy_type_id, policy_instance_id) - return SDL.get(_generate_instance_key(policy_type_id, policy_instance_id)) + return SDL.get(A1NS, _generate_instance_key(policy_type_id, policy_instance_id)) def get_instance_list(policy_type_id): @@ -272,7 +231,7 @@ def get_instance_list(policy_type_id): def delete_policy_instance(policy_type_id, policy_instance_id): """ - initially sets has_been_deleted + initially sets has_been_deleted in the status then launches a thread that waits until the relevent timer expires, and finally deletes the instance """ _instance_is_valid(policy_type_id, policy_instance_id) @@ -282,6 +241,7 @@ def delete_policy_instance(policy_type_id, policy_instance_id): metadata_key = _generate_instance_metadata_key(policy_type_id, policy_instance_id) existing_metadata = _get_metadata(policy_type_id, policy_instance_id) SDL.set( + A1NS, metadata_key, {"created_at": existing_metadata["created_at"], "has_been_deleted": True, "deleted_at": deleted_timestamp}, ) @@ -309,7 +269,7 @@ def set_policy_instance_status(policy_type_id, policy_instance_id, handler_id, s """ _type_is_valid(policy_type_id) _instance_is_valid(policy_type_id, policy_instance_id) - SDL.set(_generate_handler_key(policy_type_id, policy_instance_id, handler_id), status) + SDL.set(A1NS, _generate_handler_key(policy_type_id, policy_instance_id, handler_id), status) def get_policy_instance_status(policy_type_id, policy_instance_id):