Kea  2.5.3
lease_mgr_factory.cc
Go to the documentation of this file.
1 // Copyright (C) 2012-2023 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 
9 #include <dhcpsrv/dhcpsrv_log.h>
12 #ifdef HAVE_MYSQL
14 #endif
15 #ifdef HAVE_PGSQL
17 #endif
18 
19 #include <boost/algorithm/string.hpp>
20 #include <boost/foreach.hpp>
21 
22 #include <algorithm>
23 #include <iostream>
24 #include <iterator>
25 #include <map>
26 #include <sstream>
27 #include <utility>
28 
29 using namespace isc::db;
30 using namespace std;
31 
32 namespace isc {
33 namespace dhcp {
34 
35 boost::scoped_ptr<TrackingLeaseMgr>&
36 LeaseMgrFactory::getLeaseMgrPtr() {
37  static boost::scoped_ptr<TrackingLeaseMgr> lease_mgr_ptr;
38  return (lease_mgr_ptr);
39 }
40 
41 void
42 LeaseMgrFactory::create(const std::string& dbaccess) {
43  const std::string type = "type";
44 
45  // Parse the access string and create a redacted string for logging.
47  std::string redacted = DatabaseConnection::redactedAccessString(parameters);
48 
49  // Is "type" present?
50  if (parameters.find(type) == parameters.end()) {
52  isc_throw(InvalidParameter, "Database configuration parameters do not "
53  "contain the 'type' keyword");
54  }
55 
56 
57  // Yes, check what it is.
58  if (parameters[type] == string("mysql")) {
59 #ifdef HAVE_MYSQL
61  getLeaseMgrPtr().reset(new MySqlLeaseMgr(parameters));
62  return;
63 #else
65  isc_throw(InvalidType, "The Kea server has not been compiled with "
66  "support for database type: mysql");
67 #endif
68  }
69 
70  if (parameters[type] == string("postgresql")) {
71 #ifdef HAVE_PGSQL
73  getLeaseMgrPtr().reset(new PgSqlLeaseMgr(parameters));
74  return;
75 #else
76  LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg("postgresql");
77  isc_throw(InvalidType, "The Kea server has not been compiled with "
78  "support for database type: postgresql");
79 #endif
80  }
81  if (parameters[type] == string("memfile")) {
83  getLeaseMgrPtr().reset(new Memfile_LeaseMgr(parameters));
84  return;
85  }
86 
87  // Get here on no match
88  LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg(parameters[type]);
89  isc_throw(InvalidType, "Database access parameter 'type' does "
90  "not specify a supported database backend: " << parameters[type]);
91 }
92 
93 void
94 LeaseMgrFactory::destroy() {
95  // Destroy current lease manager. This is a no-op if no lease manager
96  // is available.
97  if (getLeaseMgrPtr()) {
99  .arg(getLeaseMgrPtr()->getType());
100  }
101  getLeaseMgrPtr().reset();
102 }
103 
104 void
105 LeaseMgrFactory::recreate(const std::string& dbaccess, bool preserve_callbacks) {
107  // Preserve the callbacks if needed.
108  if (preserve_callbacks && haveInstance()) {
109  callbacks = instance().callbacks_;
110  }
111 
112  // Re-create the manager.
113  destroy();
114  create(dbaccess);
115 
116  if (callbacks) {
117  // Copy the callbacks to the new instance. It should be fast
118  // because we merely copy the pointer.
119  instance().callbacks_ = callbacks;
120  }
121 }
122 
123 bool
124 LeaseMgrFactory::haveInstance() {
125  return (getLeaseMgrPtr().get());
126 }
127 
129 LeaseMgrFactory::instance() {
130  TrackingLeaseMgr* lmptr = getLeaseMgrPtr().get();
131  if (lmptr == NULL) {
132  isc_throw(NoLeaseManager, "no current lease manager is available");
133  }
134  return (*lmptr);
135 }
136 
137 } // namespace dhcp
138 } // 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.
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.