From: Francis Dupont Date: Sun, 20 Jan 2019 16:16:06 +0000 (+0100) Subject: [421-create-config-backend-for-dhcpv6] Updated config backend for DHCPv6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02d43845dec4e96e1d984185c7f102227152fcd4;p=thirdparty%2Fkea.git [421-create-config-backend-for-dhcpv6] Updated config backend for DHCPv6 --- diff --git a/src/bin/dhcp6/dhcp6_messages.mes b/src/bin/dhcp6/dhcp6_messages.mes index 9079efb09e..8896715477 100644 --- a/src/bin/dhcp6/dhcp6_messages.mes +++ b/src/bin/dhcp6/dhcp6_messages.mes @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") +# Copyright (C) 2012-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 @@ -472,6 +472,11 @@ This warning message is issued when current server configuration specifies no interfaces that server should listen on, or specified interfaces are not configured to receive the traffic. +% DHCP6_OPEN_CONFIG_DB Opening configuration database: %1 +This message is printed when the DHCPv6 server is attempting to open a +configuration database. The database access string with password redacted +is logged. + % DHCP6_OPEN_SOCKET opening service sockets on port %1 A debug message issued during startup, this indicates that the IPv6 DHCP server is about to open sockets on the specified port. diff --git a/src/bin/dhcp6/json_config_parser.cc b/src/bin/dhcp6/json_config_parser.cc index 26d4831061..2593030a7b 100644 --- a/src/bin/dhcp6/json_config_parser.cc +++ b/src/bin/dhcp6/json_config_parser.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-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 @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -62,6 +63,7 @@ using namespace isc::data; using namespace isc::dhcp; using namespace isc::asiolink; using namespace isc::hooks; +using namespace isc::process; namespace { @@ -619,8 +621,8 @@ configureDhcp6Server(Dhcpv6Srv& server, isc::data::ConstElementPtr config_set, } if (config_pair.first == "config-control") { - process::ConfigControlParser parser; - process::ConfigControlInfoPtr config_ctl_info = parser.parse(config_pair.second); + ConfigControlParser parser; + ConfigControlInfoPtr config_ctl_info = parser.parse(config_pair.second); CfgMgr::instance().getStagingCfg()->setConfigControlInfo(config_ctl_info); continue; } @@ -711,6 +713,11 @@ configureDhcp6Server(Dhcpv6Srv& server, isc::data::ConstElementPtr config_set, const HooksConfig& libraries = CfgMgr::instance().getStagingCfg()->getHooksConfig(); libraries.loadLibraries(); + +#ifdef CONFIG_BACKEND // Disabled until we restart CB work + // If there are config backends, fetch and merge into staging config + databaseConfigFetch(srv_cfg, mutable_cfg); +#endif } catch (const isc::Exception& ex) { LOG_ERROR(dhcp6_logger, DHCP6_PARSER_COMMIT_FAIL).arg(ex.what()); @@ -744,5 +751,48 @@ configureDhcp6Server(Dhcpv6Srv& server, isc::data::ConstElementPtr config_set, return (answer); } +bool databaseConfigConnect(const SrvConfigPtr& srv_cfg) { + // We need to get rid of any existing backends. These would be any + // opened by previous configuration cycle. + ConfigBackendDHCPv6Mgr& mgr = ConfigBackendDHCPv6Mgr::instance(); + mgr.delAllBackends(); + + // Fetch the config-control info. + ConstConfigControlInfoPtr config_ctl = srv_cfg->getConfigControlInfo(); + if (!config_ctl || config_ctl->getConfigDatabases().empty()) { + // No config dbs, nothing to do. + return (false); + } + + // Iterate over the configured DBs and instantiate them. + for (auto db : config_ctl->getConfigDatabases()) { + LOG_INFO(dhcp6_logger, DHCP6_OPEN_CONFIG_DB) + .arg(db.redactedAccessString()); + mgr.addBackend(db.getAccessString()); + } + + // Let the caller know we have opened DBs. + return (true); +} + +void databaseConfigFetch(const SrvConfigPtr& srv_cfg, ElementPtr /* mutable_cfg */) { + + // Close any existing CB databasess, then open all in srv_cfg (if any) + if (!databaseConfigConnect(srv_cfg)) { + // There are no CB databases so we're done + return; + } + + // @todo Fetching and merging the configuration falls under #99 + // ConfigBackendDHCPv6Mgr& mgr = ConfigBackendDHCPv6Mgr::instance(); + // Next we have to fetch the pieces we care about it and merge them + // probably in this order? + // globals + // option defs + // options + // shared networks + // subnets +} + }; // end of isc::dhcp namespace }; // end of isc namespace diff --git a/src/bin/dhcp6/json_config_parser.h b/src/bin/dhcp6/json_config_parser.h index 3267eac4ba..95a42dc126 100644 --- a/src/bin/dhcp6/json_config_parser.h +++ b/src/bin/dhcp6/json_config_parser.h @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2017 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-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 @@ -44,6 +44,35 @@ isc::data::ConstElementPtr configureDhcp6Server(Dhcpv6Srv& server, isc::data::ConstElementPtr config_set, bool check_only = false); +/// @brief Attempts to connect to configured CB databases +/// +/// First, this function will close all existing CB backends. It +/// will then attempt to connect to all of the CB backends defined +/// in the given SrvConfig (if any). +/// +/// It will return true if there are configured CB databases, +/// and false otherwise. Any errors encountered along the way +/// should generate throws. +/// +/// @param srv_cfg Server configuration from which to get +/// the config-control information to use. +/// +/// @return True if there are configured CB databases, false if not. +bool +databaseConfigConnect(const SrvConfigPtr& srv_cfg); + +/// @brief Fetch configuration from CB databases and merge it into the given configuration +/// +/// It will call @c databaseConfigConnect, passing in the given server configuration. If +/// that call results in open CB databases, the function will then proceed to fetch +/// configuration components from said databases and merge them into the given server +/// configuration. +/// +/// @param srv_cfg Server configuration into which database configuration should be merged +/// @param mutable_cfg parsed configuration from the configuration file plus default values (ignored) +void +databaseConfigFetch(const SrvConfigPtr& srv_cfg, isc::data::ElementPtr mutable_cfg); + }; // end of isc::dhcp namespace }; // end of isc namespace diff --git a/src/bin/dhcp6/tests/config_parser_unittest.cc b/src/bin/dhcp6/tests/config_parser_unittest.cc index f2f96d9c47..395f484071 100644 --- a/src/bin/dhcp6/tests/config_parser_unittest.cc +++ b/src/bin/dhcp6/tests/config_parser_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2012-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 @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -6919,6 +6920,19 @@ TEST_F(Dhcp6ParserTest, globalReservations) { EXPECT_EQ(11, static_cast(opt_prf->getValue())); } +// Rather than disable these tests they are compiled out. This avoids them +// reporting as disbabled and thereby drawing attention to them. +#ifdef CONFIG_BACKEND +// This test verifies that configuration control with unsupported type fails +TEST_F(Dhcp6ParserTest, configControlInfoNoFactory) { + string config = PARSER_CONFIGS[6]; + extractConfig(config); + + // Should fail because "type=mysql" has no factories. + configure(config, CONTROL_RESULT_ERROR, + "The type of the configuration backend: 'mysql' is not supported"); +#endif // CONFIG_BACKEND} + // This test verifies that configuration control info gets populated. TEST_F(Dhcp6ParserTest, configControlInfo) { string config = PARSER_CONFIGS[8]; diff --git a/src/lib/config_backend/constants.h b/src/lib/config_backend/constants.h index 95550b29ee..79056dfac6 100644 --- a/src/lib/config_backend/constants.h +++ b/src/lib/config_backend/constants.h @@ -1,4 +1,4 @@ -// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2018-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 @@ -16,7 +16,11 @@ namespace cb { /// fetched variable length data in them. /// //@{ -constexpr unsigned long SUBNET_PREFIX_BUF_LENGTH = 32; +constexpr unsigned long POOL_ADDRESS6_BUF_LENGTH = 45; + +constexpr unsigned long SUBNET4_PREFIX_BUF_LENGTH = 32; + +constexpr unsigned long SUBNET6_PREFIX_BUF_LENGTH = 64; constexpr unsigned long DHCP4O6_INTERFACE_BUF_LENGTH = 128; diff --git a/src/lib/dhcpsrv/Makefile.am b/src/lib/dhcpsrv/Makefile.am index eaf77320d3..14bb0e189e 100644 --- a/src/lib/dhcpsrv/Makefile.am +++ b/src/lib/dhcpsrv/Makefile.am @@ -109,9 +109,11 @@ libkea_dhcpsrv_la_SOURCES += cfg_subnets6.cc cfg_subnets6.h libkea_dhcpsrv_la_SOURCES += cfg_mac_source.cc cfg_mac_source.h libkea_dhcpsrv_la_SOURCES += cfgmgr.cc cfgmgr.h libkea_dhcpsrv_la_SOURCES += client_class_def.cc client_class_def.h -libkea_dhcpsrv_la_SOURCES += config_backend_dhcp4.h +libkea_dhcpsrv_la_SOURCES += config_backend_dhcp4.h config_backend_dhcp6.h libkea_dhcpsrv_la_SOURCES += config_backend_pool_dhcp4.cc config_backend_pool_dhcp4.h +libkea_dhcpsrv_la_SOURCES += config_backend_pool_dhcp6.cc config_backend_pool_dhcp6.h libkea_dhcpsrv_la_SOURCES += config_backend_dhcp4_mgr.cc config_backend_dhcp4_mgr.h +libkea_dhcpsrv_la_SOURCES += config_backend_dhcp6_mgr.cc config_backend_dhcp6_mgr.h libkea_dhcpsrv_la_SOURCES += csv_lease_file4.cc csv_lease_file4.h libkea_dhcpsrv_la_SOURCES += csv_lease_file6.cc csv_lease_file6.h libkea_dhcpsrv_la_SOURCES += d2_client_cfg.cc d2_client_cfg.h diff --git a/src/lib/dhcpsrv/config_backend_dhcp6.h b/src/lib/dhcpsrv/config_backend_dhcp6.h new file mode 100644 index 0000000000..5daf736fdc --- /dev/null +++ b/src/lib/dhcpsrv/config_backend_dhcp6.h @@ -0,0 +1,411 @@ +// 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 CONFIG_BACKEND_DHCP6_H +#define CONFIG_BACKEND_DHCP6_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace isc { +namespace dhcp { + +/// @brief Interface implemented by DHCPv6 configuration backends. +class ConfigBackendDHCPv6 : public cb::BaseConfigBackend { +public: + + /// @brief Virtual destructor. + virtual ~ConfigBackendDHCPv6() { } + + /// @brief Retrieves a single subnet by subnet_prefix. + /// + /// @param server_selector Server selector. + /// @param subnet_prefix Prefix of the subnet to be retrieved. + /// @return Pointer to the retrieved subnet or NULL if not found. + virtual Subnet6Ptr + getSubnet6(const db::ServerSelector& server_selector, + const std::string& subnet_prefix) const = 0; + + /// @brief Retrieves a single subnet by subnet identifier. + /// + /// @param server_selector Server selector. + /// @param subnet_id Identifier of a subnet to be retrieved. + /// @return Pointer to the retrieved subnet or NULL if not found. + virtual Subnet6Ptr + getSubnet6(const db::ServerSelector& server_selector, const SubnetID& subnet_id) const = 0; + + /// @brief Retrieves all subnets. + /// + /// @param server_selector Server selector. + /// @return Collection of subnets or empty collection if no subnet found. + virtual Subnet6Collection + getAllSubnets6(const db::ServerSelector& server_selector) const = 0; + + /// @brief Retrieves subnets modified after specified time. + /// + /// @param server_selector Server selector. + /// @param modification_time Lower bound subnet modification time. + /// @return Collection of subnets or empty collection if no subnet found. + virtual Subnet6Collection + getModifiedSubnets6(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const = 0; + + /// @brief Retrieves shared network by name. + /// + /// @param server_selector Server selector. + /// @param name Name of the shared network to be retrieved. + /// @return Pointer to the shared network or NULL if not found. + virtual SharedNetwork6Ptr + getSharedNetwork6(const db::ServerSelector& server_selector, + const std::string& name) const = 0; + + /// @brief Retrieves all shared networks. + /// + /// @param server_selector Server selector. + /// @return Collection of shared network or empty collection if + /// no shared network found. + virtual SharedNetwork6Collection + getAllSharedNetworks6(const db::ServerSelector& server_selector) const = 0; + + /// @brief Retrieves shared networks modified after specified time. + /// + /// @param server_selector Server selector. + /// @param modification_time Lower bound shared network modification time. + /// @return Collection of shared network or empty collection if + /// no shared network found. + virtual SharedNetwork6Collection + getModifiedSharedNetworks6(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const = 0; + + /// @brief Retrieves single option definition by code and space. + /// + /// @param server_selector Server selector. + /// @param code Code of the option to be retrieved. + /// @param space Option space of the option to be retrieved. + /// @return Pointer to the option definition or NULL if not found. + virtual OptionDefinitionPtr + getOptionDef6(const db::ServerSelector& server_selector, const uint16_t code, + const std::string& space) const = 0; + + /// @brief Retrieves all option definitions. + /// + /// @param server_selector Server selector. + /// @return Collection of option definitions or empty collection if + /// no option definition found. + virtual OptionDefContainer + getAllOptionDefs6(const db::ServerSelector& server_selector) const = 0; + + /// @brief Retrieves option definitions modified after specified time. + /// + /// @param server_selector Server selector. + /// @param modification_time Lower bound option definition modification + /// time. + /// @return Collection of option definitions or empty collection if + /// no option definition found. + virtual OptionDefContainer + getModifiedOptionDefs6(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const = 0; + + /// @brief Retrieves single option by code and space. + /// + /// @param server_selector Server selector. + /// @param code Option code. + /// @param space Option space. + /// @return Pointer to the retrieved option descriptor or null if + /// no option was found. + virtual OptionDescriptorPtr + getOption6(const db::ServerSelector& server_selector, const uint16_t code, + const std::string& space) const = 0; + + /// @brief Retrieves all global options. + /// + /// @param server_selector Server selector. + /// @return Collection of global options or empty collection if no + /// option found. + virtual OptionContainer + getAllOptions6(const db::ServerSelector& server_selector) const = 0; + + /// @brief Retrieves option modified after specified time. + /// + /// @param selector Server selector. + /// @param modification_time Lower bound option modification time. + /// @return Collection of global options or empty collection if no + /// option found. + virtual OptionContainer + getModifiedOptions6(const db::ServerSelector& selector, + const boost::posix_time::ptime& modification_time) const = 0; + + /// @brief Retrieves global parameter value. + /// + /// @param selector Server selector. + /// @param name Name of the global parameter to be retrieved. + /// @return Value of the global parameter or null if parameter doesn't + /// exist. + virtual data::StampedValuePtr + getGlobalParameter6(const db::ServerSelector& selector, + const std::string& name) const = 0; + + /// @return Collection of global parameters. + virtual data::StampedValueCollection + getAllGlobalParameters6(const db::ServerSelector& selector) const = 0; + + /// @brief Retrieves global parameters modified after specified time. + /// + /// @param selector Server selector. + /// @param modification_time Modification time. + /// @return Collection of modified global parameters. + virtual data::StampedValueCollection + getModifiedGlobalParameters6(const db::ServerSelector& selector, + const boost::posix_time::ptime& modification_time) const = 0; + + /// @brief Creates or updates a subnet. + /// + /// @param server_selector Server selector. + /// @param subnet Subnet to be added or updated. + virtual void + createUpdateSubnet6(const db::ServerSelector& server_selector, + const Subnet6Ptr& subnet) = 0; + + /// @brief Creates or updates a shared network. + /// + /// @param server_selector Server selector. + /// @param shared_network Shared network to be added or updated. + virtual void + createUpdateSharedNetwork6(const db::ServerSelector& server_selector, + const SharedNetwork6Ptr& shared_network) = 0; + + /// @brief Creates or updates an option definition. + /// + /// @param server_selector Server selector. + /// @param option_def Option definition to be added or updated. + virtual void + createUpdateOptionDef6(const db::ServerSelector& server_selector, + const OptionDefinitionPtr& option_def) = 0; + + /// @brief Creates or updates global option. + /// + /// @param server_selector Server selector. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const OptionDescriptorPtr& option) = 0; + + /// @brief Creates or updates shared network level option. + /// + /// @param selector Server selector. + /// @param shared_network_name Name of a shared network to which option + /// belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& selector, + const std::string& shared_network_name, + const OptionDescriptorPtr& option) = 0; + + /// @brief Creates or updates subnet level option. + /// + /// @param server_selector Server selector. + /// @param subnet_id Identifier of a subnet to which option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const SubnetID& subnet_id, + const OptionDescriptorPtr& option) = 0; + + /// @brief Creates or updates pool level option. + /// + /// @param server_selector Server selector. + /// @param pool_start_address Lower bound address of the pool to which + /// the option belongs. + /// @param pool_end_address Upper bound address of the pool to which the + /// option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const OptionDescriptorPtr& option) = 0; + + /// @brief Creates or updates pd pool level option. + /// + /// @param server_selector Server selector. + /// @param pd_pool_prefix Address part of the prefix of the pd pool + /// to which the the option belongs. + /// @param pd_pool_prefix_length Prefix length of the pd pool to which + /// the option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const OptionDescriptorPtr& option) = 0; + + /// @brief Creates or updates global parameter. + /// + /// @param server_selector Server selector. + /// @param value Value of the global parameter. + virtual void + createUpdateGlobalParameter6(const db::ServerSelector& server_selector, + const data::StampedValuePtr& value) = 0; + + /// @brief Deletes subnet by prefix. + /// + /// @param server_selector Server selector. + /// @param subnet_prefix Prefix of the subnet to be deleted. + /// @return Number of deleted subnets. + virtual uint64_t + deleteSubnet6(const db::ServerSelector& server_selector, + const std::string& subnet_prefix) = 0; + + /// @brief Deletes subnet by identifier. + /// + /// @param server_selector Server selector. + /// @param subnet_id Identifier of the subnet to be deleted. + /// @return Number of deleted subnets. + virtual uint64_t + deleteSubnet6(const db::ServerSelector& server_selector, const SubnetID& subnet_id) = 0; + + /// @brief Deletes all subnets. + /// + /// @param server_selector Server selector. + /// @return Number of deleted subnets. + virtual uint64_t + deleteAllSubnets6(const db::ServerSelector& server_selector) = 0; + + /// @brief Deletes shared network by name. + /// + /// @param server_selector Server selector. + /// @param name Name of the shared network to be deleted. + /// @return Number of deleted shared networks.. + virtual uint64_t + deleteSharedNetwork6(const db::ServerSelector& server_selector, + const std::string& name) = 0; + + /// @brief Deletes all shared networks. + /// + /// @param server_selector Server selector. + /// @return Number of deleted shared networks. + virtual uint64_t + deleteAllSharedNetworks6(const db::ServerSelector& server_selector) = 0; + + /// @brief Deletes option definition. + /// + /// @param server_selector Server selector. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + /// @return Number of deleted option definitions. + virtual uint64_t + deleteOptionDef6(const db::ServerSelector& server_selector, const uint16_t code, + const std::string& space) = 0; + + /// @brief Deletes all option definitions. + /// + /// @param server_selector Server selector. + /// @return Number of deleted option definitions. + virtual uint64_t + deleteAllOptionDefs6(const db::ServerSelector& server_selector) = 0; + + /// @brief Deletes global option. + /// + /// @param server_selector Server selector. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, const uint16_t code, + const std::string& space) = 0; + + /// @brief Deletes shared network level option. + /// + /// @param selector Server selector. + /// @param shared_network_name Name of the shared network which option + /// belongs to. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + virtual uint64_t + deleteOption6(const db::ServerSelector& selector, + const std::string& shared_network_name, + const uint16_t code, + const std::string& space) = 0; + + /// @brief Deletes subnet level option. + /// + /// @param server_selector Server selector. + /// @param subnet_id Identifier of the subnet to which deleted option + /// belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, const SubnetID& subnet_id, + const uint16_t code, const std::string& space) = 0; + + /// @brief Deletes pool level option. + /// + /// @param server_selector Server selector. + /// @param pool_start_address Lower bound address of the pool to which + /// deleted option belongs. + /// @param pool_end_address Upper bound address of the pool to which the + /// deleted option belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const uint16_t code, + const std::string& space) = 0; + + /// @brief Deletes pd pool level option. + /// + /// @param server_selector Server selector. + /// @param pd_pool_prefix Address part of the prefix of the pd pool + /// to which the the option belongs. + /// @param pd_pool_prefix_length Prefix length of the pd pool to which + /// the option belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const uint16_t code, + const std::string& space) = 0; + + /// @brief Deletes global parameter. + /// + /// @param server_selector Server selector. + /// @param name Name of the global parameter to be deleted. + /// @return Number of deleted global parameters. + virtual uint64_t + deleteGlobalParameter6(const db::ServerSelector& server_selector, + const std::string& name) = 0; + + /// @brief Deletes all global parameters. + /// + /// @param server_selector Server selector. + /// @return Number of deleted global parameters. + virtual uint64_t + deleteAllGlobalParameters6(const db::ServerSelector& server_selector) = 0; +}; + +/// @brief Shared pointer to the @c ConfigBackendDHCPv6 instance. +typedef boost::shared_ptr ConfigBackendDHCPv6Ptr; + +} // end of namespace isc::dhcp +} // end of namespace isc + +#endif // CONFIG_BACKEND_DHCP6_H diff --git a/src/lib/dhcpsrv/config_backend_dhcp6_mgr.cc b/src/lib/dhcpsrv/config_backend_dhcp6_mgr.cc new file mode 100644 index 0000000000..9d14e0ea8c --- /dev/null +++ b/src/lib/dhcpsrv/config_backend_dhcp6_mgr.cc @@ -0,0 +1,41 @@ +// 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 + +namespace isc { +namespace dhcp { + +boost::scoped_ptr& +ConfigBackendDHCPv6Mgr::getConfigBackendDHCPv6MgrPtr() { + static boost::scoped_ptr cb_dhcp6_mgr; + return (cb_dhcp6_mgr); +} + +void +ConfigBackendDHCPv6Mgr::create() { + getConfigBackendDHCPv6MgrPtr().reset(new ConfigBackendDHCPv6Mgr()); +} + +void +ConfigBackendDHCPv6Mgr::destroy() { + getConfigBackendDHCPv6MgrPtr().reset(new ConfigBackendDHCPv6Mgr()); +} + +ConfigBackendDHCPv6Mgr& +ConfigBackendDHCPv6Mgr::instance() { + boost::scoped_ptr& cb_dhcp6_mgr = getConfigBackendDHCPv6MgrPtr(); + if (!cb_dhcp6_mgr) { + create(); + } + return (*cb_dhcp6_mgr); +} + +} // end of isc::dhcp namespace +} // end of isc namespace diff --git a/src/lib/dhcpsrv/config_backend_dhcp6_mgr.h b/src/lib/dhcpsrv/config_backend_dhcp6_mgr.h new file mode 100644 index 0000000000..3c1062bb28 --- /dev/null +++ b/src/lib/dhcpsrv/config_backend_dhcp6_mgr.h @@ -0,0 +1,71 @@ +// 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 CONFIG_BACKEND_DHCP6_MGR_H +#define CONFIG_BACKEND_DHCP6_MGR_H + +#include +#include + +#include + +namespace isc { +namespace dhcp { + +/// @brief Configuration Backend Manager for DHPCv6 servers. +/// +/// Implements the "manager" class which holds information about the +/// supported and configured backends and provides access to those +/// backends. This is similar to @c HostMgr and @c LeaseMgr singletons +/// being used by the DHCP servers. +/// +/// It is implemented as a singleton that can be accessed from any place +/// within the server code. This includes server configuration, data +/// fetching during normal server operation and data management, including +/// processing of control commands implemented within hooks libraries. +/// +/// Unlike @c HostMgr, the it does not directly expose the API to fetch and +/// manipulate the data in the database. This is done via, the Configuration +/// Backend Pool, see @c ConfigBackendPoolDHCPv6 for details. +class ConfigBackendDHCPv6Mgr : public cb::BaseConfigBackendMgr, + public boost::noncopyable { +public: + /// @brief Creates new instance of the @c ConfigBackendDHCPv6Mgr. + /// + /// If an instance of the @c ConfigBackendDHCPv6Mgr already exists, + /// it will be replaced by the new instance. Thus, all factories + /// will be unregistered and config databases will be dropped. + static void create(); + + /// @brief Destroys the instance of the @c ConfigBackendDHCPv6Mgr. + /// + /// If an instance of the @c ConfigBackendDHCPv6Mgr exists, + /// it will be destroyed. Thus, all factories will be unregistered + /// and config databases will be dropped. + static void destroy(); + + /// @brief Returns a sole instance of the @c ConfigBackendDHCPv6Mgr. + /// + /// This method should be used to retrieve an instance of the @c ConfigBackendDHCPv6Mgr + /// to be used to gather/manage config backends. It returns an instance + /// of the @c ConfigBackendDHCPv6Mgr created by the @c create method. If + /// the instance doesn't exist yet, it is created using the @c create method + /// with the an empty set of configuration databases. + static ConfigBackendDHCPv6Mgr& instance(); + +private: + /// @brief Private default constructor. + ConfigBackendDHCPv6Mgr() {} + + /// @brief Returns a pointer to the currently used instance of the + /// @c ConfigBackendDHCPv6Mgr. + static boost::scoped_ptr& getConfigBackendDHCPv6MgrPtr(); +}; + +} // end of namespace isc::dhcp +} // end of namespace isc + +#endif // CONFIG_BACKEND_DHCP6_MGR_H diff --git a/src/lib/dhcpsrv/config_backend_pool_dhcp6.cc b/src/lib/dhcpsrv/config_backend_pool_dhcp6.cc new file mode 100644 index 0000000000..f69bdc3e56 --- /dev/null +++ b/src/lib/dhcpsrv/config_backend_pool_dhcp6.cc @@ -0,0 +1,418 @@ +// 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 + +using namespace isc::asiolink; +using namespace isc::data; +using namespace isc::db; + +namespace isc { +namespace dhcp { + +Subnet6Ptr +ConfigBackendPoolDHCPv6::getSubnet6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const std::string& subnet_prefix) const { + Subnet6Ptr subnet; + getPropertyPtrConst + (&ConfigBackendDHCPv6::getSubnet6, backend_selector, server_selector, + subnet, subnet_prefix); + return (subnet); +} + +Subnet6Ptr +ConfigBackendPoolDHCPv6::getSubnet6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const SubnetID& subnet_id) const { + Subnet6Ptr subnet; + getPropertyPtrConst + (&ConfigBackendDHCPv6::getSubnet6, backend_selector, server_selector, + subnet, subnet_id); + return (subnet); +} + +Subnet6Collection +ConfigBackendPoolDHCPv6::getAllSubnets6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) const { + Subnet6Collection subnets; + getAllPropertiesConst + (&ConfigBackendDHCPv6::getAllSubnets6, backend_selector, server_selector, + subnets); + return (subnets); +} + +Subnet6Collection +ConfigBackendPoolDHCPv6::getModifiedSubnets6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const { + Subnet6Collection subnets; + getMultiplePropertiesConst + (&ConfigBackendDHCPv6::getModifiedSubnets6, backend_selector, server_selector, + subnets, modification_time); + return (subnets); +} + +SharedNetwork6Ptr +ConfigBackendPoolDHCPv6::getSharedNetwork6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const std::string& name) const { + SharedNetwork6Ptr shared_network; + getPropertyPtrConst + (&ConfigBackendDHCPv6::getSharedNetwork6, backend_selector, server_selector, + shared_network, name); + return (shared_network); +} + +SharedNetwork6Collection +ConfigBackendPoolDHCPv6::getAllSharedNetworks6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) const { + SharedNetwork6Collection shared_networks; + getAllPropertiesConst + (&ConfigBackendDHCPv6::getAllSharedNetworks6, backend_selector, server_selector, + shared_networks); + return (shared_networks); +} + +SharedNetwork6Collection +ConfigBackendPoolDHCPv6:: +getModifiedSharedNetworks6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const { + SharedNetwork6Collection shared_networks; + getMultiplePropertiesConst + (&ConfigBackendDHCPv6::getModifiedSharedNetworks6, backend_selector, server_selector, + shared_networks, modification_time); + return (shared_networks); +} + +OptionDefinitionPtr +ConfigBackendPoolDHCPv6::getOptionDef6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const uint16_t code, + const std::string& space) const { + OptionDefinitionPtr option_def; + getPropertyPtrConst + (&ConfigBackendDHCPv6::getOptionDef6, backend_selector, server_selector, + option_def, code, space); + return (option_def); +} + +OptionDefContainer +ConfigBackendPoolDHCPv6::getAllOptionDefs6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) const { + OptionDefContainer option_defs; + getAllPropertiesConst + (&ConfigBackendDHCPv6::getAllOptionDefs6, backend_selector, server_selector, + option_defs); + return (option_defs); +} + +OptionDefContainer +ConfigBackendPoolDHCPv6::getModifiedOptionDefs6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const { + OptionDefContainer option_defs; + getMultiplePropertiesConst + (&ConfigBackendDHCPv6::getModifiedOptionDefs6, backend_selector, server_selector, + option_defs, modification_time); + return (option_defs); +} + +OptionDescriptorPtr +ConfigBackendPoolDHCPv6::getOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const uint16_t code, + const std::string& space) const { + OptionDescriptorPtr option; + getPropertyPtrConst + (&ConfigBackendDHCPv6::getOption6, backend_selector, server_selector, + option, code, space); + return (option); +} + +OptionContainer +ConfigBackendPoolDHCPv6::getAllOptions6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) const { + OptionContainer options; + getAllPropertiesConst + (&ConfigBackendDHCPv6::getAllOptions6, backend_selector, server_selector, + options); + return (options); +} + +OptionContainer +ConfigBackendPoolDHCPv6::getModifiedOptions6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const { + OptionContainer options; + getMultiplePropertiesConst + (&ConfigBackendDHCPv6::getModifiedOptions6, backend_selector, server_selector, + options, modification_time); + return (options); +} + +StampedValuePtr +ConfigBackendPoolDHCPv6::getGlobalParameter6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const std::string& name) const { + StampedValuePtr parameter; + getPropertyPtrConst + (&ConfigBackendDHCPv6::getGlobalParameter6, backend_selector, + server_selector, parameter, name); + return (parameter); +} + +StampedValueCollection +ConfigBackendPoolDHCPv6::getAllGlobalParameters6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) const { + StampedValueCollection parameters; + getAllPropertiesConst + (&ConfigBackendDHCPv6::getAllGlobalParameters6, backend_selector, + server_selector, parameters); + return (parameters); +} + +StampedValueCollection +ConfigBackendPoolDHCPv6:: +getModifiedGlobalParameters6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const { + StampedValueCollection parameters; + getMultiplePropertiesConst + (&ConfigBackendDHCPv6::getModifiedGlobalParameters6, backend_selector, + server_selector, parameters, modification_time); + return (parameters); +} + +void +ConfigBackendPoolDHCPv6::createUpdateSubnet6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const Subnet6Ptr& subnet) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateSubnet6, backend_selector, + server_selector, subnet); +} + +void +ConfigBackendPoolDHCPv6::createUpdateSharedNetwork6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const SharedNetwork6Ptr& shared_network) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateSharedNetwork6, backend_selector, + server_selector, shared_network); +} + +void +ConfigBackendPoolDHCPv6::createUpdateOptionDef6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const OptionDefinitionPtr& option_def) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateOptionDef6, backend_selector, + server_selector, option_def); +} + +void +ConfigBackendPoolDHCPv6::createUpdateOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const OptionDescriptorPtr& option) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateOption6, backend_selector, + server_selector, option); +} + +void +ConfigBackendPoolDHCPv6::createUpdateOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const std::string& shared_network_name, + const OptionDescriptorPtr& option) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateOption6, backend_selector, + server_selector, shared_network_name, option); +} + + +void +ConfigBackendPoolDHCPv6::createUpdateOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const SubnetID& subnet_id, + const OptionDescriptorPtr& option) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateOption6, backend_selector, + server_selector, subnet_id, option); +} + +void +ConfigBackendPoolDHCPv6::createUpdateOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const IOAddress& pool_start_address, + const IOAddress& pool_end_address, + const OptionDescriptorPtr& option) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateOption6, backend_selector, + server_selector, pool_start_address, pool_end_address, option); +} + +void +ConfigBackendPoolDHCPv6::createUpdateOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const OptionDescriptorPtr& option) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateOption6, backend_selector, + server_selector, pd_pool_prefix, pd_pool_prefix_length, option); +} + +void +ConfigBackendPoolDHCPv6::createUpdateGlobalParameter6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const StampedValuePtr& value) { + createUpdateDeleteProperty + (&ConfigBackendDHCPv6::createUpdateGlobalParameter6, backend_selector, + server_selector, value); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteSubnet6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const std::string& subnet_prefix) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteSubnet6, backend_selector, server_selector, + subnet_prefix)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteSubnet6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const SubnetID& subnet_id) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteSubnet6, backend_selector, server_selector, + subnet_id)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteAllSubnets6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteAllSubnets6, backend_selector, server_selector)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteSharedNetwork6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const std::string& name) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteSharedNetwork6, backend_selector, + server_selector, name)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteAllSharedNetworks6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteAllSharedNetworks6, backend_selector, server_selector)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteOptionDef6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const uint16_t code, + const std::string& space) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteOptionDef6, backend_selector, + server_selector, code, space)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteAllOptionDefs6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteAllOptionDefs6, backend_selector, server_selector)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const uint16_t code, + const std::string& space) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteOption6, backend_selector, server_selector, + code, space)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const std::string& shared_network_name, + const uint16_t code, + const std::string& space) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteOption6, backend_selector, server_selector, + shared_network_name, code, space)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const SubnetID& subnet_id, + const uint16_t code, + const std::string& space) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteOption6, backend_selector, server_selector, + subnet_id, code, space)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const uint16_t code, + const std::string& space) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteOption6, backend_selector, server_selector, + pool_start_address, pool_end_address, code, space)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteOption6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const uint16_t code, + const std::string& space) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteOption6, backend_selector, server_selector, + pd_pool_prefix, pd_pool_prefix_length, code, space)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteGlobalParameter6(const BackendSelector& backend_selector, + const ServerSelector& server_selector, + const std::string& name) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteGlobalParameter6, backend_selector, + server_selector, name)); +} + +uint64_t +ConfigBackendPoolDHCPv6::deleteAllGlobalParameters6(const BackendSelector& backend_selector, + const ServerSelector& server_selector) { + return (createUpdateDeleteProperty + (&ConfigBackendDHCPv6::deleteAllGlobalParameters6, backend_selector, + server_selector)); +} + + +} // end of namespace isc::dhcp +} // end of namespace isc diff --git a/src/lib/dhcpsrv/config_backend_pool_dhcp6.h b/src/lib/dhcpsrv/config_backend_pool_dhcp6.h new file mode 100644 index 0000000000..0c32db6402 --- /dev/null +++ b/src/lib/dhcpsrv/config_backend_pool_dhcp6.h @@ -0,0 +1,493 @@ +// 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 CONFIG_BACKEND_POOL_DHCP6_H +#define CONFIG_BACKEND_POOL_DHCP6_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace isc { +namespace dhcp { + +/// @brief Implementation of the Configuration Backend Pool for DHCPv6. +class ConfigBackendPoolDHCPv6 : public cb::BaseConfigBackendPool { +public: + + /// @brief Retrieves a single subnet by subnet_prefix. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param subnet_prefix Prefix of the subnet to be retrieved. + /// @return Pointer to the retrieved subnet or NULL if not found. + virtual Subnet6Ptr + getSubnet6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const std::string& subnet_prefix) const; + + /// @brief Retrieves a single subnet by subnet identifier. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param subnet_id Identifier of a subnet to be retrieved. + /// @return Pointer to the retrieved subnet or NULL if not found. + virtual Subnet6Ptr + getSubnet6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const SubnetID& subnet_id) const; + + /// @brief Retrieves all subnets. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @return Collection of subnets or empty collection if no subnet found. + virtual Subnet6Collection + getAllSubnets6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector) const; + + /// @brief Retrieves subnets modified after specified time. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param modification_time Lower bound subnet modification time. + /// @return Collection of subnets or empty collection if no subnet found. + virtual Subnet6Collection + getModifiedSubnets6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Retrieves shared network by name. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param name Name of the shared network to be retrieved. + /// @return Pointer to the shared network or NULL if not found. + virtual SharedNetwork6Ptr + getSharedNetwork6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const std::string& name) const; + + /// @brief Retrieves all shared networks. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @return Collection of shared network or empty collection if + /// no shared network found. + virtual SharedNetwork6Collection + getAllSharedNetworks6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector) const; + + /// @brief Retrieves shared networks modified after specified time. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param modification_time Lower bound shared network modification time. + /// @return Collection of shared network or empty collection if + /// no shared network found. + virtual SharedNetwork6Collection + getModifiedSharedNetworks6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Retrieves single option definition by code and space. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param code Code of the option to be retrieved. + /// @param space Option space of the option to be retrieved. + /// @return Pointer to the option definition or NULL if not found. + virtual OptionDefinitionPtr + getOptionDef6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const uint16_t code, + const std::string& space) const; + + /// @brief Retrieves all option definitions. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @return Collection of option definitions or empty collection if + /// no option definition found. + virtual OptionDefContainer + getAllOptionDefs6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector) const; + + /// @brief Retrieves option definitions modified after specified time. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param modification_time Lower bound option definition modification + /// time. + /// @return Collection of option definitions or empty collection if + /// no option definition found. + virtual OptionDefContainer + getModifiedOptionDefs6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Retrieves single option by code and space. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param code Option code. + /// @param space Option space. + /// @return Pointer to the retrieved option descriptor or null if + /// no option was found. + virtual OptionDescriptorPtr + getOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const uint16_t code, + const std::string& space) const; + + /// @brief Retrieves all global options. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @return Collection of global options or empty collection if no + /// option found. + virtual OptionContainer + getAllOptions6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector) const; + + /// @brief Retrieves option modified after specified time. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param modification_time Lower bound option modification time. + /// @return Collection of global options or empty collection if no + /// option found. + virtual OptionContainer + getModifiedOptions6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Retrieves global parameter value. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param name Name of the global parameter to be retrieved. + /// @return Value of the global parameter. + virtual data::StampedValuePtr + getGlobalParameter6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const std::string& name) const; + + /// @brief Retrieves all global parameters. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + virtual data::StampedValueCollection + getAllGlobalParameters6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector) const; + + /// @brief Retrieves global parameters modified after specified time. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param modification_time Lower bound subnet modification time. + /// @return Collection of modified global parameters. + virtual data::StampedValueCollection + getModifiedGlobalParameters6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Creates or updates a subnet. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param subnet Subnet to be added or updated. + virtual void + createUpdateSubnet6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const Subnet6Ptr& subnet); + + /// @brief Creates or updates a shared network. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param shared_network Shared network to be added or updated. + virtual void + createUpdateSharedNetwork6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const SharedNetwork6Ptr& shared_network); + + /// @brief Creates or updates an option definition. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param option_def Option definition to be added or updated. + virtual void + createUpdateOptionDef6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const OptionDefinitionPtr& option_def); + + /// @brief Creates or updates global option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates shared network level option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param shared_network_name Name of a shared network to which option + /// belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const std::string& shared_network_name, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates subnet level option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param subnet_id Identifier of a subnet to which option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const SubnetID& subnet_id, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates pool level option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param pool_start_address Lower bound address of the pool to which + /// the option belongs. + /// @param pool_end_address Upper bound address of the pool to which the + /// option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates pd pool level option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param pd_pool_prefix Address part of the prefix of the pd pool + /// to which the the option belongs. + /// @param pd_pool_prefix_length Prefix length of the pd pool to which + /// the option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates global string parameter. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param value Value of the global parameter. + virtual void + createUpdateGlobalParameter6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const data::StampedValuePtr& value); + + /// @brief Deletes subnet by prefix. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param subnet_prefix Prefix of the subnet to be deleted. + /// @return Number of deleted subnets. + virtual uint64_t + deleteSubnet6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const std::string& subnet_prefix); + + /// @brief Deletes subnet by identifier. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param subnet_id Identifier of the subnet to be deleted. + /// @return Number of deleted subnets. + virtual uint64_t + deleteSubnet6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const SubnetID& subnet_id); + + /// @brief Deletes all subnets. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @return Number of deleted subnets. + virtual uint64_t + deleteAllSubnets6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector); + + /// @brief Deletes shared network by name. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param name Name of the shared network to be deleted. + /// @return Number of deleted shared networks. + virtual uint64_t + deleteSharedNetwork6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const std::string& name); + + /// @brief Deletes all shared networks. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @return Number of deleted shared networks. + virtual uint64_t + deleteAllSharedNetworks6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector); + + /// @brief Deletes option definition. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + /// @return Number of deleted option definitions. + virtual uint64_t + deleteOptionDef6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const uint16_t code, + const std::string& space); + + /// @brief Deletes all option definitions. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @return Number of deleted option definitions. + virtual uint64_t + deleteAllOptionDefs6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector); + + /// @brief Deletes global option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const uint16_t code, + const std::string& space); + + /// @brief Deletes shared network level option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param shared_network_name Name of the shared network which option + /// belongs to. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + virtual uint64_t + deleteOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const std::string& shared_network_name, + const uint16_t code, + const std::string& space); + + /// @brief Deletes subnet level option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param subnet_id Identifier of the subnet to which deleted option + /// belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const SubnetID& subnet_id, + const uint16_t code, const std::string& space); + + /// @brief Deletes pool level option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param pool_start_address Lower bound address of the pool to which + /// deleted option belongs. + /// @param pool_end_address Upper bound address of the pool to which the + /// deleted option belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const uint16_t code, + const std::string& space); + + /// @brief Deletes pd pool level option. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param pd_pool_prefix Address part of the prefix of the pd pool + /// to which the the option belongs. + /// @param pd_pool_prefix_length Prefix length of the pd pool to which + /// the option belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const uint16_t code, + const std::string& space); + + /// @brief Deletes global parameter. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @param name Name of the global parameter to be deleted. + /// @return Number of deleted global parameters. + virtual uint64_t + deleteGlobalParameter6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector, + const std::string& name); + + /// @brief Deletes all global parameters. + /// + /// @param backend_selector Backend selector. + /// @param server_selector Server selector. + /// @return Number of deleted global parameters. + virtual uint64_t + deleteAllGlobalParameters6(const db::BackendSelector& backend_selector, + const db::ServerSelector& server_selector); +}; + + +} // end of namespace isc::dhcp +} // end of namespace isc + +#endif // CONFIG_BACKEND_POOL_DHCP6_H diff --git a/src/lib/dhcpsrv/testutils/Makefile.am b/src/lib/dhcpsrv/testutils/Makefile.am index 5d339b9ae2..56ebaa2779 100644 --- a/src/lib/dhcpsrv/testutils/Makefile.am +++ b/src/lib/dhcpsrv/testutils/Makefile.am @@ -21,6 +21,7 @@ libdhcpsrvtest_la_SOURCES += generic_host_data_source_unittest.cc generic_host_d libdhcpsrvtest_la_SOURCES += lease_file_io.cc lease_file_io.h libdhcpsrvtest_la_SOURCES += test_config_backend.h libdhcpsrvtest_la_SOURCES += test_config_backend_dhcp4.cc test_config_backend_dhcp4.h +libdhcpsrvtest_la_SOURCES += test_config_backend_dhcp6.cc test_config_backend_dhcp6.h libdhcpsrvtest_la_CXXFLAGS = $(AM_CXXFLAGS) libdhcpsrvtest_la_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) diff --git a/src/lib/dhcpsrv/testutils/test_config_backend.h b/src/lib/dhcpsrv/testutils/test_config_backend.h index b82cc00b6a..03900a6814 100644 --- a/src/lib/dhcpsrv/testutils/test_config_backend.h +++ b/src/lib/dhcpsrv/testutils/test_config_backend.h @@ -1,4 +1,4 @@ -// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2018-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 @@ -10,6 +10,7 @@ #include #include +#include #include #include diff --git a/src/lib/dhcpsrv/testutils/test_config_backend_dhcp6.cc b/src/lib/dhcpsrv/testutils/test_config_backend_dhcp6.cc new file mode 100644 index 0000000000..0568952370 --- /dev/null +++ b/src/lib/dhcpsrv/testutils/test_config_backend_dhcp6.cc @@ -0,0 +1,493 @@ +// 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::data; + +namespace isc { +namespace dhcp { +namespace test { + +bool +TestConfigBackendDHCPv6::registerBackendType(ConfigBackendDHCPv6Mgr& mgr, + const std::string& db_type) { + return(mgr.registerBackendFactory(db_type, + [](const db::DatabaseConnection::ParameterMap& params) + -> dhcp::ConfigBackendDHCPv6Ptr { + return (TestConfigBackendDHCPv6Ptr(new TestConfigBackendDHCPv6(params))); + }) + ); +} + +void +TestConfigBackendDHCPv6::unregisterBackendType(ConfigBackendDHCPv6Mgr& mgr, + const std::string& db_type) { + mgr.unregisterBackendFactory(db_type); +} + +Subnet6Ptr +TestConfigBackendDHCPv6::getSubnet6(const db::ServerSelector& /* server_selector */, + const std::string& subnet_prefix) const{ + const auto& index = subnets_.get(); + auto subnet_it = index.find(subnet_prefix); + return ((subnet_it != index.cend()) ? (*subnet_it) : Subnet6Ptr()); +} + +Subnet6Ptr +TestConfigBackendDHCPv6::getSubnet6(const db::ServerSelector& /* server_selector */, + const SubnetID& subnet_id) const { + const auto& index = subnets_.get(); + auto subnet_it = index.find(subnet_id); + return ((subnet_it != index.cend()) ? (*subnet_it) : Subnet6Ptr()); +} + +Subnet6Collection +TestConfigBackendDHCPv6::getAllSubnets6(const db::ServerSelector& /* server_selector */) const { + return (subnets_); +} + +Subnet6Collection +TestConfigBackendDHCPv6::getModifiedSubnets6(const db::ServerSelector& /* server_selector */, + const boost::posix_time::ptime& modification_time) const { + const auto& index = subnets_.get(); + Subnet6Collection subnets; + auto lb = index.lower_bound(modification_time); + for (auto subnet = lb; subnet != index.end(); ++subnet) { + subnets.push_back(*subnet); + } + return (subnets); +} + +SharedNetwork6Ptr +TestConfigBackendDHCPv6::getSharedNetwork6(const db::ServerSelector& /* server_selector */, + const std::string& name) const { + const auto& index = shared_networks_.get(); + auto network_it = index.find(name); + return ((network_it != index.cend()) ? (*network_it) : SharedNetwork6Ptr()); +} + +SharedNetwork6Collection +TestConfigBackendDHCPv6::getAllSharedNetworks6(const db::ServerSelector& /* server_selector */) const{ + return (shared_networks_); +} + +SharedNetwork6Collection +TestConfigBackendDHCPv6::getModifiedSharedNetworks6(const db::ServerSelector& /* server_selector */, + const boost::posix_time::ptime& modification_time) const { + const auto& index = shared_networks_.get(); + SharedNetwork6Collection shared_networks; + auto lb = index.lower_bound(modification_time); + for (auto shared_network = lb; shared_network != index.end(); ++shared_network) { + shared_networks.push_back(*shared_network); + } + return (shared_networks); +} + +OptionDefinitionPtr +TestConfigBackendDHCPv6::getOptionDef6(const db::ServerSelector& /* server_selector */, + const uint16_t code, + const std::string& space) const { + const auto& index = option_defs_.get<1>(); + auto option_def_it_pair = index.equal_range(code); + + for (auto option_def_it = option_def_it_pair.first; + option_def_it != option_def_it_pair.second; + ++option_def_it) { + if ((*option_def_it)->getOptionSpaceName() == space) { + return (*option_def_it); + } + } + return (OptionDefinitionPtr()); +} + +OptionDefContainer +TestConfigBackendDHCPv6::getAllOptionDefs6(const db::ServerSelector& /* server_selector */) const { + return (option_defs_); +} + +OptionDefContainer +TestConfigBackendDHCPv6::getModifiedOptionDefs6(const db::ServerSelector& /* server_selector */, + const boost::posix_time::ptime& modification_time) const { + const auto& index = option_defs_.get<3>(); + OptionDefContainer option_defs; + auto lb = index.lower_bound(modification_time); + for (auto option_def = lb; option_def != index.end(); ++option_def) { + option_defs.push_back(*option_def); + } + return (option_defs); +} + +OptionDescriptorPtr +TestConfigBackendDHCPv6::getOption6(const db::ServerSelector& /* server_selector */, + const uint16_t code, + const std::string& space) const { + const auto& index = options_.get<1>(); + auto option_it_pair = index.equal_range(code); + + for (auto option_it = option_it_pair.first; option_it != option_it_pair.second; + ++option_it) { + if (option_it->space_name_ == space) { + return (OptionDescriptorPtr(new OptionDescriptor(*option_it))); + } + } + + return (OptionDescriptorPtr()); +} + +OptionContainer +TestConfigBackendDHCPv6::getAllOptions6(const db::ServerSelector& /* server_selector */) const { + return (options_); +} + +OptionContainer +TestConfigBackendDHCPv6::getModifiedOptions6(const db::ServerSelector& /* server_selector */, + const boost::posix_time::ptime& modification_time) const { + const auto& index = options_.get<3>(); + OptionContainer options; + auto lb = index.lower_bound(modification_time); + for (auto option = lb; option != index.end(); ++option) { + options.push_back(*option); + } + return (options); +} + +StampedValuePtr +TestConfigBackendDHCPv6::getGlobalParameter6(const db::ServerSelector& /* server_selector */, + const std::string& name) const { + const auto& index = globals_.get(); + auto global_it = index.find(name); + return ((global_it != index.cend()) ? (*global_it) : StampedValuePtr()); +} + + +StampedValueCollection +TestConfigBackendDHCPv6::getAllGlobalParameters6(const db::ServerSelector& /* server_selector */) const { + return (globals_); +} + +StampedValueCollection +TestConfigBackendDHCPv6::getModifiedGlobalParameters6(const db::ServerSelector& /* server_selector */, + const boost::posix_time::ptime& modification_time) const { + const auto& index = globals_.get(); + StampedValueCollection globals; + auto lb = index.lower_bound(modification_time); + for (auto global = lb; global != index.end(); ++global) { + globals.insert(*global); + } + return (globals); +} + +void +TestConfigBackendDHCPv6::createUpdateSubnet6(const db::ServerSelector& /* server_selector */, + const Subnet6Ptr& subnet) { + auto& index = subnets_.get(); + auto subnet_it = index.find(subnet->getID()); + + if (subnet_it != index.cend()) { + index.replace(subnet_it, subnet); + + } else { + index.insert(subnet); + } +} + +void +TestConfigBackendDHCPv6::createUpdateSharedNetwork6(const db::ServerSelector& /* server_selector */, + const SharedNetwork6Ptr& shared_network) { + auto& index = shared_networks_.get(); + auto network_it = index.find(shared_network->getName()); + + if (network_it != index.cend()) { + index.replace(network_it, shared_network); + + } else { + index.insert(shared_network); + } +} + +void +TestConfigBackendDHCPv6::createUpdateOptionDef6(const db::ServerSelector& /* server_selector */, + const OptionDefinitionPtr& option_def) { + auto& index = option_defs_.get<1>(); + auto option_def_it = index.find(option_def->getCode()); + + if (option_def_it != index.cend()) { + index.replace(option_def_it, option_def); + + } else { + index.insert(option_def); + } +} + +void +TestConfigBackendDHCPv6::createUpdateOption6(const db::ServerSelector& /* server_selector */, + const OptionDescriptorPtr& option) { + auto& index = options_.get<1>(); + auto option_it = index.find(option->option_->getType()); + + if (option_it != index.end()) { + index.replace(option_it, *option); + + } else { + index.insert(*option); + } +} + +void +TestConfigBackendDHCPv6::createUpdateOption6(const db::ServerSelector& /* server_selector */, + const std::string& shared_network_name, + const OptionDescriptorPtr& option) { + auto& index = shared_networks_.get(); + auto network_it = index.find(shared_network_name); + + if (network_it != index.end()) { + auto shared_network = *network_it; + shared_network->getCfgOption()->del(option->space_name_, option->option_->getType()); + shared_network->getCfgOption()->add(*option, option->space_name_); + + } else { + isc_throw(BadValue, "attempted to create or update option in a non existing " + "shared network " << shared_network_name); + } +} + +void +TestConfigBackendDHCPv6::createUpdateOption6(const db::ServerSelector& /* server_selector */, + const SubnetID& subnet_id, + const OptionDescriptorPtr& option) { + auto& index = subnets_.get(); + auto subnet_it = index.find(subnet_id); + + if (subnet_it != index.cend()) { + auto subnet = *subnet_it; + subnet->getCfgOption()->del(option->space_name_, option->option_->getType()); + subnet->getCfgOption()->add(*option, option->space_name_); + + } else { + isc_throw(BadValue, "attempted to create or update option in a non existing " + "subnet ID " << subnet_id); + } +} + +void +TestConfigBackendDHCPv6::createUpdateOption6(const db::ServerSelector& /* server_selector */, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const OptionDescriptorPtr& option) { + for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) { + auto pool = (*subnet)->getPool(Lease::TYPE_NA, pool_start_address); + if (pool) { + pool->getCfgOption()->del(option->space_name_, option->option_->getType()); + pool->getCfgOption()->add(*option, option->space_name_); + + return; + } + } + + isc_throw(BadValue, "attempted to create or update option in a non existing " + "pool " << pool_start_address << " - " << pool_end_address); +} + +void +TestConfigBackendDHCPv6::createUpdateOption6(const db::ServerSelector& /* server_selector */, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const OptionDescriptorPtr& option) { + for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) { + auto pdpool = (*subnet)->getPool(Lease::TYPE_PD, pd_pool_prefix); + if (pdpool) { + pdpool->getCfgOption()->del(option->space_name_, option->option_->getType()); + pdpool->getCfgOption()->add(*option, option->space_name_); + + return; + } + } + + isc_throw(BadValue, "attempted to create or update option in a non existing " + "pd pool " << pd_pool_prefix << "/" + << static_cast(pd_pool_prefix_length)); +} + +void +TestConfigBackendDHCPv6::createUpdateGlobalParameter6(const db::ServerSelector& /* server_selector */, + const data::StampedValuePtr& value) { + auto& index = globals_.get(); + auto global_it = index.find(value->getName()); + + if (global_it != index.end()) { + index.replace(global_it, value); + + } else { + index.insert(value); + } +} + +uint64_t +TestConfigBackendDHCPv6::deleteSubnet6(const db::ServerSelector& /* server_selector */, + const std::string& subnet_prefix) { + auto& index = subnets_.get(); + return (index.erase(subnet_prefix)); +} + +uint64_t +TestConfigBackendDHCPv6::deleteSubnet6(const db::ServerSelector& /* server_selector */, + const SubnetID& subnet_id) { + auto& index = subnets_.get(); + return (index.erase(subnet_id)); +} + +uint64_t +TestConfigBackendDHCPv6::deleteAllSubnets6(const db::ServerSelector& /* server_selector */) { + auto subnets_size = subnets_.size(); + subnets_.clear(); + return (subnets_size); +} + +uint64_t +TestConfigBackendDHCPv6::deleteSharedNetwork6(const db::ServerSelector& /* server_selector */, + const std::string& name) { + auto& index = shared_networks_.get(); + return (index.erase(name)); +} + +uint64_t +TestConfigBackendDHCPv6::deleteAllSharedNetworks6(const db::ServerSelector& /* server_selector */) { + auto shared_networks_size = shared_networks_.size(); + shared_networks_.clear(); + return (shared_networks_size); +} + +uint64_t +TestConfigBackendDHCPv6::deleteOptionDef6(const db::ServerSelector& /* server_selector */, + const uint16_t code, + const std::string& space) { + uint64_t erased = 0; + for (auto option_def_it = option_defs_.begin(); option_def_it != option_defs_.end(); + ++option_def_it) { + if (((*option_def_it)->getCode() == code) && + ((*option_def_it)->getOptionSpaceName() == space)) { + option_def_it = option_defs_.erase(option_def_it); + ++erased; + } + } + return (erased); +} + +uint64_t +TestConfigBackendDHCPv6::deleteAllOptionDefs6(const db::ServerSelector& /* server_selector */) { + auto option_defs_size = option_defs_.size(); + option_defs_.clear(); + return (option_defs_size); +} + +uint64_t +TestConfigBackendDHCPv6::deleteOption6(const db::ServerSelector& /* server_selector */, + const uint16_t code, + const std::string& space) { + uint64_t erased = 0; + for (auto option_it = options_.begin(); option_it != options_.end(); + ++option_it) { + if ((option_it->option_->getType() == code) && + (option_it->space_name_ == space)) { + option_it = options_.erase(option_it); + ++erased; + } + } + return (erased); +} + +uint64_t +TestConfigBackendDHCPv6::deleteOption6(const db::ServerSelector& /* server_selector */, + const std::string& shared_network_name, + const uint16_t code, + const std::string& space) { + auto& index = shared_networks_.get(); + auto network_it = index.find(shared_network_name); + + if (network_it != index.end()) { + auto shared_network = *network_it; + return (shared_network->getCfgOption()->del(space, code)); + + } else { + isc_throw(BadValue, "attempted to delete an option in a non existing " + "shared network " << shared_network_name); + } +} + +uint64_t +TestConfigBackendDHCPv6::deleteOption6(const db::ServerSelector& /* server_selector */, + const SubnetID& subnet_id, + const uint16_t code, + const std::string& space) { + auto& index = subnets_.get(); + auto subnet_it = index.find(subnet_id); + + if (subnet_it != index.cend()) { + auto subnet = *subnet_it; + return (subnet->getCfgOption()->del(space, code)); + + } else { + isc_throw(BadValue, "attempted to delete an option in a non existing " + "subnet ID " << subnet_id); + } +} + +uint64_t +TestConfigBackendDHCPv6::deleteOption6(const db::ServerSelector& /* server_selector */, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const uint16_t code, + const std::string& space) { + for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) { + auto pool = (*subnet)->getPool(Lease::TYPE_NA, pool_start_address); + if (pool) { + return (pool->getCfgOption()->del(space, code)); + } + } + + isc_throw(BadValue, "attempted to delete an option in a non existing " + "pool " << pool_start_address << " - " << pool_end_address); +} + +uint64_t +TestConfigBackendDHCPv6::deleteOption6(const db::ServerSelector& /* server_selector */, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const uint16_t code, + const std::string& space) { + for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) { + auto pdpool = (*subnet)->getPool(Lease::TYPE_PD, pd_pool_prefix); + if (pdpool) { + return (pdpool->getCfgOption()->del(space, code)); + } + } + + isc_throw(BadValue, "attempted to delete an option in a non existing " + "pd pool " << pd_pool_prefix << "/" + << static_cast(pd_pool_prefix_length)); +} + +uint64_t +TestConfigBackendDHCPv6::deleteGlobalParameter6(const db::ServerSelector& /* server_selector */, + const std::string& name) { + auto& index = globals_.get(); + return (index.erase(name)); +} + +uint64_t +TestConfigBackendDHCPv6::deleteAllGlobalParameters6(const db::ServerSelector& /* server_selector */) { + auto globals_size = globals_.size(); + globals_.clear(); + return (globals_size); +} + +} // namespace test +} // namespace dhcp +} // namespace isc diff --git a/src/lib/dhcpsrv/testutils/test_config_backend_dhcp6.h b/src/lib/dhcpsrv/testutils/test_config_backend_dhcp6.h new file mode 100644 index 0000000000..6124ac0b20 --- /dev/null +++ b/src/lib/dhcpsrv/testutils/test_config_backend_dhcp6.h @@ -0,0 +1,446 @@ +// 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 TEST_CONFIG_BACKEND_DHCP6 +#define TEST_CONFIG_BACKEND_DHCP6 + +#include + +#include +#include +#include + +#include + +#include +#include + +namespace isc { +namespace dhcp { +namespace test { + +/// @brief Test config backend that implements all of the DHCPv6 API calls +/// +/// This backend should be used for unit testing the DHCPv6 server and the +/// commands which manpiluate the configuration information stored in the +/// database. +/// +/// This backend stores server configuration information in memory. +class TestConfigBackendDHCPv6 : public TestConfigBackend { +public: + /// @brief Constructor + /// + /// @param params Database connection parameters. + TestConfigBackendDHCPv6(const db::DatabaseConnection::ParameterMap& params) + : TestConfigBackend(params) { + } + + /// @brief virtual Destructor. + virtual ~TestConfigBackendDHCPv6(){}; + + /// @brief Registers the backend type with the given backend manager + /// + /// @param mgr configuration manager to register with + /// @brief db_type back end type - Note you will need to + /// use the same value here as you do when creating backend instances. + static bool registerBackendType(ConfigBackendDHCPv6Mgr& mgr, + const std::string& db_type); + + /// @brief Unregisters the backend from the given backend manager + /// + /// @param mgr configuration manager to unregister from + /// @brief db_type back end type - Note you will need to + /// use the same value here as you do when registering the backend type + static void unregisterBackendType(ConfigBackendDHCPv6Mgr& mgr, + const std::string& db_type); + + /// @brief Retrieves a single subnet by subnet_prefix. + /// + /// @param server_selector Server selector. + /// @param subnet_prefix Prefix of the subnet to be retrieved. + /// @return Pointer to the retrieved subnet or NULL if not found. + virtual Subnet6Ptr + getSubnet6(const db::ServerSelector& server_selector, + const std::string& subnet_prefix) const; + + /// @brief Retrieves a single subnet by subnet identifier. + /// + /// @param server_selector Server selector. + /// @param subnet_id Identifier of a subnet to be retrieved. + /// @return Pointer to the retrieved subnet or NULL if not found. + virtual Subnet6Ptr + getSubnet6(const db::ServerSelector& server_selector, const SubnetID& subnet_id) const; + + /// @brief Retrieves all subnets. + /// + /// @param server_selector Server selector. + /// @return Collection of subnets or empty collection if no subnet found. + virtual Subnet6Collection + getAllSubnets6(const db::ServerSelector& server_selector) const; + + /// @brief Retrieves subnets modified after specified time. + /// + /// @param server_selector Server selector. + /// @param modification_time Lower bound subnet modification time. + /// @return Collection of subnets or empty collection if no subnet found. + virtual Subnet6Collection + getModifiedSubnets6(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Retrieves shared network by name. + /// + /// @param server_selector Server selector. + /// @param name Name of the shared network to be retrieved. + /// @return Pointer to the shared network or NULL if not found. + virtual SharedNetwork6Ptr + getSharedNetwork6(const db::ServerSelector& server_selector, + const std::string& name) const; + + /// @brief Retrieves all shared networks. + /// + /// @param server_selector Server selector. + /// @return Collection of shared network or empty collection if + /// no shared network found. + virtual SharedNetwork6Collection + getAllSharedNetworks6(const db::ServerSelector& server_selector) const; + + /// @brief Retrieves shared networks modified after specified time. + /// + /// @param server_selector Server selector. + /// @param modification_time Lower bound shared network modification time. + /// @return Collection of shared network or empty collection if + /// no shared network found. + virtual SharedNetwork6Collection + getModifiedSharedNetworks6(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Retrieves single option definition by code and space. + /// + /// @param server_selector Server selector. + /// @param code Code of the option to be retrieved. + /// @param space Option space of the option to be retrieved. + /// @return Pointer to the option definition or NULL if not found. + virtual OptionDefinitionPtr + getOptionDef6(const db::ServerSelector& server_selector, const uint16_t code, + const std::string& space) const; + + /// @brief Retrieves all option definitions. + /// + /// @param server_selector Server selector. + /// @return Collection of option definitions or empty collection if + /// no option definition found. + virtual OptionDefContainer + getAllOptionDefs6(const db::ServerSelector& server_selector) const; + + /// @brief Retrieves option definitions modified after specified time. + /// + /// @param server_selector Server selector. + /// @param modification_time Lower bound option definition modification + /// time. + /// @return Collection of option definitions or empty collection if + /// no option definition found. + virtual OptionDefContainer + getModifiedOptionDefs6(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Retrieves single option by code and space. + /// + /// @param server_selector Server selector. + /// @return Pointer to the retrieved option descriptor or null if + /// no option was found. + virtual OptionDescriptorPtr + getOption6(const db::ServerSelector& server_selector, const uint16_t code, + const std::string& space) const; + + /// @brief Retrieves all global options. + /// + /// @param server_selector Server selector. + /// @return Collection of global options or empty collection if no + /// option found. + virtual OptionContainer + getAllOptions6(const db::ServerSelector& server_selector) const; + + /// @brief Retrieves option modified after specified time. + /// + /// @param selector Server selector. + /// @param modification_time Lower bound option modification time. + /// @return Collection of global options or empty collection if no + /// option found. + virtual OptionContainer + getModifiedOptions6(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Retrieves global parameter value. + /// + /// @param server_selector Server selector. + /// @param name Name of the global parameter to be retrieved. + /// @return Value of the global parameter or null if parameter doesn't + /// exist. + virtual data::StampedValuePtr + getGlobalParameter6(const db::ServerSelector& server_selector, + const std::string& name) const; + + /// @return Collection of global parameters. + virtual data::StampedValueCollection + getAllGlobalParameters6(const db::ServerSelector& server_selector) const; + + /// @brief Retrieves global parameters modified after specified time. + /// + /// @param selector Server selector. + /// @return Collection of modified global parameters. + virtual data::StampedValueCollection + getModifiedGlobalParameters6(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time) const; + + /// @brief Creates or updates a subnet. + /// + /// @param server_selector Server selector. + /// @param subnet Subnet to be added or updated. + virtual void + createUpdateSubnet6(const db::ServerSelector& server_selector, + const Subnet6Ptr& subnet); + + /// @brief Creates or updates a shared network. + /// + /// @param server_selector Server selector. + /// @param shared_network Shared network to be added or updated. + virtual void + createUpdateSharedNetwork6(const db::ServerSelector& server_selector, + const SharedNetwork6Ptr& shared_network); + + /// @brief Creates or updates an option definition. + /// + /// @param server_selector Server selector. + /// @param option_def Option definition to be added or updated. + virtual void + createUpdateOptionDef6(const db::ServerSelector& server_selector, + const OptionDefinitionPtr& option_def); + + /// @brief Creates or updates global option. + /// + /// @param server_selector Server selector. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates shared network level option. + /// + /// @param selector Server selector. + /// @param shared_network_name Name of a shared network to which option + /// belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const std::string& shared_network_name, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates subnet level option. + /// + /// @param server_selector Server selector. + /// @param subnet_id Identifier of a subnet to which option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const SubnetID& subnet_id, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates pool level option. + /// + /// @param server_selector Server selector. + /// @param pool_start_address Lower bound address of the pool to which + /// the option belongs. + /// @param pool_end_address Upper bound address of the pool to which the + /// option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates pd pool level option. + /// + /// @param server_selector Server selector. + /// @param pd_pool_prefix Address part of the prefix of the pd pool + /// to which the the option belongs. + /// @param pd_pool_prefix_length Prefix length of the pd pool to which + /// the option belongs. + /// @param option Option to be added or updated. + virtual void + createUpdateOption6(const db::ServerSelector& server_selector, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const OptionDescriptorPtr& option); + + /// @brief Creates or updates global parameter. + /// + /// @param server_selector Server selector. + /// @param value Value of the global parameter. + virtual void + createUpdateGlobalParameter6(const db::ServerSelector& server_selector, + const data::StampedValuePtr& value); + + /// @brief Deletes subnet by prefix. + /// + /// @param server_selector Server selector. + /// @param subnet_prefix Prefix of the subnet to be deleted. + /// @return Number of deleted subnets. + virtual uint64_t + deleteSubnet6(const db::ServerSelector& server_selector, + const std::string& subnet_prefix); + + /// @brief Deletes subnet by identifier. + /// + /// @param server_selector Server selector. + /// @param subnet_id Identifier of the subnet to be deleted. + /// @return Number of deleted subnets. + virtual uint64_t + deleteSubnet6(const db::ServerSelector& server_selector, const SubnetID& subnet_id); + + /// @brief Deletes all subnets. + /// + /// @param server_selector Server selector. + /// @return Number of deleted subnets. + virtual uint64_t + deleteAllSubnets6(const db::ServerSelector& server_selector); + + /// @brief Deletes shared network by name. + /// + /// @param server_selector Server selector. + /// @param name Name of the shared network to be deleted. + /// @return Number of deleted shared networks.. + virtual uint64_t + deleteSharedNetwork6(const db::ServerSelector& server_selector, + const std::string& name); + + /// @brief Deletes all shared networks. + /// + /// @param server_selector Server selector. + /// @return Number of deleted shared networks. + virtual uint64_t + deleteAllSharedNetworks6(const db::ServerSelector& server_selector); + + /// @brief Deletes option definition. + /// + /// @param server_selector Server selector. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + /// @return Number of deleted option definitions. + virtual uint64_t + deleteOptionDef6(const db::ServerSelector& server_selector, const uint16_t code, + const std::string& space); + + /// @brief Deletes all option definitions. + /// + /// @param server_selector Server selector. + /// @return Number of deleted option definitions. + virtual uint64_t + deleteAllOptionDefs6(const db::ServerSelector& server_selector); + + /// @brief Deletes global option. + /// + /// @param server_selector Server selector. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, const uint16_t code, + const std::string& space); + + /// @brief Deletes shared network level option. + /// + /// @param selector Server selector. + /// @param shared_network_name Name of the shared network which option + /// belongs to. + /// @param code Code of the option to be deleted. + /// @param space Option space of the option to be deleted. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, + const std::string& shared_network_name, + const uint16_t code, + const std::string& space); + + /// @brief Deletes subnet level option. + /// + /// @param server_selector Server selector. + /// @param subnet_id Identifier of the subnet to which deleted option + /// belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, const SubnetID& subnet_id, + const uint16_t code, const std::string& space); + + /// @brief Deletes pool level option. + /// + /// @param server_selector Server selector. + /// @param pool_start_address Lower bound address of the pool to which + /// deleted option belongs. + /// @param pool_end_address Upper bound address of the pool to which the + /// deleted option belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, + const asiolink::IOAddress& pool_start_address, + const asiolink::IOAddress& pool_end_address, + const uint16_t code, + const std::string& space); + + /// @brief Deletes pd pool level option. + /// + /// @param server_selector Server selector. + /// @param pd_pool_prefix Address part of the prefix of the pd pool + /// to which the the option belongs. + /// @param pd_pool_prefix_length Prefix length of the pd pool to which + /// the option belongs. + /// @param code Code of the deleted option. + /// @param space Option space of the deleted option. + /// @return Number of deleted options. + virtual uint64_t + deleteOption6(const db::ServerSelector& server_selector, + const asiolink::IOAddress& pd_pool_prefix, + const uint8_t pd_pool_prefix_length, + const uint16_t code, + const std::string& space); + + /// @brief Deletes global parameter. + /// + /// @param server_selector Server selector. + /// @param name Name of the global parameter to be deleted. + /// @return Number of deleted global parameters. + virtual uint64_t + deleteGlobalParameter6(const db::ServerSelector& server_selector, + const std::string& name); + + /// @brief Deletes all global parameters. + /// + /// @param server_selector Server selector. + /// @return Number of deleted global parameters. + virtual uint64_t + deleteAllGlobalParameters6(const db::ServerSelector& server_selector); + +/// @{ +/// @brief Containers used to house the "database" entries + Subnet6Collection subnets_; + SharedNetwork6Collection shared_networks_; + OptionDefContainer option_defs_; + OptionContainer options_; + data::StampedValueCollection globals_; +/// @} +}; + +/// @brief Shared pointer to the @c TestConfigBackend. +typedef boost::shared_ptr TestConfigBackendDHCPv6Ptr; + +} // namespace test +} // namespace dhcp +} // namespace isc + +#endif // TEST_CONFIG_BACKEND_DHCP6