// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
+#include <iostream>
+#include <iomanip>
+#include <string>
#include <config.h>
#include <dhcp/mysql_lease_mgr.h>
+using namespace std;
+
namespace isc {
namespace dhcp {
+void
+MySqlLeaseMgr::openDatabase() {
+
+ // Set up the values of the parameters
+ const char* host = NULL;
+ string shost;
+ try {
+ shost = getParameter("host");
+ host = shost.c_str();
+ } catch (...) {
+ // No host. Fine, we'll use NULL
+ ;
+ }
+
+ const char* user = NULL;
+ string suser;
+ try {
+ suser = getParameter("user");
+ user = suser.c_str();
+ } catch (...) {
+ // No user. Fine, we'll use NULL
+ ;
+ }
+
+ const char* password = NULL;
+ string spassword;
+ try {
+ spassword = getParameter("password");
+ password = spassword.c_str();
+ } catch (...) {
+ // No password. Fine, we'll use NULL
+ ;
+ }
+
+ const char* name = NULL;
+ string sname;
+ try {
+ sname = getParameter("name");
+ name = sname.c_str();
+ } catch (...) {
+ // No database name. Fine, we'll use NULL
+ ;
+ }
+
+ // Open the database. Use defaults for non-specified options.
+ MYSQL* status = mysql_real_connect(mysql_, host, user, password, name,
+ 0, NULL, 0);
+ if (status != mysql_) {
+ isc_throw(DbOpenError, mysql_error(mysql_));
+ }
+}
+
+
MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters)
- : LeaseMgr(parameters), major_(0), minor_(0) {
+ : LeaseMgr(parameters), mysql_(NULL), major_(0), minor_(0) {
+
+ // Allocate context for MySQL - it is destroyed in the destructor.
+ mysql_ = mysql_init(NULL);
+ std::cerr << "cerr: mysql_ is " << long(mysql_) << std::endl;
+ std::cout << "cout: mysql_ is " << long(mysql_) << std::endl;
+
+ // Open the database
+ openDatabase();
+
}
MySqlLeaseMgr::~MySqlLeaseMgr() {
+ mysql_close(mysql_);
+ mysql_ = NULL;
}
bool
-MySqlLeaseMgr::addLease(Lease4Ptr /* lease */) {
+MySqlLeaseMgr::addLease(isc::dhcp::Lease4Ptr /* lease */) {
return (false);
}
bool
-MySqlLeaseMgr::addLease(Lease6Ptr /* lease */) {
+MySqlLeaseMgr::addLease(isc::dhcp::Lease6Ptr /* lease */) {
return (false);
}
#ifndef __MYSQL_LEASE_MGR_H
#define __MYSQL_LEASE_MGR_H
+#include <mysql.h>
#include <dhcp/lease_mgr.h>
namespace isc {
/// @brief Adds an IPv4 lease.
///
/// @param lease lease to be added
- virtual bool addLease(Lease4Ptr lease) = 0;
+ virtual bool addLease(Lease4Ptr lease);
/// @brief Adds an IPv6 lease.
///
/// @param lease lease to be added
- virtual bool addLease(Lease6Ptr lease) = 0;
+ virtual bool addLease(Lease6Ptr lease);
/// @brief Returns existing IPv4 lease for specified IPv4 address and subnet_id
///
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr,
- SubnetID subnet_id) const = 0;
+ SubnetID subnet_id) const;
/// @brief Returns an IPv4 lease for specified IPv4 address
///
/// @param subnet_id ID of the subnet the lease must belong to
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
- virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const = 0;
+ virtual Lease4Ptr getLease4(isc::asiolink::IOAddress addr) const;
/// @brief Returns existing IPv4 leases for specified hardware address.
///
/// @param hwaddr hardware address of the client
///
/// @return lease collection
- virtual Lease4Collection getLease4(const HWAddr& hwaddr) const = 0;
+ virtual Lease4Collection getLease4(const HWAddr& hwaddr) const;
/// @brief Returns existing IPv4 leases for specified hardware address
/// and a subnet
///
/// @return a pointer to the lease (or NULL if a lease is not found)
virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
- SubnetID subnet_id) const = 0;
+ SubnetID subnet_id) const;
/// @brief Returns existing IPv4 lease for specified client-id
///
/// @param clientid client identifier
///
/// @return lease collection
- virtual Lease4Collection getLease4(const ClientId& clientid) const = 0;
+ virtual Lease4Collection getLease4(const ClientId& clientid) const;
/// @brief Returns existing IPv4 lease for specified client-id
///
///
/// @return a pointer to the lease (or NULL if a lease is not found)
virtual Lease4Ptr getLease4(const ClientId& clientid,
- SubnetID subnet_id) const = 0;
+ SubnetID subnet_id) const;
/// @brief Returns existing IPv6 lease for a given IPv6 address.
///
/// @param addr address of the searched lease
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
- virtual Lease6Ptr getLease6(isc::asiolink::IOAddress addr) const = 0;
+ virtual Lease6Ptr getLease6(isc::asiolink::IOAddress addr) const;
/// @brief Returns existing IPv6 leases for a given DUID+IA combination
///
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
virtual Lease6Collection getLease6(const DUID& duid,
- uint32_t iaid) const = 0;
+ uint32_t iaid) const;
/// @brief Returns existing IPv6 lease for a given DUID+IA combination
///
///
/// @return smart pointer to the lease (or NULL if a lease is not found)
virtual Lease6Ptr getLease6(const DUID& duid, uint32_t iaid,
- SubnetID subnet_id) const = 0;
+ SubnetID subnet_id) const;
/// @brief Updates IPv4 lease.
///
/// @param lease4 The lease to be updated.
///
/// If no such lease is present, an exception will be thrown.
- virtual void updateLease4(Lease4Ptr lease4) = 0;
+ virtual void updateLease4(Lease4Ptr lease4);
/// @brief Updates IPv4 lease.
///
/// @param lease4 The lease to be updated.
///
/// If no such lease is present, an exception will be thrown.
- virtual void updateLease6(Lease6Ptr lease6) = 0;
+ virtual void updateLease6(Lease6Ptr lease6);
/// @brief Deletes a lease.
///
/// @param addr IPv4 address of the lease to be deleted.
///
/// @return true if deletion was successful, false if no such lease exists
- virtual bool deleteLease4(uint32_t addr) = 0;
+ virtual bool deleteLease4(uint32_t addr);
/// @brief Deletes a lease.
///
/// @param addr IPv4 address of the lease to be deleted.
///
/// @return true if deletion was successful, false if no such lease exists
- virtual bool deleteLease6(isc::asiolink::IOAddress addr) = 0;
+ virtual bool deleteLease6(isc::asiolink::IOAddress addr);
/// @brief Returns backend name.
///
/// Each backend have specific name, e.g. "mysql" or "sqlite".
- virtual std::string getName() const = 0;
+ virtual std::string getName() const;
/// @brief Returns description of the backend.
///
/// This description may be multiline text that describes the backend.
- virtual std::string getDescription() const = 0;
+ virtual std::string getDescription() const;
/// @brief Returns backend version.
///
virtual std::pair<uint32_t, uint32_t> getVersion() const;
private:
+ /// @brief Open Database
+ ///
+ /// Opens the database using the information supplied in the parameters
+ /// passed to the constructor.
+ ///
+ /// @exception DbOpenError Error opening the database
+ void openDatabase();
+
+ // Members
+ MYSQL* mysql_; ///< MySQL context object
uint32_t major_; ///< Major version number
uint32_t minor_; ///< Minor version number
};
// Connection strings
const char* VALID_TYPE = "type=mysql";
const char* INVALID_TYPE = "type=unknown";
-const char* VALID_NAME = "name=keattest";
+const char* VALID_NAME = "name=keatest";
const char* INVALID_NAME = "name=invalidname";
const char* VALID_HOST = "host=localhost";
const char* INVALID_HOST = "host=invalidhost";
TEST_F(MySqlLeaseMgrTest, OpenDatabase) {
LeaseMgrPtr lmptr;
- // Check that failure to open the database generates an exception
+ // Check that wrong specification of backend throws an exception.
+ // (This is really a check on LeaseMgrFactory, but is convenient to
+ // perform here.)
EXPECT_THROW(lmptr = LeaseMgrFactory::create(connectionString(
INVALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
- InvalidType);
+ InvalidParameter);
+
+ // Check that invalid login data causes an exception.
EXPECT_THROW(lmptr = LeaseMgrFactory::create(connectionString(
VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
DbOpenError);
VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)));
ASSERT_TRUE(lmptr);
+/*
pair<uint32_t, uint32_t> version = lmptr->getVersion();
EXPECT_EQ(0, version.first);
EXPECT_EQ(1, version.second);
+*/
}
}; // end of anonymous namespace