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/item_updater.cpp b/item_updater.cpp
index 60ea7ce..03ff20b 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -131,19 +131,20 @@
                         activationState,
                         associations)));
 
-        activations.find(versionId)->second->deleteObject =
-                std::make_unique<Delete>(bus,
-                                         path,
-                                         *activations.find(versionId)->second);
-
-        versions.insert(std::make_pair(
-                            versionId,
-                            std::make_unique<Version>(
-                                bus,
-                                path,
-                                version,
-                                purpose,
-                                filePath)));
+        auto versionPtr = std::make_unique<Version>(
+                                  bus,
+                                  path,
+                                  *this,
+                                  versionId,
+                                  version,
+                                  purpose,
+                                  filePath,
+                                  std::bind(&ItemUpdater::erase,
+                                            this,
+                                            std::placeholders::_1));
+        versionPtr->deleteObject =
+                std::make_unique<Delete>(bus, path, *versionPtr);
+        versions.insert(std::make_pair(versionId, std::move(versionPtr)));
     }
     return;
 }
@@ -222,11 +223,6 @@
                                        activationState,
                                        associations)));
 
-            activations.find(id)->second->deleteObject =
-                    std::make_unique<Delete>(bus,
-                                             path,
-                                             *activations.find(id)->second);
-
             // If Active, create RedundancyPriority instance for this version.
             if (activationState == server::Activation::Activations::Active)
             {
@@ -245,14 +241,22 @@
             }
 
             // Create Version instance for this version.
+            auto versionPtr = std::make_unique<Version>(
+                                      bus,
+                                      path,
+                                      *this,
+                                      id,
+                                      version,
+                                      purpose,
+                                      "",
+                                      std::bind(&ItemUpdater::erase,
+                                                this,
+                                                std::placeholders::_1));
+            versionPtr->deleteObject =
+                        std::make_unique<Delete>(bus, path, *versionPtr);
             versions.insert(std::make_pair(
-                                id,
-                                std::make_unique<Version>(
-                                     bus,
-                                     path,
-                                     version,
-                                     purpose,
-                                     "")));
+                                    id,
+                                    std::move(versionPtr)));
         }
         else if (0 == iter.path().native().compare(0, PNOR_RW_PREFIX_LEN,
                                                       PNOR_RW_PREFIX))
@@ -488,9 +492,11 @@
         log<level::ERR>(("Error: Failed to find version " + entryId + \
                          " in item updater versions map." \
                          " Unable to remove.").c_str());
-        return;
     }
-    versions.erase(entryId);
+    else
+    {
+        versions.erase(entryId);
+    }
 
     // Removing entry in activations map
     auto ita = activations.find(entryId);
@@ -499,28 +505,24 @@
         log<level::ERR>(("Error: Failed to find version " + entryId + \
                          " in item updater activations map." \
                          " Unable to remove.").c_str());
-        return;
     }
-    activations.erase(entryId);
+    else
+    {
+        activations.erase(entryId);
+    }
+    return;
 }
 
 void ItemUpdater::deleteAll()
 {
-    std::vector<std::string> deletableActivations;
-
     for (const auto& activationIt : activations)
     {
         if (!isVersionFunctional(activationIt.first))
         {
-            deletableActivations.push_back(activationIt.first);
+            ItemUpdater::erase(activationIt.first);
         }
     }
 
-    for (const auto& deletableIt : deletableActivations)
-    {
-        ItemUpdater::erase(deletableIt);
-    }
-
     // Remove any remaining pnor-ro- or pnor-rw- volumes that do not match
     // the current version.
     auto method = bus.new_method_call(