Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 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 | #ifndef SHAREDDATALAYER_CONFIGURATIONREADER_HPP_ |
| 18 | #define SHAREDDATALAYER_CONFIGURATIONREADER_HPP_ |
| 19 | |
| 20 | #define DB_HOST_ENV_VAR_NAME "DBAAS_SERVICE_HOST" |
| 21 | #define DB_PORT_ENV_VAR_NAME "DBAAS_SERVICE_PORT" |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame^] | 22 | #define SENTINEL_PORT_ENV_VAR_NAME "DBAAS_SERVICE_SENTINEL_PORT" |
| 23 | #define SENTINEL_MASTER_NAME_ENV_VAR_NAME "DBAAS_MASTER_NAME" |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 24 | |
| 25 | #include <iosfwd> |
| 26 | #include <string> |
| 27 | #include <unordered_map> |
| 28 | #include <boost/property_tree/ptree.hpp> |
| 29 | #include <sdl/exception.hpp> |
| 30 | #include "private/configurationpaths.hpp" |
| 31 | #include "private/logger.hpp" |
| 32 | |
| 33 | namespace shareddatalayer |
| 34 | { |
| 35 | class DatabaseConfiguration; |
| 36 | class NamespaceConfigurations; |
| 37 | class System; |
| 38 | |
| 39 | class ConfigurationReader |
| 40 | { |
| 41 | public: |
| 42 | explicit ConfigurationReader(std::shared_ptr<Logger> logger); |
| 43 | |
| 44 | ConfigurationReader(const Directories& directories, |
| 45 | System& system, |
| 46 | std::shared_ptr<Logger> logger); |
| 47 | |
| 48 | ~ConfigurationReader(); |
| 49 | |
| 50 | void readDatabaseConfiguration(DatabaseConfiguration& databaseConfiguration); |
| 51 | |
| 52 | void readNamespaceConfigurations(NamespaceConfigurations& namespaceConfigurations); |
| 53 | |
| 54 | /** |
| 55 | * Overrides existing json configuration with json format input stream given as |
| 56 | * parameter. Does not override existing configration if existing configration |
| 57 | * originates from environment variable. Meant for UT usage. |
| 58 | */ |
| 59 | void readConfigurationFromInputStream(const std::istream& input); |
| 60 | |
| 61 | private: |
| 62 | const std::string dbHostEnvVariableName; |
| 63 | std::string dbHostEnvVariableValue; |
| 64 | const std::string dbPortEnvVariableName; |
| 65 | std::string dbPortEnvVariableValue; |
Rolf Badorek | 2dcf940 | 2019-10-01 18:33:58 +0300 | [diff] [blame^] | 66 | const std::string sentinelPortEnvVariableName; |
| 67 | std::string sentinelPortEnvVariableValue; |
| 68 | const std::string sentinelMasterNameEnvVariableName; |
| 69 | std::string sentinelMasterNameEnvVariableValue; |
Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 70 | boost::optional<boost::property_tree::ptree> jsonDatabaseConfiguration; |
| 71 | std::string sourceForDatabaseConfiguration; |
| 72 | std::unordered_map<std::string, std::pair<boost::property_tree::ptree, std::string>> jsonNamespaceConfigurations; |
| 73 | std::shared_ptr<Logger> logger; |
| 74 | |
| 75 | void readConfigurationFromDirectories(const Directories& directories); |
| 76 | |
| 77 | template<typename T> |
| 78 | void readConfiguration(T& input, const std::string& sourceName); |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | #endif |