Clear vpd before recollection

Some FRUs qualifies for recollection at standby.
To avoid any misleading representation because of the stale data,
VPD is being cleared and present is set to false before
recollection is triggered for those FRUs.
The commit also checks for any sub tree under the parent path and
if found resets data for them as well.

This will also help in detecting any error that can happen during
recollection of those FRUs as their present property will remain
set to false.

Change-Id: I9b706df39648ded63c44363fc817104028e60014
Signed-off-by: Sunny Srivastava <sunnsr25@in.ibm.com>
diff --git a/vpd-manager/manager.cpp b/vpd-manager/manager.cpp
index 62220db..156bbcc 100644
--- a/vpd-manager/manager.cpp
+++ b/vpd-manager/manager.cpp
@@ -560,6 +560,41 @@
             singleFru["inventoryPath"]
                 .get_ref<const nlohmann::json::string_t&>();
 
+        // clear VPD of FRU before re-collection.
+        inventory::InterfaceMap interfacesPropMap;
+        clearVpdOnRemoval(INVENTORY_PATH + inventoryPath, interfacesPropMap);
+
+        inventory::ObjectMap objectMap;
+        objectMap.emplace(inventoryPath, move(interfacesPropMap));
+
+        common::utility::callPIM(move(objectMap));
+
+        // check if any subtree exist under the parent path. If so clear VPD for
+        // them as well.
+        std::vector<std::string> interfaceList{
+            "xyz.openbmc_project.Inventory.Item"};
+        MapperResponse subTree = getObjectSubtreeForInterfaces(
+            INVENTORY_PATH + inventoryPath, 0, interfaceList);
+
+        for (auto [objectPath, serviceInterfaceMap] : subTree)
+        {
+            interfacesPropMap.clear();
+            clearVpdOnRemoval(objectPath, interfacesPropMap);
+
+            std::string subTreeObjPath{objectPath};
+            // Strip any inventory prefix in path
+            if (subTreeObjPath.find(INVENTORY_PATH) == 0)
+            {
+                subTreeObjPath =
+                    subTreeObjPath.substr(sizeof(INVENTORY_PATH) - 1);
+            }
+
+            objectMap.clear();
+            objectMap.emplace(subTreeObjPath, move(interfacesPropMap));
+
+            common::utility::callPIM(move(objectMap));
+        }
+
         bool prePostActionRequired = false;
 
         if ((jsonFile["frus"][item].at(0)).find("preAction") !=