]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#78,!85] HA command creator supports leaseX-get-page commands.
authorMarcin Siodelski <marcin@isc.org>
Fri, 19 Oct 2018 12:57:33 +0000 (14:57 +0200)
committerMarcin Siodelski <marcin@isc.org>
Mon, 5 Nov 2018 18:38:41 +0000 (13:38 -0500)
src/hooks/dhcp/high_availability/command_creator.cc
src/hooks/dhcp/high_availability/command_creator.h
src/hooks/dhcp/high_availability/tests/command_creator_unittest.cc

index 1c93605ea07730f3379e229e064c50284606a230..0b2ce4435483eb8a99417d7af78fe3a1042056c4 100644 (file)
@@ -71,6 +71,31 @@ CommandCreator::createLease4GetAll() {
     return (command);
 }
 
+ConstElementPtr
+CommandCreator::createLease4GetPage(const Lease4Ptr& last_lease4,
+                                    const uint32_t limit) {
+    // Zero value is not allowed.
+    if (limit == 0) {
+        isc_throw(BadValue, "limit value for lease4-get-page command must not be 0");
+    }
+
+    // Get the last lease returned on the previous page. A null pointer means that
+    // we're fetching first page. In that case a keyword "start" is used to indicate
+    // that first page should be returned.
+    ElementPtr from_element = Element::create(last_lease4 ? last_lease4->addr_.toText() : "start");
+    // Set the maximum size of the page.
+    ElementPtr limit_element = Element::create(static_cast<long long int>(limit));
+    // Put both parameters into arguments map.
+    ElementPtr args = Element::createMap();
+    args->set("from", from_element);
+    args->set("limit", limit_element);
+
+    // Create the command.
+    ConstElementPtr command = config::createCommand("lease4-get-page", args);
+    insertService(command, HAServerType::DHCPv4);
+    return (command);
+}
+
 ConstElementPtr
 CommandCreator::createLease6Update(const Lease6& lease6) {
     ElementPtr lease_as_json = lease6.toElement();
@@ -97,6 +122,31 @@ CommandCreator::createLease6GetAll() {
     return (command);
 }
 
+ConstElementPtr
+CommandCreator::createLease6GetPage(const Lease6Ptr& last_lease6,
+                                    const uint32_t limit) {
+    // Zero value is not allowed.
+    if (limit == 0) {
+        isc_throw(BadValue, "limit value for lease6-get-page command must not be 0");
+    }
+
+    // Get the last lease returned on the previous page. A null pointer means that
+    // we're fetching first page. In that case a keyword "start" is used to indicate
+    // that first page should be returned.
+    ElementPtr from_element = Element::create(last_lease6 ? last_lease6->addr_.toText() : "start");
+    // Set the maximum size of the page.
+    ElementPtr limit_element = Element::create(static_cast<long long int>(limit));
+    // Put both parameters into arguments map.
+    ElementPtr args = Element::createMap();
+    args->set("from", from_element);
+    args->set("limit", limit_element);
+
+    // Create the command.
+    ConstElementPtr command = config::createCommand("lease6-get-page", args);
+    insertService(command, HAServerType::DHCPv6);
+    return (command);
+}
+
 void
 CommandCreator::insertLeaseExpireTime(ElementPtr& lease) {
     if ((lease->getType() != Element::map) ||
index 3de2a489138912b9bb42b56f1d0bb1e549f2a0de..040298c477fcfde7a209a63a08ac775ed10c4c4b 100644 (file)
@@ -67,6 +67,19 @@ public:
     static data::ConstElementPtr
     createLease4GetAll();
 
+    /// @brief Creates lease4-get-page command.
+    ///
+    /// @param lease4 Pointer to the last lease returned on the previous
+    /// page of leases. This lease is used to set the value of the "from"
+    /// parameter in the lease4-get-page command. If this command is sent
+    /// to fetch the first page, the @c lease4 parameter should be set to
+    /// null.
+    /// @param limit Limit of leases on the page.
+    /// @return Pointer to the JSON representation of the command.
+    static data::ConstElementPtr
+    createLease4GetPage(const dhcp::Lease4Ptr& lease4,
+                        const uint32_t limit);
+
     /// @brief Creates lease6-update command.
     ///
     /// It adds "force-create" parameter to the lease information to force
@@ -95,6 +108,19 @@ public:
     static data::ConstElementPtr
     createLease6GetAll();
 
+    /// @brief Creates lease6-get-page command.
+    ///
+    /// @param lease6 Pointer to the last lease returned on the previous
+    /// page of leases. This lease is used to set the value of the "from"
+    /// parameter in the lease6-get-page command. If this command is sent
+    /// to fetch the first page, the @c lease6 parameter should be set to
+    /// null.
+    /// @param limit Limit of leases on the page.
+    /// @return Pointer to the JSON representation of the command.
+    static data::ConstElementPtr
+    createLease6GetPage(const dhcp::Lease6Ptr& lease4,
+                        const uint32_t limit);
+
 private:
 
     /// @brief Replaces "cltt" with "expire" value within the lease.
index 3b769cc70320d61320459e6e7149cb4f8e768b16..da9e19ea717e68bf669e325c846898f7587158ee 100644 (file)
 #include <command_creator.h>
 #include <asiolink/io_address.h>
 #include <cc/data.h>
+#include <exceptions/exceptions.h>
 #include <dhcp/hwaddr.h>
 #include <dhcpsrv/lease.h>
 #include <boost/pointer_cast.hpp>
 #include <gtest/gtest.h>
 #include <vector>
 
+using namespace isc;
 using namespace isc::asiolink;
 using namespace isc::data;
 using namespace isc::dhcp;
@@ -204,6 +206,55 @@ TEST(CommandCreatorTest, createLease4GetAll) {
     ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "lease4-get-all", "dhcp4"));
 }
 
+// This test verifies that the lease4-get-page command is correct when
+// first page is fetched.
+TEST(CommandCreatorTest, createLease4GetPageStart) {
+    Lease4Ptr lease4;
+    ConstElementPtr command = CommandCreator::createLease4GetPage(lease4, 10);
+    ConstElementPtr arguments;
+    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "lease4-get-page", "dhcp4",
+                                              arguments));
+
+    ConstElementPtr from = arguments->get("from");
+    ASSERT_TRUE(from);
+    EXPECT_EQ(Element::string, from->getType());
+    EXPECT_EQ("start", from->stringValue());
+
+    ConstElementPtr limit = arguments->get("limit");
+    ASSERT_TRUE(limit);
+    ASSERT_EQ(Element::integer, limit->getType());
+    EXPECT_EQ(10, limit->intValue());
+}
+
+// This test verifies that the lease4-get-page command is correct when next
+// page is fetched.
+TEST(CommandCreatorTest, createLease4GetPageAddress) {
+    Lease4Ptr lease4(new Lease4());
+    lease4->addr_ = IOAddress("1.2.3.4");
+
+    ConstElementPtr command = CommandCreator::createLease4GetPage(lease4, 15);
+    ConstElementPtr arguments;
+    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "lease4-get-page", "dhcp4",
+                                              arguments));
+
+    ConstElementPtr from = arguments->get("from");
+    ASSERT_TRUE(from);
+    EXPECT_EQ(Element::string, from->getType());
+    EXPECT_EQ("1.2.3.4", from->stringValue());
+
+    ConstElementPtr limit = arguments->get("limit");
+    ASSERT_TRUE(limit);
+    ASSERT_EQ(Element::integer, limit->getType());
+    EXPECT_EQ(15, limit->intValue());
+}
+
+// This test verifies that exception is thrown if limit is set to 0 while
+// creating lease4-get-page command.
+TEST(CommandCreatorTest, createLease4GetPageZeroLimit) {
+    Lease4Ptr lease4;
+    EXPECT_THROW(CommandCreator::createLease4GetPage(lease4, 0), BadValue);
+}
+
 // This test verifies that the dhcp-disable command (DHCPv6 case) is
 // correct.
 TEST(CommandCreatorTest, createDHCPDisable6) {
@@ -259,4 +310,53 @@ TEST(CommandCreatorTest, createLease6GetAll) {
 }
 
 
+// This test verifies that the lease6-get-page command is correct when
+// first page is fetched.
+TEST(CommandCreatorTest, createLease6GetPageStart) {
+    Lease6Ptr lease6;
+    ConstElementPtr command = CommandCreator::createLease6GetPage(lease6, 10);
+    ConstElementPtr arguments;
+    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "lease6-get-page", "dhcp6",
+                                              arguments));
+
+    ConstElementPtr from = arguments->get("from");
+    ASSERT_TRUE(from);
+    EXPECT_EQ(Element::string, from->getType());
+    EXPECT_EQ("start", from->stringValue());
+
+    ConstElementPtr limit = arguments->get("limit");
+    ASSERT_TRUE(limit);
+    ASSERT_EQ(Element::integer, limit->getType());
+    EXPECT_EQ(10, limit->intValue());
+}
+
+// This test verifies that the lease6-get-page command is correct when next
+// page is fetched.
+TEST(CommandCreatorTest, createLease6GetPageAddress) {
+    Lease6Ptr lease6(new Lease6());
+    lease6->addr_ = IOAddress("2001:db8:1::1");
+
+    ConstElementPtr command = CommandCreator::createLease6GetPage(lease6, 15);
+    ConstElementPtr arguments;
+    ASSERT_NO_FATAL_FAILURE(testCommandBasics(command, "lease6-get-page", "dhcp6",
+                                              arguments));
+
+    ConstElementPtr from = arguments->get("from");
+    ASSERT_TRUE(from);
+    EXPECT_EQ(Element::string, from->getType());
+    EXPECT_EQ("2001:db8:1::1", from->stringValue());
+
+    ConstElementPtr limit = arguments->get("limit");
+    ASSERT_TRUE(limit);
+    ASSERT_EQ(Element::integer, limit->getType());
+    EXPECT_EQ(15, limit->intValue());
+}
+
+// This test verifies that exception is thrown if limit is set to 0 while
+// creating lease6-get-page command.
+TEST(CommandCreatorTest, createLease6GetPageZeroLimit) {
+    Lease6Ptr lease6;
+    EXPECT_THROW(CommandCreator::createLease6GetPage(lease6, 0), BadValue);
+}
+
 }