Commit 0fe0d44a authored by Marcin Siodelski's avatar Marcin Siodelski

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

parent 5530bff2
......@@ -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_);
......
......@@ -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
......
......@@ -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));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment