Commit 45c67bfb authored by Francis Dupont's avatar Francis Dupont
Browse files

[5351] Factorized user context stuff

parent 29a504c8
......@@ -159,6 +159,7 @@ libkea_dhcpsrv_la_SOURCES += subnet_id.h
libkea_dhcpsrv_la_SOURCES += subnet_selector.h
libkea_dhcpsrv_la_SOURCES += timer_mgr.cc timer_mgr.h
libkea_dhcpsrv_la_SOURCES += triplet.h
libkea_dhcpsrv_la_SOURCES += user_context.cc user_context.h
libkea_dhcpsrv_la_SOURCES += utils.h
libkea_dhcpsrv_la_SOURCES += writable_host_data_source.h
......@@ -295,6 +296,7 @@ libkea_dhcpsrv_include_HEADERS = \
subnet_selector.h \
timer_mgr.h \
triplet.h \
user_context.h \
utils.h \
writable_host_data_source.h
......
......@@ -80,21 +80,8 @@ Pool::toElement() const {
// Prepare the map
ElementPtr map = Element::createMap();
// Set user-context extracting comment
ConstElementPtr context = getContext();
if (!isNull(context)) {
if ((context->getType() == Element::map) &&
context->contains("comment")) {
ElementPtr copied = isc::data::copy(context);
map->set("comment", copied->get("comment"));
copied->remove("comment");
if (copied->size() > 0) {
map->set("user-context", copied);
}
} else {
map->set("user-context", context);
}
}
// Set user-context
contextToElement(map);
// Set pool options
ConstCfgOptionPtr opts = getCfgOption();
......
......@@ -13,6 +13,7 @@
#include <cc/data.h>
#include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/lease.h>
#include <dhcpsrv/user_context.h>
#include <boost/shared_ptr.hpp>
#include <vector>
......@@ -24,7 +25,7 @@ namespace dhcp {
///
/// Stores information about pool of IPv4 or IPv6 addresses.
/// That is a basic component of a configuration.
class Pool {
class Pool : public UserContext {
public:
/// @note:
......@@ -95,17 +96,6 @@ public:
return (cfg_option_);
}
/// @brief Returns const pointer to the user context.
data::ConstElementPtr getContext() const {
return (user_context_);
}
/// @brief Sets user context.
/// @param ctx user context to be stored.
void setContext(const data::ConstElementPtr& ctx) {
user_context_ = ctx;
}
/// @brief Unparse a pool object.
///
/// @return A pointer to unparsed pool configuration.
......@@ -163,9 +153,6 @@ protected:
/// @brief Pointer to the option data configuration for this pool.
CfgOptionPtr cfg_option_;
/// @brief Pointer to the user context (may be NULL)
data::ConstElementPtr user_context_;
};
/// @brief Pool information for IPv4 addresses
......
......@@ -558,6 +558,9 @@ data::ElementPtr
Subnet::toElement() const {
ElementPtr map = Element::createMap();
// Add user-context
contextToElement(map);
// Set subnet id
SubnetID id = getID();
map->set("id", Element::create(static_cast<long long>(id)));
......@@ -565,23 +568,6 @@ Subnet::toElement() const {
// Set subnet
map->set("subnet", Element::create(toText()));
// Add user-context, but only if defined. Omit if it was not.
// Extract comment so it will be printed first.
ConstElementPtr ctx = getContext();
if (ctx) {
if ((ctx->getType() == Element::map) &&
ctx->contains("comment")) {
ElementPtr copied = isc::data::copy(ctx);
map->set("comment",copied->get("comment"));
copied->remove("comment");
if (copied->size() > 0) {
map->set("user-context", copied);
}
} else {
map->set("user-context", ctx);
}
}
return (map);
}
......@@ -634,6 +620,8 @@ Subnet6::toElement() const {
pool != pools.cend(); ++pool) {
// Prepare the map for a pool (@todo move this code to pool.cc)
ElementPtr pool_map = Element::createMap();
// Set user-context
(*pool)->contextToElement(pool_map);
// Set pool
const IOAddress& first = (*pool)->getFirstAddress();
const IOAddress& last = (*pool)->getLastAddress();
......@@ -646,21 +634,6 @@ Subnet6::toElement() const {
range = oss.str();
}
pool_map->set("pool", Element::create(range));
// Set user-context
ConstElementPtr context = (*pool)->getContext();
if (!isNull(context)) {
if ((context->getType() == Element::map) &&
context->contains("comment")) {
ElementPtr copied = isc::data::copy(context);
pool_map->set("comment", copied->get("comment"));
copied->remove("comment");
if (copied->size() > 0) {
pool_map->set("user-context", copied);
}
} else {
pool_map->set("user-context", context);
}
}
// Set pool options
ConstCfgOptionPtr opts = (*pool)->getCfgOption();
pool_map->set("option-data", opts->toElement());
......@@ -680,6 +653,8 @@ Subnet6::toElement() const {
}
// Prepare the map for a pd-pool
ElementPtr pool_map = Element::createMap();
// Set user-context
pdpool->contextToElement(pool_map);
// Set prefix
const IOAddress& prefix = pdpool->getFirstAddress();
pool_map->set("prefix", Element::create(prefix.toText()));
......@@ -710,21 +685,6 @@ Subnet6::toElement() const {
Element::create(static_cast<int>(xlen)));
}
// Set user-context
ConstElementPtr context = pdpool->getContext();
if (!isNull(context)) {
if ((context->getType() == Element::map) &&
context->contains("comment")) {
ElementPtr copied = isc::data::copy(context);
pool_map->set("comment", copied->get("comment"));
copied->remove("comment");
if (copied->size() > 0) {
pool_map->set("user-context", copied);
}
} else {
pool_map->set("user-context", context);
}
}
// Set pool options
ConstCfgOptionPtr opts = pdpool->getCfgOption();
pool_map->set("option-data", opts->toElement());
......
......@@ -15,6 +15,7 @@
#include <dhcpsrv/pool.h>
#include <dhcpsrv/subnet_id.h>
#include <dhcpsrv/triplet.h>
#include <dhcpsrv/user_context.h>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/indexed_by.hpp>
......@@ -27,7 +28,7 @@
namespace isc {
namespace dhcp {
class Subnet : public data::CfgToElement {
class Subnet : public UserContext, public data::CfgToElement {
// Assignable network is our friend to allow it to call
// @ref Subnet::setSharedNetwork private function.
......@@ -219,19 +220,6 @@ private:
shared_network_ = shared_network;
}
public:
/// @brief Sets user context.
/// @param ctx user context to be stored.
void setContext(const data::ConstElementPtr& ctx) {
user_context_ = ctx;
}
/// @brief Returns const pointer to the user context.
data::ConstElementPtr getContext() const {
return (user_context_);
}
protected:
/// @brief Returns all pools (non-const variant)
///
......@@ -362,9 +350,6 @@ protected:
/// @brief Pointer to a shared network that subnet belongs to.
WeakNetworkPtr shared_network_;
/// @brief Pointer to the user context (may be NULL)
data::ConstElementPtr user_context_;
};
/// @brief A generic pointer to either Subnet4 or Subnet6 object
......
// Copyright (C) 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
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <dhcpsrv/user_context.h>
using namespace isc::data;
namespace isc {
namespace dhcp {
void
UserContext::contextToElement(ElementPtr map) const{
// Set user-context extracting comment
ConstElementPtr context = getContext();
if (!isNull(context)) {
if ((context->getType() == Element::map) &&
context->contains("comment")) {
ElementPtr copied = isc::data::copy(context);
map->set("comment", copied->get("comment"));
copied->remove("comment");
if (copied->size() > 0) {
map->set("user-context", copied);
}
} else {
map->set("user-context", context);
}
}
}
}; // end of isc::dhcp namespace
}; // end of isc namespace
// Copyright (C) 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
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef USER_CONTEXT_H
#define USER_CONTEXT_H
#include <cc/data.h>
#include <boost/shared_ptr.hpp>
namespace isc {
namespace dhcp {
/// @brief base class for user context
class UserContext {
public:
/// @brief constructor
UserContext() {
}
/// @brief destructor
~UserContext() {
}
/// @brief Returns const pointer to the user context.
data::ConstElementPtr getContext() const {
return (user_context_);
}
/// @brief Sets user context.
/// @param ctx user context to be stored.
void setContext(const data::ConstElementPtr& ctx) {
user_context_ = ctx;
}
/// @brief Merge unparse a user_context object.
///
/// Add user-context to map, but only if defined. Omit if it was not.
/// Extract comment so it will be pretty-printed first.
///
/// @param map A pointer to map where the user context will be unparsed.
void contextToElement(data::ElementPtr map) const;
protected:
/// @brief Pointer to the user context (may be NULL)
data::ConstElementPtr user_context_;
};
} // end of isc::dhcp namespace
} // end of isc namespace
#endif // USER_CONTEXT_H
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