blob: 6c175e7259f51b151d8f6427bbaf4eaf5e9f5307 [file] [log] [blame]
Rolf Badorekef2bf512019-08-20 11:17:15 +03001/*
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"
22
23#include <iosfwd>
24#include <string>
25#include <unordered_map>
26#include <boost/property_tree/ptree.hpp>
27#include <sdl/exception.hpp>
28#include "private/configurationpaths.hpp"
29#include "private/logger.hpp"
30
31namespace shareddatalayer
32{
33 class DatabaseConfiguration;
34 class NamespaceConfigurations;
35 class System;
36
37 class ConfigurationReader
38 {
39 public:
40 explicit ConfigurationReader(std::shared_ptr<Logger> logger);
41
42 ConfigurationReader(const Directories& directories,
43 System& system,
44 std::shared_ptr<Logger> logger);
45
46 ~ConfigurationReader();
47
48 void readDatabaseConfiguration(DatabaseConfiguration& databaseConfiguration);
49
50 void readNamespaceConfigurations(NamespaceConfigurations& namespaceConfigurations);
51
52 /**
53 * Overrides existing json configuration with json format input stream given as
54 * parameter. Does not override existing configration if existing configration
55 * originates from environment variable. Meant for UT usage.
56 */
57 void readConfigurationFromInputStream(const std::istream& input);
58
59 private:
60 const std::string dbHostEnvVariableName;
61 std::string dbHostEnvVariableValue;
62 const std::string dbPortEnvVariableName;
63 std::string dbPortEnvVariableValue;
64 boost::optional<boost::property_tree::ptree> jsonDatabaseConfiguration;
65 std::string sourceForDatabaseConfiguration;
66 std::unordered_map<std::string, std::pair<boost::property_tree::ptree, std::string>> jsonNamespaceConfigurations;
67 std::shared_ptr<Logger> logger;
68
69 void readConfigurationFromDirectories(const Directories& directories);
70
71 template<typename T>
72 void readConfiguration(T& input, const std::string& sourceName);
73 };
74}
75
76#endif