Activation: remove old objects after update done

The update on PSUs is one-by-one, after each PSU is updated, notify
ItemUpdater by onUpdateDone() so that the old object for the PSU is
removed.

Tested: With dummy PSU image and update service, verify the old
        objects are removed on Witherspoon.

Change-Id: I212b8cba9570ad96083d362bf57691fdabb4e42f
diff --git a/src/activation.cpp b/src/activation.cpp
index cab9acd..ca8c7df 100644
--- a/src/activation.cpp
+++ b/src/activation.cpp
@@ -127,9 +127,11 @@
     auto assocs = associations();
     assocs.emplace_back(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
                         currentUpdatingPsu);
-    currentUpdatingPsu.clear();
     associations(assocs);
 
+    activationListener->onUpdateDone(versionId, currentUpdatingPsu);
+    currentUpdatingPsu.clear();
+
     psuQueue.pop();
     doUpdate(); // Update the next psu
 }
@@ -218,7 +220,6 @@
     storeImage();
     activationProgress->progress(100);
 
-    // TODO: delete the old software object
     deleteImageManagerObject();
 
     associationInterface->createActiveAssociation(objPath);
diff --git a/src/activation.hpp b/src/activation.hpp
index 669686e..3b03803 100644
--- a/src/activation.hpp
+++ b/src/activation.hpp
@@ -2,6 +2,7 @@
 
 #include "config.h"
 
+#include "activation_listener.hpp"
 #include "association_interface.hpp"
 #include "types.hpp"
 #include "version.hpp"
@@ -129,8 +130,9 @@
     Activation(sdbusplus::bus::bus& bus, const std::string& objPath,
                const std::string& versionId, const std::string& extVersion,
                Status activationStatus, const AssociationList& assocs,
+               const std::string& filePath,
                AssociationInterface* associationInterface,
-               const std::string& filePath) :
+               ActivationListener* activationListener) :
         ActivationInherit(bus, objPath.c_str(), true),
         bus(bus), objPath(objPath), versionId(versionId),
         systemdSignals(
@@ -140,7 +142,8 @@
                 sdbusRule::interface("org.freedesktop.systemd1.Manager"),
             std::bind(&Activation::unitStateChange, this,
                       std::placeholders::_1)),
-        associationInterface(associationInterface)
+        associationInterface(associationInterface),
+        activationListener(activationListener)
     {
         // Set Properties.
         extendedVersion(extVersion);
@@ -278,6 +281,9 @@
     /** @brief The AssociationInterface pointer */
     AssociationInterface* associationInterface;
 
+    /** @brief The activationListener pointer */
+    ActivationListener* activationListener;
+
     /** @brief The PSU manufacturer of the software */
     std::string manufacturer;
 
diff --git a/src/activation_listener.hpp b/src/activation_listener.hpp
new file mode 100644
index 0000000..5437232
--- /dev/null
+++ b/src/activation_listener.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+#include <string>
+
+class ActivationListener
+{
+  public:
+    virtual ~ActivationListener() = default;
+
+    /** @brief Notify a PSU is updated
+     *
+     * @param[in]  versionId - The versionId of the activation
+     * @param[in]  psuInventoryPath - The PSU inventory path that is updated
+     */
+    virtual void onUpdateDone(const std::string& versionId,
+                              const std::string& psuInventoryPath) = 0;
+};
diff --git a/src/item_updater.cpp b/src/item_updater.cpp
index acbc943..6d4e4c2 100644
--- a/src/item_updater.cpp
+++ b/src/item_updater.cpp
@@ -179,14 +179,29 @@
     }
 }
 
+void ItemUpdater::onUpdateDone(const std::string& versionId,
+                               const std::string& psuInventoryPath)
+{
+    // After update is done, remove old activation objects
+    for (auto it = activations.begin(); it != activations.end(); ++it)
+    {
+        if (it->second->getVersionId() != versionId &&
+            utils::isAssociated(psuInventoryPath, it->second->associations()))
+        {
+            removePsuObject(psuInventoryPath);
+            break;
+        }
+    }
+}
+
 std::unique_ptr<Activation> ItemUpdater::createActivationObject(
     const std::string& path, const std::string& versionId,
     const std::string& extVersion, Activation::Status activationStatus,
     const AssociationList& assocs, const std::string& filePath)
 {
     return std::make_unique<Activation>(bus, path, versionId, extVersion,
-                                        activationStatus, assocs, this,
-                                        filePath);
+                                        activationStatus, assocs, filePath,
+                                        this, this);
 }
 
 void ItemUpdater::createPsuObject(const std::string& psuInventoryPath,
diff --git a/src/item_updater.hpp b/src/item_updater.hpp
index b713454..c3da8f9 100644
--- a/src/item_updater.hpp
+++ b/src/item_updater.hpp
@@ -35,7 +35,9 @@
 /** @class ItemUpdater
  *  @brief Manages the activation of the PSU version items.
  */
-class ItemUpdater : public ItemUpdaterInherit, public AssociationInterface
+class ItemUpdater : public ItemUpdaterInherit,
+                    public AssociationInterface,
+                    public ActivationListener
 {
     friend class ::TestItemUpdater;
 
@@ -84,6 +86,14 @@
      */
     void removeAssociation(const std::string& path) override;
 
+    /** @brief Notify a PSU is updated
+     *
+     * @param[in]  versionId - The versionId of the activation
+     * @param[in]  psuInventoryPath - The PSU inventory path that is updated
+     */
+    void onUpdateDone(const std::string& versionId,
+                      const std::string& psuInventoryPath) override;
+
   private:
     /** @brief Callback function for Software.Version match.
      *  @details Creates an Activation D-Bus object.