Commit b866cced authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[5272] LeaseX::toElement() implemented

parent a2924fcc
// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
......@@ -10,6 +10,7 @@
#include <iostream>
using namespace isc::util;
using namespace isc::data;
using namespace std;
namespace isc {
......@@ -220,6 +221,30 @@ Lease4::operator=(const Lease4& other) {
return (*this);
}
isc::data::ElementPtr
Lease4::toElement() const {
// Prepare the map
ElementPtr map = Element::createMap();
map->set("ip-address", Element::create(addr_.toText()));
map->set("subnet-id", Element::create(static_cast<long int>(subnet_id_)));
map->set("hw-address", Element::create(hwaddr_->toText(false)));
if (client_id_) {
map->set("client-id", Element::create(client_id_->toText()));
}
map->set("cltt", Element::create(cltt_));
map->set("valid-lft", Element::create(static_cast<long int>(valid_lft_)));
map->set("fqdn-fwd", Element::create(fqdn_fwd_));
map->set("fqdn-rev", Element::create(fqdn_rev_));
map->set("hostname", Element::create(hostname_));
map->set("state", Element::create(static_cast<int>(state_)));
return (map);
}
Lease6::Lease6(Lease::Type type, const isc::asiolink::IOAddress& addr,
DuidPtr duid, uint32_t iaid, uint32_t preferred, uint32_t valid,
uint32_t t1, uint32_t t2, SubnetID subnet_id,
......@@ -362,6 +387,35 @@ Lease6::operator==(const Lease6& other) const {
state_ == other.state_);
}
isc::data::ElementPtr
Lease6::toElement() const {
// Prepare the map
ElementPtr map = Element::createMap();
map->set("ip-address", Element::create(addr_.toText()));
map->set("type", Element::create(typeToText(type_)));
map->set("prefix-len", Element::create(prefixlen_));
map->set("iaid", Element::create(static_cast<long int>(iaid_)));
map->set("duid", Element::create(duid_->toText()));
map->set("subnet-id", Element::create(static_cast<long int>(subnet_id_)));
map->set("cltt", Element::create(cltt_));
map->set("preferred-lft", Element::create(static_cast<long int>(preferred_lft_)));
map->set("valid-lft", Element::create(static_cast<long int>(valid_lft_)));
map->set("fqdn-fwd", Element::create(fqdn_fwd_));
map->set("fqdn-rev", Element::create(fqdn_rev_));
map->set("hostname", Element::create(hostname_));
if (hwaddr_) {
map->set("hw-address", Element::create(hwaddr_->toText(false)));
}
map->set("state", Element::create(static_cast<long int>(state_)));
return (map);
}
std::ostream&
operator<<(std::ostream& os, const Lease& lease) {
os << lease.toText();
......
......@@ -11,6 +11,7 @@
#include <dhcp/duid.h>
#include <dhcp/option.h>
#include <dhcp/hwaddr.h>
#include <cc/cfg_to_element.h>
namespace isc {
namespace dhcp {
......@@ -25,7 +26,7 @@ typedef uint32_t SubnetID;
///
/// This structure holds all information that is common between IPv4 and IPv6
/// leases.
struct Lease {
struct Lease : public isc::data::CfgToElement {
/// @brief Type of lease or pool
typedef enum {
......@@ -400,6 +401,9 @@ struct Lease4 : public Lease {
/// @param probation_period valid lifetime will be set to this value
void decline(uint32_t probation_period);
/// @brief Return the JSON representation of a lease
virtual isc::data::ElementPtr toElement() const;
/// @todo: Add DHCPv4 failover related fields here
};
......@@ -532,6 +536,9 @@ struct Lease6 : public Lease {
///
/// @return String form of the lease
virtual std::string toText() const;
/// @brief Return the JSON representation of a lease
virtual isc::data::ElementPtr toElement() const;
};
/// @brief Pointer to a Lease6 structure.
......
......@@ -9,6 +9,7 @@
#include <dhcp/duid.h>
#include <dhcpsrv/lease.h>
#include <util/pointer_util.h>
#include <cc/data.h>
#include <gtest/gtest.h>
#include <vector>
#include <sstream>
......@@ -16,6 +17,7 @@
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
using namespace isc::data;
namespace {
......@@ -423,6 +425,56 @@ TEST_F(Lease4Test, toText) {
EXPECT_EQ(expected.str(), lease.toText());
}
// Verify that Lease4 structure can be converted to JSON properly.
TEST_F(Lease4Test, toElement) {
const time_t current_time = 12345678;
Lease4 lease(IOAddress("192.0.2.3"), hwaddr_, clientid_, 3600, 123,
456, current_time, 789, true, true, "urania.example.org");
ElementPtr l = lease.toElement();
ASSERT_TRUE(l);
ASSERT_TRUE(l->contains("ip-address"));
EXPECT_EQ("192.0.2.3", l->get("ip-address")->stringValue());
ASSERT_TRUE(l->contains("valid-lft"));
EXPECT_EQ(3600, l->get("valid-lft")->intValue());
ASSERT_TRUE(l->contains("cltt"));
EXPECT_EQ(current_time, l->get("cltt")->intValue());
ASSERT_TRUE(l->contains("hw-address"));
EXPECT_EQ(hwaddr_->toText(false), l->get("hw-address")->stringValue());
ASSERT_TRUE(l->contains("client-id"));
EXPECT_EQ(clientid_->toText(), l->get("client-id")->stringValue());
ASSERT_TRUE(l->contains("subnet-id"));
EXPECT_EQ(789, l->get("subnet-id")->intValue());
ASSERT_TRUE(l->contains("state"));
EXPECT_EQ(static_cast<int>(Lease::STATE_DEFAULT), l->get("state")->intValue());
ASSERT_TRUE(l->contains("fqdn-fwd"));
EXPECT_TRUE(l->get("fqdn-fwd")->boolValue());
ASSERT_TRUE(l->contains("fqdn-rev"));
EXPECT_TRUE(l->get("fqdn-rev")->boolValue());
ASSERT_TRUE(l->contains("hostname"));
EXPECT_EQ("urania.example.org", l->get("hostname")->stringValue());
// Now let's try with a lease without client-id.
lease.client_id_.reset();
l = lease.toElement();
EXPECT_FALSE(l->contains("client-id"));
}
// Verify that decline() method properly clears up specific fields.
TEST_F(Lease4Test, decline) {
......@@ -803,7 +855,7 @@ TEST(Lease6Test, hasIdenticalFqdn) {
false, false)));
}
// Verify that toText() method reports Lease4 structure properly.
// Verify that toText() method reports Lease6 structure properly.
TEST(Lease6Test, toText) {
HWAddrPtr hwaddr(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
......@@ -848,6 +900,71 @@ TEST(Lease6Test, toText) {
EXPECT_EQ(expected.str(), lease.toText());
}
// Verify that Lease6 structure can be converted to JSON properly.
TEST(Lease6Test, toElement) {
HWAddrPtr hwaddr(new HWAddr(HWADDR, sizeof(HWADDR), HTYPE_ETHER));
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;
lease.hostname_ = "urania.example.org";
ElementPtr l = lease.toElement();
ASSERT_TRUE(l);
ASSERT_TRUE(l->contains("ip-address"));
EXPECT_EQ("2001:db8::1", l->get("ip-address")->stringValue());
ASSERT_TRUE(l->contains("type"));
EXPECT_EQ("IA_NA", l->get("type")->stringValue());
ASSERT_TRUE(l->contains("prefix-len"));
EXPECT_EQ(128, l->get("prefix-len")->intValue());
ASSERT_TRUE(l->contains("iaid"));
EXPECT_EQ(123456, l->get("iaid")->intValue());
ASSERT_TRUE(l->contains("preferred-lft"));
EXPECT_EQ(400, l->get("preferred-lft")->intValue());
ASSERT_TRUE(l->contains("valid-lft"));
EXPECT_EQ(800, l->get("valid-lft")->intValue());
ASSERT_TRUE(l->contains("duid"));
EXPECT_EQ("00:01:02:03:04:05:06:0a:0b:0c:0d:0e:0f",
l->get("duid")->stringValue());
ASSERT_TRUE(l->contains("hw-address"));
EXPECT_EQ(hwaddr->toText(false), l->get("hw-address")->stringValue());
ASSERT_TRUE(l->contains("subnet-id"));
EXPECT_EQ(5678, l->get("subnet-id")->intValue());
ASSERT_TRUE(l->contains("state"));
EXPECT_EQ(static_cast<int>(Lease::STATE_DECLINED),
l->get("state")->intValue());
ASSERT_TRUE(l->contains("fqdn-fwd"));
EXPECT_FALSE(l->get("fqdn-fwd")->boolValue());
ASSERT_TRUE(l->contains("fqdn-rev"));
EXPECT_FALSE(l->get("fqdn-rev")->boolValue());
ASSERT_TRUE(l->contains("hostname"));
EXPECT_EQ("urania.example.org", l->get("hostname")->stringValue());
// Now let's try with a lease without hardware address.
lease.hwaddr_.reset();
l = lease.toElement();
EXPECT_FALSE(l->contains("hw-address"));
}
// Verify that the lease states are correctly returned in the textual format.
TEST(Lease6Test, stateToText) {
EXPECT_EQ("default", Lease6::statesToText(Lease::STATE_DEFAULT));
......
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