Browse Source

[5315] Added functions that remove subnets from a configuration.

Marcin Siodelski 7 years ago
parent
commit
03e1804d39

+ 14 - 0
src/lib/dhcpsrv/cfg_subnets4.cc

@@ -39,6 +39,20 @@ CfgSubnets4::add(const Subnet4Ptr& subnet) {
     subnets_.push_back(subnet);
     subnets_.push_back(subnet);
 }
 }
 
 
+void
+CfgSubnets4::del(const ConstSubnet4Ptr& subnet) {
+    auto& index = subnets_.get<SubnetIdIndexTag>();
+    auto subnet_it = index.find(subnet->getID());
+    if (subnet_it == index.end()) {
+        isc_throw(BadValue, "no subnet with ID of '" << subnet->getID()
+                  << "' found");
+    }
+    index.erase(subnet_it);
+
+    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_DEL_SUBNET4)
+        .arg(subnet->toText());
+}
+
 ConstSubnet4Ptr
 ConstSubnet4Ptr
 CfgSubnets4::getBySubnetId(const SubnetID& subnet_id) const {
 CfgSubnets4::getBySubnetId(const SubnetID& subnet_id) const {
     const auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
     const auto& index = subnets_.get<SubnetSubnetIdIndexTag>();

+ 7 - 0
src/lib/dhcpsrv/cfg_subnets4.h

@@ -39,6 +39,13 @@ public:
     /// duplicates id of an existing subnet.
     /// duplicates id of an existing subnet.
     void add(const Subnet4Ptr& subnet);
     void add(const Subnet4Ptr& subnet);
 
 
+    /// @brief Removes subnet from the configuration.
+    ///
+    /// @param subnet Pointer to the subnet to be removed.
+    ///
+    /// @throw isc::BadValue if such subnet doesn't exist.
+    void del(const ConstSubnet4Ptr& subnet);
+
     /// @brief Returns pointer to the collection of all IPv4 subnets.
     /// @brief Returns pointer to the collection of all IPv4 subnets.
     ///
     ///
     /// This is used in a hook (subnet4_select), where the hook is able
     /// This is used in a hook (subnet4_select), where the hook is able

+ 14 - 0
src/lib/dhcpsrv/cfg_subnets6.cc

@@ -38,6 +38,20 @@ CfgSubnets6::add(const Subnet6Ptr& subnet) {
     subnets_.push_back(subnet);
     subnets_.push_back(subnet);
 }
 }
 
 
+void
+CfgSubnets6::del(const ConstSubnet6Ptr& subnet) {
+    auto& index = subnets_.get<SubnetIdIndexTag>();
+    auto subnet_it = index.find(subnet->getID());
+    if (subnet_it == index.end()) {
+        isc_throw(BadValue, "no subnet with ID of '" << subnet->getID()
+                  << "' found");
+    }
+    index.erase(subnet_it);
+
+    LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_DEL_SUBNET6)
+        .arg(subnet->toText());
+}
+
 ConstSubnet6Ptr
 ConstSubnet6Ptr
 CfgSubnets6::getBySubnetId(const SubnetID& subnet_id) const {
 CfgSubnets6::getBySubnetId(const SubnetID& subnet_id) const {
     const auto& index = subnets_.get<SubnetSubnetIdIndexTag>();
     const auto& index = subnets_.get<SubnetSubnetIdIndexTag>();

+ 7 - 0
src/lib/dhcpsrv/cfg_subnets6.h

@@ -40,6 +40,13 @@ public:
     /// duplicates id of an existing subnet.
     /// duplicates id of an existing subnet.
     void add(const Subnet6Ptr& subnet);
     void add(const Subnet6Ptr& subnet);
 
 
+    /// @brief Removes subnet from the configuration.
+    ///
+    /// @param subnet Pointer to the subnet to be removed.
+    ///
+    /// @throw isc::BadValue if such subnet doesn't exist.
+    void del(const ConstSubnet6Ptr& subnet);
+
     /// @brief Returns pointer to the collection of all IPv6 subnets.
     /// @brief Returns pointer to the collection of all IPv6 subnets.
     ///
     ///
     /// This is used in a hook (subnet6_select), where the hook is able
     /// This is used in a hook (subnet6_select), where the hook is able

+ 7 - 0
src/lib/dhcpsrv/dhcpsrv_messages.mes

@@ -44,6 +44,13 @@ there is a good reason for it, to avoid increased number of renewals and
 a need for rebinding (increase of multicast traffic, which may be received
 a need for rebinding (increase of multicast traffic, which may be received
 by multiple servers).
 by multiple servers).
 
 
+% DHCPSRV_CFGMGR_DEL_SUBNET4 IPv4 subnet %1 removed
+This debug message is issued when a subnet is successfully removed from the
+server configuration. The argument identifies the subnet removed.
+
+% DHCPSRV_CFGMGR_DEL_SUBNET6 IPv6 subnet %1 removed
+This debug message is issued when a subnet is successfully removed from the
+
 % DHCPSRV_CFGMGR_NEW_SUBNET4 a new subnet has been added to configuration: %1
 % DHCPSRV_CFGMGR_NEW_SUBNET4 a new subnet has been added to configuration: %1
 This is an informational message reporting that the configuration has
 This is an informational message reporting that the configuration has
 been extended to include the specified IPv4 subnet.
 been extended to include the specified IPv4 subnet.

+ 28 - 0
src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc

@@ -74,6 +74,34 @@ TEST(CfgSubnets4Test, getSpecificSubnet) {
     EXPECT_FALSE(cfg.getByPrefix("10.20.30.0/29"));
     EXPECT_FALSE(cfg.getByPrefix("10.20.30.0/29"));
 }
 }
 
 
+// This test verifies that a single subnet can be removed from the configuration.
+TEST(CfgSubnets4Test, deleteSubnet) {
+    CfgSubnets4 cfg;
+
+    // Create 3 subnets.
+    Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"),
+                                   26, 1, 2, 3, SubnetID(5)));
+    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.3.0"),
+                                   26, 1, 2, 3, SubnetID(8)));
+    Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.4.0"),
+                                   26, 1, 2, 3, SubnetID(10)));
+
+    ASSERT_NO_THROW(cfg.add(subnet1));
+    ASSERT_NO_THROW(cfg.add(subnet2));
+    ASSERT_NO_THROW(cfg.add(subnet3));
+
+    // There should be three subnets.
+    ASSERT_EQ(3, cfg.getAll()->size());
+    // We're going to remove the subnet #2. Let's make sure it exists before
+    // we remove it.
+    ASSERT_TRUE(cfg.getByPrefix("192.0.3.0/26"));
+
+    // Remove the subnet and make sure it is gone.
+    ASSERT_NO_THROW(cfg.del(subnet2));
+    ASSERT_EQ(2, cfg.getAll()->size());
+    EXPECT_FALSE(cfg.getByPrefix("192.0.3.0/26"));
+}
+
 // This test verifies that it is possible to retrieve a subnet using an
 // This test verifies that it is possible to retrieve a subnet using an
 // IP address.
 // IP address.
 TEST(CfgSubnets4Test, selectSubnetByCiaddr) {
 TEST(CfgSubnets4Test, selectSubnetByCiaddr) {

+ 25 - 0
src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc

@@ -83,6 +83,31 @@ TEST(CfgSubnets6Test, getSpecificSubnet) {
     EXPECT_FALSE(cfg.getByPrefix("3000::/16"));
     EXPECT_FALSE(cfg.getByPrefix("3000::/16"));
 }
 }
 
 
+// This test verifies that a single subnet can be removed from the configuration.
+TEST(CfgSubnets6Test, deleteSubnet) {
+    CfgSubnets6 cfg;
+
+    // Create 3 subnets.
+    Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4));
+    Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4));
+    Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4));
+
+    ASSERT_NO_THROW(cfg.add(subnet1));
+    ASSERT_NO_THROW(cfg.add(subnet2));
+    ASSERT_NO_THROW(cfg.add(subnet3));
+
+    // There should be three subnets.
+    ASSERT_EQ(3, cfg.getAll()->size());
+    // We're going to remove the subnet #2. Let's make sure it exists before
+    // we remove it.
+    ASSERT_TRUE(cfg.getByPrefix("2001:db8:2::/48"));
+
+    // Remove the subnet and make sure it is gone.
+    ASSERT_NO_THROW(cfg.del(subnet2));
+    ASSERT_EQ(2, cfg.getAll()->size());
+    EXPECT_FALSE(cfg.getByPrefix("2001:db8:2::/48"));
+}
+
 // This test checks that the subnet can be selected using a relay agent's
 // This test checks that the subnet can be selected using a relay agent's
 // link address.
 // link address.
 TEST(CfgSubnets6Test, selectSubnetByRelayAddress) {
 TEST(CfgSubnets6Test, selectSubnetByRelayAddress) {