Implement a fake SDL database backend
[ric-plt/sdlpy.git] / ricsdl-package / ricsdl / backend / __init__.py
1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2018-2019 Nokia.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 #
17 # This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 # platform project (RICP).
19 #
20
21
22 """Shared Data Layer (SDL) database backend module."""
23
24 from importlib import import_module
25 from ricsdl.configuration import DbBackendType
26
27
28 def get_backend_instance(configuration):
29     """
30     Select database backend solution and return and instance of it.
31     """
32     if configuration.get_params().db_type == DbBackendType.FAKE_DICT:
33         backend_name = 'FakeDictBackend'
34         backend_module_name = 'fake_dict_db'
35     else:
36         backend_name = 'RedisBackend'
37         backend_module_name = 'redis'
38
39     package = __package__ or __name__
40     backend_module = import_module('.' + backend_module_name, package=package)
41     backend_class = getattr(backend_module, backend_name)
42     instance = backend_class(configuration)
43     return instance
44
45
46 def get_backend_lock_instance(configuration, ns, name, expiration, backend):
47     """
48     Select database backend lock solution and return and instance of it.
49     """
50     if configuration.get_params().db_type == DbBackendType.FAKE_DICT:
51         backend_lock_name = 'FakeDictBackendLock'
52         backend_module_name = 'fake_dict_db'
53     else:
54         backend_lock_name = 'RedisBackendLock'
55         backend_module_name = 'redis'
56
57     package = __package__ or __name__
58     backend_module = import_module('.' + backend_module_name, package=package)
59     backend_lock_class = getattr(backend_module, backend_lock_name)
60     instance = backend_lock_class(ns, name, expiration, backend)
61     return instance