From: Francis Dupont Date: Thu, 7 Mar 2019 15:08:28 +0000 (+0100) Subject: [465-add-subnet4-update-and-subnet6-update-commands-to-subnet-cmds-hook] Added shared... X-Git-Tag: Kea-1.6.0-beta~206 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e8b2ec70112a302c7d30fe9d9c895a7c363d5ebf;p=thirdparty%2Fkea.git [465-add-subnet4-update-and-subnet6-update-commands-to-subnet-cmds-hook] Added shared network replace(subnet) -- unit test to do --- diff --git a/src/lib/dhcpsrv/shared_network.cc b/src/lib/dhcpsrv/shared_network.cc index fcfcd11741..089fd2015f 100644 --- a/src/lib/dhcpsrv/shared_network.cc +++ b/src/lib/dhcpsrv/shared_network.cc @@ -69,6 +69,51 @@ public: subnets.push_back(subnet); } + /// @brief Replaces IPv4 subnet in a shared network. + /// + /// This generic method replaces a subnet by another subnet + /// with the same ID in a shared network. + /// The prefix should be the same too. + /// + /// @tparam SubnetPtrType Type of a pointer to a subnet, i.e. Subnet4Ptr + /// or @ref Subnet6Ptr. + + /// @tparam SubnetCollectionType Type of a container holding subnets, i.e. + /// @ref Subnet4Collection or @ref Subnet6Collection. + /// + /// @param [out] subnets Container holding subnets for this shared network. + /// @param subnet Pointer to a subnet replacing the subnet with the same ID + /// in this shared network. + /// + /// @throw isc::BadValue if subnet is null. + /// @throw InvalidOperation if a subnet is already associated with some + /// shared network. + //// + /// @return true if the operation succeeded, false otherwise. + template + static bool replace(SubnetCollectionType& subnets, + const SubnetPtrType& subnet) { + + // Subnet must be non-null. + if (!subnet) { + isc_throw(BadValue, "null pointer specified when adding a subnet" + " to a shared network"); + } + + // Get the subnet with the same ID. + const SubnetID& subnet_id = subnet->getID(); + auto& index = subnets.template get(); + auto subnet_it = index.find(subnet_id); + if (subnet_it == index.end()) { + // Nothing to replace: return false to get the whole operation + // to be rollbacked. + return (false); + } + + // Replace it. + return (index.replace(subnet_it, subnet)); + } + /// @brief Removes a subnet from the shared network. /// /// @param [out] subnets Container holding subnets for this shared network. @@ -258,6 +303,21 @@ SharedNetwork4::add(const Subnet4Ptr& subnet) { subnet->setSharedNetworkName(name_); } +bool +SharedNetwork4::replace(const Subnet4Ptr& subnet) { + const Subnet4Ptr& old = getSubnet(subnet->getID()); + bool ret = Impl::replace(subnets_, subnet); + if (ret) { + // Associate the subnet with this network. + setSharedNetwork(subnet); + subnet->setSharedNetworkName(name_); + // Deassociate the previous subnet. + clearSharedNetwork(old); + old->setSharedNetworkName(""); + } + return (ret); +} + void SharedNetwork4::del(const SubnetID& subnet_id) { Subnet4Ptr subnet = Impl::del(subnets_, subnet_id); @@ -323,6 +383,21 @@ SharedNetwork6::add(const Subnet6Ptr& subnet) { subnet->setSharedNetworkName(name_); } +bool +SharedNetwork6::replace(const Subnet6Ptr& subnet) { + const Subnet6Ptr& old = getSubnet(subnet->getID()); + bool ret = Impl::replace(subnets_, subnet); + if (ret) { + // Associate the subnet with this network. + setSharedNetwork(subnet); + subnet->setSharedNetworkName(name_); + // Deassociate the previous subnet. + clearSharedNetwork(old); + old->setSharedNetworkName(""); + } + return (ret); +} + void SharedNetwork6::del(const SubnetID& subnet_id) { Subnet6Ptr subnet = Impl::del(subnets_, subnet_id); diff --git a/src/lib/dhcpsrv/shared_network.h b/src/lib/dhcpsrv/shared_network.h index 6ed825f3e1..e2a8ddc922 100644 --- a/src/lib/dhcpsrv/shared_network.h +++ b/src/lib/dhcpsrv/shared_network.h @@ -96,6 +96,19 @@ public: /// shared network. void add(const Subnet4Ptr& subnet); + /// @brief Replaces IPv4 subnet in a shared network. + /// + /// This method replaces a subnet by another subnet with the same ID. + /// The prefix should be the same too. + /// + /// @param subnet Pointer to a subnet replacing the subnet with the same ID + /// in this shared network. + /// @throw isc::BadValue if subnet is null. + /// @throw InvalidOperation if a subnet is already associated with some + /// shared network. + /// @return true if the operation succeeded, false otherwise. + bool replace(const Subnet4Ptr& subnet); + /// @brief Removes subnet from a shared network. /// /// @param subnet_id Identifier of a subnet to be removed. @@ -273,6 +286,19 @@ public: /// shared network. void add(const Subnet6Ptr& subnet); + /// @brief Replaces IPv6 subnet in a shared network. + /// + /// This method replaces a subnet by another subnet with the same ID. + /// The prefix should be the same too. + /// + /// @param subnet Pointer to a subnet replacing the subnet with the same ID + /// in this shared network. + /// @throw isc::BadValue if subnet is null. + /// @throw InvalidOperation if a subnet is already associated with some + /// shared network. + /// @return true if the operation succeeded, false otherwise. + bool replace(const Subnet6Ptr& subnet); + /// @brief Removes subnet from a shared network. /// /// @param subnet_id Identifier of a subnet to be removed.