Kea 2.7.1
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 // Yes, check what it is.
56 if (parameters[type] == string("mysql")) {
57#ifdef HAVE_MYSQL
59 getLeaseMgrPtr().reset(new MySqlLeaseMgr(parameters));
60 return;
61#else
63 isc_throw(InvalidType, "The Kea server has not been compiled with "
64 "support for database type: mysql");
65#endif
66 }
67
68 if (parameters[type] == string("postgresql")) {
69#ifdef HAVE_PGSQL
71 getLeaseMgrPtr().reset(new PgSqlLeaseMgr(parameters));
72 return;
73#else
75 isc_throw(InvalidType, "The Kea server has not been compiled with "
76 "support for database type: postgresql");
77#endif
78 }
79 if (parameters[type] == string("memfile")) {
81 getLeaseMgrPtr().reset(new Memfile_LeaseMgr(parameters));
82 return;
83 }
84
85 // Get here on no match
86 LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg(parameters[type]);
87 isc_throw(InvalidType, "Database access parameter 'type' does "
88 "not specify a supported database backend: " << parameters[type]);
89}
90
91void
93 // Destroy current lease manager. This is a no-op if no lease manager
94 // is available.
95 if (getLeaseMgrPtr()) {
97 .arg(getLeaseMgrPtr()->getType());
98 }
99 getLeaseMgrPtr().reset();
100}
101
102void
103LeaseMgrFactory::recreate(const std::string& dbaccess, bool preserve_callbacks) {
105 // Preserve the callbacks if needed.
106 if (preserve_callbacks && haveInstance()) {
107 callbacks = instance().callbacks_;
108 }
109
110 // Re-create the manager.
111 destroy();
112 create(dbaccess);
113
114 if (callbacks) {
115 // Copy the callbacks to the new instance. It should be fast
116 // because we merely copy the pointer.
117 instance().callbacks_ = callbacks;
118 }
119}
120
121bool
123 return (getLeaseMgrPtr().get());
124}
125
128 TrackingLeaseMgr* lmptr = getLeaseMgrPtr().get();
129 if (lmptr == NULL) {
130 isc_throw(NoLeaseManager, "no current lease manager is available");
131 }
132 return (*lmptr);
133}
134
135} // namespace dhcp
136} // 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.
#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.