diff --git a/src/lib/dhcp/lease_mgr.h b/src/lib/dhcp/lease_mgr.h index 806d9bafc9..ee081aaea7 100644 --- a/src/lib/dhcp/lease_mgr.h +++ b/src/lib/dhcp/lease_mgr.h @@ -63,6 +63,7 @@ namespace dhcp { /// @brief Exception thrown on failure to open database class DbOpenError : public Exception { +public: DbOpenError(const char* file, size_t line, const char* what) : isc::Exception(file, line, what) {} }; diff --git a/src/lib/dhcp/lease_mgr_factory.cc b/src/lib/dhcp/lease_mgr_factory.cc index 8fa8fb8871..0a8c3aaeed 100644 --- a/src/lib/dhcp/lease_mgr_factory.cc +++ b/src/lib/dhcp/lease_mgr_factory.cc @@ -14,6 +14,10 @@ #include "config.h" +// TEMP +#define HAVE_MYSQL 1 + + #include #include #include diff --git a/src/lib/dhcp/mysql_lease_mgr.cc b/src/lib/dhcp/mysql_lease_mgr.cc index c388c3a1ae..8d540dd295 100644 --- a/src/lib/dhcp/mysql_lease_mgr.cc +++ b/src/lib/dhcp/mysql_lease_mgr.cc @@ -12,27 +12,96 @@ // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. +#include +#include +#include #include #include +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); } diff --git a/src/lib/dhcp/mysql_lease_mgr.h b/src/lib/dhcp/mysql_lease_mgr.h index ea2529a84d..f2069b692e 100644 --- a/src/lib/dhcp/mysql_lease_mgr.h +++ b/src/lib/dhcp/mysql_lease_mgr.h @@ -15,6 +15,7 @@ #ifndef __MYSQL_LEASE_MGR_H #define __MYSQL_LEASE_MGR_H +#include #include namespace isc { @@ -50,12 +51,12 @@ public: /// @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 /// @@ -68,7 +69,7 @@ public: /// /// @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 /// @@ -83,7 +84,7 @@ public: /// @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. /// @@ -95,7 +96,7 @@ public: /// @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 @@ -108,7 +109,7 @@ public: /// /// @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 /// @@ -120,7 +121,7 @@ public: /// @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 /// @@ -132,7 +133,7 @@ public: /// /// @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. /// @@ -143,7 +144,7 @@ public: /// @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 /// @@ -157,7 +158,7 @@ public: /// /// @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 /// @@ -167,45 +168,45 @@ public: /// /// @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. /// @@ -224,6 +225,16 @@ public: virtual std::pair 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 }; diff --git a/src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc index 986d12f7e9..397d5c63b4 100644 --- a/src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc +++ b/src/lib/dhcp/tests/mysql_lease_mgr_unittest.cc @@ -38,7 +38,7 @@ public: // 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"; @@ -70,10 +70,14 @@ string validConnectionString() { 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); @@ -92,9 +96,11 @@ TEST_F(MySqlLeaseMgrTest, OpenDatabase) { VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD))); ASSERT_TRUE(lmptr); +/* pair version = lmptr->getVersion(); EXPECT_EQ(0, version.first); EXPECT_EQ(1, version.second); +*/ } }; // end of anonymous namespace