Browse Source

[3981] Changes after review:

 - moved lease decline to Lease::decline()
Tomek Mrugalski 9 years ago
parent
commit
795863a7ee

+ 2 - 11
src/bin/dhcp4/dhcp4_srv.cc

@@ -1949,17 +1949,8 @@ Dhcpv4Srv::declineLease(const Lease4Ptr& lease, const std::string& descr) {
     // We need to disassociate the lease from the client. Once we move a lease
     // to declined state, it is no longer associated with the client in any
     // way.
-    lease->hwaddr_.reset(new HWAddr());
-    lease->client_id_.reset();
-    lease->t1_ = 0;
-    lease->t2_ = 0;
-    lease->valid_lft_ = CfgMgr::instance().getCurrentCfg()->getDeclinePeriod();
-    lease->cltt_ = time(NULL);
-    lease->hostname_ = string("");
-    lease->fqdn_fwd_ = false;
-    lease->fqdn_rev_ = false;
-
-    lease->state_ = Lease::STATE_DECLINED;
+    lease->decline(CfgMgr::instance().getCurrentCfg()->getDeclinePeriod());
+
     LeaseMgrFactory::instance().updateLease4(lease);
 
     LOG_INFO(dhcp4_logger, DHCP4_DECLINE_LEASE).arg(lease->addr_.toText())

+ 19 - 0
src/lib/dhcpsrv/lease.cc

@@ -179,6 +179,20 @@ Lease4::belongsToClient(const HWAddrPtr& hw_address,
     return (false);
 }
 
+void
+Lease4::decline(uint32_t probation_period) {
+    hwaddr_.reset(new HWAddr());
+    client_id_.reset();
+    t1_ = 0;
+    t2_ = 0;
+    cltt_ = time(NULL);
+    hostname_ = string("");
+    fqdn_fwd_ = false;
+    fqdn_rev_ = false;
+    state_ = STATE_DECLINED;
+    valid_lft_ = probation_period;
+}
+
 Lease4&
 Lease4::operator=(const Lease4& other) {
     if (this != &other) {
@@ -261,6 +275,11 @@ Lease6::getDuidVector() const {
     return (duid_->getDuid());
 }
 
+void
+Lease6::decline(uint32_t ) {
+    /// @todo: implement this
+}
+
 std::string
 Lease6::toText() const {
     ostringstream stream;

+ 26 - 0
src/lib/dhcpsrv/lease.h

@@ -200,6 +200,18 @@ struct Lease {
     /// The lease expiration time is a sum of a client last transmission time
     /// and valid lifetime.
     int64_t getExpirationTime() const;
+
+    /// @brief Sets lease to DECLINED state.
+    ///
+    /// All client identifying parameters will be stripped off (HWaddr,
+    /// client_id, hostname), timers set to 0 (t1, t2), cltt will be set
+    /// to current time and valid_lft to parameter specified as probation
+    /// period. Note that This method only sets fields in the structure.
+    /// It is caller's responsibility to clean up DDNS, bump up stats,
+    /// log, call hooks ets.
+    ///
+    /// @param probation_period lease lifetime will be set to this value
+    virtual void decline(uint32_t probation_period) = 0;
 };
 
 /// @brief Structure that holds a lease for IPv4 address
@@ -386,6 +398,13 @@ struct Lease4 : public Lease {
     /// @return Textual represenation of lease data
     virtual std::string toText() const;
 
+    /// @brief Sets IPv4 lease to declined state.
+    ///
+    /// See @ref Lease::decline for detailed description.
+    ///
+    /// @param probation_period valid lifetime will be set to this value
+    void decline(uint32_t probation_period);
+
     /// @todo: Add DHCPv4 failover related fields here
 };
 
@@ -495,6 +514,13 @@ struct Lease6 : public Lease {
     /// @return A reference to a vector holding a DUID.
     const std::vector<uint8_t>& getDuidVector() const;
 
+    /// @brief Sets IPv6 lease to declined state.
+    ///
+    /// See @ref Lease::decline for detailed description.
+    ///
+    /// @param probation_period valid lifetime will be set to this value
+    void decline(uint32_t probation_period);
+
     /// @brief Compare two leases for equality
     ///
     /// @param other lease6 object with which to compare

+ 37 - 3
src/lib/dhcpsrv/tests/lease_unittest.cc

@@ -415,7 +415,7 @@ TEST_F(Lease4Test, toText) {
     const time_t current_time = 12345678;
     Lease4 lease(IOAddress("192.0.2.3"), hwaddr_, clientid_, 3600, 123,
                  456, current_time, 789);
-    
+
     std::stringstream expected;
     expected << "Address:       192.0.2.3\n"
              << "Valid life:    3600\n"
@@ -445,6 +445,35 @@ TEST_F(Lease4Test, toText) {
     EXPECT_EQ(expected.str(), lease.toText());
 }
 
+// Verify that decline() method properly clears up specific fields.
+TEST_F(Lease4Test, decline) {
+
+    const time_t current_time = 12345678;
+    Lease4 lease(IOAddress("192.0.2.3"), hwaddr_, clientid_, 3600, 123,
+                 456, current_time, 789);
+    lease.hostname_="foo.example.org";
+    lease.fqdn_fwd_ = true;
+    lease.fqdn_rev_ = true;
+
+    time_t now = time(NULL);
+
+    // Move lease to declined state and set its valid-lifetime to 123 seconds
+    lease.decline(123);
+    ASSERT_TRUE(lease.hwaddr_);
+    EXPECT_EQ("", lease.hwaddr_->toText(false));
+    EXPECT_FALSE(lease.client_id_);
+    EXPECT_EQ(0, lease.t1_);
+    EXPECT_EQ(0, lease.t2_);
+
+    EXPECT_TRUE(now <= lease.cltt_);
+    EXPECT_TRUE(lease.cltt_ <= now + 1);
+    EXPECT_EQ("", lease.hostname_);
+    EXPECT_FALSE(lease.fqdn_fwd_);
+    EXPECT_FALSE(lease.fqdn_rev_);
+    EXPECT_EQ(Lease::STATE_DECLINED, lease.state_);
+    EXPECT_EQ(123, lease.valid_lft_);
+}
+
 // Verify that the lease states are correctly returned in the textual format.
 TEST_F(Lease4Test, stateToText) {
     EXPECT_EQ("default", Lease4::statesToText(Lease::STATE_DEFAULT));
@@ -741,6 +770,11 @@ TEST(Lease6Test, getDuidVector) {
     EXPECT_TRUE(returned_vec == duid_vec);
 }
 
+// Verify that decline() method properly clears up specific fields.
+TEST_F(Lease6Test, decline) {
+    /// @todo (see ticket 3981)
+}
+
 // Verify the behavior of the function which checks FQDN data for equality.
 TEST(Lease6Test, hasIdenticalFqdn) {
     Lease6 lease = createLease6("myhost.example.com.", true, true);
@@ -765,12 +799,12 @@ TEST(Lease6Test, toText) {
 
     uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
     DuidPtr duid(new DUID(llt, sizeof(llt)));
-    
+
     Lease6 lease(Lease::TYPE_NA, IOAddress("2001:db8::1"), duid, 123456,
                  400, 800, 100, 200, 5678, hwaddr, 128);
     lease.cltt_ = 12345678;
     lease.state_ = Lease::STATE_DECLINED;
-    
+
     std::stringstream expected;
     expected << "Type:          IA_NA(" << static_cast<int>(Lease::TYPE_NA) << ")\n"
              << "Address:       2001:db8::1\n"