Commit fb98f142 authored by Thomas Markwalder's avatar Thomas Markwalder

[5314] Added Pool::toElement() and Pool<4/6>::toElement()

    Extracted logic for elementizing pools into toElement methods
    in Pool, Pool4, and Pool6 classes.
parent 9faf7581
......@@ -10,6 +10,7 @@
#include <sstream>
using namespace isc::asiolink;
using namespace isc::data;
namespace isc {
namespace dhcp {
......@@ -74,6 +75,46 @@ Pool4::Pool4( const isc::asiolink::IOAddress& prefix, uint8_t prefix_len)
capacity_ = addrsInRange(prefix, last_);
}
data::ElementPtr
Pool::toElement() const {
// Prepare the map
ElementPtr map = Element::createMap();
// Set user-context
ConstElementPtr context = getContext();
if (!isNull(context)) {
map->set("user-context", context);
}
// Set pool options
ConstCfgOptionPtr opts = getCfgOption();
map->set("option-data", opts->toElement());
return (map);
}
data::ElementPtr
Pool4::toElement() const {
// Prepare the map
ElementPtr map = Pool::toElement();
// Set pool
const IOAddress& first = getFirstAddress();
const IOAddress& last = getLastAddress();
std::string range = first.toText() + "-" + last.toText();
// Try to output a prefix (vs a range)
int prefix_len = prefixLengthFromRange(first, last);
if (prefix_len >= 0) {
std::ostringstream oss;
oss << first.toText() << "/" << prefix_len;
range = oss.str();
}
map->set("pool", Element::create(range));
return (map);
}
Pool6::Pool6(Lease::Type type, const isc::asiolink::IOAddress& first,
const isc::asiolink::IOAddress& last)
: Pool(type, first, last), prefix_len_(128), pd_exclude_option_() {
......@@ -235,6 +276,72 @@ Pool6::init(const Lease::Type& type,
}
}
data::ElementPtr
Pool6::toElement() const {
// Prepare the map
ElementPtr map = Pool::toElement();
switch (getType()) {
case Lease::TYPE_NA: {
const IOAddress& first = getFirstAddress();
const IOAddress& last = getLastAddress();
std::string range = first.toText() + "-" + last.toText();
// Try to output a prefix (vs a range)
int prefix_len = prefixLengthFromRange(first, last);
if (prefix_len >= 0) {
std::ostringstream oss;
oss << first.toText() << "/" << prefix_len;
range = oss.str();
}
map->set("pool", Element::create(range));
break;
}
case Lease::TYPE_PD: {
// Set prefix
const IOAddress& prefix = getFirstAddress();
map->set("prefix", Element::create(prefix.toText()));
// Set prefix-len (get it from min - max)
const IOAddress& last = getLastAddress();
int prefix_len = prefixLengthFromRange(prefix, last);
if (prefix_len < 0) {
// The pool is bad: give up
isc_throw(ToElementError, "invalid prefix range "
<< prefix.toText() << "-" << last.toText());
}
// Set delegated-len
uint8_t len = getLength();
map->set("delegated-len", Element::create(static_cast<int>(len)));
// Set excluded prefix
const Option6PDExcludePtr& xopt = getPrefixExcludeOption();
if (xopt) {
const IOAddress& xprefix = xopt->getExcludedPrefix(prefix, len);
map->set("excluded-prefix", Element::create(xprefix.toText()));
uint8_t xlen = xopt->getExcludedPrefixLength();
map->set("excluded-prefix-len",
Element::create(static_cast<int>(xlen)));
} else {
map->set("excluded-prefix", Element::create(std::string("::")));
map->set("excluded-prefix-len", Element::create(0));
}
break;
}
default:
isc_throw(ToElementError, "Lease type: " << getType()
<< ", unsupported for Pool6");
break;
}
return (map);
}
std::string
Pool6::toText() const {
std::ostringstream s;
......
......@@ -106,6 +106,11 @@ public:
user_context_ = ctx;
}
/// @brief Unparse a pool object.
///
/// @return A pointer to unparsed pool configuration.
virtual data::ElementPtr toElement() const;
protected:
/// @brief protected constructor
......@@ -182,6 +187,11 @@ public:
/// @param prefix_len specifies length of the prefix of the pool
Pool4(const isc::asiolink::IOAddress& prefix,
uint8_t prefix_len);
/// @brief Unparse a Pool4 object.
///
/// @return A pointer to unparsed Pool4 configuration.
virtual data::ElementPtr toElement() const;
};
/// @brief a pointer an IPv4 Pool
......@@ -275,6 +285,11 @@ public:
return (pd_exclude_option_);
}
/// @brief Unparse a Pool6 object.
///
/// @return A pointer to unparsed Pool6 configuration.
virtual data::ElementPtr toElement() const;
/// @brief returns textual representation of the pool
///
/// @return textual representation
......
......@@ -565,30 +565,8 @@ Subnet4::toElement() const {
ElementPtr pool_list = Element::createList();
for (PoolCollection::const_iterator pool = pools.cbegin();
pool != pools.cend(); ++pool) {
// Prepare the map for a pool (@todo move this code to pool.cc)
ElementPtr pool_map = Element::createMap();
// Set pool
const IOAddress& first = (*pool)->getFirstAddress();
const IOAddress& last = (*pool)->getLastAddress();
std::string range = first.toText() + "-" + last.toText();
// Try to output a prefix (vs a range)
int prefix_len = prefixLengthFromRange(first, last);
if (prefix_len >= 0) {
std::ostringstream oss;
oss << first.toText() << "/" << prefix_len;
range = oss.str();
}
pool_map->set("pool", Element::create(range));
// Set user-context
ConstElementPtr context = (*pool)->getContext();
if (!isNull(context)) {
pool_map->set("user-context", context);
}
// Set pool options
ConstCfgOptionPtr opts = (*pool)->getCfgOption();
pool_map->set("option-data", opts->toElement());
// Push on the pool list
pool_list->add(pool_map);
// Add the elementized pool to the list
pool_list->add((*pool)->toElement());
}
map->set("pools", pool_list);
......
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