Commit e1f7b0ed authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[#78,!85] HA command creator supports leaseX-get-page commands.

parent 33931432
......@@ -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) ||
......
......@@ -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.
......
......@@ -10,12 +10,14 @@
#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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment