1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2018-2022 Nokia.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 # platform project (RICP).
22 """The module provides implementation of Shared Data Layer (SDL) configurability."""
25 from collections import namedtuple
28 class DbBackendType(Enum):
29 """Enumeration class of supported SDL database backend types."""
34 class _Configuration():
35 """This class implements Shared Data Layer (SDL) configurability."""
36 Params = namedtuple('Params', ['db_host', 'db_ports', 'db_sentinel_ports',
37 'db_sentinel_master_names',
38 'db_cluster_addrs', 'db_type'])
40 def __init__(self, fake_db_backend):
41 self.params = self._read_configuration(fake_db_backend)
46 "DB host": self.params.db_host,
47 "DB ports": self.params.db_ports,
48 "DB master sentinels": self.params.db_sentinel_master_names,
49 "DB sentinel ports": self.params.db_sentinel_ports,
50 "DB cluster addresses": self.params.db_cluster_addrs,
51 "DB type": self.params.db_type.name,
56 """Return SDL configuration."""
60 def _read_configuration(cls, fake_db_backend):
61 backend_type = DbBackendType.REDIS
63 if fake_db_backend.lower() != 'dict':
64 msg = ("Configuration error: "
65 "SDL instance was initiated with wrong "
66 "'fake_db_backend' argument value: {}. "
67 "Value 'dict' is only supported.".
68 format(fake_db_backend))
71 backend_type = DbBackendType.FAKE_DICT
72 host = os.getenv('DBAAS_SERVICE_HOST', "")
74 port_env = os.getenv('DBAAS_SERVICE_PORT')
75 ports = port_env.split(",") if port_env is not None else list()
77 sentinel_port_env = os.getenv('DBAAS_SERVICE_SENTINEL_PORT')
78 sentinel_ports = sentinel_port_env.split(",") if sentinel_port_env is not None else list()
80 sentinel_name_env = os.getenv('DBAAS_MASTER_NAME')
81 sentinel_names = sentinel_name_env.split(",") if sentinel_name_env is not None else list()
83 addr_env = os.getenv('DBAAS_CLUSTER_ADDR_LIST')
84 addrs = addr_env.split(",") if addr_env is not None else list()
86 if len(addrs) == 0 and len(host) > 0:
89 addrs, ports, sentinel_ports, sentinel_names = cls._complete_configuration(
90 addrs, ports, sentinel_ports, sentinel_names)
92 return _Configuration.Params(db_host=host,
94 db_sentinel_ports=sentinel_ports,
95 db_sentinel_master_names=sentinel_names,
96 db_cluster_addrs=addrs,
100 def _complete_configuration(cls, addrs, ports, sentinel_ports, sentinel_names):
101 if len(sentinel_ports) == 0:
102 if len(addrs) > len(ports) and len(ports) > 0:
103 for i in range(len(ports), len(addrs)):
104 ports.append(ports[i - 1])
106 if len(addrs) > len(sentinel_ports):
107 for i in range(len(sentinel_ports), len(addrs)):
108 sentinel_ports.append(sentinel_ports[i - 1])
109 if len(addrs) > len(sentinel_names) and len(sentinel_names) > 0:
110 for i in range(len(sentinel_names), len(addrs)):
111 sentinel_names.append(sentinel_names[i - 1])
112 return addrs, ports, sentinel_ports, sentinel_names
115 def get_event_separator(cls):