item_updater : Implementing Object.Delete for version

Change-Id: If962dec1da2d2fe925374bb37f89ed23f04c5ab6
Signed-off-by: Leonel Gonzalez <lgonzalez@us.ibm.com>
diff --git a/activation.cpp b/activation.cpp
index 3d70811..b63853d 100755
--- a/activation.cpp
+++ b/activation.cpp
@@ -266,6 +266,11 @@
     return;
 }
 
+void Activation::delete_()
+{
+    parent.erase(versionId);
+}
+
 } // namespace updater
 } // namespace software
 } // namespace openpower
diff --git a/activation.hpp b/activation.hpp
index bc99fbe..7702c52 100755
--- a/activation.hpp
+++ b/activation.hpp
@@ -6,6 +6,7 @@
 #include "xyz/openbmc_project/Software/ExtendedVersion/server.hpp"
 #include "xyz/openbmc_project/Software/RedundancyPriority/server.hpp"
 #include "xyz/openbmc_project/Software/ActivationProgress/server.hpp"
+#include "xyz/openbmc_project/Object/Delete/server.hpp"
 
 namespace openpower
 {
@@ -15,6 +16,7 @@
 {
 
 using ActivationInherit = sdbusplus::server::object::object<
+    sdbusplus::xyz::openbmc_project::Object::server::Delete,
     sdbusplus::xyz::openbmc_project::Software::server::ExtendedVersion,
     sdbusplus::xyz::openbmc_project::Software::server::Activation>;
 using ActivationBlocksTransitionInherit = sdbusplus::server::object::object<
@@ -279,6 +281,19 @@
         /** @brief Tracks whether the read-write volumes have been created as
          * part of the activation process. **/
         bool rwVolumesCreated = false;
+
+        /** @brief activation status property get function
+         *
+         * @returns Activations - The activation value
+         */
+        using ActivationInherit::activation;
+
+        /** @brief Deletes the d-bus object.
+         *
+         *
+         * */
+        void delete_() override;
+
 };
 
 } // namespace updater
diff --git a/item_updater.cpp b/item_updater.cpp
index 7466d12..1c0a6b8 100755
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -121,7 +121,8 @@
                                 path,
                                 version,
                                 purpose,
-                                filePath)));
+                                filePath,
+                                *this)));
     }
     return;
 }
@@ -162,7 +163,8 @@
                              path,
                              version,
                              purpose,
-                             "")));
+                             "",
+                             *this)));
     return;
 }
 
@@ -183,11 +185,24 @@
     }
 }
 
-void ItemUpdater::reset()
+void ItemUpdater::removeReadOnlyPartition(std::string versionId)
 {
-    for(const auto& it : activations)
-    {
-        auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + it.first +
+        auto serviceFile = "obmc-flash-bios-ubiumount-ro@" + versionId +
+                ".service";
+
+        // Remove the read-only partitions.
+        auto method = bus.new_method_call(
+                SYSTEMD_BUSNAME,
+                SYSTEMD_PATH,
+                SYSTEMD_INTERFACE,
+                "StartUnit");
+        method.append(serviceFile, "replace");
+        bus.call_noreply(method);
+}
+
+void ItemUpdater::removeReadWritePartition(std::string versionId)
+{
+        auto serviceFile = "obmc-flash-bios-ubiumount-rw@" + versionId +
                 ".service";
 
         // Remove the read-write partitions.
@@ -198,8 +213,10 @@
                 "StartUnit");
         method.append(serviceFile, "replace");
         bus.call_noreply(method);
-    }
+}
 
+void ItemUpdater::removePreservedPartition()
+{
     // Remove the preserved partition.
     auto method = bus.new_method_call(
             SYSTEMD_BUSNAME,
@@ -212,6 +229,16 @@
     return;
 }
 
+void ItemUpdater::reset()
+{
+    for(const auto& it : activations)
+    {
+        removeReadWritePartition(it.first);
+    }
+    removePreservedPartition();
+    return;
+}
+
 void ItemUpdater::freePriority(uint8_t value)
 {
     //TODO openbmc/openbmc#1896 Improve the performance of this function
@@ -242,6 +269,35 @@
     return true;
 }
 
+void ItemUpdater::erase(std::string entryId)
+{
+    // Removing partitions
+    removeReadWritePartition(entryId);
+    removeReadOnlyPartition(entryId);
+
+    // Removing entry in versions map
+    auto it = versions.find(entryId);
+    if (it == versions.end())
+    {
+        log<level::ERR>(("Error: Failed to find version " + entryId + \
+                        " in item updater versions map." \
+                        " Unable to remove.").c_str());
+        return;
+    }
+    versions.erase(entryId);
+
+    // Removing entry in activations map
+    auto ita = activations.find(entryId);
+    if (ita == activations.end())
+    {
+        log<level::ERR>(("Error: Failed to find version " + entryId + \
+                        " in item updater activations map." \
+                        " Unable to remove.").c_str());
+        return;
+    }
+    activations.erase(entryId);
+}
+
 } // namespace updater
 } // namespace software
 } // namespace openpower
diff --git a/item_updater.hpp b/item_updater.hpp
index 213b456..934ec3c 100755
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -65,6 +65,14 @@
          */
         void processPNORImage();
 
+        /** @brief Deletes version
+         *
+         *  @param[in] entryId - Id of the version to delete
+         *
+         *  @return None
+         */
+        void erase(std::string entryId);
+
     private:
         /** @brief Callback function for Software.Version match.
          *  @details Creates an Activation dbus object.
@@ -96,6 +104,23 @@
         /** @brief sdbusplus signal match for Software.Version */
         sdbusplus::bus::match_t versionMatch;
 
+        /** @brief Clears read only PNOR partition for
+         *  given Activation dbus object
+         *
+         * @param[in]  versionId - The id of the ro partition to remove.
+         */
+        void removeReadOnlyPartition(std::string versionId);
+
+        /** @brief Clears read write PNOR partition for
+         *  given Activation dbus object
+         *
+         * @param[in]  versionId - The id of the rw partition to remove.
+         */
+        void removeReadWritePartition(std::string versionId);
+
+        /** @brief Clears preserved PNOR partition */
+        void removePreservedPartition();
+
         /** @brief Host factory reset - clears PNOR partitions for each
           * Activation dbus object */
         void reset() override;
diff --git a/version.cpp b/version.cpp
index 6cdc723..4defa77 100644
--- a/version.cpp
+++ b/version.cpp
@@ -7,6 +7,7 @@
 #include "version.hpp"
 #include <phosphor-logging/elog-errors.hpp>
 #include "xyz/openbmc_project/Common/error.hpp"
+#include "item_updater.hpp"
 
 namespace openpower
 {
@@ -82,6 +83,11 @@
     return keys;
 }
 
+void Version::delete_()
+{
+    parent.erase(getId(version()));
+}
+
 } // namespace updater
 } // namespace software
 } // namespace openpower
diff --git a/version.hpp b/version.hpp
index 23bf604..507d4d0 100644
--- a/version.hpp
+++ b/version.hpp
@@ -2,6 +2,7 @@
 
 #include <sdbusplus/bus.hpp>
 #include "xyz/openbmc_project/Software/Version/server.hpp"
+#include "xyz/openbmc_project/Object/Delete/server.hpp"
 #include "xyz/openbmc_project/Common/FilePath/server.hpp"
 
 namespace openpower
@@ -11,8 +12,11 @@
 namespace updater
 {
 
+class ItemUpdater;
+
 using VersionInherit = sdbusplus::server::object::object<
     sdbusplus::xyz::openbmc_project::Software::server::Version,
+    sdbusplus::xyz::openbmc_project::Object::server::Delete,
     sdbusplus::xyz::openbmc_project::Common::server::FilePath>;
 
 /** @class Version
@@ -30,13 +34,16 @@
          * @param[in] versionId      - The version identifier
          * @param[in] versionPurpose - The version purpose
          * @param[in] filePath       - The image filesystem path
+         * @param[in] parent         - The version's parent
          */
         Version(sdbusplus::bus::bus& bus,
                 const std::string& objPath,
                 const std::string& versionId,
                 VersionPurpose versionPurpose,
-                const std::string& filePath) : VersionInherit(
-                    bus, (objPath).c_str(), true)
+                const std::string& filePath,
+                ItemUpdater& parent) : VersionInherit(
+                    bus, (objPath).c_str(), true),
+                parent(parent)
         {
             // Set properties.
             purpose(versionPurpose);
@@ -68,6 +75,15 @@
          * @return The id.
          */
         static std::string getId(const std::string& version);
+
+        /** @brief Deletes the d-bus object and removes image.
+         *
+         */
+        void delete_() override;
+
+    private:
+        ItemUpdater& parent;
+
 };
 
 } // namespace updater