Refactor getValidChassisPath

getValidChassisPath shouldn't rely on SensorAsyncResp object. Move
it to utils to wider use.

Tested: Redfish service Validator passed.

Change-Id: I418b7f0f3846fd001392536e2943f062b1bdb5cd
Signed-off-by: Zhenwei Chen <zhenweichen0207@gmail.com>
diff --git a/redfish-core/include/utils/chassis_utils.hpp b/redfish-core/include/utils/chassis_utils.hpp
new file mode 100644
index 0000000..0acbf33
--- /dev/null
+++ b/redfish-core/include/utils/chassis_utils.hpp
@@ -0,0 +1,67 @@
+#pragma once
+#include <async_resp.hpp>
+
+namespace redfish
+{
+
+namespace chassis_utils
+{
+/**
+ * @brief Retrieves valid chassis path
+ * @param asyncResp   Pointer to object holding response data
+ * @param callback  Callback for next step to get valid chassis path
+ */
+template <typename Callback>
+void getValidChassisPath(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+                         const std::string& chassisId, Callback&& callback)
+{
+    BMCWEB_LOG_DEBUG << "checkChassisId enter";
+    const std::array<const char*, 2> interfaces = {
+        "xyz.openbmc_project.Inventory.Item.Board",
+        "xyz.openbmc_project.Inventory.Item.Chassis"};
+
+    auto respHandler =
+        [callback{std::forward<Callback>(callback)}, asyncResp,
+         chassisId](const boost::system::error_code ec,
+                    const dbus::utility::MapperGetSubTreePathsResponse&
+                        chassisPaths) mutable {
+        BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
+        if (ec)
+        {
+            BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
+                             << ec;
+            messages::internalError(asyncResp->res);
+            return;
+        }
+
+        std::optional<std::string> chassisPath;
+        std::string chassisName;
+        for (const std::string& chassis : chassisPaths)
+        {
+            sdbusplus::message::object_path path(chassis);
+            chassisName = path.filename();
+            if (chassisName.empty())
+            {
+                BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
+                continue;
+            }
+            if (chassisName == chassisId)
+            {
+                chassisPath = chassis;
+                break;
+            }
+        }
+        callback(chassisPath);
+    };
+
+    // Get the Chassis Collection
+    crow::connections::systemBus->async_method_call(
+        respHandler, "xyz.openbmc_project.ObjectMapper",
+        "/xyz/openbmc_project/object_mapper",
+        "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
+        "/xyz/openbmc_project/inventory", 0, interfaces);
+    BMCWEB_LOG_DEBUG << "checkChassisId exit";
+}
+
+} // namespace chassis_utils
+} // namespace redfish
diff --git a/redfish-core/lib/power.hpp b/redfish-core/lib/power.hpp
index bc18157..cecaeab 100644
--- a/redfish-core/lib/power.hpp
+++ b/redfish-core/lib/power.hpp
@@ -17,6 +17,7 @@
 #pragma once
 
 #include "sensors.hpp"
+#include "utils/chassis_utils.hpp"
 
 #include <app.hpp>
 #include <dbus_utility.hpp>
@@ -110,7 +111,9 @@
                 std::variant<uint32_t>(*value));
             });
     };
-    getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
+    redfish::chassis_utils::getValidChassisPath(sensorsAsyncResp->asyncResp,
+                                                sensorsAsyncResp->chassisId,
+                                                std::move(getChassisPath));
 }
 inline void requestRoutesPower(App& app)
 {
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 04b5190..44faca6 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -472,62 +472,6 @@
 }
 
 /**
- * @brief Retrieves valid chassis path
- * @param asyncResp   Pointer to object holding response data
- * @param callback  Callback for next step to get valid chassis path
- */
-template <typename Callback>
-void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
-                         Callback&& callback)
-{
-    BMCWEB_LOG_DEBUG << "checkChassisId enter";
-    const std::array<const char*, 2> interfaces = {
-        "xyz.openbmc_project.Inventory.Item.Board",
-        "xyz.openbmc_project.Inventory.Item.Chassis"};
-
-    auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
-                           const boost::system::error_code ec,
-                           const dbus::utility::MapperGetSubTreePathsResponse&
-                               chassisPaths) mutable {
-        BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
-        if (ec)
-        {
-            BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
-                             << ec;
-            messages::internalError(asyncResp->asyncResp->res);
-            return;
-        }
-
-        std::optional<std::string> chassisPath;
-        std::string chassisName;
-        for (const std::string& chassis : chassisPaths)
-        {
-            sdbusplus::message::object_path path(chassis);
-            chassisName = path.filename();
-            if (chassisName.empty())
-            {
-                BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
-                continue;
-            }
-            if (chassisName == asyncResp->chassisId)
-            {
-                chassisPath = chassis;
-                break;
-            }
-        }
-        callback(chassisPath);
-    };
-
-    // Get the Chassis Collection
-    crow::connections::systemBus->async_method_call(
-        respHandler, "xyz.openbmc_project.ObjectMapper",
-        "/xyz/openbmc_project/object_mapper",
-        "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
-        "/xyz/openbmc_project/inventory", 0, interfaces);
-    BMCWEB_LOG_DEBUG << "checkChassisId exit";
-}
-
-/**
  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
  * @param SensorsAsyncResp   Pointer to object holding response data
  * @param callback  Callback for next step in gathered sensor processing