1 // Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef PROCESS_CONFIG_CTL_INFO_H
8 #define PROCESS_CONFIG_CTL_INFO_H
9 
10 #include <cc/cfg_to_element.h>
11 #include <database/database_connection.h>
12 #include <util/optional.h>
13 
14 #include <boost/shared_ptr.hpp>
15 #include <stdint.h>
16 #include <vector>
17 
18 namespace isc {
19 namespace process {
20 
21 /// @brief Provides configuration information used during a server's
22 /// configuration process.
23 ///
24 class ConfigDbInfo : public isc::data::CfgToElement {
25 public:
26     /// @brief Constructor
ConfigDbInfo()27     ConfigDbInfo() {};
28 
29     /// @brief Set the access string.
30     ///
31     /// Sets the db's access string to the given value and then parses it
32     /// into name-value pairs and storing them internally as a
33     /// DatabaseConnection::ParameterMap.  It discards the existing content
34     /// of the map first.  It does not validate the parameter names of values,
35     /// ensuring the validity of the string content is placed upon the caller.
36     ///
37     /// @param access_str string of name=value pairs separated by spaces.
38     /// @param test_mode flag used in unittests only to skip parsing the access
39     /// string and storing the parameters.
40     void setAccessString(const std::string& access_str, bool test_mode = false);
41 
42     /// @brief Retrieves the database access string.
43     ///
44     /// @return database access string.
getAccessString()45     std::string getAccessString() const {
46         return (access_str_);
47     }
48 
49     /// @brief Retrieves the database access string with password redacted.
50     ///
51     /// @return database access string with password redacted.
redactedAccessString()52     std::string redactedAccessString() const {
53         return(db::DatabaseConnection::redactedAccessString(access_params_));
54     }
55 
56     /// @brief Retrieve the map of parameter values.
57     ///
58     /// @return Constant reference to the database's parameter map.
getParameters()59     const db::DatabaseConnection::ParameterMap& getParameters() const {
60         return (access_params_);
61     }
62 
63     /// @brief Fetch the value of a given parameter.
64     ///
65     /// @param name name of the parameter value to fetch.
66     /// @param[out] value string which will contain the value of the
67     /// parameter (if found).
68     ///
69     /// @return Boolean true if the parameter named is found in the map,
70     /// false otherwise.
71     bool getParameterValue(const std::string& name,
72                            std::string& value) const;
73 
74     /// @brief Unparse a configuration object.
75     ///
76     /// @return a pointer to unparsed configuration.
77     virtual isc::data::ElementPtr toElement() const;
78 
79     /// @brief Compares two objects for equality.
80     ///
81     /// @param other An object to be compared with this object.
82     ///
83     /// @return true if objects are equal, false otherwise.
84     bool equals(const ConfigDbInfo& other) const;
85 
86     /// @brief Compares two objects for equality.
87     ///
88     /// @param other An object to be compared with this object.
89     ///
90     /// @return true if objects are equal, false otherwise.
91     bool operator==(const ConfigDbInfo& other) const {
92         return (equals(other));
93     }
94 
95     /// @brief Compares two objects for inequality.
96     ///
97     /// @param other An object to be compared with this object.
98     ///
99     /// @return true if objects are not equal, false otherwise.
100     bool operator!=(const ConfigDbInfo& other) const {
101         return (!equals(other));
102     }
103 
104 private:
105     /// @brief Access string of parameters used to access this database.
106     std::string access_str_;
107 
108     /// @brief Map of the access parameters and their values.
109     db::DatabaseConnection::ParameterMap access_params_;
110 };
111 
112 typedef std::vector<ConfigDbInfo> ConfigDbInfoList;
113 
114 /// @brief Embodies configuration information used during a server's
115 /// configuration process.
116 ///
117 /// This is class conveys the configuration control information
118 /// described by the following JSON text:
119 ///
120 /// @code
121 /// "config-control" :
122 /// {
123 ///     "config-databases":
124 ///     [
125 ///     {
126 ///         # first config db
127 ///         # common database access parameters
128 ///         "type": <"mysql"|"postgresql"|"cql">,
129 ///         "name": <"db name">,
130 ///         "host": <"db host name">,
131 ///             :
132 ///     },
133 ///     {
134 ///         # next config db
135 ///     }
136 ///     ]
137 ///
138 /// }
139 /// @endcode
140 ///
141 class ConfigControlInfo : public isc::data::CfgToElement {
142 public:
143 
144     /// @brief Constructor.
ConfigControlInfo()145     ConfigControlInfo()
146         : config_fetch_wait_time_(30, true) {};
147 
148     /// @brief Copy Constructor.
149     ConfigControlInfo(const ConfigControlInfo& other);
150 
151     /// @brief Sets new value of the config-fetch-wait-time.
152     ///
153     /// @param config_fetch_wait_time New value of the parameter which
154     /// specifies a time period in seconds between the attempts to
155     /// fetch the server configuration updates. The value of 0 disables
156     /// the periodic attempts to fetch the updates.
setConfigFetchWaitTime(const util::Optional<uint16_t> & config_fetch_wait_time)157     void setConfigFetchWaitTime(const util::Optional<uint16_t>& config_fetch_wait_time) {
158         config_fetch_wait_time_ = config_fetch_wait_time;
159     }
160 
161     /// @brief Returns configured config-fetch-wait-time value.
162     ///
163     /// This value specifies the time period in seconds between the
164     /// attempts to fetch the server configuration updates via the
165     /// configuration backends. The value of 0 means that the
166     /// mechanism to periodically fetch the configuration updates
167     /// is disabled.
168     ///
169     /// @return Time period between the subsequent attempts to
170     /// fetch server configuration updates in seconds.
getConfigFetchWaitTime()171     const util::Optional<uint16_t>& getConfigFetchWaitTime() const {
172         return (config_fetch_wait_time_);
173     }
174 
175     /// @brief Sets configuration database access string.
176     ///
177     /// @param access_str database access string.
178     ///
179     /// @throw BadValue if an entry exists that matches the parameters
180     /// in the given access string, or if the access string is invalid.
181     void addConfigDatabase(const std::string& access_str);
182 
183     /// @brief Retrieves the list of databases.
184     ///
185     /// The entries in the list are stored in the order they were
186     /// added to it (FIFO).
187     ///
188     /// @return a reference to a const list of databases.
getConfigDatabases()189     const ConfigDbInfoList& getConfigDatabases() const {
190         return (db_infos_);
191     }
192 
193     /// @brief Retrieves the database with the given access parameter value.
194     ///
195     /// @return A reference to the matching database or the not-found value
196     /// available via @c EMPTY_DB().
197     const ConfigDbInfo& findConfigDb(const std::string& param_name,
198                                      const std::string& param_value);
199 
200     /// @brief Empties the contents of the class, including the database list.
201     void clear();
202 
203     /// @brief Merges specified configuration into this configuration.
204     ///
205     /// If the other configuration is non-empty it completely replaces
206     /// this configuration.
207     ///
208     /// @param other the other configuration to be merged into this
209     /// configuration.
210     void merge(const ConfigControlInfo& other);
211 
212     /// @brief Unparse a configuration object.
213     ///
214     /// @return a pointer to unparsed configuration.
215     virtual isc::data::ElementPtr toElement() const;
216 
217     /// @brief Fetches the not-found value returned by database list searches.
218     ///
219     /// @return a reference to the empty ConfigDBInfo.
220     static const ConfigDbInfo& EMPTY_DB();
221 
222     /// @brief Compares two objects for equality.
223     ///
224     /// @param other An object to be compared with this object.
225     ///
226     /// @return true if objects are equal, false otherwise.
227     bool equals(const ConfigControlInfo& other) const;
228 
229 private:
230 
231     /// @brief Configured value of the config-fetch-wait-time.
232     util::Optional<uint16_t> config_fetch_wait_time_;
233 
234     /// @brief List of configuration databases.
235     ConfigDbInfoList db_infos_;
236 };
237 
238 /// @brief Defines a pointer to a ConfigControlInfo.
239 typedef boost::shared_ptr<ConfigControlInfo> ConfigControlInfoPtr;
240 
241 /// @brief Defines a pointer to a const ConfigControlInfo.
242 typedef boost::shared_ptr<const ConfigControlInfo> ConstConfigControlInfoPtr;
243 
244 } // namespace process
245 } // end namespace isc
246 
247 #endif // PROCESS_CONFIG_CTL_INFO_H
248