]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#103,!277] Implemented the CBControlDHCPv4::databaseConfigApply.
authorMarcin Siodelski <marcin@isc.org>
Tue, 19 Mar 2019 12:25:28 +0000 (13:25 +0100)
committerMarcin Siodelski <marcin@isc.org>
Tue, 26 Mar 2019 07:08:56 +0000 (03:08 -0400)
src/lib/dhcpsrv/Makefile.am
src/lib/dhcpsrv/cb_ctl_dhcp.h [new file with mode: 0644]
src/lib/dhcpsrv/cb_ctl_dhcp4.cc
src/lib/dhcpsrv/cb_ctl_dhcp4.h
src/lib/dhcpsrv/tests/Makefile.am
src/lib/dhcpsrv/tests/cb_ctl_dhcp_unittest.cc [new file with mode: 0644]

index 89a26aad78577ddd0c1a733d5766a8c805dd44f3..4c862958ffe9e27ad26ec279bfcc009b9bd3e71c 100644 (file)
@@ -66,6 +66,7 @@ libkea_dhcpsrv_la_SOURCES += assignable_network.h
 libkea_dhcpsrv_la_SOURCES += base_host_data_source.h
 libkea_dhcpsrv_la_SOURCES += cache_host_data_source.h
 libkea_dhcpsrv_la_SOURCES += callout_handle_store.h
+libkea_dhcpsrv_la_SOURCES += cb_ctl_dhcp.h
 libkea_dhcpsrv_la_SOURCES += cb_ctl_dhcp4.cc cb_ctl_dhcp4.h
 libkea_dhcpsrv_la_SOURCES += cfg_4o6.cc cfg_4o6.h
 libkea_dhcpsrv_la_SOURCES += cfg_consistency.cc cfg_consistency.h
@@ -283,6 +284,7 @@ libkea_dhcpsrv_include_HEADERS = \
        base_host_data_source.h \
        cache_host_data_source.h \
        callout_handle_store.h \
+       cb_ctl_dhcp.h \
        cb_ctl_dhcp4.h \
        cfg_4o6.h \
        cfg_consistency.h \
diff --git a/src/lib/dhcpsrv/cb_ctl_dhcp.h b/src/lib/dhcpsrv/cb_ctl_dhcp.h
new file mode 100644 (file)
index 0000000..c34f6f6
--- /dev/null
@@ -0,0 +1,63 @@
+// Copyright (C) 2019 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 CB_CTL_DHCP_H
+#define CB_CTL_DHCP_H
+
+#include <cc/stamped_value.h>
+#include <process/cb_ctl_base.h>
+#include <dhcpsrv/srv_config.h>
+
+namespace isc {
+namespace dhcp {
+
+/// @brief Base class for implementing mechanisms to control the use
+/// of the Configuration Backends by DHCPv4 and DHCPv6 servers.
+///
+/// It includes common methods used by the DHCPv4 and DHCPv6 specific
+/// derivations.
+///
+/// @tparam ConfigBackendMgrType Type of the Config Backend Manager used
+/// by the server implementing this class. For example, for the DHCPv4
+/// server it will be @c ConfigBackendDHCPv4Mgr.
+template<typename ConfigBackendMgrType>
+class CBControlDHCP : public process::CBControlBase<ConfigBackendMgrType> {
+public:
+
+    /// @brief Constructor.
+    CBControlDHCP()
+        : process::CBControlBase<ConfigBackendMgrType>() {
+    }
+
+protected:
+
+    /// @brief Adds globals fetched from config backend(s) to a SrvConfig instance
+    ///
+    /// Iterates over the given collection of global parameters and adds them to the
+    /// given configuration's list of configured globals.
+    ///
+    /// @param external_cfg SrvConfig instance to update
+    /// @param cb_globals collection of global parameters supplied by configuration
+    /// backend
+    void addGlobalsToConfig(SrvConfigPtr external_cfg,
+                            data::StampedValueCollection& cb_globals) const {
+        const auto& index = cb_globals.get<data::StampedValueNameIndexTag>();
+        for (auto cb_global = index.begin(); cb_global != index.end(); ++cb_global) {
+
+            if ((*cb_global)->amNull()) {
+                continue;
+            }
+
+            external_cfg->addConfiguredGlobal((*cb_global)->getName(), 
+                                              (*cb_global)->getElementValue());
+        }
+    }
+};
+
+} // end of namespace isc::dhcp
+} // end of namespace isc
+
+#endif // CB_CTL_DHCP_H
index 6c46b60628b22308433ce65443c12b53c8e154a4..f6324fba1382538a59a35c54612fcc78956d94f6 100644 (file)
@@ -6,15 +6,81 @@
 
 #include <config.h>
 #include <dhcpsrv/cb_ctl_dhcp4.h>
+#include <dhcpsrv/cfgmgr.h>
+#include <dhcpsrv/dhcpsrv_log.h>
 
+using namespace isc::data;
 using namespace isc::process;
 
 namespace isc {
 namespace dhcp {
 
-CBControlDHCPv4::CBControlDHCPv4()
-    : CBControlBase<ConfigBackendDHCPv4Mgr>() {
+void
+CBControlDHCPv4::databaseConfigApply(const ConfigPtr& srv_cfg,
+                                     const db::BackendSelector& backend_selector,
+                                     const db::ServerSelector& server_selector,
+                                     const boost::posix_time::ptime& lb_modification_time,
+                                     const db::AuditEntryCollection& audit_entries) {
+    // Create the external config into which we'll fetch backend config data.
+    SrvConfigPtr external_cfg = CfgMgr::instance().createExternalCfg();
+
+    // First let's fetch the globals and add them to external config.
+    if (fetchConfigElement(audit_entries, "dhcp4_global_parameter")) {
+        data::StampedValueCollection globals;
+        globals = getMgr().getPool()->getModifiedGlobalParameters4(backend_selector, server_selector,
+                                                                   lb_modification_time);
+        addGlobalsToConfig(external_cfg, globals);
+    }
+
+    // Now we fetch the option definitions and add them.
+    if (fetchConfigElement(audit_entries, "dhcp4_option_def")) {
+        OptionDefContainer option_defs =
+            getMgr().getPool()->getModifiedOptionDefs4(backend_selector, server_selector,
+                                                       lb_modification_time);
+        for (auto option_def = option_defs.begin(); option_def != option_defs.end(); ++option_def) {
+            external_cfg->getCfgOptionDef()->add((*option_def), (*option_def)->getOptionSpaceName());
+        }
+    }
+
+    // Next fetch the options. They are returned as a container of OptionDescriptors.
+    if (fetchConfigElement(audit_entries, "dhcp4_options")) {
+        OptionContainer options = getMgr().getPool()->getModifiedOptions4(backend_selector,
+                                                                          server_selector,
+                                                                          lb_modification_time);
+        for (auto option = options.begin(); option != options.end(); ++option) {
+            external_cfg->getCfgOption()->add((*option), (*option).space_name_);
+        }
+    }
+
+    // Now fetch the shared networks.
+    if (fetchConfigElement(audit_entries, "dhcp4_shared_network")) {
+        SharedNetwork4Collection networks =
+            getMgr().getPool()->getModifiedSharedNetworks4(backend_selector, server_selector,
+                                                           lb_modification_time);
+        for (auto network = networks.begin(); network != networks.end(); ++network) {
+            external_cfg->getCfgSharedNetworks4()->add((*network));
+        }
+    }
+
+    // Next we fetch subnets.
+    if (fetchConfigElement(audit_entries, "dhcp4_subnet")) {
+        Subnet4Collection subnets = getMgr().getPool()->getModifiedSubnets4(backend_selector,
+                                                                            server_selector,
+                                                                            lb_modification_time);
+        for (auto subnet = subnets.begin(); subnet != subnets.end(); ++subnet) {
+            external_cfg->getCfgSubnets4()->add((*subnet));
+        }
+    }
+
+    if (audit_entries.empty()) {
+        CfgMgr::instance().mergeIntoStagingCfg(external_cfg->getSequence());
+
+    } else {
+        CfgMgr::instance().mergeIntoCurrentCfg(external_cfg->getSequence());
+    }
+    LOG_INFO(dhcpsrv_logger, DHCPSRV_CFGMGR_CONFIG4_MERGED);
 }
 
+
 } // end of namespace isc::dhcp
 } // end of namespace isc
index d8ff9f2fbff878d5146f7579999c3cc84fac95aa..3768925b3031c56787c621d6491974f9957f0fa3 100644 (file)
@@ -7,18 +7,48 @@
 #ifndef CB_CTL_DHCP4_H
 #define CB_CTL_DHCP4_H
 
-#include <process/cb_ctl_base.h>
+#include <dhcpsrv/cb_ctl_dhcp.h>
 #include <dhcpsrv/config_backend_dhcp4_mgr.h>
+#include <dhcpsrv/srv_config.h>
 
 namespace isc {
 namespace dhcp {
 
-class CBControlDHCPv4 : public process::CBControlBase<ConfigBackendDHCPv4Mgr> {
-public:
-
-    CBControlDHCPv4();
+/// @brief Implementation of the mechanisms to control the use of
+/// the Configuration Backends by the DHCPv4 server.
+///
+/// It implements fetching and merging DHCPv4 server configuration from
+/// the database into the staging or current configuration.
+///
+/// @tparam ConfigBackendMgrType Type of the Config Backend Manager used
+/// by the server implementing this class. For example, for the DHCPv4
+/// server it will be @c ConfigBackendDHCPv4Mgr.
+class CBControlDHCPv4 : public CBControlDHCP<ConfigBackendDHCPv4Mgr> {
+protected:
+
+    /// @brief Fetches the entire or partial configuration from the database.
+    ///
+    /// This method is called by the starting up server to fetch and merge
+    /// the entire configuration from the database or to fetch configuration
+    /// updates periodically, e.g. as a result of triggering an interval
+    /// timer callback.
+    ///
+    /// @param srv_cfg pointer to the staging configuration that should
+    /// hold the config backends list and other partial configuration read
+    /// from the file in case the method is called upon the server's start
+    /// up. It is a pointer to the current server configuration if the
+    /// method is called to fetch configuration updates.
+    /// @param fetch_updates_only boolean value indicating if the method is
+    /// called upon the server start up (false) or it is called to fetch
+    /// configuration updates (true).
+    virtual void databaseConfigApply(const process::ConfigPtr& srv_cfg,
+                                     const db::BackendSelector& backend_selector,
+                                     const db::ServerSelector& server_selector,
+                                     const boost::posix_time::ptime& lb_modification_time,
+                                     const db::AuditEntryCollection& audit_entries);
 };
 
+typedef boost::shared_ptr<CBControlDHCPv4> CBControlDHCPv4Ptr;
 
 } // end of namespace isc::dhcp
 } // end of namespace isc
index 253ef0d7270e2fb658bcf6b6dc621dd9c965bbc2..b5c4610d78d47a012ac67372542d3d8fefacbe6c 100644 (file)
@@ -63,6 +63,7 @@ libdhcpsrv_unittests_SOURCES += alloc_engine_hooks_unittest.cc
 libdhcpsrv_unittests_SOURCES += alloc_engine4_unittest.cc
 libdhcpsrv_unittests_SOURCES += alloc_engine6_unittest.cc
 libdhcpsrv_unittests_SOURCES += callout_handle_store_unittest.cc
+libdhcpsrv_unittests_SOURCES += cb_ctl_dhcp_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_db_access_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_duid_unittest.cc
 libdhcpsrv_unittests_SOURCES += cfg_expiration_unittest.cc
diff --git a/src/lib/dhcpsrv/tests/cb_ctl_dhcp_unittest.cc b/src/lib/dhcpsrv/tests/cb_ctl_dhcp_unittest.cc
new file mode 100644 (file)
index 0000000..83d998a
--- /dev/null
@@ -0,0 +1,32 @@
+// Copyright (C) 2019 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 <config.h>
+
+#include <dhcpsrv/cb_ctl_dhcp4.h>
+#include <gtest/gtest.h>
+
+using namespace isc::dhcp;
+
+namespace {
+
+/// @brief Test fixture class for @c CBControlDHCPv4 unit tests.
+class TestCBControlDHCPv4 : public CBControlDHCPv4 {
+public:
+
+    using CBControlDHCPv4::databaseConfigApply;
+};
+
+
+// This test verifies that the configuration updates are
+// merged into the current configuration.
+TEST(CBControlDHCPv4Test, databaseConfigApplyUpdates) {
+    TestCBControlDHCPv4 ctl;
+    /// @todo implement the actual test.
+}
+
+
+}