From: Marcin Siodelski Date: Tue, 19 Mar 2019 12:25:28 +0000 (+0100) Subject: [#103,!277] Implemented the CBControlDHCPv4::databaseConfigApply. X-Git-Tag: Kea-1.6.0-beta~328 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b620e0ac57911cf2e39c0fa610a27b3868be6ea;p=thirdparty%2Fkea.git [#103,!277] Implemented the CBControlDHCPv4::databaseConfigApply. --- diff --git a/src/lib/dhcpsrv/Makefile.am b/src/lib/dhcpsrv/Makefile.am index 89a26aad78..4c862958ff 100644 --- a/src/lib/dhcpsrv/Makefile.am +++ b/src/lib/dhcpsrv/Makefile.am @@ -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 index 0000000000..c34f6f6117 --- /dev/null +++ b/src/lib/dhcpsrv/cb_ctl_dhcp.h @@ -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 +#include +#include + +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 +class CBControlDHCP : public process::CBControlBase { +public: + + /// @brief Constructor. + CBControlDHCP() + : process::CBControlBase() { + } + +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(); + 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 diff --git a/src/lib/dhcpsrv/cb_ctl_dhcp4.cc b/src/lib/dhcpsrv/cb_ctl_dhcp4.cc index 6c46b60628..f6324fba13 100644 --- a/src/lib/dhcpsrv/cb_ctl_dhcp4.cc +++ b/src/lib/dhcpsrv/cb_ctl_dhcp4.cc @@ -6,15 +6,81 @@ #include #include +#include +#include +using namespace isc::data; using namespace isc::process; namespace isc { namespace dhcp { -CBControlDHCPv4::CBControlDHCPv4() - : CBControlBase() { +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 diff --git a/src/lib/dhcpsrv/cb_ctl_dhcp4.h b/src/lib/dhcpsrv/cb_ctl_dhcp4.h index d8ff9f2fbf..3768925b30 100644 --- a/src/lib/dhcpsrv/cb_ctl_dhcp4.h +++ b/src/lib/dhcpsrv/cb_ctl_dhcp4.h @@ -7,18 +7,48 @@ #ifndef CB_CTL_DHCP4_H #define CB_CTL_DHCP4_H -#include +#include #include +#include namespace isc { namespace dhcp { -class CBControlDHCPv4 : public process::CBControlBase { -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 { +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 CBControlDHCPv4Ptr; } // end of namespace isc::dhcp } // end of namespace isc diff --git a/src/lib/dhcpsrv/tests/Makefile.am b/src/lib/dhcpsrv/tests/Makefile.am index 253ef0d727..b5c4610d78 100644 --- a/src/lib/dhcpsrv/tests/Makefile.am +++ b/src/lib/dhcpsrv/tests/Makefile.am @@ -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 index 0000000000..83d998a9ec --- /dev/null +++ b/src/lib/dhcpsrv/tests/cb_ctl_dhcp_unittest.cc @@ -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 + +#include +#include + +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. +} + + +}