diff options
author | John Edward Broadbent <jebr@google.com> | 2021-04-09 01:57:16 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2021-06-03 21:18:02 +0300 |
commit | 7e860f1550c8686eec42f7a75bc5f2ef51e756ad (patch) | |
tree | 989da47a8427bc1a60119f480e2523151b1433aa /redfish-core/lib/sensors.hpp | |
parent | eb75770c6c4369984cb150ded4f5ace410ed24a9 (diff) | |
download | bmcweb-7e860f1550c8686eec42f7a75bc5f2ef51e756ad.tar.xz |
Remove Redfish Node class
Reduces the total number of lines and will allow for easier testing of
the redfish responses.
A main purpose of the node class was to set app.routeDynamic(). However
now app.routeDynamic can handle the complexity that was once in critical
to node. The macro app.routeDynamic() provides a shorter cleaner
interface to the unerlying app.routeDyanic call. The old pattern set
permissions for 6 interfaces (get, head, patch, put, delete_, and post)
even if only one interface is created. That pattern creates unneeded
code that can be safely removed with no effect.
Unit test for the responses would have to mock the node the class in
order to fully test responses.
see https://github.com/openbmc/bmcweb/issues/181
The following files still need node to be extracted.
virtual_media.hpp
account_service.hpp
redfish_sessions.hpp
ethernet.hpp
The files above use a pattern that is not trivial to address. Often their
responses call an async lambda capturing the inherited class. ie
(https://github.com/openbmc/bmcweb/blob/ffed87b5ad1797ca966d030e7f979770
28d258fa/redfish-core/lib/account_service.hpp#L1393)
At a later point I plan to remove node from the files above.
Tested:
I ran the docker unit test with the following command.
WORKSPACE=$(pwd) UNIT_TEST_PKG=bmcweb
./openbmc-build-scripts/run-unit-test-docker.sh
I ran the validator and this change did not create any issues.
python3 RedfishServiceValidator.py -c config.ini
Signed-off-by: John Edward Broadbent <jebr@google.com>
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I147a0289c52cb4198345b1ad9bfe6fdddf57f3df
Diffstat (limited to 'redfish-core/lib/sensors.hpp')
-rw-r--r-- | redfish-core/lib/sensors.hpp | 288 |
1 files changed, 126 insertions, 162 deletions
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp index b20f7ebc22..0fc7e702db 100644 --- a/redfish-core/lib/sensors.hpp +++ b/redfish-core/lib/sensors.hpp @@ -15,8 +15,7 @@ */ #pragma once -#include "node.hpp" - +#include <app.hpp> #include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/split.hpp> #include <boost/container/flat_map.hpp> @@ -3106,177 +3105,142 @@ inline void retrieveUriToDbusMap(const std::string& chassis, getChassisData(resp); } -class SensorCollection : public Node +inline void requestRoutesSensorCollection(App& app) { - public: - SensorCollection(App& app) : - Node(app, "/redfish/v1/Chassis/<str>/Sensors/", std::string()) - { - entityPrivileges = { - {boost::beast::http::verb::get, {{"Login"}}}, - {boost::beast::http::verb::head, {{"Login"}}}, - {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, - {boost::beast::http::verb::put, {{"ConfigureManager"}}}, - {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, - {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; - } - - private: - void doGet(const std::shared_ptr<bmcweb::AsyncResp>& aResp, - const crow::Request&, - const std::vector<std::string>& params) override - { - BMCWEB_LOG_DEBUG << "SensorCollection doGet enter"; - if (params.size() != 1) - { - BMCWEB_LOG_DEBUG << "SensorCollection doGet param size < 1"; - messages::internalError(aResp->res); - - return; - } + BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/") + .privileges({"Login"}) + .methods( + boost::beast::http::verb::get)([](const crow::Request&, + const std::shared_ptr< + bmcweb::AsyncResp>& aResp, + const std::string& chassisId) { + BMCWEB_LOG_DEBUG << "SensorCollection doGet enter"; + + std::shared_ptr<SensorsAsyncResp> asyncResp = + std::make_shared<SensorsAsyncResp>( + aResp, chassisId, + sensors::dbus::paths.at(sensors::node::sensors), + sensors::node::sensors); + + auto getChassisCb = + [asyncResp]( + const std::shared_ptr< + boost::container::flat_set<std::string>>& sensorNames) { + BMCWEB_LOG_DEBUG << "getChassisCb enter"; + + nlohmann::json& entriesArray = + asyncResp->asyncResp->res.jsonValue["Members"]; + for (auto& sensor : *sensorNames) + { + BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; - const std::string& chassisId = params[0]; - std::shared_ptr<SensorsAsyncResp> asyncResp = - std::make_shared<SensorsAsyncResp>( - aResp, chassisId, - sensors::dbus::paths.at(sensors::node::sensors), - sensors::node::sensors); + sdbusplus::message::object_path path(sensor); + std::string sensorName = path.filename(); + if (sensorName.empty()) + { + BMCWEB_LOG_ERROR << "Invalid sensor path: " + << sensor; + messages::internalError(asyncResp->asyncResp->res); + return; + } + entriesArray.push_back( + {{"@odata.id", "/redfish/v1/Chassis/" + + asyncResp->chassisId + "/" + + asyncResp->chassisSubNode + "/" + + sensorName}}); + } - auto getChassisCb = - [asyncResp]( - const std::shared_ptr<boost::container::flat_set<std::string>>& - sensorNames) { - BMCWEB_LOG_DEBUG << "getChassisCb enter"; + asyncResp->asyncResp->res.jsonValue["Members@odata.count"] = + entriesArray.size(); + BMCWEB_LOG_DEBUG << "getChassisCb exit"; + }; - nlohmann::json& entriesArray = - asyncResp->asyncResp->res.jsonValue["Members"]; - for (auto& sensor : *sensorNames) - { - BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor; + // Get set of sensors in chassis + getChassis(asyncResp, std::move(getChassisCb)); + BMCWEB_LOG_DEBUG << "SensorCollection doGet exit"; + }); +} - sdbusplus::message::object_path path(sensor); - std::string sensorName = path.filename(); - if (sensorName.empty()) +inline void requestRoutesSensor(App& app) +{ + BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/") + .privileges({"Login"}) + .methods( + boost::beast::http::verb::get)([](const crow::Request&, + const std::shared_ptr< + bmcweb::AsyncResp>& aResp, + const std::string& chassisId, + const std::string& sensorName) { + BMCWEB_LOG_DEBUG << "Sensor doGet enter"; + std::shared_ptr<SensorsAsyncResp> asyncResp = + std::make_shared<SensorsAsyncResp>(aResp, chassisId, + std::vector<const char*>(), + sensors::node::sensors); + + const std::array<const char*, 1> interfaces = { + "xyz.openbmc_project.Sensor.Value"}; + + // Get a list of all of the sensors that implement Sensor.Value + // and get the path and service name associated with the sensor + crow::connections::systemBus->async_method_call( + [asyncResp, sensorName](const boost::system::error_code ec, + const GetSubTreeType& subtree) { + BMCWEB_LOG_DEBUG << "respHandler1 enter"; + if (ec) { - BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor; messages::internalError(asyncResp->asyncResp->res); + BMCWEB_LOG_ERROR + << "Sensor getSensorPaths resp_handler: " + << "Dbus error " << ec; return; } - entriesArray.push_back( - {{"@odata.id", - "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" + - asyncResp->chassisSubNode + "/" + sensorName}}); - } - asyncResp->asyncResp->res.jsonValue["Members@odata.count"] = - entriesArray.size(); - BMCWEB_LOG_DEBUG << "getChassisCb exit"; - }; - - // Get set of sensors in chassis - getChassis(asyncResp, std::move(getChassisCb)); - BMCWEB_LOG_DEBUG << "SensorCollection doGet exit"; - } -}; - -class Sensor : public Node -{ - public: - Sensor(App& app) : - Node(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/", std::string(), - std::string()) - { - entityPrivileges = { - {boost::beast::http::verb::get, {{"Login"}}}, - {boost::beast::http::verb::head, {{"Login"}}}, - {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, - {boost::beast::http::verb::put, {{"ConfigureManager"}}}, - {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, - {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; - } - - private: - void doGet(const std::shared_ptr<bmcweb::AsyncResp>& aResp, - const crow::Request&, - const std::vector<std::string>& params) override - { - BMCWEB_LOG_DEBUG << "Sensor doGet enter"; - if (params.size() != 2) - { - BMCWEB_LOG_DEBUG << "Sensor doGet param size < 2"; - messages::internalError(aResp->res); - - return; - } - const std::string& chassisId = params[0]; - std::shared_ptr<SensorsAsyncResp> asyncResp = - std::make_shared<SensorsAsyncResp>(aResp, chassisId, - std::vector<const char*>(), - sensors::node::sensors); - - const std::string& sensorName = params[1]; - const std::array<const char*, 1> interfaces = { - "xyz.openbmc_project.Sensor.Value"}; - - // Get a list of all of the sensors that implement Sensor.Value - // and get the path and service name associated with the sensor - crow::connections::systemBus->async_method_call( - [asyncResp, sensorName](const boost::system::error_code ec, - const GetSubTreeType& subtree) { - BMCWEB_LOG_DEBUG << "respHandler1 enter"; - if (ec) - { - messages::internalError(asyncResp->asyncResp->res); - BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: " - << "Dbus error " << ec; - return; - } - - GetSubTreeType::const_iterator it = std::find_if( - subtree.begin(), subtree.end(), - [sensorName]( - const std::pair< - std::string, - std::vector<std::pair<std::string, - std::vector<std::string>>>>& - object) { - sdbusplus::message::object_path path(object.first); - std::string name = path.filename(); - if (name.empty()) - { - BMCWEB_LOG_ERROR << "Invalid sensor path: " - << object.first; - return false; - } - - return name == sensorName; - }); - - if (it == subtree.end()) - { - BMCWEB_LOG_ERROR << "Could not find path for sensor: " - << sensorName; - messages::resourceNotFound(asyncResp->asyncResp->res, - "Sensor", sensorName); - return; - } - std::string_view sensorPath = (*it).first; - BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" - << sensorName << "': " << sensorPath; + GetSubTreeType::const_iterator it = std::find_if( + subtree.begin(), subtree.end(), + [sensorName]( + const std::pair< + std::string, + std::vector<std::pair< + std::string, std::vector<std::string>>>>& + object) { + sdbusplus::message::object_path path(object.first); + std::string name = path.filename(); + if (name.empty()) + { + BMCWEB_LOG_ERROR << "Invalid sensor path: " + << object.first; + return false; + } - const std::shared_ptr<boost::container::flat_set<std::string>> - sensorList = std::make_shared< - boost::container::flat_set<std::string>>(); + return name == sensorName; + }); - sensorList->emplace(sensorPath); - processSensorList(asyncResp, sensorList); - BMCWEB_LOG_DEBUG << "respHandler1 exit"; - }, - "xyz.openbmc_project.ObjectMapper", - "/xyz/openbmc_project/object_mapper", - "xyz.openbmc_project.ObjectMapper", "GetSubTree", - "/xyz/openbmc_project/sensors", 2, interfaces); - } -}; + if (it == subtree.end()) + { + BMCWEB_LOG_ERROR << "Could not find path for sensor: " + << sensorName; + messages::resourceNotFound(asyncResp->asyncResp->res, + "Sensor", sensorName); + return; + } + std::string_view sensorPath = (*it).first; + BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" + << sensorName << "': " << sensorPath; + + const std::shared_ptr< + boost::container::flat_set<std::string>> + sensorList = std::make_shared< + boost::container::flat_set<std::string>>(); + + sensorList->emplace(sensorPath); + processSensorList(asyncResp, sensorList); + BMCWEB_LOG_DEBUG << "respHandler1 exit"; + }, + "xyz.openbmc_project.ObjectMapper", + "/xyz/openbmc_project/object_mapper", + "xyz.openbmc_project.ObjectMapper", "GetSubTree", + "/xyz/openbmc_project/sensors", 2, interfaces); + }); +} } // namespace redfish |