summaryrefslogtreecommitdiff
path: root/redfish-core
diff options
context:
space:
mode:
authorMyung Bae <myungbae@us.ibm.com>2024-02-19 21:39:56 +0300
committerEd Tanous <ed@tanous.net>2024-02-23 20:25:06 +0300
commit78c9020305038b7974b4737c9b1d0b4afb9740f2 (patch)
treee202f33dcbc2f41409b155de43347233fa85c19c /redfish-core
parentb4d593f1c6113311da4b29fc352187fbcb6d1fd4 (diff)
downloadbmcweb-78c9020305038b7974b4737c9b1d0b4afb9740f2.tar.xz
Refactor to pass dbus error code to caller
The aux function `getValidFabricAdapterPath()` currently handles the error uniformly for all callers. This commit is to make the function to pass the error conddition to the caller so that it can be handled appropriately in the caller's context. Tested: - Check `resourceNotFound` is generated by caller. ``` $ curl -k -X GET https://${bmc}:18080/redfish/v1/Systems/system/FabricAdapters/badAdapter [WARNING fabric_adapters.hpp:270] Adapter not found ``` - Validator passes Change-Id: I37a61a3a79138aa898ab18332f58e3007496e302 Signed-off-by: Myung Bae <myungbae@us.ibm.com>
Diffstat (limited to 'redfish-core')
-rw-r--r--redfish-core/lib/fabric_adapters.hpp161
1 files changed, 102 insertions, 59 deletions
diff --git a/redfish-core/lib/fabric_adapters.hpp b/redfish-core/lib/fabric_adapters.hpp
index a8373a107d..87015dd3c5 100644
--- a/redfish-core/lib/fabric_adapters.hpp
+++ b/redfish-core/lib/fabric_adapters.hpp
@@ -16,27 +16,13 @@
#include <array>
#include <functional>
#include <memory>
+#include <optional>
#include <string>
#include <string_view>
namespace redfish
{
-inline void handleAdapterError(const boost::system::error_code& ec,
- crow::Response& res,
- const std::string& adapterId)
-{
- if (ec.value() == boost::system::errc::io_error)
- {
- messages::resourceNotFound(res, "#FabricAdapter.v1_4_0.FabricAdapter",
- adapterId);
- return;
- }
-
- BMCWEB_LOG_ERROR("DBus method call failed with error {}", ec.value());
- messages::internalError(res);
-}
-
inline void getFabricAdapterLocation(
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const std::string& serviceName, const std::string& fabricAdapterPath)
@@ -198,51 +184,76 @@ inline void doAdapterGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
getFabricAdapterHealth(asyncResp, serviceName, fabricAdapterPath);
}
-inline bool checkFabricAdapterId(const std::string& adapterPath,
- const std::string& adapterId)
+inline void afterGetValidFabricAdapterPath(
+ const std::string& adapterId,
+ std::function<void(const boost::system::error_code&,
+ const std::string& fabricAdapterPath,
+ const std::string& serviceName)>& callback,
+ const boost::system::error_code& ec,
+ const dbus::utility::MapperGetSubTreeResponse& subtree)
{
- std::string fabricAdapterName =
- sdbusplus::message::object_path(adapterPath).filename();
+ std::string fabricAdapterPath;
+ std::string serviceName;
+ if (ec)
+ {
+ callback(ec, fabricAdapterPath, serviceName);
+ return;
+ }
- return !(fabricAdapterName.empty() || fabricAdapterName != adapterId);
+ for (const auto& [adapterPath, serviceMap] : subtree)
+ {
+ std::string fabricAdapterName =
+ sdbusplus::message::object_path(adapterPath).filename();
+ if (fabricAdapterName == adapterId)
+ {
+ fabricAdapterPath = adapterPath;
+ serviceName = serviceMap.begin()->first;
+ break;
+ }
+ }
+ callback(ec, fabricAdapterPath, serviceName);
}
inline void getValidFabricAdapterPath(
- const std::string& adapterId, const std::string& systemName,
- const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
- std::function<void(const std::string& fabricAdapterPath,
+ const std::string& adapterId,
+ std::function<void(const boost::system::error_code& ec,
+ const std::string& fabricAdapterPath,
const std::string& serviceName)>&& callback)
{
- if (systemName != "system")
- {
- messages::resourceNotFound(asyncResp->res, "ComputerSystem",
- systemName);
- return;
- }
constexpr std::array<std::string_view, 1> interfaces{
"xyz.openbmc_project.Inventory.Item.FabricAdapter"};
+ dbus::utility::getSubTree("/xyz/openbmc_project/inventory", 0, interfaces,
+ std::bind_front(afterGetValidFabricAdapterPath,
+ adapterId, std::move(callback)));
+}
- dbus::utility::getSubTree(
- "/xyz/openbmc_project/inventory", 0, interfaces,
- [adapterId, asyncResp,
- callback](const boost::system::error_code& ec,
- const dbus::utility::MapperGetSubTreeResponse& subtree) {
- if (ec)
+inline void afterHandleFabricAdapterGet(
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const std::string& systemName, const std::string& adapterId,
+ const boost::system::error_code& ec, const std::string& fabricAdapterPath,
+ const std::string& serviceName)
+{
+ if (ec)
+ {
+ if (ec.value() == boost::system::errc::io_error)
{
- handleAdapterError(ec, asyncResp->res, adapterId);
+ messages::resourceNotFound(asyncResp->res, "FabricAdapter",
+ adapterId);
return;
}
- for (const auto& [adapterPath, serviceMap] : subtree)
- {
- if (checkFabricAdapterId(adapterPath, adapterId))
- {
- callback(adapterPath, serviceMap.begin()->first);
- return;
- }
- }
+
+ BMCWEB_LOG_ERROR("DBus method call failed with error {}", ec.value());
+ messages::internalError(asyncResp->res);
+ return;
+ }
+ if (fabricAdapterPath.empty() || serviceName.empty())
+ {
BMCWEB_LOG_WARNING("Adapter not found");
messages::resourceNotFound(asyncResp->res, "FabricAdapter", adapterId);
- });
+ return;
+ }
+ doAdapterGet(asyncResp, systemName, adapterId, fabricAdapterPath,
+ serviceName);
}
inline void
@@ -262,14 +273,15 @@ inline void
systemName);
return;
}
-
+ if (systemName != "system")
+ {
+ messages::resourceNotFound(asyncResp->res, "ComputerSystem",
+ systemName);
+ return;
+ }
getValidFabricAdapterPath(
- adapterId, systemName, asyncResp,
- [asyncResp, systemName, adapterId](const std::string& fabricAdapterPath,
- const std::string& serviceName) {
- doAdapterGet(asyncResp, systemName, adapterId, fabricAdapterPath,
- serviceName);
- });
+ adapterId, std::bind_front(afterHandleFabricAdapterGet, asyncResp,
+ systemName, adapterId));
}
inline void handleFabricAdapterCollectionGet(
@@ -339,6 +351,35 @@ inline void handleFabricAdapterCollectionHead(
"</redfish/v1/JsonSchemas/FabricAdapterCollection/FabricAdapterCollection.json>; rel=describedby");
}
+inline void afterHandleFabricAdapterHead(
+ const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
+ const std::string& adapterId, const boost::system::error_code& ec,
+ const std::string& fabricAdapterPath, const std::string& serviceName)
+{
+ if (ec)
+ {
+ if (ec.value() == boost::system::errc::io_error)
+ {
+ messages::resourceNotFound(asyncResp->res, "FabricAdapter",
+ adapterId);
+ return;
+ }
+
+ BMCWEB_LOG_ERROR("DBus method call failed with error {}", ec.value());
+ messages::internalError(asyncResp->res);
+ return;
+ }
+ if (fabricAdapterPath.empty() || serviceName.empty())
+ {
+ BMCWEB_LOG_WARNING("Adapter not found");
+ messages::resourceNotFound(asyncResp->res, "FabricAdapter", adapterId);
+ return;
+ }
+ asyncResp->res.addHeader(
+ boost::beast::http::field::link,
+ "</redfish/v1/JsonSchemas/FabricAdapter/FabricAdapter.json>; rel=describedby");
+}
+
inline void
handleFabricAdapterHead(crow::App& app, const crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
@@ -357,13 +398,15 @@ inline void
systemName);
return;
}
- getValidFabricAdapterPath(adapterId, systemName, asyncResp,
- [asyncResp, systemName, adapterId](
- const std::string&, const std::string&) {
- asyncResp->res.addHeader(
- boost::beast::http::field::link,
- "</redfish/v1/JsonSchemas/FabricAdapter/FabricAdapter.json>; rel=describedby");
- });
+ if (systemName != "system")
+ {
+ messages::resourceNotFound(asyncResp->res, "ComputerSystem",
+ systemName);
+ return;
+ }
+ getValidFabricAdapterPath(
+ adapterId,
+ std::bind_front(afterHandleFabricAdapterHead, asyncResp, adapterId));
}
inline void requestRoutesFabricAdapterCollection(App& app)