From: Marcin Siodelski Date: Thu, 4 Apr 2019 10:21:21 +0000 (+0200) Subject: [#103,!289] Added new function to delete option definitions by id. X-Git-Tag: Kea-1.6.0-beta~276 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf5f4dde91a2bd2f9493f06b435aee5de2264fd3;p=thirdparty%2Fkea.git [#103,!289] Added new function to delete option definitions by id. --- diff --git a/src/lib/dhcp/option_space_container.h b/src/lib/dhcp/option_space_container.h index 4f54bb0cf3..f480db0585 100644 --- a/src/lib/dhcp/option_space_container.h +++ b/src/lib/dhcp/option_space_container.h @@ -7,6 +7,7 @@ #ifndef OPTION_SPACE_CONTAINER_H #define OPTION_SPACE_CONTAINER_H +#include #include #include @@ -91,6 +92,29 @@ public: option_space_map_.clear(); } + /// @brief Remove all options or option definitions with a given + /// database identifier. + /// + /// Note that there are cases when there will be multiple options + /// or option definitions having the same id (typically id of 0). + /// When configuration backend is in use it sets the unique ids + /// from the database. In cases when the configuration backend is + /// not used, the ids default to 0. + + /// @param id Identifier of the items to be deleted. + /// + /// @return Number of deleted options or option definitions. + uint64_t deleteItems(const uint64_t id) { + uint64_t num_deleted = 0; + for (auto space : option_space_map_) { + auto container = space.second; + auto& index = container->template get(); + num_deleted += index.erase(id); + } + + return (num_deleted); + } + /// @brief Check if two containers are equal. /// /// This method checks if option space container contains exactly diff --git a/src/lib/dhcpsrv/cfg_option_def.cc b/src/lib/dhcpsrv/cfg_option_def.cc index 2bb2f889a7..26aff35f10 100644 --- a/src/lib/dhcpsrv/cfg_option_def.cc +++ b/src/lib/dhcpsrv/cfg_option_def.cc @@ -152,6 +152,11 @@ CfgOptionDef::get(const std::string& option_space, return (OptionDefinitionPtr()); } +uint64_t +CfgOptionDef::del(const uint64_t id) { + return (option_definitions_.deleteItems(id)); +} + ElementPtr CfgOptionDef::toElement() const { // option-defs value is a list of maps diff --git a/src/lib/dhcpsrv/cfg_option_def.h b/src/lib/dhcpsrv/cfg_option_def.h index 8a93280d23..80f95e70e9 100644 --- a/src/lib/dhcpsrv/cfg_option_def.h +++ b/src/lib/dhcpsrv/cfg_option_def.h @@ -113,6 +113,19 @@ public: OptionDefinitionPtr get(const std::string& option_space, const std::string& option_name) const; + /// @brief Deletes all option definitions having a given database id. + /// + /// Note that there are cases when there will be multiple option + /// definitions having the same id (typically id of 0). When + /// configuration backend is in use it sets the unique ids from the + /// database. In cases when the configuration backend is not used, + /// the ids default to 0. + /// + /// @param id Identifier of the option definitions to be deleted. + /// + /// @return Number of deleted option definitions. + uint64_t del(const uint64_t id); + /// @brief Returns reference to container holding option definitions. const OptionDefSpaceContainer& getContainer() const { return (option_definitions_); diff --git a/src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc b/src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc index ec548202da..5c9a9075c9 100644 --- a/src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc +++ b/src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc @@ -50,8 +50,8 @@ TEST(CfgOptionDefTest, equal) { } // This test verifies that multiple option definitions can be added -// under different option spaces. -TEST(CfgOptionDefTest, getAll) { +// under different option spaces and then removed. +TEST(CfgOptionDefTest, getAllThenDelete) { CfgOptionDef cfg; // Create a set of option definitions with codes between 100 and 109. @@ -61,6 +61,8 @@ TEST(CfgOptionDefTest, getAll) { option_name << "option-" << code; OptionDefinitionPtr def(new OptionDefinition(option_name.str(), code, "uint16")); + def->setId(123); + // Add option definition to "isc" option space. // Option codes are not duplicated so expect no error // when adding them. @@ -74,6 +76,9 @@ TEST(CfgOptionDefTest, getAll) { option_name << "option-" << code; OptionDefinitionPtr def(new OptionDefinition(option_name.str(), code, "uint16")); + + def->setId(234); + ASSERT_NO_THROW(cfg.add(def, "abcde")); } @@ -112,6 +117,32 @@ TEST(CfgOptionDefTest, getAll) { OptionDefContainerPtr option_defs3 = cfg.getAll("non-existing"); ASSERT_TRUE(option_defs3); EXPECT_TRUE(option_defs3->empty()); + + // Check that we can delete option definitions by id. + uint64_t num_deleted = 0; + ASSERT_NO_THROW(num_deleted = cfg.del(123)); + EXPECT_EQ(10, num_deleted); + + option_defs1 = cfg.getAll("isc"); + ASSERT_TRUE(option_defs1); + ASSERT_EQ(0, option_defs1->size()); + + option_defs2 = cfg.getAll("abcde"); + ASSERT_TRUE(option_defs2); + ASSERT_EQ(10, option_defs2->size()); + + // Second attempt to delete the same option definitions should + // result in 0 deletions. + ASSERT_NO_THROW(num_deleted = cfg.del(123)); + EXPECT_EQ(0, num_deleted); + + // Delete all other option definitions. + ASSERT_NO_THROW(num_deleted = cfg.del(234)); + EXPECT_EQ(10, num_deleted); + + option_defs2 = cfg.getAll("abcde"); + ASSERT_TRUE(option_defs2); + ASSERT_EQ(0, option_defs2->size()); } // This test verifies that single option definition is correctly