Kea: base_config_backend_mgr.h Source File

| Kea 3.2.0-git

base_config_backend_mgr.h
Go to the documentation of this file.
1// Copyright (C) 2018-2025 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
10#include <config_backend/base_config_backend.h>
11#include <database/database_connection.h>
12#include <database/backend_selector.h>
13#include <database/db_log.h>
14#include <database/db_messages.h>
15#include <exceptions/exceptions.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>
61 class BaseConfigBackendMgr {
62public:
63
65 typedef boost::shared_ptr<ConfigBackendPoolType> ConfigBackendPoolPtr;
66
71 typedef std::function<typename ConfigBackendPoolType::ConfigBackendTypePtr
72 (const db::DatabaseConnection::ParameterMap&)> Factory;
73
75 BaseConfigBackendMgr()
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.
152 db::DatabaseConnection::ParameterMap parameters =
153 db::DatabaseConnection::parse(dbaccess);
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 libdhcp(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 -D "
172 << db_type << "=enabled during setup or to load libdhcp_"
173 << libdhcp << " 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
191 void delAllBackends() {
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
214 ConfigBackendPoolPtr getPool() const {
215 return (pool_);
216 }
217
223 void logRegistered() {
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
233 LOG_INFO(isc::db::database_logger, isc::db::CONFIG_BACKENDS_REGISTERED)
234 .arg(txt.str());
235 }
236
237protected:
238
240 std::map<std::string, Factory> factories_;
241
243 ConfigBackendPoolPtr pool_;
244};
245
246} // end of namespace isc::cb
247} // end of namespace isc
248
249#endif // BASE_CONFIG_BACKEND_MGR_H
backend_selector.h
base_config_backend.h
isc::InvalidParameter
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
Definition exceptions/exceptions.h:124
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition exceptions/exceptions.h:153
isc::cb::BaseConfigBackendMgr::delBackend
bool delBackend(const std::string &db_type, const std::string &dbaccess, bool if_unusable)
Delete a config backend manager.
Definition base_config_backend_mgr.h:208
isc::cb::BaseConfigBackendMgr::registerBackendFactory
bool registerBackendFactory(const std::string &db_type, const Factory &factory)
Registers new backend factory function for a given backend type.
Definition base_config_backend_mgr.h:98
isc::cb::BaseConfigBackendMgr::addBackend
void addBackend(const std::string &dbaccess)
Create an instance of a configuration backend.
Definition base_config_backend_mgr.h:150
isc::cb::BaseConfigBackendMgr::BaseConfigBackendMgr
BaseConfigBackendMgr()
Constructor.
Definition base_config_backend_mgr.h:75
isc::cb::BaseConfigBackendMgr::getPool
ConfigBackendPoolPtr getPool() const
Returns underlying config backend pool.
Definition base_config_backend_mgr.h:214
isc::cb::BaseConfigBackendMgr::ConfigBackendPoolPtr
boost::shared_ptr< ConfigBackendPoolType > ConfigBackendPoolPtr
Pointer to the configuration backend pool.
Definition base_config_backend_mgr.h:65
isc::cb::BaseConfigBackendMgr::Factory
std::function< typename ConfigBackendPoolType::ConfigBackendTypePtr(const db::DatabaseConnection::ParameterMap &)> Factory
Type of the backend factory function.
Definition base_config_backend_mgr.h:72
isc::cb::BaseConfigBackendMgr::delAllBackends
void delAllBackends()
Removes all backends from the pool.
Definition base_config_backend_mgr.h:191
isc::cb::BaseConfigBackendMgr::logRegistered
void logRegistered()
Logs out all registered backends.
Definition base_config_backend_mgr.h:223
isc::cb::BaseConfigBackendMgr::factories_
std::map< std::string, Factory > factories_
A map holding registered backend factory functions.
Definition base_config_backend_mgr.h:240
isc::cb::BaseConfigBackendMgr::unregisterBackendFactory
bool unregisterBackendFactory(const std::string &db_type)
Unregisters the backend factory function for a given backend type.
Definition base_config_backend_mgr.h:121
isc::cb::BaseConfigBackendMgr::pool_
ConfigBackendPoolPtr pool_
Pointer to the configuration backends pool.
Definition base_config_backend_mgr.h:243
isc::db::DatabaseConnection::parse
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
Definition database_connection.cc:41
isc::db::DatabaseConnection::ParameterMap
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Definition database_connection.h:137
isc::db::InvalidType
Invalid type exception.
Definition database_connection.h:62
database_connection.h
db_log.h
We want to reuse the database backend connection and exchange code for other uses,...
db_messages.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
LOG_INFO
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
isc::cb
Definition base_config_backend.h:18
isc::db::database_logger
isc::log::Logger database_logger("database")
Common database library logger.
Definition db_log.h:46
isc::db::CONFIG_BACKENDS_REGISTERED
const isc::log::MessageID CONFIG_BACKENDS_REGISTERED
Definition db_messages.h:11
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18