Kea  1.9.9-git
lease_mgr_factory.cc
Go to the documentation of this file.
1 // Copyright (C) 2012-2020 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 #ifdef HAVE_CQL
19 #include <dhcpsrv/cql_lease_mgr.h>
20 #endif
21 
22 #include <boost/algorithm/string.hpp>
23 #include <boost/foreach.hpp>
24 
25 #include <algorithm>
26 #include <iostream>
27 #include <iterator>
28 #include <map>
29 #include <sstream>
30 #include <utility>
31 
32 using namespace isc::db;
33 using namespace std;
34 
35 namespace isc {
36 namespace dhcp {
37 
38 boost::scoped_ptr<LeaseMgr>&
39 LeaseMgrFactory::getLeaseMgrPtr() {
40  static boost::scoped_ptr<LeaseMgr> leaseMgrPtr;
41  return (leaseMgrPtr);
42 }
43 
44 void
45 LeaseMgrFactory::create(const std::string& dbaccess) {
46  const std::string type = "type";
47 
48  // Parse the access string and create a redacted string for logging.
50  std::string redacted = DatabaseConnection::redactedAccessString(parameters);
51 
52  // Is "type" present?
53  if (parameters.find(type) == parameters.end()) {
55  isc_throw(InvalidParameter, "Database configuration parameters do not "
56  "contain the 'type' keyword");
57  }
58 
59 
60  // Yes, check what it is.
61  if (parameters[type] == string("mysql")) {
62 #ifdef HAVE_MYSQL
64  getLeaseMgrPtr().reset(new MySqlLeaseMgr(parameters));
65  return;
66 #else
68  isc_throw(InvalidType, "The Kea server has not been compiled with "
69  "support for database type: mysql");
70 #endif
71  }
72 
73  if (parameters[type] == string("postgresql")) {
74 #ifdef HAVE_PGSQL
76  getLeaseMgrPtr().reset(new PgSqlLeaseMgr(parameters));
77  return;
78 #else
79  LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg("postgresql");
80  isc_throw(InvalidType, "The Kea server has not been compiled with "
81  "support for database type: postgresql");
82 #endif
83  }
84  if (parameters[type] == string("cql")) {
85 #ifdef HAVE_CQL
86  LOG_INFO(dhcpsrv_logger, DHCPSRV_CQL_DB).arg(redacted);
87  getLeaseMgrPtr().reset(new CqlLeaseMgr(parameters));
88  return;
89 #else
91  isc_throw(InvalidType, "The Kea server has not been compiled with "
92  "support for database type: cql");
93 #endif
94  }
95  if (parameters[type] == string("memfile")) {
97  getLeaseMgrPtr().reset(new Memfile_LeaseMgr(parameters));
98  return;
99  }
100 
101  // Get here on no match
102  LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg(parameters[type]);
103  isc_throw(InvalidType, "Database access parameter 'type' does "
104  "not specify a supported database backend: " << parameters[type]);
105 }
106 
107 void
108 LeaseMgrFactory::destroy() {
109  // Destroy current lease manager. This is a no-op if no lease manager
110  // is available.
111  if (getLeaseMgrPtr()) {
113  .arg(getLeaseMgrPtr()->getType());
114  }
115  getLeaseMgrPtr().reset();
116 }
117 
118 bool
119 LeaseMgrFactory::haveInstance() {
120  return (getLeaseMgrPtr().get());
121 }
122 
123 LeaseMgr&
124 LeaseMgrFactory::instance() {
125  LeaseMgr* lmptr = getLeaseMgrPtr().get();
126  if (lmptr == NULL) {
127  isc_throw(NoLeaseManager, "no current lease manager is available");
128  }
129  return (*lmptr);
130 }
131 
132 } // namespace dhcp
133 } // namespace isc
const isc::log::MessageID DHCPSRV_MYSQL_DB
const isc::log::MessageID DHCPSRV_PGSQL_DB
Concrete implementation of a lease database backend using flat file.
Abstract Lease Manager.
Definition: lease_mgr.h:222
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
PostgreSQL Lease Manager.
const isc::log::MessageID DHCPSRV_CQL_DB
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
STL namespace.
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
Invalid type exception.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const isc::log::MessageID DHCPSRV_MEMFILE_DB
MySQL Lease Manager.
const isc::log::MessageID DHCPSRV_CLOSE_DB
Defines the logger used by the top-level component of kea-dhcp-ddns.
No lease manager exception.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
Definition: dhcpsrv_log.h:26
const isc::log::MessageID DHCPSRV_UNKNOWN_DB
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Cassandra Lease Manager.
Definition: cql_lease_mgr.h:42
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
const isc::log::MessageID DHCPSRV_NOTYPE_DB