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/metric_report_definition.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/metric_report_definition.hpp')
-rw-r--r-- | redfish-core/lib/metric_report_definition.hpp | 281 |
1 files changed, 125 insertions, 156 deletions
diff --git a/redfish-core/lib/metric_report_definition.hpp b/redfish-core/lib/metric_report_definition.hpp index a1231b14a8..6dce89fd53 100644 --- a/redfish-core/lib/metric_report_definition.hpp +++ b/redfish-core/lib/metric_report_definition.hpp @@ -1,10 +1,10 @@ #pragma once -#include "node.hpp" #include "sensors.hpp" #include "utils/telemetry_utils.hpp" #include "utils/time_utils.hpp" +#include <app.hpp> #include <boost/container/flat_map.hpp> #include <tuple> @@ -347,171 +347,140 @@ class AddReport }; } // namespace telemetry -class MetricReportDefinitionCollection : public Node +inline void requestRoutesMetricReportDefinitionCollection(App& app) { - public: - MetricReportDefinitionCollection(App& app) : - Node(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/") - { - 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>& asyncResp, - const crow::Request&, const std::vector<std::string>&) override - { - asyncResp->res.jsonValue["@odata.type"] = - "#MetricReportDefinitionCollection." - "MetricReportDefinitionCollection"; - asyncResp->res.jsonValue["@odata.id"] = - "/redfish/v1/TelemetryService/MetricReportDefinitions"; - asyncResp->res.jsonValue["Name"] = "Metric Definition Collection"; - - telemetry::getReportCollection(asyncResp, - telemetry::metricReportDefinitionUri); - } - - void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, - const crow::Request& req, - const std::vector<std::string>&) override - { - telemetry::AddReportArgs args; - if (!telemetry::getUserParameters(asyncResp->res, req, args)) - { - return; - } - - boost::container::flat_set<std::pair<std::string, std::string>> - chassisSensors; - if (!telemetry::getChassisSensorNode(asyncResp, args.metrics, - chassisSensors)) - { - return; - } - - auto addReportReq = - std::make_shared<telemetry::AddReport>(std::move(args), asyncResp); - for (const auto& [chassis, sensorType] : chassisSensors) - { - retrieveUriToDbusMap( - chassis, sensorType, - [asyncResp, addReportReq]( - const boost::beast::http::status status, - const boost::container::flat_map<std::string, std::string>& - uriToDbus) { - if (status != boost::beast::http::status::ok) - { - BMCWEB_LOG_ERROR << "Failed to retrieve URI to dbus " - "sensors map with err " - << static_cast<unsigned>(status); - return; - } - addReportReq->insert(uriToDbus); - }); - } - } -}; - -class MetricReportDefinition : public Node -{ - public: - MetricReportDefinition(App& app) : - Node(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/", - 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>& asyncResp, - const crow::Request&, - const std::vector<std::string>& params) override - { - - if (params.size() != 1) - { - messages::internalError(asyncResp->res); - return; - } - - const std::string& id = params[0]; - crow::connections::systemBus->async_method_call( - [asyncResp, - id](const boost::system::error_code ec, - const std::vector<std::pair< - std::string, std::variant<std::string, bool, uint64_t, - telemetry::ReadingParameters>>>& - ret) { - if (ec.value() == EBADR || - ec == boost::system::errc::host_unreachable) - { - messages::resourceNotFound(asyncResp->res, - "MetricReportDefinition", id); - return; - } - if (ec) + BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/") + .privileges({"Login"}) + .methods(boost::beast::http::verb::get)( + [](const crow::Request&, + const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { + asyncResp->res.jsonValue["@odata.type"] = + "#MetricReportDefinitionCollection." + "MetricReportDefinitionCollection"; + asyncResp->res.jsonValue["@odata.id"] = + "/redfish/v1/TelemetryService/MetricReportDefinitions"; + asyncResp->res.jsonValue["Name"] = + "Metric Definition Collection"; + + telemetry::getReportCollection( + asyncResp, telemetry::metricReportDefinitionUri); + }); + + BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/") + .privileges({"ConfigureManager"}) + .methods(boost::beast::http::verb::post)( + [](const crow::Request& req, + const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { + telemetry::AddReportArgs args; + if (!telemetry::getUserParameters(asyncResp->res, req, args)) { - BMCWEB_LOG_ERROR << "respHandler DBus error " << ec; - messages::internalError(asyncResp->res); return; } - telemetry::fillReportDefinition(asyncResp, id, ret); - }, - telemetry::service, telemetry::getDbusReportPath(id), - "org.freedesktop.DBus.Properties", "GetAll", - telemetry::reportInterface); - } - - void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, - const crow::Request&, - const std::vector<std::string>& params) override - { - if (params.size() != 1) - { - messages::internalError(asyncResp->res); - return; - } - - const std::string& id = params[0]; - const std::string reportPath = telemetry::getDbusReportPath(id); - - crow::connections::systemBus->async_method_call( - [asyncResp, id](const boost::system::error_code ec) { - /* - * boost::system::errc and std::errc are missing value for - * EBADR error that is defined in Linux. - */ - if (ec.value() == EBADR) + boost::container::flat_set<std::pair<std::string, std::string>> + chassisSensors; + if (!telemetry::getChassisSensorNode(asyncResp, args.metrics, + chassisSensors)) { - messages::resourceNotFound(asyncResp->res, - "MetricReportDefinition", id); return; } - if (ec) + auto addReportReq = std::make_shared<telemetry::AddReport>( + std::move(args), asyncResp); + for (const auto& [chassis, sensorType] : chassisSensors) { - BMCWEB_LOG_ERROR << "respHandler DBus error " << ec; - messages::internalError(asyncResp->res); - return; + retrieveUriToDbusMap( + chassis, sensorType, + [asyncResp, addReportReq]( + const boost::beast::http::status status, + const boost::container::flat_map< + std::string, std::string>& uriToDbus) { + if (status != boost::beast::http::status::ok) + { + BMCWEB_LOG_ERROR + << "Failed to retrieve URI to dbus " + "sensors map with err " + << static_cast<unsigned>(status); + return; + } + addReportReq->insert(uriToDbus); + }); } + }); +} - asyncResp->res.result(boost::beast::http::status::no_content); - }, - telemetry::service, reportPath, "xyz.openbmc_project.Object.Delete", - "Delete"); - } -}; +inline void requestRoutesMetricReportDefinition(App& app) +{ + BMCWEB_ROUTE(app, + "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/") + .privileges({"Login"}) + .methods(boost::beast::http::verb::get)( + [](const crow::Request&, + const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, + const std::string& id) { + crow::connections::systemBus->async_method_call( + [asyncResp, id]( + const boost::system::error_code ec, + const std::vector<std::pair< + std::string, + std::variant<std::string, bool, uint64_t, + telemetry::ReadingParameters>>>& ret) { + if (ec.value() == EBADR || + ec == boost::system::errc::host_unreachable) + { + messages::resourceNotFound( + asyncResp->res, "MetricReportDefinition", id); + return; + } + if (ec) + { + BMCWEB_LOG_ERROR << "respHandler DBus error " << ec; + messages::internalError(asyncResp->res); + return; + } + + telemetry::fillReportDefinition(asyncResp, id, ret); + }, + telemetry::service, telemetry::getDbusReportPath(id), + "org.freedesktop.DBus.Properties", "GetAll", + telemetry::reportInterface); + }); + BMCWEB_ROUTE(app, + "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/") + .privileges({"ConfigureManager"}) + .methods(boost::beast::http::verb::delete_)( + [](const crow::Request&, + const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, + const std::string& id) + + { + const std::string reportPath = telemetry::getDbusReportPath(id); + + crow::connections::systemBus->async_method_call( + [asyncResp, id](const boost::system::error_code ec) { + /* + * boost::system::errc and std::errc are missing value + * for EBADR error that is defined in Linux. + */ + if (ec.value() == EBADR) + { + messages::resourceNotFound( + asyncResp->res, "MetricReportDefinition", id); + return; + } + + if (ec) + { + BMCWEB_LOG_ERROR << "respHandler DBus error " << ec; + messages::internalError(asyncResp->res); + return; + } + + asyncResp->res.result( + boost::beast::http::status::no_content); + }, + telemetry::service, reportPath, + "xyz.openbmc_project.Object.Delete", "Delete"); + }); +} } // namespace redfish |