BMC Updater: Add function to delete all versions

This commit extends the item updater with a function to delete all
non-current versions uploaded to the machine.

Resolves openbmc/openbmc#2264

Change-Id: I4d835d5020766f8bb5e9653b28535acfb191fe53
Signed-off-by: Michael Tritz <mtritz@us.ibm.com>
diff --git a/item_updater.cpp b/item_updater.cpp
index a19aebc..9f79963 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -297,6 +297,33 @@
     this->activations.erase(entryId);
 }
 
+void ItemUpdater::deleteAll()
+{
+    std::vector<std::string> deletableVersions;
+
+    for (const auto& versionIt : versions)
+    {
+        if (!versionIt.second->isFunctional())
+        {
+            deletableVersions.push_back(versionIt.first);
+        }
+    }
+
+    for (const auto& deletableIt : deletableVersions)
+    {
+        ItemUpdater::erase(deletableIt);
+    }
+
+    // Remove any volumes that do not match current versions.
+    auto method = bus.new_method_call(
+            SYSTEMD_BUSNAME,
+            SYSTEMD_PATH,
+            SYSTEMD_INTERFACE,
+            "StartUnit");
+    method.append("obmc-flash-bmc-cleanup.service", "replace");
+    bus.call_noreply(method);
+}
+
 ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
         const std::string& filePath)
 {
diff --git a/item_updater.hpp b/item_updater.hpp
index d55e807..580aecf 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -6,6 +6,7 @@
 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
 #include <xyz/openbmc_project/Control/FieldMode/server.hpp>
 #include "org/openbmc/Associations/server.hpp"
+#include "xyz/openbmc_project/Collection/DeleteAll/server.hpp"
 
 namespace phosphor
 {
@@ -17,7 +18,8 @@
 using ItemUpdaterInherit = sdbusplus::server::object::object<
     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset,
     sdbusplus::xyz::openbmc_project::Control::server::FieldMode,
-    sdbusplus::org::openbmc::server::Associations>;
+    sdbusplus::org::openbmc::server::Associations,
+    sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
 
 namespace MatchRules = sdbusplus::bus::match::rules;
 
@@ -85,6 +87,10 @@
      */
     void erase(std::string entryId);
 
+    /**
+     * @brief Deletes all versions except for the current one
+     */
+    void deleteAll();
 
     /** @brief Creates an active association to the
      *  newly active software image