Commit 9faf7581 authored by Thomas Markwalder's avatar Thomas Markwalder

[5314] Implemented Subnet::toElement()

    Provided implementation for Subnet::toElement() which handles all
    common members, reworked Subnet<4/6>::toElement() to use this.
parent 7bb418a5
......@@ -474,22 +474,29 @@ void Subnet6::checkType(Lease::Type type) const {
}
data::ElementPtr
Subnet4::toElement() const {
Subnet::toElement() const {
// Prepare the map
ElementPtr map = Element::createMap();
// Set subnet id
SubnetID id = getID();
map->set("id", Element::create(static_cast<long long>(id)));
// Set relay info
const Subnet::RelayInfo& relay_info = getRelayInfo();
ElementPtr relay = Element::createMap();
relay->set("ip-address", Element::create(relay_info.addr_.toText()));
map->set("relay", relay);
// Set subnet
map->set("subnet", Element::create(toText()));
// Set interface
const std::string& iface = getIface();
map->set("interface", Element::create(iface));
if (!iface.empty()) {
map->set("interface", Element::create(iface));
}
// Set renew-timer
map->set("renew-timer",
Element::create(static_cast<long long>
......@@ -502,6 +509,57 @@ Subnet4::toElement() const {
map->set("valid-lifetime",
Element::create(static_cast<long long>
(getValid().get())));
// Set reservation mode
Subnet::HRMode hrmode = getHostReservationMode();
std::string mode;
switch (hrmode) {
case Subnet::HR_DISABLED:
mode = "disabled";
break;
case Subnet::HR_OUT_OF_POOL:
mode = "out-of-pool";
break;
case Subnet::HR_ALL:
mode = "all";
break;
default:
isc_throw(ToElementError,
"invalid host reservation mode: " << hrmode);
}
map->set("reservation-mode", Element::create(mode));
// Set client-class
const ClientClasses& cclasses = getClientClasses();
if (cclasses.size() > 1) {
isc_throw(ToElementError, "client-class has too many items: "
<< cclasses.size());
} else if (!cclasses.empty()) {
map->set("client-class", Element::create(*cclasses.cbegin()));
}
// Set options
ConstCfgOptionPtr opts = getCfgOption();
map->set("option-data", opts->toElement());
return (map);
}
data::ElementPtr
Subnet4::toElement() const {
// Prepare the map
ElementPtr map = Subnet::toElement();
// Set match-client-id
map->set("match-client-id", Element::create(getMatchClientId()));
// Set DHCP4o6
const Cfg4o6& d4o6 = get4o6();
isc::data::merge(map, d4o6.toElement());
// Set next-server
map->set("next-server", Element::create(getSiaddr().toText()));
// Set pools
const PoolCollection& pools = getPools(Lease::TYPE_V4);
ElementPtr pool_list = Element::createList();
......@@ -533,46 +591,6 @@ Subnet4::toElement() const {
pool_list->add(pool_map);
}
map->set("pools", pool_list);
// Set host reservation-mode
Subnet::HRMode hrmode = getHostReservationMode();
std::string mode;
switch (hrmode) {
case Subnet::HR_DISABLED:
mode = "disabled";
break;
case Subnet::HR_OUT_OF_POOL:
mode = "out-of-pool";
break;
case Subnet::HR_ALL:
mode = "all";
break;
default:
isc_throw(ToElementError,
"invalid host reservation mode: " << hrmode);
}
map->set("reservation-mode", Element::create(mode));
// Set match-client-id
map->set("match-client-id",
Element::create(getMatchClientId()));
// Set next-server
map->set("next-server",
Element::create(getSiaddr().toText()));
// Set DHCP4o6
const Cfg4o6& d4o6 = get4o6();
isc::data::merge(map, d4o6.toElement());
// Set client-class
const ClientClasses& cclasses = getClientClasses();
if (cclasses.size() > 1) {
isc_throw(ToElementError, "client-class has too many items: "
<< cclasses.size());
} else if (!cclasses.empty()) {
map->set("client-class", Element::create(*cclasses.cbegin()));
}
// Set options
ConstCfgOptionPtr opts = getCfgOption();
map->set("option-data", opts->toElement());
// Not supported: interface-id
// Not supported: rapid-commit
return (map);
}
......@@ -580,20 +598,8 @@ Subnet4::toElement() const {
data::ElementPtr
Subnet6::toElement() const {
// Prepare the map
ElementPtr map = Element::createMap();
// Set subnet id
SubnetID id = getID();
map->set("id", Element::create(static_cast<long long>(id)));
// Set relay info
const Subnet::RelayInfo& relay_info = getRelayInfo();
ElementPtr relay = Element::createMap();
relay->set("ip-address", Element::create(relay_info.addr_.toText()));
map->set("relay", relay);
// Set subnet
map->set("subnet", Element::create(toText()));
// Set interface
const std::string& iface = getIface();
map->set("interface", Element::create(iface));
ElementPtr map = Subnet::toElement();
// Set interface-id
const OptionPtr& ifaceid = getInterfaceId();
if (ifaceid) {
......@@ -604,28 +610,16 @@ Subnet6::toElement() const {
std::memcpy(&ifid[0], &bin[0], bin.size());
}
map->set("interface-id", Element::create(ifid));
} else {
map->set("interface-id", Element::create(std::string()));
}
// Set renew-timer
map->set("renew-timer",
Element::create(static_cast<long long>
(getT1().get())));
// Set rebind-timer
map->set("rebind-timer",
Element::create(static_cast<long long>
(getT2().get())));
}
// Set preferred-lifetime
map->set("preferred-lifetime",
Element::create(static_cast<long long>
(getPreferred().get())));
// Set valid-lifetime
map->set("valid-lifetime",
Element::create(static_cast<long long>
(getValid().get())));
// Set rapid-commit
bool rapid_commit = getRapidCommit();
map->set("rapid-commit", Element::create(rapid_commit));
// Set pools
const PoolCollection& pools = getPools(Lease::TYPE_NA);
ElementPtr pool_list = Element::createList();
......@@ -713,35 +707,6 @@ Subnet6::toElement() const {
pdpool_list->add(pool_map);
}
map->set("pd-pools", pdpool_list);
// Set host reservation-mode
Subnet::HRMode hrmode = getHostReservationMode();
std::string mode;
switch (hrmode) {
case Subnet::HR_DISABLED:
mode = "disabled";
break;
case Subnet::HR_OUT_OF_POOL:
mode = "out-of-pool";
break;
case Subnet::HR_ALL:
mode = "all";
break;
default:
isc_throw(ToElementError,
"invalid host reservation mode: " << hrmode);
}
map->set("reservation-mode", Element::create(mode));
// Set client-class
const ClientClasses& cclasses = getClientClasses();
if (cclasses.size() > 1) {
isc_throw(ToElementError, "client-class has too many items: "
<< cclasses.size());
} else if (!cclasses.empty()) {
map->set("client-class", Element::create(*cclasses.cbegin()));
}
// Set options
ConstCfgOptionPtr opts = getCfgOption();
map->set("option-data", opts->toElement());
return (map);
}
......
......@@ -438,7 +438,7 @@ protected:
/// @brief Unparse a subnet object.
///
/// @return A pointer to unparsed subnet configuration.
virtual data::ElementPtr toElement() const = 0;
virtual data::ElementPtr toElement() const;
/// @brief subnet-id
///
......
......@@ -514,7 +514,6 @@ TEST(CfgSubnets4Test, unparseSubnet) {
" \"id\": 123,\n"
" \"subnet\": \"192.0.2.0/26\",\n"
" \"relay\": { \"ip-address\": \"0.0.0.0\" },\n"
" \"interface\": \"\",\n"
" \"match-client-id\": true,\n"
" \"next-server\": \"0.0.0.0\",\n"
" \"renew-timer\": 1,\n"
......@@ -582,7 +581,6 @@ TEST(CfgSubnets4Test, unparsePool) {
" \"id\": 123,\n"
" \"subnet\": \"192.0.2.0/24\",\n"
" \"relay\": { \"ip-address\": \"0.0.0.0\" },\n"
" \"interface\": \"\",\n"
" \"match-client-id\": true,\n"
" \"next-server\": \"0.0.0.0\",\n"
" \"renew-timer\": 1,\n"
......
......@@ -425,7 +425,6 @@ TEST(CfgSubnets6Test, unparseSubnet) {
" \"subnet\": \"2001:db8:1::/48\",\n"
" \"relay\": { \"ip-address\": \"::\" },\n"
" \"interface-id\": \"relay.eth0\",\n"
" \"interface\": \"\",\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
" \"preferred-lifetime\": 3,\n"
......@@ -440,7 +439,6 @@ TEST(CfgSubnets6Test, unparseSubnet) {
" \"id\": 124,\n"
" \"subnet\": \"2001:db8:2::/48\",\n"
" \"relay\": { \"ip-address\": \"2001:db8:ff::2\" },\n"
" \"interface-id\": \"\",\n"
" \"interface\": \"lo\",\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
......@@ -455,7 +453,6 @@ TEST(CfgSubnets6Test, unparseSubnet) {
" \"id\": 125,\n"
" \"subnet\": \"2001:db8:3::/48\",\n"
" \"relay\": { \"ip-address\": \"::\" },\n"
" \"interface-id\": \"\",\n"
" \"interface\": \"eth1\",\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
......@@ -492,8 +489,6 @@ TEST(CfgSubnets6Test, unparsePool) {
" \"id\": 123,\n"
" \"subnet\": \"2001:db8:1::/48\",\n"
" \"relay\": { \"ip-address\": \"::\" },\n"
" \"interface-id\": \"\",\n"
" \"interface\": \"\",\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
" \"preferred-lifetime\": 3,\n"
......@@ -538,8 +533,6 @@ TEST(CfgSubnets6Test, unparsePdPool) {
" \"id\": 123,\n"
" \"subnet\": \"2001:db8:1::/48\",\n"
" \"relay\": { \"ip-address\": \"::\" },\n"
" \"interface-id\": \"\",\n"
" \"interface\": \"\",\n"
" \"renew-timer\": 1,\n"
" \"rebind-timer\": 2,\n"
" \"preferred-lifetime\": 3,\n"
......
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