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
subnet_selector.h \
timer_mgr.h \
triplet.h \
+ user_context.h \
utils.h \
writable_host_data_source.h
// 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();
#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>
///
/// 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:
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.
/// @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
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)));
// 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);
}
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();
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());
}
// 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()));
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());
#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>
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.
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)
///
/// @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
--- /dev/null
+// 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
--- /dev/null
+// 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