Kea 2.5.8
lease_mgr_factory.cc
Go to the documentation of this file.
1// Copyright (C) 2012-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
12#ifdef HAVE_MYSQL
14#endif
15#ifdef HAVE_PGSQL
17#endif
18
19#include <boost/algorithm/string.hpp>
20
21#include <algorithm>
22#include <iostream>
23#include <iterator>
24#include <map>
25#include <sstream>
26#include <utility>
27
28using namespace isc::db;
29using namespace std;
30
31namespace isc {
32namespace dhcp {
33
34boost::scoped_ptr<TrackingLeaseMgr>&
35LeaseMgrFactory::getLeaseMgrPtr() {
36 static boost::scoped_ptr<TrackingLeaseMgr> lease_mgr_ptr;
37 return (lease_mgr_ptr);
38}
39
40void
41LeaseMgrFactory::create(const std::string& dbaccess) {
42 const std::string type = "type";
43
44 // Parse the access string and create a redacted string for logging.
46 std::string redacted = DatabaseConnection::redactedAccessString(parameters);
47
48 // Is "type" present?
49 if (parameters.find(type) == parameters.end()) {
51 isc_throw(InvalidParameter, "Database configuration parameters do not "
52 "contain the 'type' keyword");
53 }
54
55
56 // Yes, check what it is.
57 if (parameters[type] == string("mysql")) {
58#ifdef HAVE_MYSQL
60 getLeaseMgrPtr().reset(new MySqlLeaseMgr(parameters));
61 return;
62#else
64 isc_throw(InvalidType, "The Kea server has not been compiled with "
65 "support for database type: mysql");
66#endif
67 }
68
69 if (parameters[type] == string("postgresql")) {
70#ifdef HAVE_PGSQL
72 getLeaseMgrPtr().reset(new PgSqlLeaseMgr(parameters));
73 return;
74#else
76 isc_throw(InvalidType, "The Kea server has not been compiled with "
77 "support for database type: postgresql");
78#endif
79 }
80 if (parameters[type] == string("memfile")) {
82 getLeaseMgrPtr().reset(new Memfile_LeaseMgr(parameters));
83 return;
84 }
85
86 // Get here on no match
87 LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg(parameters[type]);
88 isc_throw(InvalidType, "Database access parameter 'type' does "
89 "not specify a supported database backend: " << parameters[type]);
90}
91
92void
94 // Destroy current lease manager. This is a no-op if no lease manager
95 // is available.
96 if (getLeaseMgrPtr()) {
98 .arg(getLeaseMgrPtr()->getType());
99 }
100 getLeaseMgrPtr().reset();
101}
102
103void
104LeaseMgrFactory::recreate(const std::string& dbaccess, bool preserve_callbacks) {
106 // Preserve the callbacks if needed.
107 if (preserve_callbacks && haveInstance()) {
108 callbacks = instance().callbacks_;
109 }
110
111 // Re-create the manager.
112 destroy();
113 create(dbaccess);
114
115 if (callbacks) {
116 // Copy the callbacks to the new instance. It should be fast
117 // because we merely copy the pointer.
118 instance().callbacks_ = callbacks;
119 }
120}
121
122bool
124 return (getLeaseMgrPtr().get());
125}
126
129 TrackingLeaseMgr* lmptr = getLeaseMgrPtr().get();
130 if (lmptr == NULL) {
131 isc_throw(NoLeaseManager, "no current lease manager is available");
132 }
133 return (*lmptr);
134}
135
136} // namespace dhcp
137} // namespace isc
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
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 create(const std::string &dbaccess)
Create an instance of a lease manager.
static TrackingLeaseMgr & instance()
Return current lease manager.
static void destroy()
Destroy lease manager.
static bool haveInstance()
Indicates if the lease manager has been instantiated.
static void recreate(const std::string &dbaccess, bool preserve_callbacks=true)
Recreate an instance of a lease manager with optionally preserving registered callbacks.
Concrete implementation of a lease database backend using flat file.
MySQL Lease Manager.
No lease manager exception.
PostgreSQL Lease Manager.
Introduces callbacks into the LeaseMgr.
boost::shared_ptr< CallbackContainer > CallbackContainerPtr
Pointer to the callback container.
CallbackContainerPtr callbacks_
The multi-index container holding registered callbacks.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
#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 dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
const isc::log::MessageID DHCPSRV_MEMFILE_DB
const isc::log::MessageID DHCPSRV_PGSQL_DB
const isc::log::MessageID DHCPSRV_CLOSE_DB
const isc::log::MessageID DHCPSRV_UNKNOWN_DB
const isc::log::MessageID DHCPSRV_NOTYPE_DB
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
Definition: dhcpsrv_log.h:26
const isc::log::MessageID DHCPSRV_MYSQL_DB
Defines the logger used by the top-level component of kea-lfc.