item_updater: Added erase method

Implements Object.Delete, so item_updater deletes
ReadWrite, ReadOnly partitions, and removes
Version, and Activation from maps.

Resolves openbmc/openbmc#1550

Change-Id: If6d08bc7d380d043c28a26570e4698201cd7f5b0
Signed-off-by: Leonel Gonzalez <lgonzalez@us.ibm.com>
diff --git a/image_manager.cpp b/image_manager.cpp
index bc9ce9e..5242d8b 100644
--- a/image_manager.cpp
+++ b/image_manager.cpp
@@ -172,7 +172,10 @@
                                   objPath,
                                   version,
                                   purpose,
-                                  imageDirPath.string())));
+                                  imageDirPath.string(),
+                                  std::bind(&Manager::erase,
+                                            this,
+                                            std::placeholders::_1))));
 
     return 0;
 }
diff --git a/item_updater.cpp b/item_updater.cpp
index fe9af2f..04a9eb4 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -121,7 +121,10 @@
                                 path,
                                 version,
                                 purpose,
-                                filePath)));
+                                filePath,
+                                std::bind(&ItemUpdater::erase,
+                                          this,
+                                          std::placeholders::_1))));
     }
     return;
 }
@@ -148,10 +151,47 @@
                              path,
                              version,
                              purpose,
-                             "")));
+                             "",
+                             std::bind(&ItemUpdater::erase,
+                                       this,
+                                       std::placeholders::_1))));
+
     return;
 }
 
+void ItemUpdater::erase(std::string entryId)
+{
+    // Delete ReadWrite and ReadOnly 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;
+    }
+    this->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;
+    }
+    // TODO: openbmc/openbmc#1986
+    //       Test if this is the currently running image
+    //       If not, don't continue.
+
+    this->activations.erase(entryId);
+}
+
 ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
              const std::string& filePath)
 {
@@ -202,6 +242,35 @@
     return;
 }
 
+void ItemUpdater::removeReadOnlyPartition(std::string versionId)
+{
+    auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + 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-bmc-ubirw-remove.service";
+
+    // Remove the read-write partitions.
+    auto method = bus.new_method_call(
+            SYSTEMD_BUSNAME,
+            SYSTEMD_PATH,
+            SYSTEMD_INTERFACE,
+            "StartUnit");
+    method.append(serviceFile, "replace");
+    bus.call_noreply(method);
+}
+
 } // namespace updater
 } // namespace software
 } // namespace phosphor
diff --git a/item_updater.hpp b/item_updater.hpp
index e4fdd69..5b2a71b 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -66,6 +66,14 @@
      */
     void processBMCImage();
 
+    /**
+     * @brief Erase specified entry d-bus object
+     *        if Action property is not set to Active
+     *
+     * @param[in] entryId - unique identifier of the entry
+     */
+    void erase(std::string entryId);
+
     private:
         /** @brief Callback function for Software.Version match.
          *  @details Creates an Activation dbus object.
@@ -105,6 +113,20 @@
         /** @brief sdbusplus signal match for Software.Version */
         sdbusplus::bus::match_t versionMatch;
 
+        /** @brief Clears read only partition for
+          * given Activation dbus object.
+          *
+          * @param[in]  versionId - The version id.
+          */
+        void removeReadOnlyPartition(std::string versionId);
+
+        /** @brief Clears read write partition for
+          * given Activation dbus object.
+          *
+          * @param[in]  versionId - The version id.
+          */
+        void removeReadWritePartition(std::string versionId);
+
 };
 
 
diff --git a/version.hpp b/version.hpp
index fcd57eb..ef6747f 100755
--- a/version.hpp
+++ b/version.hpp
@@ -35,14 +35,18 @@
          * @param[in] versionId      - The version identifier
          * @param[in] versionPurpose - The version purpose
          * @param[in] filePath       - The image filesystem path
+         * @param[in] callback       - The parent's erase callback
          */
         Version(sdbusplus::bus::bus& bus,
                 const std::string& objPath,
                 const std::string& versionId,
                 VersionPurpose versionPurpose,
-                const std::string& filePath) : VersionInherit(
+                const std::string& filePath,
+                eraseFunc callback) : VersionInherit(
                     bus, (objPath).c_str(), true)
         {
+            // Bind erase method
+            eraseCallback = callback;
             // Set properties.
             purpose(versionPurpose);
             version(versionId);