Kea 2.7.7
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
13#include <database/db_log.h>
16#include <boost/shared_ptr.hpp>
17#include <functional>
18#include <map>
19#include <string>
20
21namespace isc {
22namespace cb {
23
60template<typename ConfigBackendPoolType>
62public:
63
65 typedef boost::shared_ptr<ConfigBackendPoolType> ConfigBackendPoolPtr;
66
71 typedef std::function<typename ConfigBackendPoolType::ConfigBackendTypePtr
73
76 : factories_(), pool_(new ConfigBackendPoolType()) {
77 }
78
98 bool registerBackendFactory(const std::string& db_type,
99 const Factory& factory) {
100 // Check if this backend has been already registered.
101 if (factories_.count(db_type)) {
102 return (false);
103 }
104
105 // Register the new backend.
106 factories_.insert(std::make_pair(db_type, factory));
107 return (true);
108 }
109
121 bool unregisterBackendFactory(const std::string& db_type) {
122 // Look for it.
123 auto index = factories_.find(db_type);
124
125 // If it's there remove it
126 if (index != factories_.end()) {
127 factories_.erase(index);
128 pool_->delAllBackends(db_type);
129 return (true);
130
131 }
132
133 return (false);
134 }
135
150 void addBackend(const std::string& dbaccess) {
151 // Parse the access string into a map of parameters.
154
155 // Get the database type to locate a factory function.
156 db::DatabaseConnection::ParameterMap::iterator it = parameters.find("type");
157 if (it == parameters.end()) {
158 isc_throw(InvalidParameter, "Config backend specification lacks the "
159 "'type' keyword");
160 }
161
162 std::string db_type = it->second;
163 auto index = factories_.find(db_type);
164
165 // No match?
166 if (index == factories_.end()) {
167 if ((db_type == "mysql") || (db_type == "postgresql")) {
168 std::string with = (db_type == "postgresql" ? "pgsql" : db_type);
169 isc_throw(db::InvalidType, "The Kea server has not been compiled with "
170 "support for configuration database type: " << db_type
171 << ". Did you forget to use --with-"
172 << with << " during compilation or to load libdhcp_"
173 << with << " hook library?");
174 }
175 isc_throw(db::InvalidType, "The type of the configuration backend: '" <<
176 db_type << "' is not supported");
177 }
178
179 // Call the factory and push the pointer on sources.
180 auto backend = index->second(parameters);
181 if (!backend) {
182 isc_throw(Unexpected, "Config database " << db_type <<
183 " factory returned NULL");
184 }
185
186 // Backend instance created successfully.
187 pool_->addBackend(backend);
188 }
189
192 pool_->delAllBackends();
193 }
194
208 bool delBackend(const std::string& db_type, const std::string& dbaccess,
209 bool if_unusable) {
210 return (pool_->del(db_type, dbaccess, if_unusable));
211 }
212
215 return (pool_);
216 }
217
224 std::stringstream txt;
225
226 for (auto const& x : factories_) {
227 if (!txt.str().empty()) {
228 txt << " ";
229 }
230 txt << x.first;
231 }
232
234 .arg(txt.str());
235 }
236
237protected:
238
240 std::map<std::string, Factory> factories_;
241
244};
245
246} // end of namespace isc::cb
247} // end of namespace isc
248
249#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.
std::function< typename ConfigBackendPoolType::ConfigBackendTypePtr(const db::DatabaseConnection::ParameterMap &)> Factory
Type of the backend factory function.
boost::shared_ptr< ConfigBackendPoolType > ConfigBackendPoolPtr
Pointer to the configuration backend pool.
void delAllBackends()
Removes all backends from the pool.
void logRegistered()
Logs out all registered backends.
std::map< std::string, Factory > factories_
A map holding registered backend factory functions.
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.
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
Invalid type exception.
We want to reuse the database backend connection and exchange code for other uses,...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
isc::log::Logger database_logger("database")
Common database library logger.
Definition db_log.h:46
const isc::log::MessageID CONFIG_BACKENDS_REGISTERED
Definition db_messages.h:11
Defines the logger used by the top-level component of kea-lfc.