Refactor powerSupplyPath association retrieval

Improved functionality for retrieving powered_by association endpoints
with the "xyz.openbmc_project.Inventory.Item.PowerSupply" interface.

This commit introduces an enhanced approach for obtaining the powered_by
association endpoints that specifically have the
"xyz.openbmc_project.Inventory.Item.PowerSupply" interface. The code now
utilizes the getAssociatedSubTreePaths() function, ensuring that only
endpoints representing power supplies are retrieved.

This updated functionality provides a more accurate and efficient method
for retrieving the relevant associations, filtering out any endpoints
that do not correspond to power supplies. This approach is particularly
beneficial in cases where the powered_by association may be used by
entities other than power supplies.

Refactor powerSupply list handling in powerSupply subsystem.

Tested: Validator Passed

'''
curl -k https://${bmc}/redfish/v1/Chassis/chassis/PowerSubsystem/PowerSupplies
{
  "@odata.id": "/redfish/v1/Chassis/chassis/PowerSubsystem/PowerSupplies",
  "@odata.type": "#PowerSupplyCollection.PowerSupplyCollection",
  "Description": "The collection of PowerSupply resource instances.",
  "Members": [
    {
      "@odata.id": "/redfish/v1/Chassis/chassis/PowerSubsystem/PowerSupplies/powersupply2"
    },
    {
      "@odata.id": "/redfish/v1/Chassis/chassis/PowerSubsystem/PowerSupplies/powersupply3"
    }
  ],
  "Members@odata.count": 2,
  "Name": "Power Supply Collection"
}
'''

Change-Id: Icb56621b578460e65380a633028269a7023c4674
Signed-off-by: Lakshmi Yadlapati <lakshmiy@us.ibm.com>
diff --git a/redfish-core/lib/power_supply.hpp b/redfish-core/lib/power_supply.hpp
index 190352a..4a766d3 100644
--- a/redfish-core/lib/power_supply.hpp
+++ b/redfish-core/lib/power_supply.hpp
@@ -18,25 +18,31 @@
 namespace redfish
 {
 
-inline void
-    updatePowerSupplyList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
-                          const std::string& chassisId,
-                          const std::string& powerSupplyPath)
+static constexpr std::array<std::string_view, 1> powerSupplyInterface = {
+    "xyz.openbmc_project.Inventory.Item.PowerSupply"};
+
+inline void updatePowerSupplyList(
+    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+    const std::string& chassisId,
+    const dbus::utility::MapperGetSubTreePathsResponse& powerSupplyPaths)
 {
-    std::string powerSupplyName =
-        sdbusplus::message::object_path(powerSupplyPath).filename();
-    if (powerSupplyName.empty())
-    {
-        return;
-    }
-
-    nlohmann::json item = nlohmann::json::object();
-    item["@odata.id"] = boost::urls::format(
-        "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies/{}", chassisId,
-        powerSupplyName);
-
     nlohmann::json& powerSupplyList = asyncResp->res.jsonValue["Members"];
-    powerSupplyList.emplace_back(std::move(item));
+    for (const std::string& powerSupplyPath : powerSupplyPaths)
+    {
+        std::string powerSupplyName =
+            sdbusplus::message::object_path(powerSupplyPath).filename();
+        if (powerSupplyName.empty())
+        {
+            continue;
+        }
+
+        nlohmann::json item = nlohmann::json::object();
+        item["@odata.id"] = boost::urls::format(
+            "/redfish/v1/Chassis/{}/PowerSubsystem/PowerSupplies/{}", chassisId,
+            powerSupplyName);
+
+        powerSupplyList.emplace_back(std::move(item));
+    }
     asyncResp->res.jsonValue["Members@odata.count"] = powerSupplyList.size();
 }
 
@@ -65,24 +71,24 @@
     asyncResp->res.jsonValue["Members@odata.count"] = 0;
 
     std::string powerPath = *validChassisPath + "/powered_by";
-    dbus::utility::getAssociationEndPoints(
-        powerPath, [asyncResp, chassisId](
-                       const boost::system::error_code& ec,
-                       const dbus::utility::MapperEndPoints& endpoints) {
-            if (ec)
+    dbus::utility::getAssociatedSubTreePaths(
+        powerPath,
+        sdbusplus::message::object_path("/xyz/openbmc_project/inventory"), 0,
+        powerSupplyInterface,
+        [asyncResp, chassisId](
+            const boost::system::error_code& ec,
+            const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths) {
+        if (ec)
+        {
+            if (ec.value() != EBADR)
             {
-                if (ec.value() != EBADR)
-                {
-                    BMCWEB_LOG_ERROR << "DBUS response error" << ec.value();
-                    messages::internalError(asyncResp->res);
-                }
-                return;
+                BMCWEB_LOG_ERROR << "DBUS response error" << ec.value();
+                messages::internalError(asyncResp->res);
             }
+            return;
+        }
 
-            for (const auto& endpoint : endpoints)
-            {
-                updatePowerSupplyList(asyncResp, chassisId, endpoint);
-            }
+        updatePowerSupplyList(asyncResp, chassisId, subtreePaths);
         });
 }
 
@@ -154,42 +160,44 @@
     std::function<void(const std::string& powerSupplyPath)>&& callback)
 {
     std::string powerPath = validChassisPath + "/powered_by";
-    dbus::utility::getAssociationEndPoints(
-        powerPath, [asyncResp, powerSupplyId, callback{std::move(callback)}](
-                       const boost::system::error_code& ec,
-                       const dbus::utility::MapperEndPoints& endpoints) {
-            if (ec)
+    dbus::utility::getAssociatedSubTreePaths(
+        powerPath,
+        sdbusplus::message::object_path("/xyz/openbmc_project/inventory"), 0,
+        powerSupplyInterface,
+        [asyncResp, powerSupplyId, callback{std::move(callback)}](
+            const boost::system::error_code& ec,
+            const dbus::utility::MapperGetSubTreePathsResponse& subtreePaths) {
+        if (ec)
+        {
+            if (ec.value() != EBADR)
             {
-                if (ec.value() != EBADR)
-                {
-                    BMCWEB_LOG_ERROR
-                        << "DBUS response error for getAssociationEndPoints"
-                        << ec.value();
-                    messages::internalError(asyncResp->res);
-                    return;
-                }
-                messages::resourceNotFound(asyncResp->res, "PowerSupplies",
-                                           powerSupplyId);
+                BMCWEB_LOG_ERROR
+                    << "DBUS response error for getAssociatedSubTreePaths"
+                    << ec.value();
+                messages::internalError(asyncResp->res);
                 return;
             }
+            messages::resourceNotFound(asyncResp->res, "PowerSupplies",
+                                       powerSupplyId);
+            return;
+        }
 
-            for (const auto& endpoint : endpoints)
+        for (const std::string& path : subtreePaths)
+        {
+            if (checkPowerSupplyId(path, powerSupplyId))
             {
-                if (checkPowerSupplyId(endpoint, powerSupplyId))
-                {
-                    callback(endpoint);
-                    return;
-                }
-            }
-
-            if (!endpoints.empty())
-            {
-                BMCWEB_LOG_WARNING << "Power supply not found: "
-                                   << powerSupplyId;
-                messages::resourceNotFound(asyncResp->res, "PowerSupplies",
-                                           powerSupplyId);
+                callback(path);
                 return;
             }
+        }
+
+        if (!subtreePaths.empty())
+        {
+            BMCWEB_LOG_WARNING << "Power supply not found: " << powerSupplyId;
+            messages::resourceNotFound(asyncResp->res, "PowerSupplies",
+                                       powerSupplyId);
+            return;
+        }
         });
 }
 
@@ -387,10 +395,8 @@
         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
 
-        constexpr std::array<std::string_view, 1> interfaces = {
-            "xyz.openbmc_project.Inventory.Item.PowerSupply"};
         dbus::utility::getDbusObject(
-            powerSupplyPath, interfaces,
+            powerSupplyPath, powerSupplyInterface,
             [asyncResp,
              powerSupplyPath](const boost::system::error_code& ec,
                               const dbus::utility::MapperGetObject& object) {