Browse Source

[2940] Implemented Lease6::getDuidVector function and remove key extractors

Marcin Siodelski 11 years ago
parent
commit
338d7d58b4

+ 1 - 1
src/lib/dhcp/duid.cc

@@ -46,7 +46,7 @@ DUID::DUID(const uint8_t* data, size_t len) {
     duid_ = std::vector<uint8_t>(data, data + len);
 }
 
-std::vector<uint8_t> DUID::getDuid() const {
+const std::vector<uint8_t>& DUID::getDuid() const {
     return (duid_);
 }
 

+ 9 - 6
src/lib/dhcp/duid.h

@@ -58,12 +58,13 @@ class DUID {
 
     /// @brief Returns a const reference to the actual DUID value
     ///
-    /// Note: For safety reasons, this method returns a copy of data as
-    /// otherwise the reference would be only valid as long as the object that
-    /// returned it. In any case, this method should be used only sporadically.
-    /// If there are frequent uses, we must implement some other method
-    /// (e.g. storeSelf()) that will avoid data copying.
-    std::vector<uint8_t> getDuid() const;
+    /// @warning Since this function returns a reference to the vector (not a
+    /// copy) the returned object must be used with caution because it remains
+    /// valid only for the time period when the object which returned it is
+    /// valid.
+    ///
+    /// @return A reference to a vector holding a DUID.
+    const std::vector<uint8_t>& getDuid() const;
 
     /// @brief Returns the DUID type
     DUIDType getType() const;
@@ -122,6 +123,8 @@ public:
     /// copy) the returned object must be used with caution because it remains
     /// valid only for the time period when the object which returned it is
     /// valid.
+    ///
+    /// @return A reference to a vector holding a client identifier.
     const std::vector<uint8_t>& getClientId() const;
 
     /// @brief Returns textual representation of a DUID (e.g. 00:01:02:03:ff)

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

@@ -139,6 +139,16 @@ Lease6::Lease6(Type type, const isc::asiolink::IOAddress& addr,
     cltt_ = time(NULL);
 }
 
+const std::vector<uint8_t>&
+Lease6::getDuidVector() const {
+    if (!duid_) {
+        static std::vector<uint8_t> empty_vec;
+        return (empty_vec);
+    }
+
+    return (duid_->getDuid());
+}
+
 std::string
 Lease6::toText() const {
     ostringstream stream;

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

@@ -331,6 +331,16 @@ struct Lease6 : public Lease {
         type_(TYPE_NA) {
     }
 
+    /// @brief Returns a reference to a vector representing a DUID.
+    ///
+    /// @warning Since the function returns the reference to a vector (not a
+    /// copy), the returned object should be used with caution because it will
+    /// remain valid only for the period of time when an object which returned
+    /// it exists.
+    ///
+    /// @return A reference to a vector holding a DUID.
+    const std::vector<uint8_t>& getDuidVector() const;
+
     /// @brief Compare two leases for equality
     ///
     /// @param other lease6 object with which to compare

+ 8 - 15
src/lib/dhcpsrv/memfile_lease_mgr.h

@@ -16,7 +16,6 @@
 #define MEMFILE_LEASE_MGR_H
 
 #include <dhcp/hwaddr.h>
-#include <dhcpsrv/key_from_key.h>
 #include <dhcpsrv/lease_mgr.h>
 
 #include <boost/multi_index/indexed_by.hpp>
@@ -267,20 +266,10 @@ protected:
                 // the lease using three attributes: DUID, IAID, Subnet Id.
                 boost::multi_index::composite_key<
                     Lease6,
-                    // The DUID value can't be directly accessed from the Lease6
-                    // object because it is wrapped with the DUID object (actually
-                    // pointer to this object). Therefore we need to use
-                    // KeyFromKeyExtractor class to extract the DUID value from
-                    // this cascaded structure.
-                    KeyFromKeyExtractor<
-                        // The value of the DUID is accessed by the getDuid() method
-                        // from the DUID object.
-                        boost::multi_index::const_mem_fun<DUID, std::vector<uint8_t>,
-                                                          &DUID::getDuid>,
-                        // The DUID object is stored in the duid_ member of the
-                        // Lease6 object.
-                        boost::multi_index::member<Lease6, DuidPtr, &Lease6::duid_>
-                    >,
+                    // The DUID can be retrieved from the Lease6 object using
+                    // a getDuidVector const function.
+                    boost::multi_index::const_mem_fun<Lease6, const std::vector<uint8_t>&,
+                                                      &Lease6::getDuidVector>,
                     // The two other ingredients of this index are IAID and
                     // subnet id.
                     boost::multi_index::member<Lease6, uint32_t, &Lease6::iaid_>,
@@ -330,6 +319,8 @@ protected:
                 // lease: client id and subnet id.
                 boost::multi_index::composite_key<
                     Lease4,
+                    // The client id can be retrieved from the Lease4 object by
+                    // calling getClientIdVector const function.
                     boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
                                                       &Lease4::getClientIdVector>,
                     // The subnet id is accessed through the subnet_id_ member.
@@ -343,6 +334,8 @@ protected:
                 // lease: client id and subnet id.
                 boost::multi_index::composite_key<
                     Lease4,
+                    // The client id can be retrieved from the Lease4 object by
+                    // calling getClientIdVector const function.
                     boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
                                                       &Lease4::getClientIdVector>,
                     // The hardware address is held in the hwaddr_ member of the

+ 24 - 2
src/lib/dhcpsrv/tests/lease_unittest.cc

@@ -13,6 +13,7 @@
 // PERFORMANCE OF THIS SOFTWARE.
 
 #include <config.h>
+#include <dhcp/duid.h>
 #include <dhcpsrv/lease.h>
 #include <gtest/gtest.h>
 #include <vector>
@@ -22,8 +23,9 @@ using namespace isc::dhcp;
 
 namespace {
 
-// @todo Currently this file contains a single test. Other tests for Lease
-// objects must be implemented. See http://bind10.isc.org/ticket/3240.
+// @todo Currently this file contains tests for new functions which return DUID
+// or client identifier. Other tests for Lease objects must be implemented.
+// See http://bind10.isc.org/ticket/3240.
 
 // Verify that the client id can be returned as a vector object and if client
 // id is NULL the empty vector is returned.
@@ -45,5 +47,25 @@ TEST(Lease4Test, getClientIdVector) {
     EXPECT_TRUE(returned_vec == client_id_vec);
 }
 
+// Verify that the DUID can be returned as a vector object and if DUID is NULL
+// the empty vector is returned.
+TEST(Lease6Test, getDuidVector) {
+    // Create a lease.
+    Lease6 lease;
+    // By default, the lease should have client id set to NULL. If it doesn't,
+    // continuing the test makes no sense.
+    ASSERT_FALSE(lease.duid_);
+    // When client id is NULL the vector returned should be empty.
+    EXPECT_TRUE(lease.getDuidVector().empty());
+    // Now, let's set the non NULL DUID. Fill it with the 8 bytes, each
+    // holding a value of 0x42.
+    std::vector<uint8_t> duid_vec(8, 0x42);
+    lease.duid_ = DuidPtr(new DUID(duid_vec));
+    // Check that the returned vector, encapsulating DUID is equal to
+    // the one that has been used to set the DUID for the lease.
+    std::vector<uint8_t> returned_vec = lease.getDuidVector();
+    EXPECT_TRUE(returned_vec == duid_vec);
+}
+
 
 }; // end of anonymous namespace