Browse Source

[2404] Add method to get lease4 by client ID and subnet ID

Stephen Morris 12 years ago
parent
commit
a08894f451

+ 25 - 7
src/lib/dhcpsrv/mysql_lease_mgr.cc

@@ -71,6 +71,11 @@ TaggedStatement tagged_statements[] = {
                         "valid_lifetime, expire, subnet_id "
                             "FROM lease4 "
                             "WHERE client_id = ?"},
+    {MySqlLeaseMgr::GET_LEASE4_CLIENTID_SUBID,
+                    "SELECT address, hwaddr, client_id, "
+                        "valid_lifetime, expire, subnet_id "
+                            "FROM lease4 "
+                            "WHERE client_id = ? AND subnet_id = ?"},
     {MySqlLeaseMgr::GET_LEASE4_HWADDR,
                     "SELECT address, hwaddr, client_id, "
                         "valid_lifetime, expire, subnet_id "
@@ -1144,7 +1149,6 @@ MySqlLeaseMgr::getLease4(const HWAddr& hwaddr, SubnetID subnet_id) const {
 }
 
 
-
 Lease4Collection
 MySqlLeaseMgr::getLease4(const ClientId& clientid) const {
     // Set up the WHERE clause value
@@ -1167,14 +1171,28 @@ MySqlLeaseMgr::getLease4(const ClientId& clientid) const {
 
 
 Lease4Ptr
-MySqlLeaseMgr::getLease4(const ClientId& /* clientid */,
-                         SubnetID /* subnet_id */) const {
-    isc_throw(NotImplemented, "MySqlLeaseMgr::getLease4(const ClientID&, SubnetID) "
-              "not implemented yet");
-    return (Lease4Ptr());
-}
+MySqlLeaseMgr::getLease4(const ClientId& clientid, SubnetID subnet_id) const {
+    // Set up the WHERE clause value
+    MYSQL_BIND inbind[2];
+    memset(inbind, 0, sizeof(inbind));
+
+    std::vector<uint8_t> client_data = clientid.getClientId();
+    unsigned long client_data_length = client_data.size();
+    inbind[0].buffer_type = MYSQL_TYPE_BLOB;
+    inbind[0].buffer = reinterpret_cast<char*>(&client_data[0]);
+    inbind[0].buffer_length = client_data_length;
+    inbind[0].length = &client_data_length;
+
+    inbind[1].buffer_type = MYSQL_TYPE_LONG;
+    inbind[1].buffer = reinterpret_cast<char*>(&subnet_id);
+    inbind[1].is_unsigned = my_bool(1);
 
+    // Get the data
+    Lease4Ptr result;
+    getLease(GET_LEASE4_CLIENTID_SUBID, inbind, exchange4_, result);
 
+    return (result);
+}
 
 
 Lease6Ptr

+ 1 - 0
src/lib/dhcpsrv/mysql_lease_mgr.h

@@ -356,6 +356,7 @@ public:
         DELETE_LEASE6,              // Delete from lease6 by address
         GET_LEASE4_ADDR,            // Get lease4 by address
         GET_LEASE4_CLIENTID,        // Get lease4 by Client ID
+        GET_LEASE4_CLIENTID_SUBID,  // Get lease4 by Client ID
         GET_LEASE4_HWADDR,          // Get lease4 by HW address
         GET_LEASE4_HWADDR_SUBID,    // Get lease4 by HW address & subnet ID
         GET_LEASE6_ADDR,            // Get lease6 by address

+ 39 - 0
src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc

@@ -976,6 +976,45 @@ TEST_F(MySqlLeaseMgrTest, getLease4ClientId) {
 }
 
 
+// @brief Check GetLease4 methods - Access by Client ID & Subnet ID
+//
+// Adds leases to the database and checks that they can be accessed via
+// a combination of client and subnet IDs.
+
+TEST_F(MySqlLeaseMgrTest, getLease4ClientIdSubnetId) {
+    // Get the leases to be used for the test and add to the database
+    vector<Lease4Ptr> leases = createLeases4();
+    for (int i = 0; i < leases.size(); ++i) {
+        EXPECT_TRUE(lmptr_->addLease(leases[i]));
+    }
+
+    // Get the leases matching the client ID of lease 1 and
+    // subnet ID of lease 1.  Result should be a single lease - lease 1.
+    Lease4Ptr returned = lmptr_->getLease4(*leases[1]->client_id_,
+                                           leases[1]->subnet_id_);
+    ASSERT_TRUE(returned);
+    detailCompareLease(leases[1], returned);
+
+    // Try for a match to the client ID of lease 1 and the wrong
+    // subnet ID.
+    returned = lmptr_->getLease4(*leases[1]->client_id_,
+                                 leases[1]->subnet_id_ + 1);
+    EXPECT_FALSE(returned);
+
+    // Try for a match to the subnet ID of lease 1 (and lease 4) but
+    // the wrong client ID
+    const uint8_t invalid_data[] = {0, 0, 0};
+    ClientId invalid(invalid_data, sizeof(invalid_data));
+    returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_);
+    EXPECT_FALSE(returned);
+
+    // Try for a match to an unknown hardware address and an unknown
+    // subnet ID.
+    returned = lmptr_->getLease4(invalid, leases[1]->subnet_id_ + 1);
+    EXPECT_FALSE(returned);
+}
+
+
 // @brief Check GetLease6 methods - Access by DUID/IAID
 //
 // Adds leases to the database and checks that they can be accessed via