API to read IM value from persisted location

This commit implements API to read IM value from PIM persisted
location.

Change-Id: I9cd4f6ed9ded2dd4f42c65902509f7bea9e15849
Signed-off-by: Anupama B R <anupama.b.r1@ibm.com>
diff --git a/vpd-manager/include/constants.hpp b/vpd-manager/include/constants.hpp
index f71b18e..cddc8ab 100644
--- a/vpd-manager/include/constants.hpp
+++ b/vpd-manager/include/constants.hpp
@@ -121,6 +121,8 @@
 constexpr auto systemInvPath = "/xyz/openbmc_project/inventory/system";
 constexpr auto pimPath = "/xyz/openbmc_project/inventory";
 constexpr auto pimIntf = "xyz.openbmc_project.Inventory.Manager";
+constexpr auto pimPersistVsbpPath =
+    "/var/lib/phosphor-inventory-manager/xyz/openbmc_project/inventory/system/chassis/motherboard/com.ibm.ipzvpd.VSBP";
 constexpr auto ipzVpdInf = "com.ibm.ipzvpd.";
 constexpr auto kwdVpdInf = "com.ibm.ipzvpd.VINI";
 constexpr auto vsysInf = "com.ibm.ipzvpd.VSYS";
diff --git a/vpd-manager/include/utility/json_utility.hpp b/vpd-manager/include/utility/json_utility.hpp
index 8a4ae44..55010ad 100644
--- a/vpd-manager/include/utility/json_utility.hpp
+++ b/vpd-manager/include/utility/json_utility.hpp
@@ -1088,5 +1088,46 @@
         return nlohmann::json{};
     }
 }
+
+/** @brief API to get IM value from persisted data location.
+ *
+ * @return IM value on success, empty string otherwise.
+ */
+inline std::string getImFromPersistedLocation() noexcept
+{
+    std::string l_imFilePath{constants::pimPersistVsbpPath};
+
+    try
+    {
+        auto l_parsedVsbpJsonObj = getParsedJson(l_imFilePath);
+        if (!l_parsedVsbpJsonObj.contains("value0") ||
+            !l_parsedVsbpJsonObj["value0"].contains(constants::kwdIM) ||
+            !l_parsedVsbpJsonObj["value0"][constants::kwdIM].is_array())
+        {
+            throw std::runtime_error(
+                "Json is empty or mandatory tag(s) missing from JSON");
+        }
+
+        types::BinaryVector l_imValue =
+            l_parsedVsbpJsonObj["value0"][constants::kwdIM]
+                .get<types::BinaryVector>();
+
+        std::ostringstream l_imData;
+        for (auto& l_byte : l_imValue)
+        {
+            l_imData << std::setw(2) << std::setfill('0') << std::hex
+                     << static_cast<int>(l_byte);
+        }
+        return l_imData.str();
+    }
+    catch (const std::exception& l_ex)
+    {
+        logging::logMessage(
+            "Error while getting IM value from PIM persisted file: " +
+            l_imFilePath + ", reason: " + std::string(l_ex.what()));
+    }
+
+    return std::string();
+}
 } // namespace jsonUtility
 } // namespace vpd