RIC:1060: Change in PTL
[ric-plt/sdl.git] / include / private / configurationreader.hpp
1 /*
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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #ifndef SHAREDDATALAYER_CONFIGURATIONREADER_HPP_
23 #define SHAREDDATALAYER_CONFIGURATIONREADER_HPP_
24
25 #define DB_HOST_ENV_VAR_NAME "DBAAS_SERVICE_HOST"
26 #define DB_PORT_ENV_VAR_NAME "DBAAS_SERVICE_PORT"
27 #define SENTINEL_PORT_ENV_VAR_NAME "DBAAS_SERVICE_SENTINEL_PORT"
28 #define SENTINEL_MASTER_NAME_ENV_VAR_NAME "DBAAS_MASTER_NAME"
29 #define DB_CLUSTER_ADDR_LIST_ENV_VAR_NAME "DBAAS_CLUSTER_ADDR_LIST"
30
31 #include <iosfwd>
32 #include <string>
33 #include <unordered_map>
34 #include <boost/property_tree/ptree.hpp>
35 #include <sdl/exception.hpp>
36 #include "private/configurationpaths.hpp"
37 #include "private/logger.hpp"
38
39 namespace shareddatalayer
40 {
41     class DatabaseConfiguration;
42     class NamespaceConfigurations;
43     class System;
44
45     class ConfigurationReader
46     {
47     public:
48         explicit ConfigurationReader(std::shared_ptr<Logger> logger);
49
50         ConfigurationReader(const Directories& directories,
51                             System& system,
52                             std::shared_ptr<Logger> logger);
53
54         ~ConfigurationReader();
55
56         void readDatabaseConfiguration(DatabaseConfiguration& databaseConfiguration);
57
58         void readNamespaceConfigurations(NamespaceConfigurations& namespaceConfigurations);
59
60         /**
61          * Overrides existing json configuration with json format input stream given as
62          * parameter. Does not override existing configration if existing configration
63          * originates from environment variable. Meant for UT usage.
64          */
65         void readConfigurationFromInputStream(const std::istream& input);
66
67     private:
68         const std::string dbHostEnvVariableName;
69         std::string dbHostEnvVariableValue;
70         const std::string dbPortEnvVariableName;
71         std::string dbPortEnvVariableValue;
72         const std::string sentinelPortEnvVariableName;
73         std::string sentinelPortEnvVariableValue;
74         const std::string sentinelMasterNameEnvVariableName;
75         std::string sentinelMasterNameEnvVariableValue;
76         const std::string dbClusterAddrListEnvVariableName;
77         std::string dbClusterAddrListEnvVariableValue;
78         boost::optional<boost::property_tree::ptree> jsonDatabaseConfiguration;
79         std::string sourceForDatabaseConfiguration;
80         std::unordered_map<std::string, std::pair<boost::property_tree::ptree, std::string>> jsonNamespaceConfigurations;
81         std::shared_ptr<Logger> logger;
82
83         void readConfigurationFromDirectories(const Directories& directories);
84
85         template<typename T>
86         void readConfiguration(T& input, const std::string& sourceName);
87     };
88 }
89
90 #endif