]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#103,!289] Added new function to delete option definitions by id.
authorMarcin Siodelski <marcin@isc.org>
Thu, 4 Apr 2019 10:21:21 +0000 (12:21 +0200)
committerMarcin Siodelski <marcin@isc.org>
Wed, 10 Apr 2019 14:57:43 +0000 (16:57 +0200)
src/lib/dhcp/option_space_container.h
src/lib/dhcpsrv/cfg_option_def.cc
src/lib/dhcpsrv/cfg_option_def.h
src/lib/dhcpsrv/tests/cfg_option_def_unittest.cc

index 4f54bb0cf34e4d9a6eb85b3b255bfafe46211e48..f480db058524f423dd94d3e95eb0ebbd284d761c 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef OPTION_SPACE_CONTAINER_H
 #define OPTION_SPACE_CONTAINER_H
 
+#include <exceptions/exceptions.h>
 #include <list>
 #include <string>
 
@@ -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<OptionIdIndexTag>();
+            num_deleted += index.erase(id);
+        }
+
+        return (num_deleted);
+    }
+
     /// @brief Check if two containers are equal.
     ///
     /// This method checks if option space container contains exactly
index 2bb2f889a7081de72ed2408064ce5b8773c95bc4..26aff35f10033e6ae185c5bbd9bd5b856f49280f 100644 (file)
@@ -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
index 8a93280d230edfa72f1739309a03f62dc1930f9d..80f95e70e9a9f914f98dd8c586992e087390de8e 100644 (file)
@@ -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_);
index ec548202da617803bcacb7348b1b76c576b353c1..5c9a9075c9c2a6920bbd6102f7899c3259777136 100644 (file)
@@ -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