Kea 2.7.5
base_config_backend_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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 BASE_CONFIG_BACKEND_MGR_H
8#define BASE_CONFIG_BACKEND_MGR_H
9
14#include <boost/shared_ptr.hpp>
15#include <functional>
16#include <map>
17#include <string>
18
19namespace isc {
20namespace cb {
21
58template<typename ConfigBackendPoolType>
60public:
61
63 typedef boost::shared_ptr<ConfigBackendPoolType> ConfigBackendPoolPtr;
64
69 typedef std::function<typename ConfigBackendPoolType::ConfigBackendTypePtr
71
74 : factories_(), pool_(new ConfigBackendPoolType()) {
75 }
76
96 bool registerBackendFactory(const std::string& db_type,
97 const Factory& factory) {
98 // Check if this backend has been already registered.
99 if (factories_.count(db_type)) {
100 return (false);
101 }
102
103 // Register the new backend.
104 factories_.insert(std::make_pair(db_type, factory));
105 return (true);
106 }
107
119 bool unregisterBackendFactory(const std::string& db_type) {
120 // Look for it.
121 auto index = factories_.find(db_type);
122
123 // If it's there remove it
124 if (index != factories_.end()) {
125 factories_.erase(index);
126 pool_->delAllBackends(db_type);
127 return (true);
128
129 }
130
131 return (false);
132 }
133
148 void addBackend(const std::string& dbaccess) {
149 // Parse the access string into a map of parameters.
152
153 // Get the database type to locate a factory function.
154 db::DatabaseConnection::ParameterMap::iterator it = parameters.find("type");
155 if (it == parameters.end()) {
156 isc_throw(InvalidParameter, "Config backend specification lacks the "
157 "'type' keyword");
158 }
159
160 std::string db_type = it->second;
161 auto index = factories_.find(db_type);
162
163 // No match?
164 if (index == factories_.end()) {
165 if ((db_type == "mysql") || (db_type == "postgresql")) {
166 std::string with = (db_type == "postgresql" ? "pgsql" : db_type);
167 isc_throw(db::InvalidType, "The Kea server has not been compiled with "
168 "support for configuration database type: " << db_type
169 << ". Did you forget to use --with-"
170 << with << " during compilation or to load libdhcp_"
171 << with << " hook library?");
172 }
173 isc_throw(db::InvalidType, "The type of the configuration backend: '" <<
174 db_type << "' is not supported");
175 }
176
177 // Call the factory and push the pointer on sources.
178 auto backend = index->second(parameters);
179 if (!backend) {
180 isc_throw(Unexpected, "Config database " << db_type <<
181 " factory returned NULL");
182 }
183
184 // Backend instance created successfully.
185 pool_->addBackend(backend);
186 }
187
190 pool_->delAllBackends();
191 }
192
206 bool delBackend(const std::string& db_type, const std::string& dbaccess,
207 bool if_unusable) {
208 return (pool_->del(db_type, dbaccess, if_unusable));
209 }
210
213 return (pool_);
214 }
215
216protected:
217
219 std::map<std::string, Factory> factories_;
220
223};
224
225} // end of namespace isc::cb
226} // end of namespace isc
227
228#endif // BASE_CONFIG_BACKEND_MGR_H
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
A generic exception that is thrown when an unexpected error condition occurs.
Base class for Configuration Backend Managers (CBM).
bool delBackend(const std::string &db_type, const std::string &dbaccess, bool if_unusable)
Delete a config backend manager.
bool registerBackendFactory(const std::string &db_type, const Factory &factory)
Registers new backend factory function for a given backend type.
void addBackend(const std::string &dbaccess)
Create an instance of a configuration backend.
ConfigBackendPoolPtr getPool() const
Returns underlying config backend pool.
boost::shared_ptr< ConfigBackendPoolType > ConfigBackendPoolPtr
Pointer to the configuration backend pool.
void delAllBackends()
Removes all backends from the pool.
std::map< std::string, Factory > factories_
A map holding registered backend factory functions.
std::function< typename ConfigBackendPoolType::ConfigBackendTypePtr(const db::DatabaseConnection::ParameterMap &) Factory)
Type of the backend factory function.
bool unregisterBackendFactory(const std::string &db_type)
Unregisters the backend factory function for a given backend type.
ConfigBackendPoolPtr pool_
Pointer to the configuration backends pool.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Invalid type exception.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.