Browse Source

[5272] getSubnet implemented.

Tomek Mrugalski 7 years ago
parent
commit
c16e3e91e6

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

@@ -196,6 +196,19 @@ CfgSubnets4::selectSubnet(const std::string& iface,
 }
 
 Subnet4Ptr
+CfgSubnets4::getSubnet(const SubnetID id) const {
+
+    /// @todo: Once this code is migrated to multi-index container, use
+    /// an index rather than full scan.
+    for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) {
+        if ((*subnet)->getID() == id) {
+            return (*subnet);
+        }
+    }
+    return (Subnet4Ptr());
+}
+
+Subnet4Ptr
 CfgSubnets4::selectSubnet(const IOAddress& address,
                  const ClientClasses& client_classes) const {
     for (Subnet4Collection::const_iterator subnet = subnets_.begin();

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

@@ -97,6 +97,14 @@ public:
     /// or they are insufficient to select a subnet.
     Subnet4Ptr selectSubnet(const SubnetSelector& selector) const;
 
+    /// @brief Returns subnet with specified subnet-id value
+    ///
+    /// Warning: this method uses full scan. Its use is not recommeded for
+    /// packet processing.
+    ///
+    /// @return Subnet (or NULL)
+    Subnet4Ptr getSubnet(const SubnetID id) const;
+
     /// @brief Returns a pointer to a subnet if provided address is in its range.
     ///
     /// This method returns a pointer to the subnet if the address passed in

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

@@ -166,6 +166,20 @@ CfgSubnets6::selectSubnet(const OptionPtr& interface_id,
     return (Subnet6Ptr());
 }
 
+Subnet6Ptr
+CfgSubnets6::getSubnet(const SubnetID id) const {
+
+    /// @todo: Once this code is migrated to multi-index container, use
+    /// an index rather than full scan.
+    for (auto subnet = subnets_.begin(); subnet != subnets_.end(); ++subnet) {
+        if ((*subnet)->getID() == id) {
+            return (*subnet);
+        }
+    }
+    return (Subnet6Ptr());
+}
+
+
 bool
 CfgSubnets6::isDuplicate(const Subnet6& subnet) const {
     for (Subnet6Collection::const_iterator subnet_it = subnets_.begin();

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

@@ -87,6 +87,14 @@ public:
     /// @return Pointer to the selected subnet or NULL if no subnet found.
     Subnet6Ptr selectSubnet(const SubnetSelector& selector) const;
 
+    /// @brief Returns subnet with specified subnet-id value
+    ///
+    /// Warning: this method uses full scan. Its use is not recommeded for
+    /// packet processing.
+    ///
+    /// @return Subnet (or NULL)
+    Subnet6Ptr getSubnet(const SubnetID id) const;
+
     /// @brief Selects the subnet using a specified address.
     ///
     /// This method searches for the subnet using the specified address. If

+ 21 - 1
src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc

@@ -524,7 +524,7 @@ TEST(CfgSubnets4Test, unparsePool) {
     subnet->addPool(pool1);
     subnet->addPool(pool2);
     cfg.add(subnet);
-    
+
     // Unparse
     std::string expected = "[\n"
         "{\n"
@@ -555,4 +555,24 @@ TEST(CfgSubnets4Test, unparsePool) {
     runToElementTest<CfgSubnets4>(expected, cfg);
 }
 
+// This test verifies that it is possible to retrieve a subnet using subnet-id.
+TEST(CfgSubnets4Test, getSubnet) {
+    CfgSubnets4 cfg;
+
+    // Create 3 subnets.
+    Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3, 100));
+    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3, 200));
+    Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3, 300));
+
+    // Add one subnet and make sure it is returned.
+    cfg.add(subnet1);
+    cfg.add(subnet2);
+    cfg.add(subnet3);
+
+    EXPECT_EQ(subnet1, cfg.getSubnet(100));
+    EXPECT_EQ(subnet2, cfg.getSubnet(200));
+    EXPECT_EQ(subnet3, cfg.getSubnet(300));
+    EXPECT_EQ(Subnet4Ptr(), cfg.getSubnet(400)); // no such subnet
+}
+
 } // end of anonymous namespace

+ 21 - 3
src/lib/dhcpsrv/tests/cfg_subnets6_unittest.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2017 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
@@ -434,7 +434,7 @@ TEST(CfgSubnets6Test, unparsePool) {
     subnet->addPool(pool1);
     subnet->addPool(pool2);
     cfg.add(subnet);
-    
+
     // Unparse
     std::string expected = "[\n"
         "{\n"
@@ -480,7 +480,7 @@ TEST(CfgSubnets6Test, unparsePdPool) {
     subnet->addPool(pdpool1);
     subnet->addPool(pdpool2);
     cfg.add(subnet);
-    
+
     // Unparse
     std::string expected = "[\n"
         "{\n"
@@ -518,4 +518,22 @@ TEST(CfgSubnets6Test, unparsePdPool) {
     runToElementTest<CfgSubnets6>(expected, cfg);
 }
 
+// This test verifies that it is possible to retrieve a subnet using subnet-id.
+TEST(CfgSubnets6Test, getSubnet) {
+    CfgSubnets6 cfg;
+
+    // Let's configure 3 subnets
+    Subnet6Ptr subnet1(new Subnet6(IOAddress("2001:db8:1::"), 48, 1, 2, 3, 4, 100));
+    Subnet6Ptr subnet2(new Subnet6(IOAddress("2001:db8:2::"), 48, 1, 2, 3, 4, 200));
+    Subnet6Ptr subnet3(new Subnet6(IOAddress("2001:db8:3::"), 48, 1, 2, 3, 4, 300));
+    cfg.add(subnet1);
+    cfg.add(subnet2);
+    cfg.add(subnet3);
+
+    EXPECT_EQ(subnet1, cfg.getSubnet(100));
+    EXPECT_EQ(subnet2, cfg.getSubnet(200));
+    EXPECT_EQ(subnet3, cfg.getSubnet(300));
+    EXPECT_EQ(Subnet6Ptr(), cfg.getSubnet(400)); // no such subnet
+}
+
 } // end of anonymous namespace