1 // Copyright (C) 2016-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 CFG_DBACCESS_H
8 #define CFG_DBACCESS_H
9 
10 #include <cc/cfg_to_element.h>
11 #include <database/database_connection.h>
12 
13 #include <boost/shared_ptr.hpp>
14 #include <string>
15 #include <list>
16 
17 namespace isc {
18 namespace dhcp {
19 
20 /// @brief Holds access parameters and the configuration of the
21 /// lease and hosts database connection.
22 ///
23 /// The database access strings use the same format as the strings
24 /// passed to the @ref isc::dhcp::LeaseMgrFactory::create function.
25 class CfgDbAccess {
26 public:
27     /// @brief Constructor.
28     CfgDbAccess();
29 
30     /// @brief Sets parameters which will be appended to the database
31     /// access strings.
32     ///
33     /// @param appended_parameters String holding collection of parameters
34     /// in the following format: "parameter0=value0 parameter1=value1 ...".
setAppendedParameters(const std::string & appended_parameters)35     void setAppendedParameters(const std::string& appended_parameters) {
36         appended_parameters_ = appended_parameters;
37     }
38 
39     /// @brief Retrieves lease database access string.
40     ///
41     /// @return Lease database access string with additional parameters
42     /// specified with @ref CfgDbAccess::setAppendedParameters.
43     std::string getLeaseDbAccessString() const;
44 
45     /// @brief Sets lease database access string.
46     ///
47     /// @param lease_db_access New lease database access string.
setLeaseDbAccessString(const std::string & lease_db_access)48     void setLeaseDbAccessString(const std::string& lease_db_access) {
49         lease_db_access_ = lease_db_access;
50     }
51 
52     /// @brief Retrieves host database access string.
53     ///
54     /// @return Host database access string with additional parameters
55     /// specified with @ref CfgDbAccess::setAppendedParameters.
56     std::string getHostDbAccessString() const;
57 
58     /// @brief Sets host database access string.
59     ///
60     /// @param host_db_access New host database access string.
61     /// @param front Add at front if true, at back if false (default).
62     void setHostDbAccessString(const std::string& host_db_access,
63                                bool front = false) {
64         if (front) {
65             host_db_access_.push_front(host_db_access);
66         } else {
67             host_db_access_.push_back(host_db_access);
68         }
69     }
70 
71     /// @brief Retrieves host database access string.
72     ///
73     /// @return Database access strings with additional parameters
74     /// specified with @ref CfgDbAccess::setAppendedParameters
75     std::list<std::string> getHostDbAccessStringList() const;
76 
77     /// @brief Modifies the setting imposing whether the IP reservations
78     /// are unique or can be non unique.
79     ///
80     /// This flag can be set to @c false when the server is explicitly
81     /// configured to allow multiple hosts to have the same IP reservation
82     /// in a subnet. In that case, the @c createManagers function will
83     /// attempt to use this setting for @c HostMgr.
84     ///
85     /// Note that the @c HostMgr can reject the new setting if any of the
86     /// host backends used does not support specifying multipe hosts with
87     /// the same IP address in a subnet.
88     ///
89     /// @param unique new setting to be used by @c HostMgr.
setIPReservationsUnique(const bool unique)90     void setIPReservationsUnique(const bool unique) {
91         ip_reservations_unique_ = unique;
92     }
93 
94     /// @brief Returns the setting indicating if the IP reservations are
95     /// unique or can be non unique.
96     ///
97     /// @return true if the IP reservations must be unique or false if
98     /// the reservations can be non unique.
getIPReservationsUnique()99     bool getIPReservationsUnique() const {
100         return (ip_reservations_unique_);
101     }
102 
103     /// @brief Creates instance of lease manager and host data sources
104     /// according to the configuration specified.
105     void createManagers() const;
106 
107 protected:
108 
109     /// @brief Returns lease or host database access string.
110     ///
111     /// @param access_string without additional (appended) parameters.
112     std::string getAccessString(const std::string& access_string) const;
113 
114     /// @brief Parameters to be appended to the database access
115     /// strings.
116     std::string appended_parameters_;
117 
118     /// @brief Holds lease database access string.
119     std::string lease_db_access_;
120 
121     /// @brief Holds host database access strings.
122     std::list<std::string> host_db_access_;
123 
124     /// @brief Holds the setting whether IP reservations should be unique
125     /// or can be non-unique.
126     bool ip_reservations_unique_;
127 };
128 
129 /// @brief A pointer to the @c CfgDbAccess.
130 typedef boost::shared_ptr<CfgDbAccess> CfgDbAccessPtr;
131 
132 /// @brief A pointer to the const @c CfgDbAccess.
133 typedef boost::shared_ptr<const CfgDbAccess> ConstCfgDbAccessPtr;
134 
135 /// @brief utility class for unparsing
136 struct CfgLeaseDbAccess : public CfgDbAccess, public isc::data::CfgToElement {
137     /// @brief Constructor
CfgLeaseDbAccessCfgLeaseDbAccess138     CfgLeaseDbAccess(const CfgDbAccess& super) : CfgDbAccess(super) { }
139 
140     /// @brief Unparse
141     ///
142     /// @ref isc::data::CfgToElement::toElement
143     ///
144     /// @result a pointer to a configuration
toElementCfgLeaseDbAccess145     virtual isc::data::ElementPtr toElement() const {
146         return (db::DatabaseConnection::toElementDbAccessString(lease_db_access_));
147     }
148 };
149 
150 struct CfgHostDbAccess : public CfgDbAccess, public isc::data::CfgToElement {
151     /// @brief Constructor
CfgHostDbAccessCfgHostDbAccess152     CfgHostDbAccess(const CfgDbAccess& super) : CfgDbAccess(super) { }
153 
154     /// @brief Unparse
155     ///
156     /// @ref isc::data::CfgToElement::toElement
157     ///
158     /// @result a pointer to a configuration
toElementCfgHostDbAccess159     virtual isc::data::ElementPtr toElement() const {
160         isc::data::ElementPtr result = isc::data::Element::createList();
161         for (const std::string& dbaccess : host_db_access_) {
162             isc::data::ElementPtr entry =
163                 db::DatabaseConnection::toElementDbAccessString(dbaccess);
164             if (entry->size() > 0) {
165                 result->add(entry);
166             }
167         }
168         return (result);
169     }
170 };
171 
172 }
173 }
174 
175 #endif // CFG_DBACCESS_H
176