blob: 99cde0fdd40cb376f8baa8af23526c572989e5b4 [file] [log] [blame]
Timo Tietavainendada8462019-11-27 11:50:01 +02001# Copyright (c) 2019 AT&T Intellectual Property.
Timo Tietavainene67b9ab2022-03-14 07:27:30 +02002# Copyright (c) 2018-2022 Nokia.
Timo Tietavainendada8462019-11-27 11:50:01 +02003#
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"""The module provides implementation of Shared Data Layer (SDL) configurability."""
23import os
Timo Tietavainen598ca392020-01-08 16:49:11 +020024from enum import Enum
Timo Tietavainendada8462019-11-27 11:50:01 +020025from collections import namedtuple
26
27
Timo Tietavainen598ca392020-01-08 16:49:11 +020028class DbBackendType(Enum):
29 """Enumeration class of supported SDL database backend types."""
30 REDIS = 1
31 FAKE_DICT = 2
32
33
Timo Tietavainendada8462019-11-27 11:50:01 +020034class _Configuration():
35 """This class implements Shared Data Layer (SDL) configurability."""
Timo Tietavainene67b9ab2022-03-14 07:27:30 +020036 Params = namedtuple('Params', ['db_host', 'db_ports', 'db_sentinel_ports',
37 'db_sentinel_master_names',
38 'db_cluster_addrs', 'db_type'])
Timo Tietavainendada8462019-11-27 11:50:01 +020039
Timo Tietavainen598ca392020-01-08 16:49:11 +020040 def __init__(self, fake_db_backend):
41 self.params = self._read_configuration(fake_db_backend)
Timo Tietavainendada8462019-11-27 11:50:01 +020042
43 def __str__(self):
44 return str(
45 {
46 "DB host": self.params.db_host,
Timo Tietavainene67b9ab2022-03-14 07:27:30 +020047 "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,
Timo Tietavainen598ca392020-01-08 16:49:11 +020051 "DB type": self.params.db_type.name,
Timo Tietavainendada8462019-11-27 11:50:01 +020052 }
53 )
54
55 def get_params(self):
56 """Return SDL configuration."""
57 return self.params
58
59 @classmethod
Timo Tietavainen598ca392020-01-08 16:49:11 +020060 def _read_configuration(cls, fake_db_backend):
61 backend_type = DbBackendType.REDIS
62 if fake_db_backend:
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))
69 raise ValueError(msg)
70
71 backend_type = DbBackendType.FAKE_DICT
Timo Tietavainene67b9ab2022-03-14 07:27:30 +020072 host = os.getenv('DBAAS_SERVICE_HOST', "")
Timo Tietavainen598ca392020-01-08 16:49:11 +020073
Timo Tietavainene67b9ab2022-03-14 07:27:30 +020074 port_env = os.getenv('DBAAS_SERVICE_PORT')
75 ports = port_env.split(",") if port_env is not None else list()
76
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()
79
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()
82
83 addr_env = os.getenv('DBAAS_CLUSTER_ADDR_LIST')
84 addrs = addr_env.split(",") if addr_env is not None else list()
85
86 if len(addrs) == 0 and len(host) > 0:
87 addrs.append(host)
88
89 addrs, ports, sentinel_ports, sentinel_names = cls._complete_configuration(
90 addrs, ports, sentinel_ports, sentinel_names)
91
92 return _Configuration.Params(db_host=host,
93 db_ports=ports,
94 db_sentinel_ports=sentinel_ports,
95 db_sentinel_master_names=sentinel_names,
96 db_cluster_addrs=addrs,
Timo Tietavainen598ca392020-01-08 16:49:11 +020097 db_type=backend_type)
Timo Tietavainendb775392021-06-09 05:56:54 +030098
99 @classmethod
Timo Tietavainene67b9ab2022-03-14 07:27:30 +0200100 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])
105 else:
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
113
114 @classmethod
Timo Tietavainendb775392021-06-09 05:56:54 +0300115 def get_event_separator(cls):
116 return "___"