memory: Precisely match DIMM ID

Currently, getDimmData uses std::string::find to determine if a DBus
path contains a dimmId. The result is if we have the following paths:

/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm1
/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm10
/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm11
...
/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm19

They will all be considered to match dimmId "dimm1" with string::find.

This change switches to sdbusplus::message::object_path::filename(), so
partial matches may be prevented.

This change also considers partition data and works for partitions.

Tested:
1. No new Redfish Validator errors
2. In my mock environment
URL: /redfish/v1/Systems/system/Memory/dimm0
{
    "@odata.id": "/redfish/v1/Systems/system/Memory/dimm0",
    "@odata.type": "#Memory.v1_11_0.Memory",
    "AllowedSpeedsMHz": [],
    "BaseModuleType": "RDIMM",
    "BusWidthBits": 0,
    "CapacityMiB": 1024,
    "DataWidthBits": 0,
    "ErrorCorrection": "NoECC",
    "FirmwareRevision": "0",
    "Id": "dimm0",
    "Name": "DIMM Slot",
    "OperatingSpeedMhz": 0,
    "RankCount": 0,
    "Regions": [
        {
            "MemoryClassification": "Volatile",
            "OffsetMiB": 0,
            "PassphraseEnabled": false,
            "RegionId": "",
            "SizeMiB": 1024
        }
    ],
    "Status": {
        "Health": "OK",
        "HealthRollup": "OK",
        "State": "Enabled"
    }
}


Signed-off-by: Sui Chen <suichen@google.com>
Change-Id: I122774be1f81da6e5c3a833b83d2bd81b437c298
diff --git a/redfish-core/lib/memory.hpp b/redfish-core/lib/memory.hpp
index 27f5a5b..2dc6afa 100644
--- a/redfish-core/lib/memory.hpp
+++ b/redfish-core/lib/memory.hpp
@@ -18,6 +18,7 @@
 #include "health.hpp"
 
 #include <app.hpp>
+#include <boost/algorithm/string.hpp>
 #include <dbus_utility.hpp>
 #include <query.hpp>
 #include <registries/privilege_registry.hpp>
@@ -795,6 +796,15 @@
         "xyz.openbmc_project.Inventory.Item.PersistentMemory.Partition");
 }
 
+inline bool pathContainsDimmId(const std::string& path, std::string_view dimmId)
+{
+    sdbusplus::message::object_path objectPath(path);
+    // for /xyz/openbmc_project/Inventory/Item/Dimm1/Partition1 or
+    // /xyz/openbmc_project/Inventory/Item/Dimm1
+    return !dimmId.empty() && (objectPath.filename() == dimmId ||
+                               objectPath.parent_path().filename() == dimmId);
+}
+
 inline void getDimmData(std::shared_ptr<bmcweb::AsyncResp> aResp,
                         const std::string& dimmId)
 {
@@ -813,7 +823,7 @@
         bool found = false;
         for (const auto& [path, object] : subtree)
         {
-            if (path.find(dimmId) != std::string::npos)
+            if (pathContainsDimmId(path, dimmId))
             {
                 for (const auto& [service, interfaces] : object)
                 {