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/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