]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5351] Factorized user context stuff
authorFrancis Dupont <fdupont@isc.org>
Tue, 28 Nov 2017 14:09:13 +0000 (15:09 +0100)
committerFrancis Dupont <fdupont@isc.org>
Tue, 28 Nov 2017 14:09:13 +0000 (15:09 +0100)
src/lib/dhcpsrv/Makefile.am
src/lib/dhcpsrv/pool.cc
src/lib/dhcpsrv/pool.h
src/lib/dhcpsrv/subnet.cc
src/lib/dhcpsrv/subnet.h
src/lib/dhcpsrv/user_context.cc [new file with mode: 0644]
src/lib/dhcpsrv/user_context.h [new file with mode: 0644]

index cc3e14863405fd9639dbbbe44758be064cde7223..82197774f63ca3a33985773066f3564345f79396 100644 (file)
@@ -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
 
index b57b0fb06a6b3faf7888b521a256577660bcd1b8..1542b8ca1876388e3b062f716873bc032ef6c098 100644 (file)
@@ -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();
index 56d769891d6899c8134f7f39b3e0bb5ddc71db46..2660b90f6b0320b936c7f4f5936c2c08f064c140 100644 (file)
@@ -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
index d8482dd3b3735b0a5f5acd9c75c3f01ba3e4e1cc..945256b7b036de7f9eaf57d6b0e683be36e016b0 100644 (file)
@@ -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());
index 03b1f7784c57415f6df0ee1285142a73a810fdb6..a7fc54355563bc849453e70b008a9d8495ed5165 100644 (file)
@@ -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
diff --git a/src/lib/dhcpsrv/user_context.cc b/src/lib/dhcpsrv/user_context.cc
new file mode 100644 (file)
index 0000000..f6d273c
--- /dev/null
@@ -0,0 +1,34 @@
+// 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
diff --git a/src/lib/dhcpsrv/user_context.h b/src/lib/dhcpsrv/user_context.h
new file mode 100644 (file)
index 0000000..2487f4d
--- /dev/null
@@ -0,0 +1,57 @@
+// 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