Implement a fake SDL database backend
[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', 'db_type'])
38
39     def __init__(self, fake_db_backend):
40         self.params = self._read_configuration(fake_db_backend)
41
42     def __str__(self):
43         return str(
44             {
45                 "DB host": self.params.db_host,
46                 "DB port": self.params.db_port,
47                 "DB master sentinel": self.params.db_sentinel_master_name,
48                 "DB sentinel port": self.params.db_sentinel_port,
49                 "DB type": self.params.db_type.name,
50             }
51         )
52
53     def get_params(self):
54         """Return SDL configuration."""
55         return self.params
56
57     @classmethod
58     def _read_configuration(cls, fake_db_backend):
59         backend_type = DbBackendType.REDIS
60         if fake_db_backend:
61             if fake_db_backend.lower() != 'dict':
62                 msg = ("Configuration error: "
63                        "SDL instance was initiated with wrong "
64                        "'fake_db_backend' argument value: {}. "
65                        "Value 'dict' is only supported.".
66                        format(fake_db_backend))
67                 raise ValueError(msg)
68
69             backend_type = DbBackendType.FAKE_DICT
70
71         return _Configuration.Params(db_host=os.getenv('DBAAS_SERVICE_HOST'),
72                                      db_port=os.getenv('DBAAS_SERVICE_PORT'),
73                                      db_sentinel_port=os.getenv('DBAAS_SERVICE_SENTINEL_PORT'),
74                                      db_sentinel_master_name=os.getenv('DBAAS_MASTER_NAME'),
75                                      db_type=backend_type)