Browse Source

[2315] Added routine to get the single option for the space and code.

Marcin Siodelski 12 years ago
parent
commit
0fe0d44af6
3 changed files with 51 additions and 1 deletions
  1. 16 0
      src/lib/dhcpsrv/subnet.cc
  2. 10 1
      src/lib/dhcpsrv/subnet.h
  3. 25 0
      src/lib/dhcpsrv/tests/subnet_unittest.cc

+ 16 - 0
src/lib/dhcpsrv/subnet.cc

@@ -85,6 +85,22 @@ Subnet::getOptions(const std::string& option_space) const {
     return (options->second);
 }
 
+Subnet::OptionDescriptor
+Subnet::getOptionSingle(const std::string& option_space,
+                        const uint16_t option_code) {
+    const OptionContainer& options = getOptions(option_space);
+    if (options.empty()) {
+        return (OptionDescriptor(false));
+    }
+    const OptionContainerTypeIndex& idx = options.get<1>();
+    const OptionContainerTypeRange& range = idx.equal_range(option_code);
+    if (std::distance(range.first, range.second) == 0) {
+        return (OptionDescriptor(false));
+    }
+
+    return (*range.first);
+}
+
 std::string Subnet::toText() const {
     std::stringstream tmp;
     tmp << prefix_.toText() << "/" << static_cast<unsigned int>(prefix_len_);

+ 10 - 1
src/lib/dhcpsrv/subnet.h

@@ -264,7 +264,7 @@ public:
         return (t2_);
     }
 
-    /// @brief Return a collection of options.
+    /// @brief Return a collection of option descriptors.
     ///
     /// @param option_space name of the option space
     ///
@@ -273,6 +273,15 @@ public:
     /// returned it still exists.
     const OptionContainer& getOptions(const std::string& option_space) const;
 
+    /// @brief Return single option descriptor.
+    ///
+    /// @param option_space name of the option space.
+    ///
+    /// @return option descriptor found for the specified option space
+    /// and option code.
+    OptionDescriptor getOptionSingle(const std::string& option_space,
+                                     const uint16_t option_code);
+
     /// @brief returns the last address that was tried from this pool
     ///
     /// This method returns the last address that was attempted to be allocated

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

@@ -427,6 +427,31 @@ TEST(Subnet6Test, addPersistentOption) {
     EXPECT_EQ(0, options.size());
 }
 
+TEST(Subnet6Test, getOptionSingle) {
+    Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 56, 1, 2, 3, 4));
+
+    // Add 10 options to a "dhcp6" option space in the subnet.
+    for (uint16_t code = 100; code < 110; ++code) {
+        OptionPtr option(new Option(Option::V6, code, OptionBuffer(10, 0xFF)));
+        ASSERT_NO_THROW(subnet->addOption(option, false, "dhcp6"));
+    }
+
+    // Check that we can get each added option descriptor using
+    // individually.
+    for (uint16_t code = 100; code < 110; ++code) {
+        std::ostringstream stream;
+        // First, try the invalid option space name.
+        Subnet::OptionDescriptor desc = subnet->getOptionSingle("isc", code);
+        // Returned descriptor should contain NULL option ptr.
+        EXPECT_FALSE(desc.option);
+        // Now, try the valid option space.
+        desc = subnet->getOptionSingle("dhcp6", code);
+        // Test that the option code matches the expected code.
+        ASSERT_TRUE(desc.option);
+        EXPECT_EQ(code, desc.option->getType());
+    }
+}
+
 // This test verifies that inRange() and inPool() methods work properly.
 TEST(Subnet6Test, inRangeinPool) {
     Subnet6Ptr subnet(new Subnet6(IOAddress("2001:db8::"), 32, 1, 2, 3, 4));