From: Francis Dupont Date: Fri, 11 Jan 2019 15:48:30 +0000 (+0100) Subject: [313-return-a-list-of-all-reservations-by-subnet-id] Added getAll[46] by subnet ... X-Git-Tag: 429-Updated-StampedValue-to-support-reals_base~87 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2a5750c931866bb27e7f8e5b8bcfb30d4cf532ac;p=thirdparty%2Fkea.git [313-return-a-list-of-all-reservations-by-subnet-id] Added getAll[46] by subnet - checkpoint of the day --- diff --git a/src/lib/dhcpsrv/base_host_data_source.h b/src/lib/dhcpsrv/base_host_data_source.h index 115c82ed57..815a0d9d07 100644 --- a/src/lib/dhcpsrv/base_host_data_source.h +++ b/src/lib/dhcpsrv/base_host_data_source.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-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 @@ -92,6 +92,28 @@ public: const uint8_t* identifier_begin, const size_t identifier_len) const = 0; + /// @brief Return all hosts in a DHCPv4 subnet. + /// + /// This method returns all @c Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of const @c Host objects. + virtual ConstHostCollection + getAll4(const SubnetID& subnet_id) const = 0; + + /// @brief Return all hosts in a DHCPv6 subnet. + /// + /// This method returns all @c Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of const @c Host objects. + virtual ConstHostCollection + getAll6(const SubnetID& subnet_id) const = 0; + /// @brief Returns a collection of hosts using the specified IPv4 address. /// /// This method may return multiple @c Host objects if they are connected diff --git a/src/lib/dhcpsrv/cfg_hosts.cc b/src/lib/dhcpsrv/cfg_hosts.cc index b07fe152ed..386bc2888e 100644 --- a/src/lib/dhcpsrv/cfg_hosts.cc +++ b/src/lib/dhcpsrv/cfg_hosts.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-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 @@ -46,6 +46,42 @@ CfgHosts::getAll(const Host::IdentifierType& identifier_type, return (collection); } +ConstHostCollection +CfgHosts::getAll4(const SubnetID& subnet_id) const { + // Do not issue logging message here because it will be logged by + // the getAllInternal method. + ConstHostCollection collection; + getAllInternal4(subnet_id, collection); + return (collection); +} + +HostCollection +CfgHosts::getAll4(const SubnetID& subnet_id) { + // Do not issue logging message here because it will be logged by + // the getAllInternal method. + HostCollection collection; + getAllInternal4(subnet_id, collection); + return (collection); +} + +ConstHostCollection +CfgHosts::getAll6(const SubnetID& subnet_id) const { + // Do not issue logging message here because it will be logged by + // the getAllInternal method. + ConstHostCollection collection; + getAllInternal6(subnet_id, collection); + return (collection); +} + +HostCollection +CfgHosts::getAll6(const SubnetID& subnet_id) { + // Do not issue logging message here because it will be logged by + // the getAllInternal method. + HostCollection collection; + getAllInternal6(subnet_id, collection); + return (collection); +} + ConstHostCollection CfgHosts::getAll4(const IOAddress& address) const { // Do not issue logging message here because it will be logged by @@ -121,6 +157,63 @@ CfgHosts::getAllInternal(const Host::IdentifierType& identifier_type, .arg(storage.size()); } +template +void +CfgHosts::getAllInternal4(const SubnetID& subnet_id, + Storage& storage) const { + + LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_SUBNET_ID4) + .arg(subnet_id); + + // Use try DHCPv4 subnet id. + const HostContainerIndex2& idx = hosts_.get<2>(); + + // Append each Host object to the storage. + for (HostContainerIndex2::iterator host = idx.lower_bound(subnet_id); + host != idx.upper_bound(subnet_id); + ++host) { + LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA, + HOSTS_CFG_GET_ALL_SUBNET_ID4_HOST) + .arg(subnet_id) + .arg((*host)->toText()); + storage.push_back(*host); + } + + // Log how many hosts have been found. + LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ALL_SUBNET_ID4_COUNT) + .arg(subnet_id) + .arg(storage.size()); +} + +template +void +CfgHosts::getAllInternal6(const SubnetID& subnet_id, + Storage& storage) const { + + LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_SUBNET_ID6) + .arg(subnet_id); + + // Use try DHCPv6 subnet id. + const HostContainerIndex3& idx = hosts_.get<3>(); + + // Append each Host object to the storage. + for (HostContainerIndex3::iterator host = idx.lower_bound(subnet_id); + host != idx.upper_bound(subnet_id); + ++host) { + LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA, + HOSTS_CFG_GET_ALL_SUBNET_ID6_HOST) + .arg(subnet_id) + .arg((*host)->toText()); + storage.push_back(*host); + } + + // Log how many hosts have been found. + LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ALL_SUBNET_ID6_COUNT) + .arg(subnet_id) + .arg(storage.size()); +} + + template void CfgHosts::getAllInternal4(const IOAddress& address, Storage& storage) const { @@ -447,7 +540,7 @@ CfgHosts::add(const HostPtr& host) { } // At least one subnet ID must be used - if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED && + if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED && host->getIPv6SubnetID() == SUBNET_ID_UNUSED) { isc_throw(BadValue, "must not use both IPv4 and IPv6 subnet ids of" " 0 when adding new host reservation"); diff --git a/src/lib/dhcpsrv/cfg_hosts.h b/src/lib/dhcpsrv/cfg_hosts.h index 52b0f4f82c..eada8a2a0c 100644 --- a/src/lib/dhcpsrv/cfg_hosts.h +++ b/src/lib/dhcpsrv/cfg_hosts.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-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 @@ -75,6 +75,50 @@ public: const uint8_t* identifier_begin, const size_t identifier_len); + /// @brief Return all hosts in a DHCPv4 subnet. + /// + /// This method returns all @c Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of const @c Host objects. + virtual ConstHostCollection + getAll4(const SubnetID& subnet_id) const; + + /// @brief Return all hosts in a DHCPv4 subnet. + /// + /// This method returns all @c Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of non-const @c Host objects. + virtual HostCollection + getAll4(const SubnetID& subnet_id); + + /// @brief Return all hosts in a DHCPv6 subnet. + /// + /// This method returns all @c Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of const @c Host objects. + virtual ConstHostCollection + getAll6(const SubnetID& subnet_id) const; + + /// @brief Return all hosts in a DHCPv6 subnet. + /// + /// This method returns all @c Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of non-const @c Host objects. + virtual HostCollection + getAll6(const SubnetID& subnet_id); + /// @brief Returns a collection of hosts using the specified IPv4 address. /// /// This method may return multiple @c Host objects if they are connected @@ -332,6 +376,34 @@ private: const size_t identifier_len, Storage& storage) const; + /// @brief Returns @c Host objects in a DHCPv4 subnet. + /// + /// This private method is called by the @c CfgHosts::getAllInternal + /// method which finds the @c Host objects in a specified subnet. + /// The retrieved objects are appended to the @c storage container. + /// + /// @param subnet_id Subnet identifier. + /// @param [out] storage Container to which the retrieved objects are + /// appended. + /// @tparam One of the @c ConstHostCollection of @c HostCollection. + template + void getAllInternal4(const SubnetID& subnet_id, + Storage& storage) const; + + /// @brief Returns @c Host objects in a DHCPv6 subnet. + /// + /// This private method is called by the @c CfgHosts::getAllInternal + /// method which finds the @c Host objects in a specified subnet. + /// The retrieved objects are appended to the @c storage container. + /// + /// @param subnet_id Subnet identifier. + /// @param [out] storage Container to which the retrieved objects are + /// appended. + /// @tparam One of the @c ConstHostCollection of @c HostCollection. + template + void getAllInternal6(const SubnetID& subnet_id, + Storage& storage) const; + /// @brief Returns @c Host objects for the specified IPv4 address. /// /// This private method is called by the @c CfgHosts::getAll4 methods diff --git a/src/lib/dhcpsrv/cql_host_data_source.h b/src/lib/dhcpsrv/cql_host_data_source.h index 1b788eb747..c657d9c394 100644 --- a/src/lib/dhcpsrv/cql_host_data_source.h +++ b/src/lib/dhcpsrv/cql_host_data_source.h @@ -1,4 +1,4 @@ -// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2018-2019 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2018 Deutsche Telekom AG. // // Author: Andrei Pavel @@ -168,6 +168,28 @@ public: const uint8_t* identifier_begin, const size_t identifier_len) const override; + /// @brief Return all hosts in a DHCPv4 subnet. + /// + /// This method returns all @ref Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id subnet identifier to filter by + /// + /// @return Collection of const @ref Host objects. + virtual ConstHostCollection + getAll4(const SubnetID& subnet_id) const override; + + /// @brief Return all hosts in a DHCPv6 subnet. + /// + /// This method returns all @ref Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id subnet identifier to filter by + /// + /// @return Collection of const @ref Host objects. + virtual ConstHostCollection + getAll6(const SubnetID& subnet_id) const override; + /// @brief Returns a collection of hosts using the specified IPv4 address. /// /// This method may return multiple @ref Host objects if they are connected diff --git a/src/lib/dhcpsrv/host_mgr.cc b/src/lib/dhcpsrv/host_mgr.cc index 87770db6bc..777fceb4d2 100644 --- a/src/lib/dhcpsrv/host_mgr.cc +++ b/src/lib/dhcpsrv/host_mgr.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-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 @@ -110,6 +110,27 @@ HostMgr::getAll(const Host::IdentifierType& identifier_type, return (hosts); } +ConstHostCollection +HostMgr::getAll4(const SubnetID& subnet_id) const { + ConstHostCollection hosts = getCfgHosts()->getAll4(subnet_id); + for (auto source : alternate_sources_) { + ConstHostCollection hosts_plus = source->getAll4(subnet_id); + hosts.insert(hosts.end(), hosts_plus.begin(), hosts_plus.end()); + } + return (hosts); +} + + +ConstHostCollection +HostMgr::getAll6(const SubnetID& subnet_id) const { + ConstHostCollection hosts = getCfgHosts()->getAll6(subnet_id); + for (auto source : alternate_sources_) { + ConstHostCollection hosts_plus = source->getAll6(subnet_id); + hosts.insert(hosts.end(), hosts_plus.begin(), hosts_plus.end()); + } + return (hosts); +} + ConstHostCollection HostMgr::getAll4(const IOAddress& address) const { @@ -186,7 +207,7 @@ HostMgr::get4(const SubnetID& subnet_id, } return (host); } - + ConstHostPtr HostMgr::get4(const SubnetID& subnet_id, const asiolink::IOAddress& address) const { diff --git a/src/lib/dhcpsrv/host_mgr.h b/src/lib/dhcpsrv/host_mgr.h index fa6248f025..a5e90a02c4 100644 --- a/src/lib/dhcpsrv/host_mgr.h +++ b/src/lib/dhcpsrv/host_mgr.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-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 @@ -124,6 +124,42 @@ public: const uint8_t* identifier_begin, const size_t identifier_len) const; + /// @brief Return all hosts in a DHCPv4 subnet. + /// + /// This method returns all @c Host objects representing reservations + /// in a specified subnet as documented in the + /// @c BaseHostDataSource::getAll4 + /// + /// It retrieves reservations from both primary and alternate host data + /// source as a single collection of @c Host objects, i.e. if matching + /// reservations are in both sources, all of them are returned. The + /// reservations from the primary data source are placed before the + /// reservations from the alternate source. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of const @c Host objects. + virtual ConstHostCollection + getAll4(const SubnetID& subnet_id) const; + + /// @brief Return all hosts in a DHCPv6 subnet. + /// + /// This method returns all @c Host objects representing reservations + /// in a specified subnet as documented in the + /// @c BaseHostDataSource::getAll6 + /// + /// It retrieves reservations from both primary and alternate host data + /// source as a single collection of @c Host objects, i.e. if matching + /// reservations are in both sources, all of them are returned. The + /// reservations from the primary data source are placed before the + /// reservations from the alternate source. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of const @c Host objects. + virtual ConstHostCollection + getAll6(const SubnetID& subnet_id) const; + /// @brief Returns a collection of hosts using the specified IPv4 address. /// /// This method may return multiple @c Host objects if they are connected to @@ -195,7 +231,7 @@ public: /// @brief Returns any host connected to the IPv6 subnet. /// /// This method returns a host connected to the IPv6 subnet as described - /// in the @c BaseHostDataSource::get6 even when the + /// in the @c BaseHostDataSource::get6 even when the /// reservation is marked as from negative caching. This allows to /// monitor negative caching. /// diff --git a/src/lib/dhcpsrv/hosts_messages.mes b/src/lib/dhcpsrv/hosts_messages.mes index bdae8d9b1e..41b933bbcd 100644 --- a/src/lib/dhcpsrv/hosts_messages.mes +++ b/src/lib/dhcpsrv/hosts_messages.mes @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2018 Internet Systems Consortium, Inc. ("ISC") +# Copyright (C) 2015-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 @@ -88,6 +88,14 @@ This debug message is issued when found host identified by the specific identifier. The arguments specify the identifier and the detailed description of the host found. +% HOSTS_CFG_GET_ALL_SUBNET_ID4 get all hosts with reservations for subnet id4 %1 +This debug message is issued when starting to retrieve all hosts connected to +the specific DHCPv4 subnet. The argument specifies subnet id. + +% HOSTS_CFG_GET_ALL_SUBNET_ID6 get all hosts with reservations for subnet id6 %1 +This debug message is issued when starting to retrieve all hosts connected to +the specific DHCPv6 subnet. The argument specifies subnet id. + % HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6 get all hosts with reservations for subnet id %1 and IPv6 address %2 This debug message is issued when starting to retrieve all hosts connected to the specific subnet and having the specific IPv6 address reserved. @@ -96,13 +104,32 @@ The arguments specify subnet id and IPv6 address respectively. % HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6_COUNT using subnet id %1 and address %2, found %3 host(s) This debug message include the details of the host found using the subnet id and address. The arguments specify subnet id, address and -found host details respectively. +the number of hosts found respectively. % HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6_HOST using subnet id %1 and address %2, found host: %3 This debug message includes the details of the host found using the subnet id and address. The arguments specify subnet id, address and +the number of hosts found respectively. found host details respectively. +% HOSTS_CFG_GET_ALL_SUBNET_ID4_COUNT using subnet id4 %1, found %2 host(s) +This debug message include the details of the host found using the DHCPv4 +subnet id. The arguments specify subnet id and the number of hosts found +respectively. + +% HOSTS_CFG_GET_ALL_SUBNET_ID6_COUNT using subnet id6 %1, found %2 host(s) +This debug message include the details of the host found using the DHCPv6 +subnet id. The arguments specify subnet id and the number of hosts found +respectively. + +% HOSTS_CFG_GET_ALL_SUBNET_ID4_HOST using subnet id4 %1, found host: %2 +This debug message includes the details of the host found using the DHCPv4 +subnet id. The arguments specify subnet id and found host details respectively. + +% HOSTS_CFG_GET_ALL_SUBNET_ID6_HOST using subnet id6 %1, found host: %2 +This debug message includes the details of the host found using the DHCPv6 +subnet id. The arguments specify subnet id and found host details respectively. + % HOSTS_CFG_GET_ONE_PREFIX get one host with reservation for prefix %1/%2 This debug message is issued when starting to retrieve a host having a reservation for a specified prefix. The arguments specify a prefix and diff --git a/src/lib/dhcpsrv/mysql_host_data_source.h b/src/lib/dhcpsrv/mysql_host_data_source.h index 9bae27107f..73b3058c4e 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.h +++ b/src/lib/dhcpsrv/mysql_host_data_source.h @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-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 @@ -131,6 +131,28 @@ public: getAll(const Host::IdentifierType& identifier_type, const uint8_t* identifier_begin, const size_t identifier_len) const; + /// @brief Return all hosts in a DHCPv4 subnet. + /// + /// This method returns all @ref Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id subnet identifier to filter by + /// + /// @return Collection of const @ref Host objects. + virtual ConstHostCollection + getAll4(const SubnetID& subnet_id) const override; + + /// @brief Return all hosts in a DHCPv6 subnet. + /// + /// This method returns all @ref Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id subnet identifier to filter by + /// + /// @return Collection of const @ref Host objects. + virtual ConstHostCollection + getAll6(const SubnetID& subnet_id) const override; + /// @brief Returns a collection of hosts using the specified IPv4 address. /// /// This method may return multiple @c Host objects if they are connected diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.h b/src/lib/dhcpsrv/pgsql_host_data_source.h index 66b024a5f7..f829d973bb 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.h +++ b/src/lib/dhcpsrv/pgsql_host_data_source.h @@ -1,4 +1,4 @@ -// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-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 @@ -158,6 +158,28 @@ public: getAll(const Host::IdentifierType& identifier_type, const uint8_t* identifier_begin, const size_t identifier_len) const; + /// @brief Return all hosts in a DHCPv4 subnet. + /// + /// This method returns all @ref Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id subnet identifier to filter by + /// + /// @return Collection of const @ref Host objects. + virtual ConstHostCollection + getAll4(const SubnetID& subnet_id) const override; + + /// @brief Return all hosts in a DHCPv6 subnet. + /// + /// This method returns all @ref Host objects which represent reservations + /// in a specified subnet. + /// + /// @param subnet_id subnet identifier to filter by + /// + /// @return Collection of const @ref Host objects. + virtual ConstHostCollection + getAll6(const SubnetID& subnet_id) const override; + /// @brief Returns a collection of hosts using the specified IPv4 address. /// /// This method may return multiple @c Host objects if they are connected diff --git a/src/lib/dhcpsrv/tests/host_cache_unittest.cc b/src/lib/dhcpsrv/tests/host_cache_unittest.cc index 2a3d4269a9..91eca09960 100644 --- a/src/lib/dhcpsrv/tests/host_cache_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_cache_unittest.cc @@ -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 @@ -182,7 +182,7 @@ TEST_F(HostCacheTest, identifier4) { // Remove it from test host data source. EXPECT_TRUE(memptr_->del(host->getIPv4SubnetID(), address)); - + // It is cached so we can still get it. got = HostMgr::instance().get4(host->getIPv4SubnetID(), host->getIdentifierType(), @@ -190,7 +190,7 @@ TEST_F(HostCacheTest, identifier4) { host->getIdentifier().size()); ASSERT_TRUE(got); HostDataSourceUtils::compareHosts(got, host); - + // Even by address. got = HostMgr::instance().get4(host->getIPv4SubnetID(), address); ASSERT_TRUE(got); @@ -215,7 +215,7 @@ TEST_F(HostCacheTest, identifier6) { Host::IDENT_DUID, false); ASSERT_TRUE(host); // Make sure the host is generated properly. - + // Get the address. IPv6ResrvRange resrvs = host->getIPv6Reservations(); ASSERT_EQ(1, std::distance(resrvs.first, resrvs.second)); @@ -239,7 +239,7 @@ TEST_F(HostCacheTest, identifier6) { // Remove it from test host data source. EXPECT_TRUE(memptr_->del(host->getIPv6SubnetID(), address)); - + // It is cached so we can still get it. got = HostMgr::instance().get6(host->getIPv6SubnetID(), host->getIdentifierType(), @@ -247,7 +247,7 @@ TEST_F(HostCacheTest, identifier6) { host->getIdentifier().size()); ASSERT_TRUE(got); HostDataSourceUtils::compareHosts(got, host); - + // Even by address. got = HostMgr::instance().get6(host->getIPv6SubnetID(), address); ASSERT_TRUE(got); @@ -288,12 +288,12 @@ TEST_F(HostCacheTest, address4) { // Remove it from test host data source. EXPECT_TRUE(memptr_->del(host->getIPv4SubnetID(), address)); - + // It is cached so we can still get it. got = HostMgr::instance().get4(host->getIPv4SubnetID(), address); ASSERT_TRUE(got); HostDataSourceUtils::compareHosts(got, host); - + // Even by identifier. got = HostMgr::instance().get4(host->getIPv4SubnetID(), host->getIdentifierType(), @@ -321,7 +321,7 @@ TEST_F(HostCacheTest, address6) { Host::IDENT_DUID, false); ASSERT_TRUE(host); // Make sure the host is generated properly. - + // Get the address. IPv6ResrvRange resrvs = host->getIPv6Reservations(); ASSERT_EQ(1, std::distance(resrvs.first, resrvs.second)); @@ -343,12 +343,12 @@ TEST_F(HostCacheTest, address6) { // Remove it from test host data source. EXPECT_TRUE(memptr_->del(host->getIPv6SubnetID(), address)); - + // It is cached so we can still get it. got = HostMgr::instance().get6(host->getIPv6SubnetID(), address); ASSERT_TRUE(got); HostDataSourceUtils::compareHosts(got, host); - + // Even by identifier. got = HostMgr::instance().get6(host->getIPv6SubnetID(), host->getIdentifierType(), @@ -565,7 +565,7 @@ TEST_F(HostCacheTest, negativeAddress6) { Host::IDENT_DUID, false); ASSERT_TRUE(host); // Make sure the host is generated properly. - + // Get the address. IPv6ResrvRange resrvs = host->getIPv6Reservations(); ASSERT_EQ(1, std::distance(resrvs.first, resrvs.second)); @@ -598,11 +598,19 @@ public: /// Destructor virtual ~TestOneBackend() { } - ConstHostCollection getAll(const Host::IdentifierType&, const uint8_t*, + ConstHostCollection getAll(const Host::IdentifierType&, const uint8_t*, const size_t) const { return (getCollection()); } + ConstHostCollection getAll4(const SubnetID&) const { + return (getCollection()); + } + + ConstHostCollection getAll6(const SubnetID&) const { + return (getCollection()); + } + ConstHostCollection getAll4(const IOAddress&) const { return (getCollection()); } diff --git a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc index f9f6e6a67c..8a53dd5930 100644 --- a/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/testutils/generic_host_data_source_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2015-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2015-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 @@ -98,7 +98,7 @@ GenericHostDataSourceTest::addTestOptions(const HostPtr& host, if ((added_options == DHCP4_ONLY) || (added_options == DHCP4_AND_DHCP6)) { // Add DHCPv4 options. CfgOptionPtr opts = host->getCfgOption4(); - OptionDescriptor desc = + OptionDescriptor desc = createOption(Option::V4, DHO_BOOT_FILE_NAME, true, formatted, "my-boot-file"); desc.setContext(user_context); @@ -129,7 +129,7 @@ GenericHostDataSourceTest::addTestOptions(const HostPtr& host, if ((added_options == DHCP6_ONLY) || (added_options == DHCP4_AND_DHCP6)) { // Add DHCPv6 options. CfgOptionPtr opts = host->getCfgOption6(); - OptionDescriptor desc = + OptionDescriptor desc = createOption(Option::V6, D6O_BOOTFILE_URL, true, formatted, "my-boot-file"); desc.setContext(user_context); @@ -1022,6 +1022,11 @@ void GenericHostDataSourceTest::testOptionsReservations4(const bool formatted, // Subnet id will be used in queries to the database. SubnetID subnet_id = host->getIPv4SubnetID(); + // getAll4(subnet_id) + ConstHostCollection hosts_by_subnet = hdsptr_->getAll4(subnet_id); + // Not yet implemented. + EXPECT_EQ(0, hosts_by_subnet.size()); + // getAll4(address) ConstHostCollection hosts_by_addr = hdsptr_->getAll4(host->getIPv4Reservation()); @@ -1069,6 +1074,13 @@ GenericHostDataSourceTest::testOptionsReservations46(const bool formatted) { ASSERT_NO_THROW(addTestOptions(host, formatted, DHCP4_AND_DHCP6)); // Insert host, options and IPv6 reservations into respective tables. ASSERT_NO_THROW(hdsptr_->add(host)); + // Subnet id will be used in queries to the database. + SubnetID subnet_id = host->getIPv6SubnetID(); + + // getAll6(subnet_id) + ConstHostCollection hosts_by_subnet = hdsptr_->getAll6(subnet_id); + // Not yet implemented. + EXPECT_EQ(0, hosts_by_subnet.size()); // getAll(identifier_type, identifier, identifier_size) ConstHostCollection hosts_by_id = @@ -1289,7 +1301,7 @@ GenericHostDataSourceTest::stressTest(unsigned int nOfHosts /* = 0xfffdU */) { const std::string prefix = std::string("2001:db8::") + n_host; hosts.push_back(HostDataSourceUtils::initializeHost6(prefix, Host::IDENT_HWADDR, false, "key##1")); - + IPv6ResrvRange range = hosts.back()->getIPv6Reservations(); ASSERT_EQ(1, std::distance(range.first, range.second)); EXPECT_TRUE(HostDataSourceUtils::reservationExists diff --git a/src/lib/dhcpsrv/testutils/memory_host_data_source.cc b/src/lib/dhcpsrv/testutils/memory_host_data_source.cc index d5589476f5..3c28949d5f 100644 --- a/src/lib/dhcpsrv/testutils/memory_host_data_source.cc +++ b/src/lib/dhcpsrv/testutils/memory_host_data_source.cc @@ -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 @@ -22,6 +22,16 @@ MemHostDataSource::getAll(const Host::IdentifierType& /*identifier_type*/, return (ConstHostCollection()); } +ConstHostCollection +MemHostDataSource::getAll4(const SubnetID& /*subnet_id*/) const { + return (ConstHostCollection()); +} + +ConstHostCollection +MemHostDataSource::getAll6(const SubnetID& /*subnet_id*/) const { + return (ConstHostCollection()); +} + ConstHostCollection MemHostDataSource::getAll4(const asiolink::IOAddress& /*address*/) const { return (ConstHostCollection()); diff --git a/src/lib/dhcpsrv/testutils/memory_host_data_source.h b/src/lib/dhcpsrv/testutils/memory_host_data_source.h index ce43090e0b..380a6dba23 100644 --- a/src/lib/dhcpsrv/testutils/memory_host_data_source.h +++ b/src/lib/dhcpsrv/testutils/memory_host_data_source.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 @@ -46,6 +46,22 @@ public: const uint8_t* identifier_begin, const size_t identifier_len) const; + /// @brief Return all hosts in a DHCPv4 subnet. + /// + /// Currently not implemented. + /// + /// @param subnet_id Subnet identifier. + virtual ConstHostCollection + getAll4(const SubnetID& subnet_id) const; + + /// @brief Return all hosts in a DHCPv6 subnet. + /// + /// Currently not implemented. + /// + /// @param subnet_id Subnet identifier. + virtual ConstHostCollection + getAll6(const SubnetID& subnet_id) const; + /// @brief Returns a collection of hosts using the specified IPv4 address. /// /// Currently not implemented. diff --git a/src/lib/dhcpsrv/writable_host_data_source.h b/src/lib/dhcpsrv/writable_host_data_source.h index 931ed12995..d3965c9e2f 100644 --- a/src/lib/dhcpsrv/writable_host_data_source.h +++ b/src/lib/dhcpsrv/writable_host_data_source.h @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2014-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 @@ -41,6 +41,22 @@ public: const uint8_t* identifier_begin, const size_t identifier_len) = 0; + /// @brief Returns a collection of hosts in the specified DHCPv4 subnet. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of non-const @c Host objects. + virtual HostCollection + getAll4(const SubnetID& subnet_id) = 0; + + /// @brief Returns a collection of hosts in the specified DHCPv6 subnet. + /// + /// @param subnet_id Subnet identifier. + /// + /// @return Collection of non-const @c Host objects. + virtual HostCollection + getAll6(const SubnetID& subnet_id) = 0; + /// @brief Returns a collection of hosts using the specified IPv4 address. /// /// This method may return multiple @c Host objects if they are connected