e99e68e18cc95b2a85d1413b9341053504a1b3e2
[ric-plt/sdlpy.git] / ricsdl-package / ricsdl / configuration.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 """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_port', 'db_sentinel_port',
37                                    'db_sentinel_master_name',
38                                    'db_cluster_addr_list', '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 port": self.params.db_port,
48                 "DB master sentinel": self.params.db_sentinel_master_name,
49                 "DB sentinel port": self.params.db_sentinel_port,
50                 "DB cluster address list": self.params.db_cluster_addr_list,
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
73         return _Configuration.Params(db_host=os.getenv('DBAAS_SERVICE_HOST'),
74                                      db_port=os.getenv('DBAAS_SERVICE_PORT'),
75                                      db_sentinel_port=os.getenv('DBAAS_SERVICE_SENTINEL_PORT'),
76                                      db_sentinel_master_name=os.getenv('DBAAS_MASTER_NAME'),
77                                      db_cluster_addr_list=os.getenv('DBAAS_CLUSTER_ADDR_LIST'),
78                                      db_type=backend_type)
79
80     @classmethod
81     def get_event_separator(cls):
82         return "___"