]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[465-add-subnet4-update-and-subnet6-update-commands-to-subnet-cmds-hook] Added shared...
authorFrancis Dupont <fdupont@isc.org>
Thu, 7 Mar 2019 15:08:28 +0000 (16:08 +0100)
committerTomek Mrugalski <tomek@isc.org>
Fri, 19 Apr 2019 10:39:47 +0000 (06:39 -0400)
src/lib/dhcpsrv/shared_network.cc
src/lib/dhcpsrv/shared_network.h

index fcfcd117413b6951cb4602610aaf1ceedf1eeb62..089fd2015f246a0803e6b2692b75fd16cf05a7fc 100644 (file)
@@ -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<typename SubnetPtrType, typename SubnetCollectionType>
+    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<SubnetSubnetIdIndexTag>();
+        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<Subnet4Ptr>(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<Subnet6Ptr>(subnets_, subnet_id);
index 6ed825f3e15ccf659b1e8e7c536b605a6aa4f75d..e2a8ddc922be2776088a05e0ede5233962f7de8b 100644 (file)
@@ -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.