-"""
-Represents A1s database and database access functions.
-"""
# ==================================================================================
# Copyright (c) 2019-2020 Nokia
# Copyright (c) 2018-2020 AT&T Intellectual Property.
# 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."
mdc_logger = Logger(name=__name__)
-SDL = SDLWrapper()
+mdc_logger.mdclog_format_init(configmap_monitor=True)
+if USE_FAKE_SDL:
+ mdc_logger.debug("Using fake SDL")
+SDL = SDLWrapper(use_fake_sdl=USE_FAKE_SDL)
# Internal helpers
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):
"""
_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):
"""
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()
- # overwrite ID in body to enforce consistency
- body['policy_type_id'] = policy_type_id
+ raise PolicyTypeAlreadyExists(policy_type_id)
SDL.set(A1NS, key, body)
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):
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)
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):
"""