PNOR: Fix the delete implementation

- In order to remove the delete object from functional
  image, the delete interface is moved inside the
  version class so that both item_updater and image_manager
  can make use of the same implementation.
- To avoid having two delete objects attached to the same
  HOST version (item_updater and image_manager), we are now
  deleting the image_manager object once the activation
  is complete.

Partially resolves openbmc/openbmc#2490

Change-Id: Ie515cc01d5f154e6e55b9a3fb71d831730cd46f6
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/activation.cpp b/activation.cpp
index cd973b5..40acc89 100755
--- a/activation.cpp
+++ b/activation.cpp
@@ -3,6 +3,7 @@
 #include "config.h"
 #include "item_updater.hpp"
 #include "serialize.hpp"
+#include <phosphor-logging/log.hpp>
 
 namespace openpower
 {
@@ -14,6 +15,8 @@
 namespace fs = std::experimental::filesystem;
 namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
 
+using namespace phosphor::logging;
+
 constexpr auto SYSTEMD_SERVICE   = "org.freedesktop.systemd1";
 constexpr auto SYSTEMD_OBJ_PATH  = "/org/freedesktop/systemd1";
 
@@ -89,6 +92,8 @@
 
     ubiVolumesCreated = false;
     Activation::unsubscribeFromSystemdSignals();
+    // Remove version object from image manager
+    Activation::deleteImageManagerObject();
     // Create active association
     parent.createActiveAssociation(path);
 }
@@ -167,6 +172,49 @@
     return softwareServer::Activation::requestedActivation(value);
 }
 
+void Activation::deleteImageManagerObject()
+{
+    // Get the Delete object for <versionID> inside image_manager
+    auto method = this->bus.new_method_call(MAPPER_BUSNAME,
+                                            MAPPER_PATH,
+                                            MAPPER_INTERFACE,
+                                            "GetObject");
+
+    method.append(path);
+    method.append(std::vector<std::string>({
+            "xyz.openbmc_project.Object.Delete"}));
+    auto mapperResponseMsg = bus.call(method);
+    if (mapperResponseMsg.is_method_error())
+    {
+        log<level::ERR>("Error in Get Delete Object",
+                        entry("VERSIONPATH=%s", path));
+        return;
+    }
+    std::map<std::string, std::vector<std::string>> mapperResponse;
+    mapperResponseMsg.read(mapperResponse);
+    if (mapperResponse.begin() == mapperResponse.end())
+    {
+        log<level::ERR>("ERROR in reading the mapper response",
+                        entry("VERSIONPATH=%s", path));
+        return;
+    }
+
+    // Call the Delete object for <versionID> inside image_manager
+    method = this->bus.new_method_call((mapperResponse.begin()->first).c_str(),
+                                       path.c_str(),
+                                       "xyz.openbmc_project.Object.Delete",
+                                       "Delete");
+    mapperResponseMsg = bus.call(method);
+
+    //Check that the bus call didn't result in an error
+    if (mapperResponseMsg.is_method_error())
+    {
+        log<level::ERR>("Error in Deleting image from image manager",
+                        entry("VERSIONPATH=%s", path));
+        return;
+    }
+}
+
 uint8_t RedundancyPriority::priority(uint8_t value)
 {
     parent.parent.freePriority(value, parent.versionId);
@@ -208,46 +256,6 @@
     return;
 }
 
-void Activation::updateDeleteInterface(sdbusplus::message::message& msg)
-{
-    std::string interface, chassisState;
-    std::map<std::string, sdbusplus::message::variant<std::string>> properties;
-
-    msg.read(interface, properties);
-
-    for (const auto& p : properties)
-    {
-        if (p.first == "CurrentPowerState")
-        {
-            chassisState = p.second.get<std::string>();
-        }
-    }
-
-    if ((parent.isVersionFunctional(this->versionId)) &&
-        (chassisState != CHASSIS_STATE_OFF))
-    {
-        if (deleteObject)
-        {
-            deleteObject.reset(nullptr);
-        }
-    }
-    else
-    {
-        if (!deleteObject)
-        {
-            deleteObject = std::make_unique<Delete>(bus, path, *this);
-        }
-    }
-}
-
-void Delete::delete_()
-{
-    // Remove active association
-    parent.parent.removeActiveAssociation(parent.path);
-
-    parent.parent.erase(parent.versionId);
-}
-
 } // namespace updater
 } // namespace software
 } // namespace openpower