+"""
+Represents A1s database and database access functions.
+"""
# ==================================================================================
-# Copyright (c) 2019 Nokia
-# Copyright (c) 2018-2019 AT&T Intellectual Property.
+# Copyright (c) 2019-2020 Nokia
+# Copyright (c) 2018-2020 AT&T Intellectual Property.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==================================================================================
-
-"""
-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
-"""
import os
import time
from threading import Thread
import msgpack
+from mdclogpy import Logger
+from ricsdl.syncstorage import SyncStorage
from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound, PolicyTypeAlreadyExists, CantDeleteNonEmptyType
-from a1 import get_module_logger
-logger = get_module_logger(__name__)
+mdc_logger = Logger(name=__name__)
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))
+A1NS = "A1m_ns"
+
class SDLWrapper:
"""
"""
def __init__(self):
- self.POLICY_DATA = {}
+ self.sdl = SyncStorage()
def set(self, key, value):
"""set a key"""
- self.POLICY_DATA[key] = msgpack.packb(value, use_bin_type=True)
+ self.sdl.set(A1NS, {key: msgpack.packb(value, use_bin_type=True)})
def get(self, key):
"""get a key"""
- if key in self.POLICY_DATA:
- return msgpack.unpackb(self.POLICY_DATA[key], raw=False)
+ 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"""
- return {k: msgpack.unpackb(v, raw=False) for k, v in self.POLICY_DATA.items() if k.startswith(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"""
- del self.POLICY_DATA[key]
+ self.sdl.remove(A1NS, {key})
SDL = SDLWrapper()
_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
- logger.debug("type %s instance %s deleted", policy_type_id, policy_instance_id)
- raise PolicyInstanceNotFound()
+ mdc_logger.debug("type {0} instance {1} deleted".format(policy_type_id, policy_instance_id))
# Types
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)