Kea 2.7.4
host_data_source_factory.cc
Go to the documentation of this file.
1// Copyright (C) 2015-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#include <config.h>
8
11#include <dhcpsrv/hosts_log.h>
12#include <log/logger_support.h>
13
14#include <boost/algorithm/string.hpp>
15
16#include <algorithm>
17#include <iostream>
18#include <iterator>
19#include <map>
20#include <sstream>
21#include <utility>
22
23using namespace isc::db;
24using namespace std;
25
26namespace isc {
27namespace dhcp {
28
29map<string, pair<HostDataSourceFactory::Factory, HostDataSourceFactory::DBVersion>> HostDataSourceFactory::map_;
30
31void
33 const string& dbaccess) {
34 // Parse the access string and create a redacted string for logging.
37
38 // Get the database type and open the corresponding database.
39 DatabaseConnection::ParameterMap::iterator it = parameters.find("type");
40 if (it == parameters.end()) {
41 isc_throw(InvalidParameter, "Host database configuration does not "
42 "contain the 'type' keyword");
43 }
44
45 string db_type = it->second;
46 auto index = map_.find(db_type);
47
48 // No match?
49 if (index == map_.end()) {
50 if ((db_type == "mysql") || (db_type == "postgresql")) {
51 string with = (db_type == "postgresql" ? "pgsql" : db_type);
52 isc_throw(InvalidType, "The type of host backend: '" << db_type
53 << "' is not compiled in. Did you forget to use --with-"
54 << with << " during compilation or to load libdhcp_"
55 << with << "_hb hook library?");
56 }
57 isc_throw(InvalidType, "The type of host backend: '" <<
58 db_type << "' is not supported");
59 }
60
61 // Call the factory and push the pointer on sources.
62 sources.push_back(index->second.first(parameters));
63
64 // Check the factory did not return null.
65 if (!sources.back()) {
66 sources.pop_back();
67 isc_throw(Unexpected, "Hosts database " << db_type <<
68 " factory returned null");
69 }
70}
71
72bool
74 const string& db_type) {
75 for (auto it = sources.begin(); it != sources.end(); ++it) {
76 if ((*it)->getType() != db_type) {
77 continue;
78 }
80 .arg(db_type);
81 sources.erase(it);
82 return (true);
83 }
84 return (false);
85}
86
87bool
89 const string& db_type,
90 const string& dbaccess,
91 bool if_unusable) {
94 bool deleted = false;
95 if (if_unusable) {
96 deleted = true;
97 }
98
99 for (auto it = sources.begin(); it != sources.end(); ++it) {
100 if ((*it)->getType() != db_type || (*it)->getParameters() != parameters) {
101 continue;
102 }
103 if (if_unusable && (!(*it)->isUnusable())) {
104 deleted = false;
105 continue;
106 }
108 .arg((*it)->getType());
109 sources.erase(it);
110 return (true);
111 }
112 return (deleted);
113}
114
115bool
117 const Factory& factory,
118 bool no_log,
119 DBVersion db_version) {
120 if (map_.count(db_type)) {
121 return (false);
122 }
123
124 static auto default_db_version = []() -> std::string {
125 return (std::string());
126 };
127
128 if (!db_version) {
129 db_version = default_db_version;
130 }
131
132 map_.insert(pair<string, pair<Factory, DBVersion>>(db_type, pair<Factory, DBVersion>(factory, db_version)));
133
134 // We are dealing here with static logger initialization fiasco.
135 // registerFactory may be called from constructors of static global
136 // objects for built in backends. The logging is not initialized yet,
137 // so the LOG_DEBUG would throw.
138 if (!no_log) {
140 .arg(db_type);
141 }
142 return (true);
143}
144
145bool
146HostDataSourceFactory::deregisterFactory(const string& db_type, bool no_log) {
147 auto index = map_.find(db_type);
148 if (index != map_.end()) {
149 map_.erase(index);
150 if (!no_log) {
153 .arg(db_type);
154 }
155 return (true);
156 } else {
157 return (false);
158 }
159}
160
161bool
162HostDataSourceFactory::registeredFactory(const std::string& db_type) {
163 auto index = map_.find(db_type);
164 return (index != map_.end());
165}
166
167void
169 std::stringstream txt;
170
171 for (auto const& x : map_) {
172 if (!txt.str().empty()) {
173 txt << " ";
174 }
175 txt << x.first;
176 }
177
179 .arg(txt.str());
180}
181
182std::list<std::string>
184 std::list<std::string> result;
185 for (auto const& x : map_) {
186 auto version = x.second.second();
187 if (!version.empty()) {
188 result.push_back(version);
189 }
190 }
191
192 return (result);
193}
194
195} // namespace dhcp
196} // namespace isc
int version()
returns Kea hooks version.
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.
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.
static void logRegistered()
Logs out all registered backends.
static bool deregisterFactory(const std::string &db_type, bool no_log=false)
Deregister a host data source factory.
static void add(HostDataSourceList &sources, const std::string &dbaccess)
Create and add an instance of a host data source.
static bool registerFactory(const std::string &db_type, const Factory &factory, bool no_log=false, DBVersion db_version=DBVersion())
Register a host data source factory.
static std::list< std::string > getDBVersions()
Return extended version info for registered backends.
static bool del(HostDataSourceList &sources, const std::string &db_type)
Delete a host data source.
static bool registeredFactory(const std::string &db_type)
Check if a host data source factory was registered.
std::function< std::string()> DBVersion
Type of host mgr version.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Logging initialization functions.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition macros.h:20
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition macros.h:14
isc::log::Logger hosts_logger("hosts")
Logger for the HostMgr and the code it calls.
Definition hosts_log.h:51
const isc::log::MessageID HOSTS_BACKEND_DEREGISTER
std::vector< HostDataSourcePtr > HostDataSourceList
HostDataSource list.
const isc::log::MessageID HOSTS_CFG_CLOSE_HOST_DATA_SOURCE
const isc::log::MessageID HOSTS_BACKEND_REGISTER
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
Definition dhcpsrv_log.h:26
const isc::log::MessageID HOSTS_BACKENDS_REGISTERED
Defines the logger used by the top-level component of kea-lfc.