Kea  1.5.0
lease_mgr_factory.cc
Go to the documentation of this file.
1 // Copyright (C) 2012-2018 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()) {
54  LOG_ERROR(dhcpsrv_logger, DHCPSRV_NOTYPE_DB).arg(dbaccess);
55  isc_throw(InvalidParameter, "Database configuration parameters do not "
56  "contain the 'type' keyword");
57  }
58 
59 
60  // Yes, check what it is.
61 #ifdef HAVE_MYSQL
62  if (parameters[type] == string("mysql")) {
63  LOG_INFO(dhcpsrv_logger, DHCPSRV_MYSQL_DB).arg(redacted);
64  getLeaseMgrPtr().reset(new MySqlLeaseMgr(parameters));
65  return;
66  }
67 #endif
68 #ifdef HAVE_PGSQL
69  if (parameters[type] == string("postgresql")) {
70  LOG_INFO(dhcpsrv_logger, DHCPSRV_PGSQL_DB).arg(redacted);
71  getLeaseMgrPtr().reset(new PgSqlLeaseMgr(parameters));
72  return;
73  }
74 #endif
75 #ifdef HAVE_CQL
76  if (parameters[type] == string("cql")) {
77  LOG_INFO(dhcpsrv_logger, DHCPSRV_CQL_DB).arg(redacted);
78  getLeaseMgrPtr().reset(new CqlLeaseMgr(parameters));
79  return;
80  }
81 #endif
82  if (parameters[type] == string("memfile")) {
83  LOG_INFO(dhcpsrv_logger, DHCPSRV_MEMFILE_DB).arg(redacted);
84  getLeaseMgrPtr().reset(new Memfile_LeaseMgr(parameters));
85  return;
86  }
87 
88  // Get here on no match
89  LOG_ERROR(dhcpsrv_logger, DHCPSRV_UNKNOWN_DB).arg(parameters[type]);
90  isc_throw(InvalidType, "Database access parameter 'type' does "
91  "not specify a supported database backend:" << parameters[type]);
92 }
93 
94 void
95 LeaseMgrFactory::destroy() {
96  // Destroy current lease manager. This is a no-op if no lease manager
97  // is available.
98  if (getLeaseMgrPtr()) {
99  LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CLOSE_DB)
100  .arg(getLeaseMgrPtr()->getType());
101  }
102  getLeaseMgrPtr().reset();
103 }
104 
105 bool
106 LeaseMgrFactory::haveInstance() {
107  return (getLeaseMgrPtr().get());
108 }
109 
110 LeaseMgr&
111 LeaseMgrFactory::instance() {
112  LeaseMgr* lmptr = getLeaseMgrPtr().get();
113  if (lmptr == NULL) {
114  isc_throw(NoLeaseManager, "no current lease manager is available");
115  }
116  return (*lmptr);
117 }
118 
119 }; // namespace dhcp
120 }; // namespace isc
isc::dhcp::PgSqlLeaseMgr
PostgreSQL Lease Manager.
Definition: pgsql_lease_mgr.h:34
isc::db::InvalidType
Invalid type exception.
Definition: database_connection.h:45
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
pgsql_lease_mgr.h
isc::db
Definition: cql_connection.cc:29
isc::dhcp::LeaseMgr
Abstract Lease Manager.
Definition: lease_mgr.h:222
isc::db::DatabaseConnection::redactedAccessString
static std::string redactedAccessString(const ParameterMap &parameters)
Redact database access string.
Definition: database_connection.cc:66
lease_mgr_factory.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::dhcp::NoLeaseManager
No lease manager exception.
Definition: lease_mgr_factory.h:26
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
LOG_DEBUG
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
isc::InvalidParameter
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
Definition: exceptions/exceptions.h:124
dhcpsrv_log.h
isc::dhcp::MySqlLeaseMgr
MySQL Lease Manager.
Definition: mysql_lease_mgr.h:35
isc::dhcp::DHCPSRV_DBG_TRACE
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
Definition: dhcpsrv_log.h:26
memfile_lease_mgr.h
isc::db::DatabaseConnection::parse
static ParameterMap parse(const std::string &dbaccess)
Parse database access string.
Definition: database_connection.cc:37
isc::dhcp::CqlLeaseMgr
Cassandra Lease Manager.
Definition: cql_lease_mgr.h:42
isc::dhcp::Memfile_LeaseMgr
Concrete implementation of a lease database backend using flat file.
Definition: memfile_lease_mgr.h:77
mysql_lease_mgr.h
cql_lease_mgr.h
isc::db::DatabaseConnection::ParameterMap
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Definition: database_connection.h:152
LOG_INFO
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
isc::dhcp::dhcpsrv_logger
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
Definition: dhcpsrv_log.h:56