libkea_dhcpsrv_la_SOURCES += d2_client_cfg.cc d2_client_cfg.h
libkea_dhcpsrv_la_SOURCES += d2_client_mgr.cc d2_client_mgr.h
libkea_dhcpsrv_la_SOURCES += daemon.cc daemon.h
-libkea_dhcpsrv_la_SOURCES += data_source.cc data_source.h
+libkea_dhcpsrv_la_SOURCES += database_connection.cc database_connection.h
libkea_dhcpsrv_la_SOURCES += dhcpsrv_log.cc dhcpsrv_log.h
libkea_dhcpsrv_la_SOURCES += host.cc host.h
libkea_dhcpsrv_la_SOURCES += host_container.h
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
-#include <dhcpsrv/data_source.h>
+#include <dhcpsrv/database_connection.h>
#include <dhcpsrv/dhcpsrv_log.h>
#include <exceptions/exceptions.h>
namespace isc {
namespace dhcp {
-std::string DataSource::getParameter(const std::string& name) const {
+std::string DatabaseConnection::getParameter(const std::string& name) const {
ParameterMap::const_iterator param = parameters_.find(name);
if (param == parameters_.end()) {
- isc_throw(BadValue, "Parameter not found");
+ isc_throw(BadValue, "Parameter " << name << " not found");
}
return (param->second);
}
-DataSource::ParameterMap
-DataSource::parse(const std::string& dbaccess) {
- DataSource::ParameterMap mapped_tokens;
+DatabaseConnection::ParameterMap
+DatabaseConnection::parse(const std::string& dbaccess) {
+ DatabaseConnection::ParameterMap mapped_tokens;
if (!dbaccess.empty()) {
vector<string> tokens;
}
std::string
-DataSource::redactedAccessString(const DataSource::ParameterMap& parameters) {
+DatabaseConnection::redactedAccessString(const ParameterMap& parameters) {
// Reconstruct the access string: start of with an empty string, then
// work through all the parameters in the original string and add them.
std::string access;
- for (DataSource::ParameterMap::const_iterator i = parameters.begin();
+ for (DatabaseConnection::ParameterMap::const_iterator i = parameters.begin();
i != parameters.end(); ++i) {
// Separate second and subsequent tokens are preceded by a space.
///
/// This class provides functions that are common for establishing
/// connection with different types of databases; enables operations
-/// on access parameters strings.
-class DataSource : public boost::noncopyable {
+/// on access parameters strings. In particular, it provides a way
+/// to parse parameters in key=value format. This class is expected
+/// to be a base class for all LeaseMgr and possibly HostDataSource
+/// derived classes.
+class DatabaseConnection : public boost::noncopyable {
public:
/// Database configuration parameter map
///
/// @param parameters A data structure relating keywords and values
/// concerned with the database.
- DataSource(const ParameterMap& parameters)
+ DatabaseConnection(const ParameterMap& parameters)
:parameters_(parameters) {
}
/// @param dbaccess Database access string.
///
/// @return std::map<std::string, std::string> Map of keyword/value pairs.
- static DataSource::ParameterMap parse(const std::string& dbaccess);
+ static DatabaseConnection::ParameterMap parse(const std::string& dbaccess);
/// @brief Redact database access string
///
///
/// @return Redacted database access string.
static std::string redactedAccessString(
- const DataSource::ParameterMap& parameters);
+ const DatabaseConnection::ParameterMap& parameters);
protected:
const std::string type = "type";
// Parse the access string and create a redacted string for logging.
- DataSource::ParameterMap parameters = DataSource::parse(dbaccess);
- std::string redacted = DataSource::redactedAccessString(parameters);
+ DatabaseConnection::ParameterMap parameters = DatabaseConnection::parse(dbaccess);
+ std::string redacted = DatabaseConnection::redactedAccessString(parameters);
// Is "type" present?
if (parameters.find(type) == parameters.end()) {
#define LEASE_MGR_FACTORY_H
#include <dhcpsrv/lease_mgr.h>
-#include <dhcpsrv/data_source.h>
+#include <dhcpsrv/database_connection.h>
#include <exceptions/exceptions.h>
#include <string>
return (process_->getExitStatus(pid_));
}
-
Memfile_LeaseMgr::Memfile_LeaseMgr(const ParameterMap& parameters)
- : DataSource(parameters),
+ : DatabaseConnection(parameters),
lfc_setup_(new LFCSetup(boost::bind(&Memfile_LeaseMgr::lfcCallback, this),
*getIOService()))
{
#include <dhcpsrv/csv_lease_file4.h>
#include <dhcpsrv/csv_lease_file6.h>
#include <dhcpsrv/memfile_lease_storage.h>
-#include <dhcpsrv/data_source.h>
+#include <dhcpsrv/database_connection.h>
#include <dhcpsrv/lease_mgr.h>
#include <util/process_spawn.h>
/// is not specified, the default location in the installation
/// directory is used: var/kea/kea-leases4.csv and
/// var/kea/kea-leases6.csv.
-class Memfile_LeaseMgr : public LeaseMgr, public DataSource {
+class Memfile_LeaseMgr : public LeaseMgr, public DatabaseConnection {
public:
/// @defgroup versions Specified memfile backend version.
MySqlConnection::prepareStatement(uint32_t index, const char* text) {
// Validate that there is space for the statement in the statements array
// and that nothing has been placed there before.
- if ((index >= this->statements_.size()) || (this->statements_[index] != NULL)) {
+ if ((index >= statements_.size()) || (statements_[index] != NULL)) {
isc_throw(InvalidParameter, "invalid prepared statement index (" <<
static_cast<int>(index) << ") or indexed prepared " <<
"statement is not null");
}
// All OK, so prepare the statement
- this->text_statements_[index] = std::string(text);
- this->statements_[index] = mysql_stmt_init(mysql_);
- if (this->statements_[index] == NULL) {
+ text_statements_[index] = std::string(text);
+ statements_[index] = mysql_stmt_init(mysql_);
+ if (statements_[index] == NULL) {
isc_throw(DbOperationError, "unable to allocate MySQL prepared "
"statement structure, reason: " << mysql_error(mysql_));
}
- int status = mysql_stmt_prepare(this->statements_[index], text, strlen(text));
+ int status = mysql_stmt_prepare(statements_[index], text, strlen(text));
if (status != 0) {
isc_throw(DbOperationError, "unable to prepare MySQL statement <" <<
text << ">, reason: " << mysql_error(mysql_));
#include <dhcp/hwaddr.h>
#include <dhcpsrv/lease_mgr.h>
-#include <dhcpsrv/data_source.h>
+#include <dhcpsrv/database_connection.h>
#include <boost/scoped_ptr.hpp>
#include <mysql.h>
/// used by both MySqlLeaseMgr and MySqlHostDataSource. It manages connecting
/// to the database and preparing compiled statements.
-class MySqlConnection : public DataSource {
+class MySqlConnection : public DatabaseConnection {
public:
/// @brief Constructor
///
/// Initialize MySqlConnection object with parameters needed for connection.
MySqlConnection(const ParameterMap& parameters)
- : DataSource(parameters) {
+ : DatabaseConnection(parameters) {
}
/// @brief Destructor
/// @throw DbOpenError Error opening the database
void openDatabase();
+protected:
+
std::vector<MYSQL_STMT*> statements_; ///< Prepared statements
std::vector<std::string> text_statements_; ///< Raw text of statements
-protected:
-
+ /// @brief MySQL connection handle
MySqlHolder mysql_;
-
};
//@}
};
-PgSqlLeaseMgr::PgSqlLeaseMgr(const DataSource::ParameterMap& parameters)
- : LeaseMgr(), DataSource(parameters), exchange4_(new PgSqlLease4Exchange()),
+PgSqlLeaseMgr::PgSqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters)
+ : LeaseMgr(), DatabaseConnection(parameters), exchange4_(new PgSqlLease4Exchange()),
exchange6_(new PgSqlLease6Exchange()), conn_(NULL) {
openDatabase();
prepareStatements();
#include <dhcp/hwaddr.h>
#include <dhcpsrv/lease_mgr.h>
-#include <dhcpsrv/data_source.h>
+#include <dhcpsrv/database_connection.h>
#include <boost/scoped_ptr.hpp>
#include <boost/utility.hpp>
#include <libpq-fe.h>
/// This class provides the \ref isc::dhcp::LeaseMgr interface to the PostgreSQL
/// database. Use of this backend presupposes that a PostgreSQL database is
/// available and that the Kea schema has been created within it.
-class PgSqlLeaseMgr : public LeaseMgr, DataSource {
+class PgSqlLeaseMgr : public LeaseMgr, DatabaseConnection {
public:
/// @brief Constructor
/// @throw isc::dhcp::DbOpenError Error opening the database
/// @throw isc::dhcp::DbOperationError An operation on the open database has
/// failed.
- PgSqlLeaseMgr(const DataSource::ParameterMap& parameters);
+ PgSqlLeaseMgr(const DatabaseConnection::ParameterMap& parameters);
/// @brief Destructor (closes database)
virtual ~PgSqlLeaseMgr();
#include <config.h>
#include <exceptions/exceptions.h>
-#include <dhcpsrv/data_source.h>
+#include <dhcpsrv/database_connection.h>
#include <gtest/gtest.h>
using namespace isc::dhcp;
///
/// This test checks if the LeaseMgr can be instantiated and that it
/// parses parameters string properly.
-TEST(DataSourceTest, getParameter) {
+TEST(DatabaseConnectionTest, getParameter) {
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap[std::string("param1")] = std::string("value1");
pmap[std::string("param2")] = std::string("value2");
- DataSource datasrc(pmap);
+ DatabaseConnection datasrc(pmap);
EXPECT_EQ("value1", datasrc.getParameter("param1"));
EXPECT_EQ("value2", datasrc.getParameter("param2"));
}
// This test checks that a database access string can be parsed correctly.
-TEST(DataSourceTest, parse) {
+TEST(DatabaseConnectionTest, parse) {
- DataSource::ParameterMap parameters = DataSource::parse(
+ DatabaseConnection::ParameterMap parameters = DatabaseConnection::parse(
"user=me password=forbidden name=kea somethingelse= type=mysql");
EXPECT_EQ(5, parameters.size());
}
// This test checks that an invalid database access string behaves as expected.
-TEST(DataSourceTest, parseInvalid) {
+TEST(DatabaseConnectionTest, parseInvalid) {
// No tokens in the string, so we expect no parameters
std::string invalid = "";
- DataSource::ParameterMap parameters = DataSource::parse(invalid);
+ DatabaseConnection::ParameterMap parameters = DatabaseConnection::parse(invalid);
EXPECT_EQ(0, parameters.size());
// With spaces, there are some tokens so we expect invalid parameter
// as there are no equals signs.
invalid = " \t ";
- EXPECT_THROW(DataSource::parse(invalid), isc::InvalidParameter);
+ EXPECT_THROW(DatabaseConnection::parse(invalid), isc::InvalidParameter);
invalid = " noequalshere ";
- EXPECT_THROW(DataSource::parse(invalid), isc::InvalidParameter);
+ EXPECT_THROW(DatabaseConnection::parse(invalid), isc::InvalidParameter);
// A single "=" is valid string, but is placed here as the result is
// expected to be nothing.
invalid = "=";
- parameters = DataSource::parse(invalid);
+ parameters = DatabaseConnection::parse(invalid);
EXPECT_EQ(1, parameters.size());
EXPECT_EQ("", parameters[""]);
}
///
/// Checks that the redacted configuration string includes the password only
/// as a set of asterisks.
-TEST(DataSourceTest, redactAccessString) {
+TEST(DatabaseConnectionTest, redactAccessString) {
- DataSource::ParameterMap parameters =
- DataSource::parse("user=me password=forbidden name=kea type=mysql");
+ DatabaseConnection::ParameterMap parameters =
+ DatabaseConnection::parse("user=me password=forbidden name=kea type=mysql");
EXPECT_EQ(4, parameters.size());
EXPECT_EQ("me", parameters["user"]);
EXPECT_EQ("forbidden", parameters["password"]);
// Redact the result. To check, break the redacted string down into its
// components.
- std::string redacted = DataSource::redactedAccessString(parameters);
- parameters = DataSource::parse(redacted);
+ std::string redacted = DatabaseConnection::redactedAccessString(parameters);
+ parameters = DatabaseConnection::parse(redacted);
EXPECT_EQ(4, parameters.size());
EXPECT_EQ("me", parameters["user"]);
///
/// Checks that the redacted configuration string includes the password only
/// as a set of asterisks, even if the password is null.
-TEST(DataSourceTest, redactAccessStringEmptyPassword) {
+TEST(DatabaseConnectionTest, redactAccessStringEmptyPassword) {
- DataSource::ParameterMap parameters =
- DataSource::parse("user=me name=kea type=mysql password=");
+ DatabaseConnection::ParameterMap parameters =
+ DatabaseConnection::parse("user=me name=kea type=mysql password=");
EXPECT_EQ(4, parameters.size());
EXPECT_EQ("me", parameters["user"]);
EXPECT_EQ("", parameters["password"]);
// Redact the result. To check, break the redacted string down into its
// components.
- std::string redacted = DataSource::redactedAccessString(parameters);
- parameters = DataSource::parse(redacted);
+ std::string redacted = DatabaseConnection::redactedAccessString(parameters);
+ parameters = DatabaseConnection::parse(redacted);
EXPECT_EQ(4, parameters.size());
EXPECT_EQ("me", parameters["user"]);
// ... and again to check that the position of the empty password in the
// string does not matter.
- parameters = DataSource::parse("user=me password= name=kea type=mysql");
+ parameters = DatabaseConnection::parse("user=me password= name=kea type=mysql");
EXPECT_EQ(4, parameters.size());
EXPECT_EQ("me", parameters["user"]);
EXPECT_EQ("", parameters["password"]);
EXPECT_EQ("kea", parameters["name"]);
EXPECT_EQ("mysql", parameters["type"]);
- redacted = DataSource::redactedAccessString(parameters);
- parameters = DataSource::parse(redacted);
+ redacted = DatabaseConnection::redactedAccessString(parameters);
+ parameters = DatabaseConnection::parse(redacted);
EXPECT_EQ(4, parameters.size());
EXPECT_EQ("me", parameters["user"]);
///
/// Checks that the redacted configuration string excludes the password if there
/// was no password to begin with.
-TEST(DataSourceTest, redactAccessStringNoPassword) {
+TEST(DatabaseConnectionTest, redactAccessStringNoPassword) {
- DataSource::ParameterMap parameters =
- DataSource::parse("user=me name=kea type=mysql");
+ DatabaseConnection::ParameterMap parameters =
+ DatabaseConnection::parse("user=me name=kea type=mysql");
EXPECT_EQ(3, parameters.size());
EXPECT_EQ("me", parameters["user"]);
EXPECT_EQ("kea", parameters["name"]);
// Redact the result. To check, break the redacted string down into its
// components.
- std::string redacted = DataSource::redactedAccessString(parameters);
- parameters = DataSource::parse(redacted);
+ std::string redacted = DatabaseConnection::redactedAccessString(parameters);
+ parameters = DatabaseConnection::parse(redacted);
EXPECT_EQ(3, parameters.size());
EXPECT_EQ("me", parameters["user"]);
// Check that the keywords and keyword values are the same: loop
// through the keywords in the database access string.
- for (DataSource::ParameterMap::const_iterator actual = parameters.begin();
+ for (DatabaseConnection::ParameterMap::const_iterator actual = parameters.begin();
actual != parameters.end(); ++actual) {
// Does the keyword exist in the set of expected keywords?
#include <config.h>
#include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
#include <dhcpsrv/tests/test_utils.h>
-#include <dhcpsrv/data_source.h>
+#include <dhcpsrv/database_connection.h>
#include <asiolink/io_address.h>
#include <gtest/gtest.h>
#include <sstream>
/// dbconfig is a generic way of passing parameters. Parameters
/// are passed in the "name=value" format, separated by spaces.
/// Values may be enclosed in double quotes, if needed.
- ConcreteLeaseMgr(const DataSource::ParameterMap&)
+ ConcreteLeaseMgr(const DatabaseConnection::ParameterMap&)
: LeaseMgr()
{}
// 1 (return the lease) and more than 1 leases (throw).
TEST_F(LeaseMgrTest, getLease6) {
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
boost::scoped_ptr<ConcreteLeaseMgr> mgr(new ConcreteLeaseMgr(pmap));
vector<Lease6Ptr> leases = createLeases6();
// This test checks if the LeaseMgr can be instantiated and that it
// parses parameters string properly.
TEST_F(MemfileLeaseMgrTest, constructor) {
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["universe"] = "4";
pmap["persist"] = "false";
boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr;
LeaseFileIO io4(getLeaseFilePath("leasefile4_1.csv"));
LeaseFileIO io6(getLeaseFilePath("leasefile6_1.csv"));
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["universe"] = "4";
pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
LeaseFileIO io4(getLeaseFilePath("leasefile4_1.csv"));
LeaseFileIO io6(getLeaseFilePath("leasefile6_1.csv"));
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["universe"] = "4";
// Specify the names of the lease files. Leases will be written.
pmap["name"] = getLeaseFilePath("leasefile4_1.csv");
// Check if it is possible to schedule the timer to perform the Lease
// File Cleanup periodically.
TEST_F(MemfileLeaseMgrTest, lfcTimer) {
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "4";
// Specify the names of the lease files. Leases will be written.
// This test checks if the LFC timer is disabled (doesn't trigger)
// cleanups when the lfc-interval is set to 0.
TEST_F(MemfileLeaseMgrTest, lfcTimerDisabled) {
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "4";
pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
previous_file.writeFile(previous_file_contents);
// Create the backend.
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "4";
pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
previous_file.writeFile(previous_file_contents);
// Create the backend.
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "6";
pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
setenv("KEA_LFC_EXECUTABLE", "foobar", 1);
// Create the backend.
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "4";
pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
finish_file.writeFile(finish_file_contents);
// Create the backend.
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "6";
pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
input_file.writeFile(input_file_contents);
// Create the backend.
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "6";
pmap["name"] = getLeaseFilePath("leasefile6_0.csv");
// at which the IOService must be executed to run the handlers
// for the installed timers.
TEST_F(MemfileLeaseMgrTest, getIOServiceExecInterval) {
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "4";
pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
// lease files if the LFC is in progress.
TEST_F(MemfileLeaseMgrTest, load4LFCInProgress) {
// Create the backend configuration.
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "4";
pmap["name"] = getLeaseFilePath("leasefile4_0.csv");
// lease files if the LFC is in progress.
TEST_F(MemfileLeaseMgrTest, load6LFCInProgress) {
// Create the backend configuration.
- DataSource::ParameterMap pmap;
+ DatabaseConnection::ParameterMap pmap;
pmap["type"] = "memfile";
pmap["universe"] = "6";
pmap["name"] = getLeaseFilePath("leasefile6_0.csv");