Read list type of environment variables
[ric-plt/sdlpy.git] / ricsdl-package / ricsdl / configuration.py
1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2018-2022 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 """The module provides implementation of Shared Data Layer (SDL) configurability."""
23 import os
24 from enum import Enum
25 from collections import namedtuple
26
27
28 class DbBackendType(Enum):
29     """Enumeration class of supported SDL database backend types."""
30     REDIS = 1
31     FAKE_DICT = 2
32
33
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'])
39
40     def __init__(self, fake_db_backend):
41         self.params = self._read_configuration(fake_db_backend)
42
43     def __str__(self):
44         return str(
45             {
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,
52             }
53         )
54
55     def get_params(self):
56         """Return SDL configuration."""
57         return self.params
58
59     @classmethod
60     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
72         host = os.getenv('DBAAS_SERVICE_HOST', "")
73
74         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,
97                                      db_type=backend_type)
98
99     @classmethod
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])
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
115     def get_event_separator(cls):
116         return "___"