oem-ibm: Adding entity path correction function

This commit introduces enhancements to rectify entity paths over DBus.
Formerly, the conventional DBus path involved appending an instance
number, such as 'system' becoming 'system1', 'motherboard' becoming
'motherboard1', and 'chassis' becoming 'chassis1'. This function is
designed to align with IBM's specific use case, ensuring the numbering
system adheres appropriately.

Testing:
Unit tests passed

Change-Id: I671f8486078054b44110ffa2cbf169c63d164cf1
Signed-off-by: Kamalkumar Patel <kamalkumar.patel@ibm.com>
diff --git a/oem/ibm/libpldmresponder/oem_ibm_handler.cpp b/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
index a6ac666..4f3969c 100644
--- a/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
+++ b/oem/ibm/libpldmresponder/oem_ibm_handler.cpp
@@ -419,6 +419,36 @@
                          uint8_t(CodeUpdateState::END));
 }
 
+void pldm::responder::oem_ibm_platform::Handler::updateOemDbusPaths(
+    std::string& dbusPath)
+{
+    std::string toFind("system1/chassis1/motherboard1");
+    if (dbusPath.find(toFind) != std::string::npos)
+    {
+        size_t pos = dbusPath.find(toFind);
+        dbusPath.replace(pos, toFind.length(), "system/chassis/motherboard");
+    }
+    toFind = "system1";
+    if (dbusPath.find(toFind) != std::string::npos)
+    {
+        size_t pos = dbusPath.find(toFind);
+        dbusPath.replace(pos, toFind.length(), "system");
+    }
+    /* below logic to replace path 'motherboard/socket/chassis' to
+       'motherboard/chassis' or 'motherboard/socket123/chassis' to
+       'motherboard/chassis' */
+    toFind = "socket";
+    size_t pos1 = dbusPath.find(toFind);
+    // while loop to detect multiple substring 'socket' in the path
+    while (pos1 != std::string::npos)
+    {
+        size_t pos2 = dbusPath.substr(pos1 + 1).find('/') + 1;
+        // Replacing starting from substring to next occurence of char '/'
+        dbusPath.replace(pos1, pos2 + 1, "");
+        pos1 = dbusPath.find(toFind);
+    }
+}
+
 void pldm::responder::oem_ibm_platform::Handler::_processSystemReboot(
     sdeventplus::source::EventBase& /*source */)
 {
diff --git a/oem/ibm/libpldmresponder/oem_ibm_handler.hpp b/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
index 01ec5bd..9ad016f 100644
--- a/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
+++ b/oem/ibm/libpldmresponder/oem_ibm_handler.hpp
@@ -186,6 +186,9 @@
     /** @brief to check the BMC state*/
     int checkBMCState();
 
+    /** @brief update the dbus object paths */
+    void updateOemDbusPaths(std::string& dbusPath);
+
     /** @brief Method to fetch the last BMC record from the PDR repo
      *
      * @param[in] repo - pointer to BMC's primary PDR repo
diff --git a/oem/ibm/test/libpldmresponder_oem_platform_test.cpp b/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
index db5283d..ea9b97a 100644
--- a/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
+++ b/oem/ibm/test/libpldmresponder_oem_platform_test.cpp
@@ -389,3 +389,23 @@
 
     pldm_pdr_destroy(inPDRRepo);
 }
+
+TEST(updateOemDbusPath, testgoodpath)
+{
+    TestInstanceIdDb instanceIdDb;
+    auto mockDbusHandler = std::make_unique<MockdBusHandler>();
+    auto event = sdeventplus::Event::get_default();
+    std::unique_ptr<CodeUpdate> mockCodeUpdate =
+        std::make_unique<MockCodeUpdate>(mockDbusHandler.get());
+    std::unique_ptr<oem_ibm_platform::Handler> mockoemPlatformHandler =
+        std::make_unique<MockOemPlatformHandler>(mockDbusHandler.get(),
+                                                 mockCodeUpdate.get(), 0x1, 0x9,
+                                                 instanceIdDb, event);
+    std::string dbuspath = "/inventory/system1/chassis1/motherboard1/dcm0";
+    mockoemPlatformHandler->updateOemDbusPaths(dbuspath);
+    EXPECT_EQ(dbuspath, "/inventory/system/chassis/motherboard/dcm0");
+
+    dbuspath = "/inventory/system/chassis/socket1/motherboard/dcm0";
+    mockoemPlatformHandler->updateOemDbusPaths(dbuspath);
+    EXPECT_EQ(dbuspath, "/inventory/system/chassis/motherboard/dcm0");
+}