xref: /openbmc/bmcweb/features/redfish/lib/metric_report_definition.hpp (revision dac07cadb8ec2f69c676fb1e2bfcab355971a7cf)
1081ebf06SWludzik, Jozef #pragma once
2081ebf06SWludzik, Jozef 
33ccb3adbSEd Tanous #include "app.hpp"
43ccb3adbSEd Tanous #include "dbus_utility.hpp"
5479e899dSKrzysztof Grobelny #include "generated/enums/metric_report_definition.hpp"
6539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
73ccb3adbSEd Tanous #include "query.hpp"
83ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
94dbb8aeaSWludzik, Jozef #include "sensors.hpp"
103ccb3adbSEd Tanous #include "utils/collection.hpp"
113ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
125b90429aSEd Tanous #include "utils/json_utils.hpp"
13081ebf06SWludzik, Jozef #include "utils/telemetry_utils.hpp"
14081ebf06SWludzik, Jozef #include "utils/time_utils.hpp"
15081ebf06SWludzik, Jozef 
164dbb8aeaSWludzik, Jozef #include <boost/container/flat_map.hpp>
17ef4c65b7SEd Tanous #include <boost/url/format.hpp>
1889474494SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
1989474494SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
204dbb8aeaSWludzik, Jozef 
217a1dbc48SGeorge Liu #include <array>
22fe04d49cSNan Zhou #include <map>
23f19ab44aSSzymon Dompke #include <optional>
24f19ab44aSSzymon Dompke #include <span>
25f19ab44aSSzymon Dompke #include <string>
267a1dbc48SGeorge Liu #include <string_view>
27081ebf06SWludzik, Jozef #include <tuple>
28f19ab44aSSzymon Dompke #include <utility>
29081ebf06SWludzik, Jozef #include <variant>
30f19ab44aSSzymon Dompke #include <vector>
31081ebf06SWludzik, Jozef 
32081ebf06SWludzik, Jozef namespace redfish
33081ebf06SWludzik, Jozef {
34081ebf06SWludzik, Jozef 
35081ebf06SWludzik, Jozef namespace telemetry
36081ebf06SWludzik, Jozef {
37081ebf06SWludzik, Jozef 
38479e899dSKrzysztof Grobelny using ReadingParameters = std::vector<std::tuple<
39479e899dSKrzysztof Grobelny     std::vector<std::tuple<sdbusplus::message::object_path, std::string>>,
40479e899dSKrzysztof Grobelny     std::string, std::string, uint64_t>>;
41479e899dSKrzysztof Grobelny 
429e6c388aSLukasz Kazmierczak inline bool verifyCommonErrors(crow::Response& res, const std::string& id,
439e6c388aSLukasz Kazmierczak                                const boost::system::error_code& ec)
449e6c388aSLukasz Kazmierczak {
459e6c388aSLukasz Kazmierczak     if (ec.value() == EBADR || ec == boost::system::errc::host_unreachable)
469e6c388aSLukasz Kazmierczak     {
479e6c388aSLukasz Kazmierczak         messages::resourceNotFound(res, "MetricReportDefinition", id);
489e6c388aSLukasz Kazmierczak         return false;
499e6c388aSLukasz Kazmierczak     }
509e6c388aSLukasz Kazmierczak 
519e6c388aSLukasz Kazmierczak     if (ec == boost::system::errc::file_exists)
529e6c388aSLukasz Kazmierczak     {
539e6c388aSLukasz Kazmierczak         messages::resourceAlreadyExists(res, "MetricReportDefinition", "Id",
549e6c388aSLukasz Kazmierczak                                         id);
559e6c388aSLukasz Kazmierczak         return false;
569e6c388aSLukasz Kazmierczak     }
579e6c388aSLukasz Kazmierczak 
589e6c388aSLukasz Kazmierczak     if (ec == boost::system::errc::too_many_files_open)
599e6c388aSLukasz Kazmierczak     {
609e6c388aSLukasz Kazmierczak         messages::createLimitReachedForResource(res);
619e6c388aSLukasz Kazmierczak         return false;
629e6c388aSLukasz Kazmierczak     }
639e6c388aSLukasz Kazmierczak 
649e6c388aSLukasz Kazmierczak     if (ec)
659e6c388aSLukasz Kazmierczak     {
669e6c388aSLukasz Kazmierczak         BMCWEB_LOG_ERROR("DBUS response error {}", ec);
679e6c388aSLukasz Kazmierczak         messages::internalError(res);
689e6c388aSLukasz Kazmierczak         return false;
699e6c388aSLukasz Kazmierczak     }
709e6c388aSLukasz Kazmierczak 
719e6c388aSLukasz Kazmierczak     return true;
729e6c388aSLukasz Kazmierczak }
739e6c388aSLukasz Kazmierczak 
74479e899dSKrzysztof Grobelny inline metric_report_definition::ReportActionsEnum
75479e899dSKrzysztof Grobelny     toRedfishReportAction(std::string_view dbusValue)
76479e899dSKrzysztof Grobelny {
77479e899dSKrzysztof Grobelny     if (dbusValue ==
78479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.ReportActions.EmitsReadingsUpdate")
79479e899dSKrzysztof Grobelny     {
80479e899dSKrzysztof Grobelny         return metric_report_definition::ReportActionsEnum::RedfishEvent;
81479e899dSKrzysztof Grobelny     }
82479e899dSKrzysztof Grobelny     if (dbusValue ==
83479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.ReportActions.LogToMetricReportsCollection")
84479e899dSKrzysztof Grobelny     {
85479e899dSKrzysztof Grobelny         return metric_report_definition::ReportActionsEnum::
86479e899dSKrzysztof Grobelny             LogToMetricReportsCollection;
87479e899dSKrzysztof Grobelny     }
88479e899dSKrzysztof Grobelny     return metric_report_definition::ReportActionsEnum::Invalid;
89479e899dSKrzysztof Grobelny }
90479e899dSKrzysztof Grobelny 
91479e899dSKrzysztof Grobelny inline std::string toDbusReportAction(std::string_view redfishValue)
92479e899dSKrzysztof Grobelny {
93479e899dSKrzysztof Grobelny     if (redfishValue == "RedfishEvent")
94479e899dSKrzysztof Grobelny     {
95479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.ReportActions.EmitsReadingsUpdate";
96479e899dSKrzysztof Grobelny     }
97479e899dSKrzysztof Grobelny     if (redfishValue == "LogToMetricReportsCollection")
98479e899dSKrzysztof Grobelny     {
99479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.ReportActions.LogToMetricReportsCollection";
100479e899dSKrzysztof Grobelny     }
101479e899dSKrzysztof Grobelny     return "";
102479e899dSKrzysztof Grobelny }
103479e899dSKrzysztof Grobelny 
104479e899dSKrzysztof Grobelny inline metric_report_definition::MetricReportDefinitionType
105479e899dSKrzysztof Grobelny     toRedfishReportingType(std::string_view dbusValue)
106479e899dSKrzysztof Grobelny {
107479e899dSKrzysztof Grobelny     if (dbusValue ==
108479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.ReportingType.OnChange")
109479e899dSKrzysztof Grobelny     {
110479e899dSKrzysztof Grobelny         return metric_report_definition::MetricReportDefinitionType::OnChange;
111479e899dSKrzysztof Grobelny     }
112479e899dSKrzysztof Grobelny     if (dbusValue ==
113479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.ReportingType.OnRequest")
114479e899dSKrzysztof Grobelny     {
115479e899dSKrzysztof Grobelny         return metric_report_definition::MetricReportDefinitionType::OnRequest;
116479e899dSKrzysztof Grobelny     }
117479e899dSKrzysztof Grobelny     if (dbusValue ==
118479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.ReportingType.Periodic")
119479e899dSKrzysztof Grobelny     {
120479e899dSKrzysztof Grobelny         return metric_report_definition::MetricReportDefinitionType::Periodic;
121479e899dSKrzysztof Grobelny     }
122479e899dSKrzysztof Grobelny     return metric_report_definition::MetricReportDefinitionType::Invalid;
123479e899dSKrzysztof Grobelny }
124479e899dSKrzysztof Grobelny 
125479e899dSKrzysztof Grobelny inline std::string toDbusReportingType(std::string_view redfishValue)
126479e899dSKrzysztof Grobelny {
127479e899dSKrzysztof Grobelny     if (redfishValue == "OnChange")
128479e899dSKrzysztof Grobelny     {
129479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.ReportingType.OnChange";
130479e899dSKrzysztof Grobelny     }
131479e899dSKrzysztof Grobelny     if (redfishValue == "OnRequest")
132479e899dSKrzysztof Grobelny     {
133479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.ReportingType.OnRequest";
134479e899dSKrzysztof Grobelny     }
135479e899dSKrzysztof Grobelny     if (redfishValue == "Periodic")
136479e899dSKrzysztof Grobelny     {
137479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.ReportingType.Periodic";
138479e899dSKrzysztof Grobelny     }
139479e899dSKrzysztof Grobelny     return "";
140479e899dSKrzysztof Grobelny }
141479e899dSKrzysztof Grobelny 
142479e899dSKrzysztof Grobelny inline metric_report_definition::CollectionTimeScope
143479e899dSKrzysztof Grobelny     toRedfishCollectionTimeScope(std::string_view dbusValue)
144479e899dSKrzysztof Grobelny {
145479e899dSKrzysztof Grobelny     if (dbusValue ==
146479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.CollectionTimescope.Point")
147479e899dSKrzysztof Grobelny     {
148479e899dSKrzysztof Grobelny         return metric_report_definition::CollectionTimeScope::Point;
149479e899dSKrzysztof Grobelny     }
150479e899dSKrzysztof Grobelny     if (dbusValue ==
151479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.CollectionTimescope.Interval")
152479e899dSKrzysztof Grobelny     {
153479e899dSKrzysztof Grobelny         return metric_report_definition::CollectionTimeScope::Interval;
154479e899dSKrzysztof Grobelny     }
155479e899dSKrzysztof Grobelny     if (dbusValue ==
156479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.CollectionTimescope.StartupInterval")
157479e899dSKrzysztof Grobelny     {
158479e899dSKrzysztof Grobelny         return metric_report_definition::CollectionTimeScope::StartupInterval;
159479e899dSKrzysztof Grobelny     }
160479e899dSKrzysztof Grobelny     return metric_report_definition::CollectionTimeScope::Invalid;
161479e899dSKrzysztof Grobelny }
162479e899dSKrzysztof Grobelny 
163479e899dSKrzysztof Grobelny inline std::string toDbusCollectionTimeScope(std::string_view redfishValue)
164479e899dSKrzysztof Grobelny {
165479e899dSKrzysztof Grobelny     if (redfishValue == "Point")
166479e899dSKrzysztof Grobelny     {
167479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.CollectionTimescope.Point";
168479e899dSKrzysztof Grobelny     }
169479e899dSKrzysztof Grobelny     if (redfishValue == "Interval")
170479e899dSKrzysztof Grobelny     {
171479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.CollectionTimescope.Interval";
172479e899dSKrzysztof Grobelny     }
173479e899dSKrzysztof Grobelny     if (redfishValue == "StartupInterval")
174479e899dSKrzysztof Grobelny     {
175479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.CollectionTimescope.StartupInterval";
176479e899dSKrzysztof Grobelny     }
177479e899dSKrzysztof Grobelny     return "";
178479e899dSKrzysztof Grobelny }
179479e899dSKrzysztof Grobelny 
180479e899dSKrzysztof Grobelny inline metric_report_definition::ReportUpdatesEnum
181479e899dSKrzysztof Grobelny     toRedfishReportUpdates(std::string_view dbusValue)
182479e899dSKrzysztof Grobelny {
183479e899dSKrzysztof Grobelny     if (dbusValue ==
184479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.ReportUpdates.Overwrite")
185479e899dSKrzysztof Grobelny     {
186479e899dSKrzysztof Grobelny         return metric_report_definition::ReportUpdatesEnum::Overwrite;
187479e899dSKrzysztof Grobelny     }
188479e899dSKrzysztof Grobelny     if (dbusValue ==
189479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.ReportUpdates.AppendWrapsWhenFull")
190479e899dSKrzysztof Grobelny     {
191479e899dSKrzysztof Grobelny         return metric_report_definition::ReportUpdatesEnum::AppendWrapsWhenFull;
192479e899dSKrzysztof Grobelny     }
193479e899dSKrzysztof Grobelny     if (dbusValue ==
194479e899dSKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report.ReportUpdates.AppendStopsWhenFull")
195479e899dSKrzysztof Grobelny     {
196479e899dSKrzysztof Grobelny         return metric_report_definition::ReportUpdatesEnum::AppendStopsWhenFull;
197479e899dSKrzysztof Grobelny     }
198479e899dSKrzysztof Grobelny     return metric_report_definition::ReportUpdatesEnum::Invalid;
199479e899dSKrzysztof Grobelny }
200479e899dSKrzysztof Grobelny 
201479e899dSKrzysztof Grobelny inline std::string toDbusReportUpdates(std::string_view redfishValue)
202479e899dSKrzysztof Grobelny {
203479e899dSKrzysztof Grobelny     if (redfishValue == "Overwrite")
204479e899dSKrzysztof Grobelny     {
205479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.ReportUpdates.Overwrite";
206479e899dSKrzysztof Grobelny     }
207479e899dSKrzysztof Grobelny     if (redfishValue == "AppendWrapsWhenFull")
208479e899dSKrzysztof Grobelny     {
209479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.ReportUpdates.AppendWrapsWhenFull";
210479e899dSKrzysztof Grobelny     }
211479e899dSKrzysztof Grobelny     if (redfishValue == "AppendStopsWhenFull")
212479e899dSKrzysztof Grobelny     {
213479e899dSKrzysztof Grobelny         return "xyz.openbmc_project.Telemetry.Report.ReportUpdates.AppendStopsWhenFull";
214479e899dSKrzysztof Grobelny     }
215479e899dSKrzysztof Grobelny     return "";
216479e899dSKrzysztof Grobelny }
217081ebf06SWludzik, Jozef 
218f19ab44aSSzymon Dompke inline std::optional<nlohmann::json::array_t> getLinkedTriggers(
219f19ab44aSSzymon Dompke     std::span<const sdbusplus::message::object_path> triggerPaths)
220f19ab44aSSzymon Dompke {
221f19ab44aSSzymon Dompke     nlohmann::json::array_t triggers;
222f19ab44aSSzymon Dompke 
223f19ab44aSSzymon Dompke     for (const sdbusplus::message::object_path& path : triggerPaths)
224f19ab44aSSzymon Dompke     {
225f19ab44aSSzymon Dompke         if (path.parent_path() !=
226f19ab44aSSzymon Dompke             "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService")
227f19ab44aSSzymon Dompke         {
22862598e31SEd Tanous             BMCWEB_LOG_ERROR("Property Triggers contains invalid value: {}",
22962598e31SEd Tanous                              path.str);
230f19ab44aSSzymon Dompke             return std::nullopt;
231f19ab44aSSzymon Dompke         }
232f19ab44aSSzymon Dompke 
233f19ab44aSSzymon Dompke         std::string id = path.filename();
234f19ab44aSSzymon Dompke         if (id.empty())
235f19ab44aSSzymon Dompke         {
23662598e31SEd Tanous             BMCWEB_LOG_ERROR("Property Triggers contains invalid value: {}",
23762598e31SEd Tanous                              path.str);
238f19ab44aSSzymon Dompke             return std::nullopt;
239f19ab44aSSzymon Dompke         }
240f19ab44aSSzymon Dompke         nlohmann::json::object_t trigger;
241f19ab44aSSzymon Dompke         trigger["@odata.id"] =
242f19ab44aSSzymon Dompke             boost::urls::format("/redfish/v1/TelemetryService/Triggers/{}", id);
243f19ab44aSSzymon Dompke         triggers.emplace_back(std::move(trigger));
244f19ab44aSSzymon Dompke     }
245f19ab44aSSzymon Dompke 
246f19ab44aSSzymon Dompke     return triggers;
247f19ab44aSSzymon Dompke }
248f19ab44aSSzymon Dompke 
249bd79bce8SPatrick Williams inline void fillReportDefinition(
250bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id,
251479e899dSKrzysztof Grobelny     const dbus::utility::DBusPropertiesMap& properties)
252081ebf06SWludzik, Jozef {
253479e899dSKrzysztof Grobelny     std::vector<std::string> reportActions;
254479e899dSKrzysztof Grobelny     ReadingParameters readingParams;
255479e899dSKrzysztof Grobelny     std::string reportingType;
256479e899dSKrzysztof Grobelny     std::string reportUpdates;
257479e899dSKrzysztof Grobelny     std::string name;
258479e899dSKrzysztof Grobelny     uint64_t appendLimit = 0;
259479e899dSKrzysztof Grobelny     uint64_t interval = 0;
260479e899dSKrzysztof Grobelny     bool enabled = false;
261f19ab44aSSzymon Dompke     std::vector<sdbusplus::message::object_path> triggers;
26289474494SKrzysztof Grobelny 
26389474494SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
264479e899dSKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), properties, "ReportingType",
265479e899dSKrzysztof Grobelny         reportingType, "Interval", interval, "ReportActions", reportActions,
266479e899dSKrzysztof Grobelny         "ReportUpdates", reportUpdates, "AppendLimit", appendLimit,
267f19ab44aSSzymon Dompke         "ReadingParameters", readingParams, "Name", name, "Enabled", enabled,
268f19ab44aSSzymon Dompke         "Triggers", triggers);
26989474494SKrzysztof Grobelny 
27089474494SKrzysztof Grobelny     if (!success)
271081ebf06SWludzik, Jozef     {
272081ebf06SWludzik, Jozef         messages::internalError(asyncResp->res);
273081ebf06SWludzik, Jozef         return;
274081ebf06SWludzik, Jozef     }
275081ebf06SWludzik, Jozef 
276479e899dSKrzysztof Grobelny     metric_report_definition::MetricReportDefinitionType redfishReportingType =
277479e899dSKrzysztof Grobelny         toRedfishReportingType(reportingType);
278479e899dSKrzysztof Grobelny     if (redfishReportingType ==
279479e899dSKrzysztof Grobelny         metric_report_definition::MetricReportDefinitionType::Invalid)
280081ebf06SWludzik, Jozef     {
281479e899dSKrzysztof Grobelny         messages::internalError(asyncResp->res);
282479e899dSKrzysztof Grobelny         return;
283081ebf06SWludzik, Jozef     }
28489474494SKrzysztof Grobelny 
285479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["MetricReportDefinitionType"] =
286479e899dSKrzysztof Grobelny         redfishReportingType;
287479e899dSKrzysztof Grobelny 
288f19ab44aSSzymon Dompke     std::optional<nlohmann::json::array_t> linkedTriggers =
289f19ab44aSSzymon Dompke         getLinkedTriggers(triggers);
290f19ab44aSSzymon Dompke     if (!linkedTriggers)
291f19ab44aSSzymon Dompke     {
292f19ab44aSSzymon Dompke         messages::internalError(asyncResp->res);
293f19ab44aSSzymon Dompke         return;
294f19ab44aSSzymon Dompke     }
295f19ab44aSSzymon Dompke 
296f19ab44aSSzymon Dompke     asyncResp->res.jsonValue["Links"]["Triggers"] = std::move(*linkedTriggers);
297f19ab44aSSzymon Dompke 
298479e899dSKrzysztof Grobelny     nlohmann::json::array_t redfishReportActions;
299479e899dSKrzysztof Grobelny     for (const std::string& action : reportActions)
300081ebf06SWludzik, Jozef     {
301479e899dSKrzysztof Grobelny         metric_report_definition::ReportActionsEnum redfishAction =
302479e899dSKrzysztof Grobelny             toRedfishReportAction(action);
303479e899dSKrzysztof Grobelny         if (redfishAction ==
304479e899dSKrzysztof Grobelny             metric_report_definition::ReportActionsEnum::Invalid)
305479e899dSKrzysztof Grobelny         {
306479e899dSKrzysztof Grobelny             messages::internalError(asyncResp->res);
307479e899dSKrzysztof Grobelny             return;
308081ebf06SWludzik, Jozef         }
309081ebf06SWludzik, Jozef 
310479e899dSKrzysztof Grobelny         redfishReportActions.emplace_back(redfishAction);
311479e899dSKrzysztof Grobelny     }
312479e899dSKrzysztof Grobelny 
313479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["ReportActions"] = std::move(redfishReportActions);
314479e899dSKrzysztof Grobelny 
315479e899dSKrzysztof Grobelny     nlohmann::json::array_t metrics = nlohmann::json::array();
316479e899dSKrzysztof Grobelny     for (const auto& [sensorData, collectionFunction, collectionTimeScope,
317479e899dSKrzysztof Grobelny                       collectionDuration] : readingParams)
31889474494SKrzysztof Grobelny     {
319479e899dSKrzysztof Grobelny         nlohmann::json::array_t metricProperties;
320479e899dSKrzysztof Grobelny 
321479e899dSKrzysztof Grobelny         for (const auto& [sensorPath, sensorMetadata] : sensorData)
322081ebf06SWludzik, Jozef         {
323479e899dSKrzysztof Grobelny             metricProperties.emplace_back(sensorMetadata);
324479e899dSKrzysztof Grobelny         }
325479e899dSKrzysztof Grobelny 
326613dabeaSEd Tanous         nlohmann::json::object_t metric;
327479e899dSKrzysztof Grobelny 
328479e899dSKrzysztof Grobelny         metric_report_definition::CalculationAlgorithmEnum
329479e899dSKrzysztof Grobelny             redfishCollectionFunction =
330479e899dSKrzysztof Grobelny                 telemetry::toRedfishCollectionFunction(collectionFunction);
331479e899dSKrzysztof Grobelny         if (redfishCollectionFunction ==
332479e899dSKrzysztof Grobelny             metric_report_definition::CalculationAlgorithmEnum::Invalid)
333479e899dSKrzysztof Grobelny         {
334479e899dSKrzysztof Grobelny             messages::internalError(asyncResp->res);
335479e899dSKrzysztof Grobelny             return;
336479e899dSKrzysztof Grobelny         }
337479e899dSKrzysztof Grobelny         metric["CollectionFunction"] = redfishCollectionFunction;
338479e899dSKrzysztof Grobelny 
339479e899dSKrzysztof Grobelny         metric_report_definition::CollectionTimeScope
340479e899dSKrzysztof Grobelny             redfishCollectionTimeScope =
341479e899dSKrzysztof Grobelny                 toRedfishCollectionTimeScope(collectionTimeScope);
342479e899dSKrzysztof Grobelny         if (redfishCollectionTimeScope ==
343479e899dSKrzysztof Grobelny             metric_report_definition::CollectionTimeScope::Invalid)
344479e899dSKrzysztof Grobelny         {
345479e899dSKrzysztof Grobelny             messages::internalError(asyncResp->res);
346479e899dSKrzysztof Grobelny             return;
347479e899dSKrzysztof Grobelny         }
348479e899dSKrzysztof Grobelny         metric["CollectionTimeScope"] = redfishCollectionTimeScope;
349479e899dSKrzysztof Grobelny 
350479e899dSKrzysztof Grobelny         metric["MetricProperties"] = std::move(metricProperties);
351479e899dSKrzysztof Grobelny         metric["CollectionDuration"] = time_utils::toDurationString(
352479e899dSKrzysztof Grobelny             std::chrono::milliseconds(collectionDuration));
353b2ba3072SPatrick Williams         metrics.emplace_back(std::move(metric));
354081ebf06SWludzik, Jozef     }
355479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["Metrics"] = std::move(metrics);
356479e899dSKrzysztof Grobelny 
357479e899dSKrzysztof Grobelny     if (enabled)
358479e899dSKrzysztof Grobelny     {
359539d8c6bSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
360479e899dSKrzysztof Grobelny     }
361479e899dSKrzysztof Grobelny     else
362479e899dSKrzysztof Grobelny     {
363539d8c6bSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = resource::State::Disabled;
36489474494SKrzysztof Grobelny     }
36589474494SKrzysztof Grobelny 
366479e899dSKrzysztof Grobelny     metric_report_definition::ReportUpdatesEnum redfishReportUpdates =
367479e899dSKrzysztof Grobelny         toRedfishReportUpdates(reportUpdates);
368479e899dSKrzysztof Grobelny     if (redfishReportUpdates ==
369479e899dSKrzysztof Grobelny         metric_report_definition::ReportUpdatesEnum::Invalid)
37089474494SKrzysztof Grobelny     {
371479e899dSKrzysztof Grobelny         messages::internalError(asyncResp->res);
372479e899dSKrzysztof Grobelny         return;
37389474494SKrzysztof Grobelny     }
374479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["ReportUpdates"] = redfishReportUpdates;
37589474494SKrzysztof Grobelny 
376479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["MetricReportDefinitionEnabled"] = enabled;
377479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["AppendLimit"] = appendLimit;
378479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["Name"] = name;
379081ebf06SWludzik, Jozef     asyncResp->res.jsonValue["Schedule"]["RecurrenceInterval"] =
380479e899dSKrzysztof Grobelny         time_utils::toDurationString(std::chrono::milliseconds(interval));
381479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["@odata.type"] =
382479e899dSKrzysztof Grobelny         "#MetricReportDefinition.v1_3_0.MetricReportDefinition";
383479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
384479e899dSKrzysztof Grobelny         "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", id);
385479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["Id"] = id;
386479e899dSKrzysztof Grobelny     asyncResp->res.jsonValue["MetricReport"]["@odata.id"] = boost::urls::format(
387479e899dSKrzysztof Grobelny         "/redfish/v1/TelemetryService/MetricReports/{}", id);
38889474494SKrzysztof Grobelny }
38989474494SKrzysztof Grobelny 
3904dbb8aeaSWludzik, Jozef struct AddReportArgs
3914dbb8aeaSWludzik, Jozef {
392479e899dSKrzysztof Grobelny     struct MetricArgs
393479e899dSKrzysztof Grobelny     {
394479e899dSKrzysztof Grobelny         std::vector<std::string> uris;
395479e899dSKrzysztof Grobelny         std::string collectionFunction;
396479e899dSKrzysztof Grobelny         std::string collectionTimeScope;
397479e899dSKrzysztof Grobelny         uint64_t collectionDuration = 0;
398479e899dSKrzysztof Grobelny     };
399479e899dSKrzysztof Grobelny 
400479e899dSKrzysztof Grobelny     std::string id;
4014dbb8aeaSWludzik, Jozef     std::string name;
4024dbb8aeaSWludzik, Jozef     std::string reportingType;
403479e899dSKrzysztof Grobelny     std::string reportUpdates;
404479e899dSKrzysztof Grobelny     uint64_t appendLimit = std::numeric_limits<uint64_t>::max();
405479e899dSKrzysztof Grobelny     std::vector<std::string> reportActions;
406479e899dSKrzysztof Grobelny     uint64_t interval = std::numeric_limits<uint64_t>::max();
407479e899dSKrzysztof Grobelny     std::vector<MetricArgs> metrics;
408479e899dSKrzysztof Grobelny     bool metricReportDefinitionEnabled = true;
4094dbb8aeaSWludzik, Jozef };
4104dbb8aeaSWludzik, Jozef 
4114dbb8aeaSWludzik, Jozef inline bool toDbusReportActions(crow::Response& res,
412479e899dSKrzysztof Grobelny                                 const std::vector<std::string>& actions,
4139e6c388aSLukasz Kazmierczak                                 std::vector<std::string>& outReportActions)
4144dbb8aeaSWludzik, Jozef {
4154dbb8aeaSWludzik, Jozef     size_t index = 0;
416479e899dSKrzysztof Grobelny     for (const std::string& action : actions)
4174dbb8aeaSWludzik, Jozef     {
418479e899dSKrzysztof Grobelny         std::string dbusReportAction = toDbusReportAction(action);
419479e899dSKrzysztof Grobelny         if (dbusReportAction.empty())
4204dbb8aeaSWludzik, Jozef         {
4219e6c388aSLukasz Kazmierczak             messages::propertyValueNotInList(
4229e6c388aSLukasz Kazmierczak                 res, action, "ReportActions/" + std::to_string(index));
4234dbb8aeaSWludzik, Jozef             return false;
4244dbb8aeaSWludzik, Jozef         }
425479e899dSKrzysztof Grobelny 
4269e6c388aSLukasz Kazmierczak         outReportActions.emplace_back(std::move(dbusReportAction));
4274dbb8aeaSWludzik, Jozef         index++;
4284dbb8aeaSWludzik, Jozef     }
4294dbb8aeaSWludzik, Jozef     return true;
4304dbb8aeaSWludzik, Jozef }
4314dbb8aeaSWludzik, Jozef 
432b14f357fSEd Tanous inline bool getUserMetric(crow::Response& res, nlohmann::json::object_t& metric,
433479e899dSKrzysztof Grobelny                           AddReportArgs::MetricArgs& metricArgs)
434479e899dSKrzysztof Grobelny {
435479e899dSKrzysztof Grobelny     std::optional<std::vector<std::string>> uris;
436479e899dSKrzysztof Grobelny     std::optional<std::string> collectionDurationStr;
437479e899dSKrzysztof Grobelny     std::optional<std::string> collectionFunction;
438479e899dSKrzysztof Grobelny     std::optional<std::string> collectionTimeScopeStr;
439479e899dSKrzysztof Grobelny 
440afc474aeSMyung Bae     if (!json_util::readJsonObject( //
441afc474aeSMyung Bae             metric, res, //
442afc474aeSMyung Bae             "CollectionDuration", collectionDurationStr, //
443afc474aeSMyung Bae             "CollectionFunction", collectionFunction, //
444afc474aeSMyung Bae             "CollectionTimeScope", collectionTimeScopeStr, //
445afc474aeSMyung Bae             "MetricProperties", uris //
446afc474aeSMyung Bae             ))
447479e899dSKrzysztof Grobelny     {
448479e899dSKrzysztof Grobelny         return false;
449479e899dSKrzysztof Grobelny     }
450479e899dSKrzysztof Grobelny 
451479e899dSKrzysztof Grobelny     if (uris)
452479e899dSKrzysztof Grobelny     {
453479e899dSKrzysztof Grobelny         metricArgs.uris = std::move(*uris);
454479e899dSKrzysztof Grobelny     }
455479e899dSKrzysztof Grobelny 
456479e899dSKrzysztof Grobelny     if (collectionFunction)
457479e899dSKrzysztof Grobelny     {
458479e899dSKrzysztof Grobelny         std::string dbusCollectionFunction =
459479e899dSKrzysztof Grobelny             telemetry::toDbusCollectionFunction(*collectionFunction);
460479e899dSKrzysztof Grobelny         if (dbusCollectionFunction.empty())
461479e899dSKrzysztof Grobelny         {
462479e899dSKrzysztof Grobelny             messages::propertyValueIncorrect(res, "CollectionFunction",
463479e899dSKrzysztof Grobelny                                              *collectionFunction);
464479e899dSKrzysztof Grobelny             return false;
465479e899dSKrzysztof Grobelny         }
466479e899dSKrzysztof Grobelny         metricArgs.collectionFunction = std::move(dbusCollectionFunction);
467479e899dSKrzysztof Grobelny     }
468479e899dSKrzysztof Grobelny 
469479e899dSKrzysztof Grobelny     if (collectionTimeScopeStr)
470479e899dSKrzysztof Grobelny     {
471479e899dSKrzysztof Grobelny         std::string dbusCollectionTimeScope =
472479e899dSKrzysztof Grobelny             toDbusCollectionTimeScope(*collectionTimeScopeStr);
473479e899dSKrzysztof Grobelny         if (dbusCollectionTimeScope.empty())
474479e899dSKrzysztof Grobelny         {
475479e899dSKrzysztof Grobelny             messages::propertyValueIncorrect(res, "CollectionTimeScope",
476479e899dSKrzysztof Grobelny                                              *collectionTimeScopeStr);
477479e899dSKrzysztof Grobelny             return false;
478479e899dSKrzysztof Grobelny         }
479479e899dSKrzysztof Grobelny         metricArgs.collectionTimeScope = std::move(dbusCollectionTimeScope);
480479e899dSKrzysztof Grobelny     }
481479e899dSKrzysztof Grobelny 
482479e899dSKrzysztof Grobelny     if (collectionDurationStr)
483479e899dSKrzysztof Grobelny     {
484479e899dSKrzysztof Grobelny         std::optional<std::chrono::milliseconds> duration =
485479e899dSKrzysztof Grobelny             time_utils::fromDurationString(*collectionDurationStr);
486479e899dSKrzysztof Grobelny 
487479e899dSKrzysztof Grobelny         if (!duration || duration->count() < 0)
488479e899dSKrzysztof Grobelny         {
489479e899dSKrzysztof Grobelny             messages::propertyValueIncorrect(res, "CollectionDuration",
490479e899dSKrzysztof Grobelny                                              *collectionDurationStr);
491479e899dSKrzysztof Grobelny             return false;
492479e899dSKrzysztof Grobelny         }
493479e899dSKrzysztof Grobelny 
494479e899dSKrzysztof Grobelny         metricArgs.collectionDuration =
495479e899dSKrzysztof Grobelny             static_cast<uint64_t>(duration->count());
496479e899dSKrzysztof Grobelny     }
497479e899dSKrzysztof Grobelny 
498479e899dSKrzysztof Grobelny     return true;
499479e899dSKrzysztof Grobelny }
500479e899dSKrzysztof Grobelny 
501479e899dSKrzysztof Grobelny inline bool getUserMetrics(crow::Response& res,
502b14f357fSEd Tanous                            std::span<nlohmann::json::object_t> metrics,
503479e899dSKrzysztof Grobelny                            std::vector<AddReportArgs::MetricArgs>& result)
504479e899dSKrzysztof Grobelny {
505479e899dSKrzysztof Grobelny     result.reserve(metrics.size());
506479e899dSKrzysztof Grobelny 
507b14f357fSEd Tanous     for (nlohmann::json::object_t& m : metrics)
508479e899dSKrzysztof Grobelny     {
509479e899dSKrzysztof Grobelny         AddReportArgs::MetricArgs metricArgs;
510479e899dSKrzysztof Grobelny 
511479e899dSKrzysztof Grobelny         if (!getUserMetric(res, m, metricArgs))
512479e899dSKrzysztof Grobelny         {
513479e899dSKrzysztof Grobelny             return false;
514479e899dSKrzysztof Grobelny         }
515479e899dSKrzysztof Grobelny 
516479e899dSKrzysztof Grobelny         result.emplace_back(std::move(metricArgs));
517479e899dSKrzysztof Grobelny     }
518479e899dSKrzysztof Grobelny 
519479e899dSKrzysztof Grobelny     return true;
520479e899dSKrzysztof Grobelny }
521479e899dSKrzysztof Grobelny 
5224dbb8aeaSWludzik, Jozef inline bool getUserParameters(crow::Response& res, const crow::Request& req,
5234dbb8aeaSWludzik, Jozef                               AddReportArgs& args)
5244dbb8aeaSWludzik, Jozef {
525479e899dSKrzysztof Grobelny     std::optional<std::string> id;
526479e899dSKrzysztof Grobelny     std::optional<std::string> name;
527479e899dSKrzysztof Grobelny     std::optional<std::string> reportingTypeStr;
528479e899dSKrzysztof Grobelny     std::optional<std::string> reportUpdatesStr;
529479e899dSKrzysztof Grobelny     std::optional<uint64_t> appendLimit;
530479e899dSKrzysztof Grobelny     std::optional<bool> metricReportDefinitionEnabled;
531b14f357fSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> metrics;
532479e899dSKrzysztof Grobelny     std::optional<std::vector<std::string>> reportActionsStr;
533b14f357fSEd Tanous     std::optional<std::string> scheduleDurationStr;
534479e899dSKrzysztof Grobelny 
535afc474aeSMyung Bae     if (!json_util::readJsonPatch( //
536afc474aeSMyung Bae             req, res, //
537afc474aeSMyung Bae             "AppendLimit", appendLimit, //
538afc474aeSMyung Bae             "Id", id, //
539afc474aeSMyung Bae             "MetricReportDefinitionEnabled", metricReportDefinitionEnabled, //
540afc474aeSMyung Bae             "MetricReportDefinitionType", reportingTypeStr, //
541afc474aeSMyung Bae             "Metrics", metrics, //
542afc474aeSMyung Bae             "Name", name, //
543afc474aeSMyung Bae             "ReportActions", reportActionsStr, //
544afc474aeSMyung Bae             "ReportUpdates", reportUpdatesStr, //
545afc474aeSMyung Bae             "Schedule/RecurrenceInterval", scheduleDurationStr //
546afc474aeSMyung Bae             ))
5474dbb8aeaSWludzik, Jozef     {
5484dbb8aeaSWludzik, Jozef         return false;
5494dbb8aeaSWludzik, Jozef     }
5504dbb8aeaSWludzik, Jozef 
551479e899dSKrzysztof Grobelny     if (id)
552479e899dSKrzysztof Grobelny     {
553479e899dSKrzysztof Grobelny         constexpr const char* allowedCharactersInId =
5544dbb8aeaSWludzik, Jozef             "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
555479e899dSKrzysztof Grobelny         if (id->empty() ||
556479e899dSKrzysztof Grobelny             id->find_first_not_of(allowedCharactersInId) != std::string::npos)
5574dbb8aeaSWludzik, Jozef         {
558479e899dSKrzysztof Grobelny             messages::propertyValueIncorrect(res, "Id", *id);
5594dbb8aeaSWludzik, Jozef             return false;
5604dbb8aeaSWludzik, Jozef         }
561479e899dSKrzysztof Grobelny         args.id = *id;
562479e899dSKrzysztof Grobelny     }
5634dbb8aeaSWludzik, Jozef 
564479e899dSKrzysztof Grobelny     if (name)
5654dbb8aeaSWludzik, Jozef     {
566479e899dSKrzysztof Grobelny         args.name = *name;
567479e899dSKrzysztof Grobelny     }
568479e899dSKrzysztof Grobelny 
569479e899dSKrzysztof Grobelny     if (reportingTypeStr)
570479e899dSKrzysztof Grobelny     {
571479e899dSKrzysztof Grobelny         std::string dbusReportingType = toDbusReportingType(*reportingTypeStr);
572479e899dSKrzysztof Grobelny         if (dbusReportingType.empty())
573479e899dSKrzysztof Grobelny         {
574479e899dSKrzysztof Grobelny             messages::propertyValueNotInList(res, *reportingTypeStr,
5754dbb8aeaSWludzik, Jozef                                              "MetricReportDefinitionType");
5764dbb8aeaSWludzik, Jozef             return false;
5774dbb8aeaSWludzik, Jozef         }
578479e899dSKrzysztof Grobelny         args.reportingType = dbusReportingType;
579479e899dSKrzysztof Grobelny     }
5804dbb8aeaSWludzik, Jozef 
581479e899dSKrzysztof Grobelny     if (reportUpdatesStr)
582479e899dSKrzysztof Grobelny     {
583479e899dSKrzysztof Grobelny         std::string dbusReportUpdates = toDbusReportUpdates(*reportUpdatesStr);
584479e899dSKrzysztof Grobelny         if (dbusReportUpdates.empty())
585479e899dSKrzysztof Grobelny         {
586479e899dSKrzysztof Grobelny             messages::propertyValueNotInList(res, *reportUpdatesStr,
587479e899dSKrzysztof Grobelny                                              "ReportUpdates");
588479e899dSKrzysztof Grobelny             return false;
589479e899dSKrzysztof Grobelny         }
590479e899dSKrzysztof Grobelny         args.reportUpdates = dbusReportUpdates;
591479e899dSKrzysztof Grobelny     }
592479e899dSKrzysztof Grobelny 
593479e899dSKrzysztof Grobelny     if (appendLimit)
594479e899dSKrzysztof Grobelny     {
595479e899dSKrzysztof Grobelny         args.appendLimit = *appendLimit;
596479e899dSKrzysztof Grobelny     }
597479e899dSKrzysztof Grobelny 
598479e899dSKrzysztof Grobelny     if (metricReportDefinitionEnabled)
599479e899dSKrzysztof Grobelny     {
600479e899dSKrzysztof Grobelny         args.metricReportDefinitionEnabled = *metricReportDefinitionEnabled;
601479e899dSKrzysztof Grobelny     }
602479e899dSKrzysztof Grobelny 
603479e899dSKrzysztof Grobelny     if (reportActionsStr)
604479e899dSKrzysztof Grobelny     {
6059e6c388aSLukasz Kazmierczak         if (!toDbusReportActions(res, *reportActionsStr, args.reportActions))
6064dbb8aeaSWludzik, Jozef         {
6074dbb8aeaSWludzik, Jozef             return false;
6084dbb8aeaSWludzik, Jozef         }
609479e899dSKrzysztof Grobelny     }
6104dbb8aeaSWludzik, Jozef 
611479e899dSKrzysztof Grobelny     if (reportingTypeStr == "Periodic")
6124dbb8aeaSWludzik, Jozef     {
613b14f357fSEd Tanous         if (!scheduleDurationStr)
6144dbb8aeaSWludzik, Jozef         {
6154dbb8aeaSWludzik, Jozef             messages::createFailedMissingReqProperties(res, "Schedule");
6164dbb8aeaSWludzik, Jozef             return false;
6174dbb8aeaSWludzik, Jozef         }
6184dbb8aeaSWludzik, Jozef 
6194dbb8aeaSWludzik, Jozef         std::optional<std::chrono::milliseconds> durationNum =
620b14f357fSEd Tanous             time_utils::fromDurationString(*scheduleDurationStr);
621479e899dSKrzysztof Grobelny         if (!durationNum || durationNum->count() < 0)
6224dbb8aeaSWludzik, Jozef         {
6234dbb8aeaSWludzik, Jozef             messages::propertyValueIncorrect(res, "RecurrenceInterval",
624b14f357fSEd Tanous                                              *scheduleDurationStr);
6254dbb8aeaSWludzik, Jozef             return false;
6264dbb8aeaSWludzik, Jozef         }
6274dbb8aeaSWludzik, Jozef         args.interval = static_cast<uint64_t>(durationNum->count());
6284dbb8aeaSWludzik, Jozef     }
6294dbb8aeaSWludzik, Jozef 
630479e899dSKrzysztof Grobelny     if (metrics)
6314dbb8aeaSWludzik, Jozef     {
632479e899dSKrzysztof Grobelny         if (!getUserMetrics(res, *metrics, args.metrics))
6334dbb8aeaSWludzik, Jozef         {
6344dbb8aeaSWludzik, Jozef             return false;
6354dbb8aeaSWludzik, Jozef         }
6364dbb8aeaSWludzik, Jozef     }
6374dbb8aeaSWludzik, Jozef 
6384dbb8aeaSWludzik, Jozef     return true;
6394dbb8aeaSWludzik, Jozef }
6404dbb8aeaSWludzik, Jozef 
641ca1600c1SSzymon Dompke inline bool getChassisSensorNodeFromMetrics(
6428d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
643479e899dSKrzysztof Grobelny     std::span<const AddReportArgs::MetricArgs> metrics,
6444dbb8aeaSWludzik, Jozef     boost::container::flat_set<std::pair<std::string, std::string>>& matched)
6454dbb8aeaSWludzik, Jozef {
646ca1600c1SSzymon Dompke     for (const auto& metric : metrics)
6474dbb8aeaSWludzik, Jozef     {
648479e899dSKrzysztof Grobelny         std::optional<IncorrectMetricUri> error =
649479e899dSKrzysztof Grobelny             getChassisSensorNode(metric.uris, matched);
650ca1600c1SSzymon Dompke         if (error)
6514dbb8aeaSWludzik, Jozef         {
652bd79bce8SPatrick Williams             messages::propertyValueIncorrect(
653bd79bce8SPatrick Williams                 asyncResp->res, error->uri,
654bd79bce8SPatrick Williams                 "MetricProperties/" + std::to_string(error->index));
6554dbb8aeaSWludzik, Jozef             return false;
6564dbb8aeaSWludzik, Jozef         }
6574dbb8aeaSWludzik, Jozef     }
6584dbb8aeaSWludzik, Jozef     return true;
6594dbb8aeaSWludzik, Jozef }
6604dbb8aeaSWludzik, Jozef 
661ba498310SKrzysztof Grobelny inline std::string toRedfishProperty(std::string_view dbusMessage)
662ba498310SKrzysztof Grobelny {
663ba498310SKrzysztof Grobelny     if (dbusMessage == "Id")
664ba498310SKrzysztof Grobelny     {
665ba498310SKrzysztof Grobelny         return "Id";
666ba498310SKrzysztof Grobelny     }
667ba498310SKrzysztof Grobelny     if (dbusMessage == "Name")
668ba498310SKrzysztof Grobelny     {
669ba498310SKrzysztof Grobelny         return "Name";
670ba498310SKrzysztof Grobelny     }
671ba498310SKrzysztof Grobelny     if (dbusMessage == "ReportingType")
672ba498310SKrzysztof Grobelny     {
673ba498310SKrzysztof Grobelny         return "MetricReportDefinitionType";
674ba498310SKrzysztof Grobelny     }
675ba498310SKrzysztof Grobelny     if (dbusMessage == "AppendLimit")
676ba498310SKrzysztof Grobelny     {
677ba498310SKrzysztof Grobelny         return "AppendLimit";
678ba498310SKrzysztof Grobelny     }
679ba498310SKrzysztof Grobelny     if (dbusMessage == "ReportActions")
680ba498310SKrzysztof Grobelny     {
681ba498310SKrzysztof Grobelny         return "ReportActions";
682ba498310SKrzysztof Grobelny     }
683ba498310SKrzysztof Grobelny     if (dbusMessage == "Interval")
684ba498310SKrzysztof Grobelny     {
685ba498310SKrzysztof Grobelny         return "RecurrenceInterval";
686ba498310SKrzysztof Grobelny     }
687ba498310SKrzysztof Grobelny     if (dbusMessage == "ReportUpdates")
688ba498310SKrzysztof Grobelny     {
689ba498310SKrzysztof Grobelny         return "ReportUpdates";
690ba498310SKrzysztof Grobelny     }
691ba498310SKrzysztof Grobelny     if (dbusMessage == "ReadingParameters")
692ba498310SKrzysztof Grobelny     {
693ba498310SKrzysztof Grobelny         return "Metrics";
694ba498310SKrzysztof Grobelny     }
695ba498310SKrzysztof Grobelny     return "";
696ba498310SKrzysztof Grobelny }
697ba498310SKrzysztof Grobelny 
698ba498310SKrzysztof Grobelny inline bool handleParamError(crow::Response& res, const char* errorMessage,
699ba498310SKrzysztof Grobelny                              std::string_view key)
700ba498310SKrzysztof Grobelny {
701ba498310SKrzysztof Grobelny     if (errorMessage == nullptr)
702ba498310SKrzysztof Grobelny     {
703ba498310SKrzysztof Grobelny         BMCWEB_LOG_ERROR("errorMessage was null");
704ba498310SKrzysztof Grobelny         return true;
705ba498310SKrzysztof Grobelny     }
706ba498310SKrzysztof Grobelny     std::string_view errorMessageSv(errorMessage);
707ba498310SKrzysztof Grobelny     if (errorMessageSv.starts_with(key))
708ba498310SKrzysztof Grobelny     {
709ba498310SKrzysztof Grobelny         std::string redfishProperty = toRedfishProperty(key);
710ba498310SKrzysztof Grobelny         if (redfishProperty.empty())
711ba498310SKrzysztof Grobelny         {
712ba498310SKrzysztof Grobelny             // Getting here means most possibly that toRedfishProperty has
713ba498310SKrzysztof Grobelny             // incomplete implementation. Return internal error for now.
714ba498310SKrzysztof Grobelny             BMCWEB_LOG_ERROR("{} has no corresponding Redfish property", key);
715ba498310SKrzysztof Grobelny             messages::internalError(res);
716ba498310SKrzysztof Grobelny             return false;
717ba498310SKrzysztof Grobelny         }
718ba498310SKrzysztof Grobelny         messages::propertyValueError(res, redfishProperty);
719ba498310SKrzysztof Grobelny         return false;
720ba498310SKrzysztof Grobelny     }
721ba498310SKrzysztof Grobelny 
722ba498310SKrzysztof Grobelny     return true;
723ba498310SKrzysztof Grobelny }
724ba498310SKrzysztof Grobelny 
725ba498310SKrzysztof Grobelny inline void afterAddReport(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
726ba498310SKrzysztof Grobelny                            const AddReportArgs& args,
727ba498310SKrzysztof Grobelny                            const boost::system::error_code& ec,
728ba498310SKrzysztof Grobelny                            const sdbusplus::message_t& msg)
729ba498310SKrzysztof Grobelny {
730ba498310SKrzysztof Grobelny     if (!ec)
731ba498310SKrzysztof Grobelny     {
732ba498310SKrzysztof Grobelny         messages::created(asyncResp->res);
733ba498310SKrzysztof Grobelny         return;
734ba498310SKrzysztof Grobelny     }
735ba498310SKrzysztof Grobelny 
736ba498310SKrzysztof Grobelny     if (ec == boost::system::errc::invalid_argument)
737ba498310SKrzysztof Grobelny     {
738ba498310SKrzysztof Grobelny         const sd_bus_error* errorMessage = msg.get_error();
739ba498310SKrzysztof Grobelny         if (errorMessage != nullptr)
740ba498310SKrzysztof Grobelny         {
741ba498310SKrzysztof Grobelny             for (const auto& arg :
742ba498310SKrzysztof Grobelny                  {"Id", "Name", "ReportingType", "AppendLimit", "ReportActions",
743ba498310SKrzysztof Grobelny                   "Interval", "ReportUpdates", "ReadingParameters"})
744ba498310SKrzysztof Grobelny             {
745ba498310SKrzysztof Grobelny                 if (!handleParamError(asyncResp->res, errorMessage->message,
746ba498310SKrzysztof Grobelny                                       arg))
747ba498310SKrzysztof Grobelny                 {
748ba498310SKrzysztof Grobelny                     return;
749ba498310SKrzysztof Grobelny                 }
750ba498310SKrzysztof Grobelny             }
751ba498310SKrzysztof Grobelny         }
752ba498310SKrzysztof Grobelny     }
753ba498310SKrzysztof Grobelny     if (!verifyCommonErrors(asyncResp->res, args.id, ec))
754ba498310SKrzysztof Grobelny     {
755ba498310SKrzysztof Grobelny         return;
756ba498310SKrzysztof Grobelny     }
757ba498310SKrzysztof Grobelny     messages::internalError(asyncResp->res);
758ba498310SKrzysztof Grobelny }
759ba498310SKrzysztof Grobelny 
7604dbb8aeaSWludzik, Jozef class AddReport
7614dbb8aeaSWludzik, Jozef {
7624dbb8aeaSWludzik, Jozef   public:
763891eaa7cSEd Tanous     AddReport(AddReportArgs&& argsIn,
7648a592810SEd Tanous               const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
765bd79bce8SPatrick Williams         asyncResp(asyncRespIn), args(std::move(argsIn))
7664dbb8aeaSWludzik, Jozef     {}
767479e899dSKrzysztof Grobelny 
7684dbb8aeaSWludzik, Jozef     ~AddReport()
7694dbb8aeaSWludzik, Jozef     {
770479e899dSKrzysztof Grobelny         boost::asio::post(crow::connections::systemBus->get_io_context(),
771479e899dSKrzysztof Grobelny                           std::bind_front(&performAddReport, asyncResp, args,
772479e899dSKrzysztof Grobelny                                           std::move(uriToDbus)));
773479e899dSKrzysztof Grobelny     }
774479e899dSKrzysztof Grobelny 
775479e899dSKrzysztof Grobelny     static void performAddReport(
776479e899dSKrzysztof Grobelny         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
777479e899dSKrzysztof Grobelny         const AddReportArgs& args,
778479e899dSKrzysztof Grobelny         const boost::container::flat_map<std::string, std::string>& uriToDbus)
779479e899dSKrzysztof Grobelny     {
7804dbb8aeaSWludzik, Jozef         if (asyncResp->res.result() != boost::beast::http::status::ok)
7814dbb8aeaSWludzik, Jozef         {
7824dbb8aeaSWludzik, Jozef             return;
7834dbb8aeaSWludzik, Jozef         }
7844dbb8aeaSWludzik, Jozef 
7854dbb8aeaSWludzik, Jozef         telemetry::ReadingParameters readingParams;
7864dbb8aeaSWludzik, Jozef         readingParams.reserve(args.metrics.size());
7874dbb8aeaSWludzik, Jozef 
788479e899dSKrzysztof Grobelny         for (const auto& metric : args.metrics)
7894dbb8aeaSWludzik, Jozef         {
790479e899dSKrzysztof Grobelny             std::vector<
791479e899dSKrzysztof Grobelny                 std::tuple<sdbusplus::message::object_path, std::string>>
792479e899dSKrzysztof Grobelny                 sensorParams;
793479e899dSKrzysztof Grobelny             sensorParams.reserve(metric.uris.size());
794479e899dSKrzysztof Grobelny 
795479e899dSKrzysztof Grobelny             for (size_t i = 0; i < metric.uris.size(); i++)
7964dbb8aeaSWludzik, Jozef             {
797479e899dSKrzysztof Grobelny                 const std::string& uri = metric.uris[i];
7984dbb8aeaSWludzik, Jozef                 auto el = uriToDbus.find(uri);
7994dbb8aeaSWludzik, Jozef                 if (el == uriToDbus.end())
8004dbb8aeaSWludzik, Jozef                 {
80162598e31SEd Tanous                     BMCWEB_LOG_ERROR(
80262598e31SEd Tanous                         "Failed to find DBus sensor corresponding to URI {}",
80362598e31SEd Tanous                         uri);
804bd79bce8SPatrick Williams                     messages::propertyValueNotInList(
805bd79bce8SPatrick Williams                         asyncResp->res, uri,
806bd79bce8SPatrick Williams                         "MetricProperties/" + std::to_string(i));
8074dbb8aeaSWludzik, Jozef                     return;
8084dbb8aeaSWludzik, Jozef                 }
8094dbb8aeaSWludzik, Jozef 
8104dbb8aeaSWludzik, Jozef                 const std::string& dbusPath = el->second;
811479e899dSKrzysztof Grobelny                 sensorParams.emplace_back(dbusPath, uri);
8124dbb8aeaSWludzik, Jozef             }
813479e899dSKrzysztof Grobelny 
814479e899dSKrzysztof Grobelny             readingParams.emplace_back(
815479e899dSKrzysztof Grobelny                 std::move(sensorParams), metric.collectionFunction,
816479e899dSKrzysztof Grobelny                 metric.collectionTimeScope, metric.collectionDuration);
8174dbb8aeaSWludzik, Jozef         }
8184dbb8aeaSWludzik, Jozef         crow::connections::systemBus->async_method_call(
819ba498310SKrzysztof Grobelny             [asyncResp, args](const boost::system::error_code& ec,
820ba498310SKrzysztof Grobelny                               const sdbusplus::message_t& msg,
821ba498310SKrzysztof Grobelny                               const std::string& /*arg1*/) {
822ba498310SKrzysztof Grobelny                 afterAddReport(asyncResp, args, ec, msg);
8234dbb8aeaSWludzik, Jozef             },
8244dbb8aeaSWludzik, Jozef             telemetry::service, "/xyz/openbmc_project/Telemetry/Reports",
8254dbb8aeaSWludzik, Jozef             "xyz.openbmc_project.Telemetry.ReportManager", "AddReport",
826479e899dSKrzysztof Grobelny             "TelemetryService/" + args.id, args.name, args.reportingType,
827479e899dSKrzysztof Grobelny             args.reportUpdates, args.appendLimit, args.reportActions,
828479e899dSKrzysztof Grobelny             args.interval, readingParams, args.metricReportDefinitionEnabled);
8294dbb8aeaSWludzik, Jozef     }
8304dbb8aeaSWludzik, Jozef 
831ecd6a3a2SEd Tanous     AddReport(const AddReport&) = delete;
832ecd6a3a2SEd Tanous     AddReport(AddReport&&) = delete;
833ecd6a3a2SEd Tanous     AddReport& operator=(const AddReport&) = delete;
834ecd6a3a2SEd Tanous     AddReport& operator=(AddReport&&) = delete;
835ecd6a3a2SEd Tanous 
836fe04d49cSNan Zhou     void insert(const std::map<std::string, std::string>& el)
8374dbb8aeaSWludzik, Jozef     {
8384dbb8aeaSWludzik, Jozef         uriToDbus.insert(el.begin(), el.end());
8394dbb8aeaSWludzik, Jozef     }
8404dbb8aeaSWludzik, Jozef 
8414dbb8aeaSWludzik, Jozef   private:
842ba498310SKrzysztof Grobelny     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
8434dbb8aeaSWludzik, Jozef     AddReportArgs args;
84447f2934cSEd Tanous     boost::container::flat_map<std::string, std::string> uriToDbus;
8454dbb8aeaSWludzik, Jozef };
8469e6c388aSLukasz Kazmierczak 
847ba498310SKrzysztof Grobelny inline std::optional<
848ba498310SKrzysztof Grobelny     std::vector<std::tuple<sdbusplus::message::object_path, std::string>>>
849ba498310SKrzysztof Grobelny     sensorPathToUri(
850ba498310SKrzysztof Grobelny         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
851ba498310SKrzysztof Grobelny         std::span<const std::string> uris,
852ba498310SKrzysztof Grobelny         const std::map<std::string, std::string>& metricPropertyToDbusPaths)
853ba498310SKrzysztof Grobelny {
854ba498310SKrzysztof Grobelny     std::vector<std::tuple<sdbusplus::message::object_path, std::string>>
855ba498310SKrzysztof Grobelny         result;
856ba498310SKrzysztof Grobelny 
857ba498310SKrzysztof Grobelny     for (const std::string& uri : uris)
858ba498310SKrzysztof Grobelny     {
859ba498310SKrzysztof Grobelny         auto it = metricPropertyToDbusPaths.find(uri);
860ba498310SKrzysztof Grobelny         if (it == metricPropertyToDbusPaths.end())
861ba498310SKrzysztof Grobelny         {
862ba498310SKrzysztof Grobelny             messages::propertyValueNotInList(asyncResp->res, uri,
863ba498310SKrzysztof Grobelny                                              "MetricProperties");
864ba498310SKrzysztof Grobelny             return {};
865ba498310SKrzysztof Grobelny         }
866ba498310SKrzysztof Grobelny         result.emplace_back(it->second, uri);
867ba498310SKrzysztof Grobelny     }
868ba498310SKrzysztof Grobelny 
869ba498310SKrzysztof Grobelny     return result;
870ba498310SKrzysztof Grobelny }
871ba498310SKrzysztof Grobelny 
872ba498310SKrzysztof Grobelny inline void afterSetReadingParams(
873ba498310SKrzysztof Grobelny     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
874ba498310SKrzysztof Grobelny     const std::string& reportId, const boost::system::error_code& ec,
875ba498310SKrzysztof Grobelny     const sdbusplus::message_t& msg)
876ba498310SKrzysztof Grobelny {
877ba498310SKrzysztof Grobelny     if (!ec)
878ba498310SKrzysztof Grobelny     {
879ba498310SKrzysztof Grobelny         messages::success(asyncResp->res);
880ba498310SKrzysztof Grobelny         return;
881ba498310SKrzysztof Grobelny     }
882ba498310SKrzysztof Grobelny     if (ec == boost::system::errc::invalid_argument)
883ba498310SKrzysztof Grobelny     {
884ba498310SKrzysztof Grobelny         const sd_bus_error* errorMessage = msg.get_error();
885ba498310SKrzysztof Grobelny         if (errorMessage != nullptr)
886ba498310SKrzysztof Grobelny         {
887ba498310SKrzysztof Grobelny             for (const auto& arg : {"Id", "ReadingParameters"})
888ba498310SKrzysztof Grobelny             {
889ba498310SKrzysztof Grobelny                 if (!handleParamError(asyncResp->res, errorMessage->message,
890ba498310SKrzysztof Grobelny                                       arg))
891ba498310SKrzysztof Grobelny                 {
892ba498310SKrzysztof Grobelny                     return;
893ba498310SKrzysztof Grobelny                 }
894ba498310SKrzysztof Grobelny             }
895ba498310SKrzysztof Grobelny         }
896ba498310SKrzysztof Grobelny     }
897ba498310SKrzysztof Grobelny     if (!verifyCommonErrors(asyncResp->res, reportId, ec))
898ba498310SKrzysztof Grobelny     {
899ba498310SKrzysztof Grobelny         return;
900ba498310SKrzysztof Grobelny     }
901ba498310SKrzysztof Grobelny     messages::internalError(asyncResp->res);
902ba498310SKrzysztof Grobelny }
903ba498310SKrzysztof Grobelny 
904ba498310SKrzysztof Grobelny inline void setReadingParams(
905ba498310SKrzysztof Grobelny     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
906ba498310SKrzysztof Grobelny     const std::string& reportId, ReadingParameters readingParams,
907ba498310SKrzysztof Grobelny     const std::vector<std::vector<std::string>>& readingParamsUris,
908ba498310SKrzysztof Grobelny     const std::map<std::string, std::string>& metricPropertyToDbusPaths)
909ba498310SKrzysztof Grobelny {
910ba498310SKrzysztof Grobelny     if (asyncResp->res.result() != boost::beast::http::status::ok)
911ba498310SKrzysztof Grobelny     {
912ba498310SKrzysztof Grobelny         return;
913ba498310SKrzysztof Grobelny     }
914ba498310SKrzysztof Grobelny 
915ba498310SKrzysztof Grobelny     for (size_t index = 0; index < readingParamsUris.size(); ++index)
916ba498310SKrzysztof Grobelny     {
917ba498310SKrzysztof Grobelny         std::span<const std::string> newUris = readingParamsUris[index];
918ba498310SKrzysztof Grobelny 
919ba498310SKrzysztof Grobelny         const std::optional<std::vector<
920ba498310SKrzysztof Grobelny             std::tuple<sdbusplus::message::object_path, std::string>>>
921ba498310SKrzysztof Grobelny             readingParam =
922ba498310SKrzysztof Grobelny                 sensorPathToUri(asyncResp, newUris, metricPropertyToDbusPaths);
923ba498310SKrzysztof Grobelny 
924ba498310SKrzysztof Grobelny         if (!readingParam)
925ba498310SKrzysztof Grobelny         {
926ba498310SKrzysztof Grobelny             return;
927ba498310SKrzysztof Grobelny         }
928ba498310SKrzysztof Grobelny 
929ba498310SKrzysztof Grobelny         for (const std::tuple<sdbusplus::message::object_path, std::string>&
930ba498310SKrzysztof Grobelny                  value : *readingParam)
931ba498310SKrzysztof Grobelny         {
932ba498310SKrzysztof Grobelny             std::get<0>(readingParams[index]).emplace_back(value);
933ba498310SKrzysztof Grobelny         }
934ba498310SKrzysztof Grobelny     }
935ba498310SKrzysztof Grobelny 
936ba498310SKrzysztof Grobelny     crow::connections::systemBus->async_method_call(
937ba498310SKrzysztof Grobelny         [asyncResp, reportId](const boost::system::error_code& ec,
938ba498310SKrzysztof Grobelny                               const sdbusplus::message_t& msg) {
939ba498310SKrzysztof Grobelny             afterSetReadingParams(asyncResp, reportId, ec, msg);
940ba498310SKrzysztof Grobelny         },
941ba498310SKrzysztof Grobelny         "xyz.openbmc_project.Telemetry", getDbusReportPath(reportId),
942ba498310SKrzysztof Grobelny         "org.freedesktop.DBus.Properties", "Set",
943ba498310SKrzysztof Grobelny         "xyz.openbmc_project.Telemetry.Report", "ReadingParameters",
944ba498310SKrzysztof Grobelny         dbus::utility::DbusVariantType{readingParams});
945ba498310SKrzysztof Grobelny }
946ba498310SKrzysztof Grobelny 
9479e6c388aSLukasz Kazmierczak class UpdateMetrics
9489e6c388aSLukasz Kazmierczak {
9499e6c388aSLukasz Kazmierczak   public:
9509e6c388aSLukasz Kazmierczak     UpdateMetrics(std::string_view idIn,
9519e6c388aSLukasz Kazmierczak                   const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
952bd79bce8SPatrick Williams         id(idIn), asyncResp(asyncRespIn)
9539e6c388aSLukasz Kazmierczak     {}
9549e6c388aSLukasz Kazmierczak 
9559e6c388aSLukasz Kazmierczak     ~UpdateMetrics()
9569e6c388aSLukasz Kazmierczak     {
957ba498310SKrzysztof Grobelny         boost::asio::post(
958ba498310SKrzysztof Grobelny             crow::connections::systemBus->get_io_context(),
959ba498310SKrzysztof Grobelny             std::bind_front(&setReadingParams, asyncResp, id,
960ba498310SKrzysztof Grobelny                             std::move(readingParams), readingParamsUris,
961ba498310SKrzysztof Grobelny                             metricPropertyToDbusPaths));
9629e6c388aSLukasz Kazmierczak     }
9639e6c388aSLukasz Kazmierczak 
9649e6c388aSLukasz Kazmierczak     UpdateMetrics(const UpdateMetrics&) = delete;
9659e6c388aSLukasz Kazmierczak     UpdateMetrics(UpdateMetrics&&) = delete;
9669e6c388aSLukasz Kazmierczak     UpdateMetrics& operator=(const UpdateMetrics&) = delete;
9679e6c388aSLukasz Kazmierczak     UpdateMetrics& operator=(UpdateMetrics&&) = delete;
9689e6c388aSLukasz Kazmierczak 
9699e6c388aSLukasz Kazmierczak     std::string id;
9709e6c388aSLukasz Kazmierczak     std::map<std::string, std::string> metricPropertyToDbusPaths;
9719e6c388aSLukasz Kazmierczak 
9729e6c388aSLukasz Kazmierczak     void insert(const std::map<std::string, std::string>&
9739e6c388aSLukasz Kazmierczak                     additionalMetricPropertyToDbusPaths)
9749e6c388aSLukasz Kazmierczak     {
9759e6c388aSLukasz Kazmierczak         metricPropertyToDbusPaths.insert(
9769e6c388aSLukasz Kazmierczak             additionalMetricPropertyToDbusPaths.begin(),
9779e6c388aSLukasz Kazmierczak             additionalMetricPropertyToDbusPaths.end());
9789e6c388aSLukasz Kazmierczak     }
9799e6c388aSLukasz Kazmierczak 
980bd79bce8SPatrick Williams     void emplace(
981bd79bce8SPatrick Williams         std::span<
982bd79bce8SPatrick Williams             const std::tuple<sdbusplus::message::object_path, std::string>>
9839e6c388aSLukasz Kazmierczak             pathAndUri,
9849e6c388aSLukasz Kazmierczak         const AddReportArgs::MetricArgs& metricArgs)
9859e6c388aSLukasz Kazmierczak     {
9869e6c388aSLukasz Kazmierczak         readingParamsUris.emplace_back(metricArgs.uris);
9879e6c388aSLukasz Kazmierczak         readingParams.emplace_back(
9889e6c388aSLukasz Kazmierczak             std::vector(pathAndUri.begin(), pathAndUri.end()),
9899e6c388aSLukasz Kazmierczak             metricArgs.collectionFunction, metricArgs.collectionTimeScope,
9909e6c388aSLukasz Kazmierczak             metricArgs.collectionDuration);
9919e6c388aSLukasz Kazmierczak     }
9929e6c388aSLukasz Kazmierczak 
9939e6c388aSLukasz Kazmierczak   private:
9949e6c388aSLukasz Kazmierczak     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
9959e6c388aSLukasz Kazmierczak     std::vector<std::vector<std::string>> readingParamsUris;
99647f2934cSEd Tanous     ReadingParameters readingParams;
9979e6c388aSLukasz Kazmierczak };
9989e6c388aSLukasz Kazmierczak 
9999e6c388aSLukasz Kazmierczak inline void
10009e6c388aSLukasz Kazmierczak     setReportEnabled(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10019e6c388aSLukasz Kazmierczak                      std::string_view id, bool enabled)
10029e6c388aSLukasz Kazmierczak {
10039e6c388aSLukasz Kazmierczak     crow::connections::systemBus->async_method_call(
10049e6c388aSLukasz Kazmierczak         [asyncResp, id = std::string(id)](const boost::system::error_code& ec) {
10059e6c388aSLukasz Kazmierczak             if (!verifyCommonErrors(asyncResp->res, id, ec))
10069e6c388aSLukasz Kazmierczak             {
10079e6c388aSLukasz Kazmierczak                 return;
10089e6c388aSLukasz Kazmierczak             }
10099e6c388aSLukasz Kazmierczak         },
10109e6c388aSLukasz Kazmierczak         "xyz.openbmc_project.Telemetry", getDbusReportPath(id),
10119e6c388aSLukasz Kazmierczak         "org.freedesktop.DBus.Properties", "Set",
10129e6c388aSLukasz Kazmierczak         "xyz.openbmc_project.Telemetry.Report", "Enabled",
10139e6c388aSLukasz Kazmierczak         dbus::utility::DbusVariantType{enabled});
10149e6c388aSLukasz Kazmierczak }
10159e6c388aSLukasz Kazmierczak 
1016ba498310SKrzysztof Grobelny inline void afterSetReportingProperties(
1017ba498310SKrzysztof Grobelny     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id,
1018ba498310SKrzysztof Grobelny     const boost::system::error_code& ec, const sdbusplus::message_t& msg)
1019ba498310SKrzysztof Grobelny {
1020ba498310SKrzysztof Grobelny     if (!ec)
1021ba498310SKrzysztof Grobelny     {
1022ba498310SKrzysztof Grobelny         asyncResp->res.result(boost::beast::http::status::no_content);
1023ba498310SKrzysztof Grobelny         return;
1024ba498310SKrzysztof Grobelny     }
1025ba498310SKrzysztof Grobelny 
1026ba498310SKrzysztof Grobelny     if (ec == boost::system::errc::invalid_argument)
1027ba498310SKrzysztof Grobelny     {
1028ba498310SKrzysztof Grobelny         const sd_bus_error* errorMessage = msg.get_error();
1029ba498310SKrzysztof Grobelny         if (errorMessage != nullptr)
1030ba498310SKrzysztof Grobelny         {
1031ba498310SKrzysztof Grobelny             for (const auto& arg : {"Id", "ReportingType", "Interval"})
1032ba498310SKrzysztof Grobelny             {
1033ba498310SKrzysztof Grobelny                 if (!handleParamError(asyncResp->res, errorMessage->message,
1034ba498310SKrzysztof Grobelny                                       arg))
1035ba498310SKrzysztof Grobelny                 {
1036ba498310SKrzysztof Grobelny                     return;
1037ba498310SKrzysztof Grobelny                 }
1038ba498310SKrzysztof Grobelny             }
1039ba498310SKrzysztof Grobelny         }
1040ba498310SKrzysztof Grobelny     }
1041ba498310SKrzysztof Grobelny     if (!verifyCommonErrors(asyncResp->res, id, ec))
1042ba498310SKrzysztof Grobelny     {
1043ba498310SKrzysztof Grobelny         return;
1044ba498310SKrzysztof Grobelny     }
1045ba498310SKrzysztof Grobelny     messages::internalError(asyncResp->res);
1046ba498310SKrzysztof Grobelny }
1047ba498310SKrzysztof Grobelny 
10489e6c388aSLukasz Kazmierczak inline void setReportTypeAndInterval(
10499e6c388aSLukasz Kazmierczak     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, std::string_view id,
1050b4361d63SEd Tanous     const std::optional<std::string>& reportingType,
1051b4361d63SEd Tanous     const std::optional<std::string>& recurrenceIntervalStr)
10529e6c388aSLukasz Kazmierczak {
1053b4361d63SEd Tanous     std::string dbusReportingType;
1054b4361d63SEd Tanous     if (reportingType)
1055b4361d63SEd Tanous     {
1056b4361d63SEd Tanous         dbusReportingType = toDbusReportingType(*reportingType);
1057b4361d63SEd Tanous         if (dbusReportingType.empty())
1058b4361d63SEd Tanous         {
1059b4361d63SEd Tanous             messages::propertyValueNotInList(asyncResp->res, *reportingType,
1060b4361d63SEd Tanous                                              "MetricReportDefinitionType");
1061b4361d63SEd Tanous             return;
1062b4361d63SEd Tanous         }
1063b4361d63SEd Tanous     }
1064b4361d63SEd Tanous 
1065b4361d63SEd Tanous     uint64_t recurrenceInterval = std::numeric_limits<uint64_t>::max();
1066b4361d63SEd Tanous     if (recurrenceIntervalStr)
1067b4361d63SEd Tanous     {
1068b4361d63SEd Tanous         std::optional<std::chrono::milliseconds> durationNum =
1069b4361d63SEd Tanous             time_utils::fromDurationString(*recurrenceIntervalStr);
1070b4361d63SEd Tanous         if (!durationNum || durationNum->count() < 0)
1071b4361d63SEd Tanous         {
1072b4361d63SEd Tanous             messages::propertyValueIncorrect(
1073b4361d63SEd Tanous                 asyncResp->res, "RecurrenceInterval", *recurrenceIntervalStr);
1074b4361d63SEd Tanous             return;
1075b4361d63SEd Tanous         }
1076b4361d63SEd Tanous 
1077b4361d63SEd Tanous         recurrenceInterval = static_cast<uint64_t>(durationNum->count());
1078b4361d63SEd Tanous     }
1079b4361d63SEd Tanous 
10809e6c388aSLukasz Kazmierczak     crow::connections::systemBus->async_method_call(
1081ba498310SKrzysztof Grobelny         [asyncResp, id = std::string(id)](const boost::system::error_code& ec,
1082ba498310SKrzysztof Grobelny                                           const sdbusplus::message_t& msg) {
1083ba498310SKrzysztof Grobelny             afterSetReportingProperties(asyncResp, id, ec, msg);
10849e6c388aSLukasz Kazmierczak         },
10859e6c388aSLukasz Kazmierczak         "xyz.openbmc_project.Telemetry", getDbusReportPath(id),
10869e6c388aSLukasz Kazmierczak         "xyz.openbmc_project.Telemetry.Report", "SetReportingProperties",
1087b4361d63SEd Tanous         dbusReportingType, recurrenceInterval);
10889e6c388aSLukasz Kazmierczak }
10899e6c388aSLukasz Kazmierczak 
1090ba498310SKrzysztof Grobelny inline void afterSetReportUpdates(
1091ba498310SKrzysztof Grobelny     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id,
1092ba498310SKrzysztof Grobelny     const boost::system::error_code& ec, const sdbusplus::message_t& msg)
1093ba498310SKrzysztof Grobelny {
1094ba498310SKrzysztof Grobelny     if (!ec)
1095ba498310SKrzysztof Grobelny     {
1096ba498310SKrzysztof Grobelny         asyncResp->res.result(boost::beast::http::status::no_content);
1097ba498310SKrzysztof Grobelny         return;
1098ba498310SKrzysztof Grobelny     }
1099ba498310SKrzysztof Grobelny     if (ec == boost::system::errc::invalid_argument)
1100ba498310SKrzysztof Grobelny     {
1101ba498310SKrzysztof Grobelny         const sd_bus_error* errorMessage = msg.get_error();
1102ba498310SKrzysztof Grobelny         if (errorMessage != nullptr)
1103ba498310SKrzysztof Grobelny         {
1104ba498310SKrzysztof Grobelny             for (const auto& arg : {"Id", "ReportUpdates"})
1105ba498310SKrzysztof Grobelny             {
1106ba498310SKrzysztof Grobelny                 if (!handleParamError(asyncResp->res, errorMessage->message,
1107ba498310SKrzysztof Grobelny                                       arg))
1108ba498310SKrzysztof Grobelny                 {
1109ba498310SKrzysztof Grobelny                     return;
1110ba498310SKrzysztof Grobelny                 }
1111ba498310SKrzysztof Grobelny             }
1112ba498310SKrzysztof Grobelny         }
1113ba498310SKrzysztof Grobelny     }
1114ba498310SKrzysztof Grobelny     if (!verifyCommonErrors(asyncResp->res, id, ec))
1115ba498310SKrzysztof Grobelny     {
1116ba498310SKrzysztof Grobelny         return;
1117ba498310SKrzysztof Grobelny     }
1118ba498310SKrzysztof Grobelny }
1119ba498310SKrzysztof Grobelny 
11209e6c388aSLukasz Kazmierczak inline void
11219e6c388aSLukasz Kazmierczak     setReportUpdates(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
11229e6c388aSLukasz Kazmierczak                      std::string_view id, const std::string& reportUpdates)
11239e6c388aSLukasz Kazmierczak {
1124b4361d63SEd Tanous     std::string dbusReportUpdates = toDbusReportUpdates(reportUpdates);
1125b4361d63SEd Tanous     if (dbusReportUpdates.empty())
1126b4361d63SEd Tanous     {
1127b4361d63SEd Tanous         messages::propertyValueNotInList(asyncResp->res, reportUpdates,
1128b4361d63SEd Tanous                                          "ReportUpdates");
1129b4361d63SEd Tanous         return;
1130b4361d63SEd Tanous     }
11319e6c388aSLukasz Kazmierczak     crow::connections::systemBus->async_method_call(
1132ba498310SKrzysztof Grobelny         [asyncResp, id = std::string(id)](const boost::system::error_code& ec,
1133ba498310SKrzysztof Grobelny                                           const sdbusplus::message_t& msg) {
1134ba498310SKrzysztof Grobelny             afterSetReportUpdates(asyncResp, id, ec, msg);
11359e6c388aSLukasz Kazmierczak         },
11369e6c388aSLukasz Kazmierczak         "xyz.openbmc_project.Telemetry", getDbusReportPath(id),
11379e6c388aSLukasz Kazmierczak         "org.freedesktop.DBus.Properties", "Set",
11389e6c388aSLukasz Kazmierczak         "xyz.openbmc_project.Telemetry.Report", "ReportUpdates",
1139ba498310SKrzysztof Grobelny         dbus::utility::DbusVariantType{dbusReportUpdates});
1140ba498310SKrzysztof Grobelny }
1141ba498310SKrzysztof Grobelny 
1142ba498310SKrzysztof Grobelny inline void afterSetReportActions(
1143ba498310SKrzysztof Grobelny     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id,
1144ba498310SKrzysztof Grobelny     const boost::system::error_code& ec, const sdbusplus::message_t& msg)
1145ba498310SKrzysztof Grobelny {
1146ba498310SKrzysztof Grobelny     if (ec == boost::system::errc::invalid_argument)
1147ba498310SKrzysztof Grobelny     {
1148ba498310SKrzysztof Grobelny         const sd_bus_error* errorMessage = msg.get_error();
1149ba498310SKrzysztof Grobelny         if (errorMessage != nullptr)
1150ba498310SKrzysztof Grobelny         {
1151ba498310SKrzysztof Grobelny             for (const auto& arg : {"Id", "ReportActions"})
1152ba498310SKrzysztof Grobelny             {
1153ba498310SKrzysztof Grobelny                 if (!handleParamError(asyncResp->res, errorMessage->message,
1154ba498310SKrzysztof Grobelny                                       arg))
1155ba498310SKrzysztof Grobelny                 {
1156ba498310SKrzysztof Grobelny                     return;
1157ba498310SKrzysztof Grobelny                 }
1158ba498310SKrzysztof Grobelny             }
1159ba498310SKrzysztof Grobelny         }
1160ba498310SKrzysztof Grobelny     }
1161ba498310SKrzysztof Grobelny 
1162ba498310SKrzysztof Grobelny     if (!verifyCommonErrors(asyncResp->res, id, ec))
1163ba498310SKrzysztof Grobelny     {
1164ba498310SKrzysztof Grobelny         return;
1165ba498310SKrzysztof Grobelny     }
1166ba498310SKrzysztof Grobelny 
1167ba498310SKrzysztof Grobelny     messages::internalError(asyncResp->res);
11689e6c388aSLukasz Kazmierczak }
11699e6c388aSLukasz Kazmierczak 
1170bd79bce8SPatrick Williams inline void setReportActions(
1171bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, std::string_view id,
1172b4361d63SEd Tanous     const std::vector<std::string>& reportActions)
11739e6c388aSLukasz Kazmierczak {
1174b4361d63SEd Tanous     std::vector<std::string> dbusReportActions;
1175b4361d63SEd Tanous     if (!toDbusReportActions(asyncResp->res, reportActions, dbusReportActions))
1176b4361d63SEd Tanous     {
1177b4361d63SEd Tanous         return;
1178b4361d63SEd Tanous     }
1179b4361d63SEd Tanous 
11809e6c388aSLukasz Kazmierczak     crow::connections::systemBus->async_method_call(
1181ba498310SKrzysztof Grobelny         [asyncResp, id = std::string(id)](const boost::system::error_code& ec,
1182ba498310SKrzysztof Grobelny                                           const sdbusplus::message_t& msg) {
1183ba498310SKrzysztof Grobelny             afterSetReportActions(asyncResp, id, ec, msg);
11849e6c388aSLukasz Kazmierczak         },
11859e6c388aSLukasz Kazmierczak         "xyz.openbmc_project.Telemetry", getDbusReportPath(id),
11869e6c388aSLukasz Kazmierczak         "org.freedesktop.DBus.Properties", "Set",
11879e6c388aSLukasz Kazmierczak         "xyz.openbmc_project.Telemetry.Report", "ReportActions",
11889e6c388aSLukasz Kazmierczak         dbus::utility::DbusVariantType{dbusReportActions});
11899e6c388aSLukasz Kazmierczak }
11909e6c388aSLukasz Kazmierczak 
1191bd79bce8SPatrick Williams inline void setReportMetrics(
1192bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, std::string_view id,
1193a64919e8SBoleslaw Ogonczyk Makowski     std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>&&
1194a64919e8SBoleslaw Ogonczyk Makowski         metrics)
11959e6c388aSLukasz Kazmierczak {
1196deae6a78SEd Tanous     dbus::utility::getAllProperties(
1197deae6a78SEd Tanous         telemetry::service, telemetry::getDbusReportPath(id),
1198deae6a78SEd Tanous         telemetry::reportInterface,
1199ba498310SKrzysztof Grobelny         [asyncResp, id = std::string(id), redfishMetrics = std::move(metrics)](
12009e6c388aSLukasz Kazmierczak             boost::system::error_code ec,
12019e6c388aSLukasz Kazmierczak             const dbus::utility::DBusPropertiesMap& properties) mutable {
1202ba498310SKrzysztof Grobelny             if (!verifyCommonErrors(asyncResp->res, id, ec))
12039e6c388aSLukasz Kazmierczak             {
12049e6c388aSLukasz Kazmierczak                 return;
12059e6c388aSLukasz Kazmierczak             }
12069e6c388aSLukasz Kazmierczak 
12079e6c388aSLukasz Kazmierczak             ReadingParameters readingParams;
12089e6c388aSLukasz Kazmierczak 
12099e6c388aSLukasz Kazmierczak             const bool success = sdbusplus::unpackPropertiesNoThrow(
1210bd79bce8SPatrick Williams                 dbus_utils::UnpackErrorPrinter(), properties,
1211bd79bce8SPatrick Williams                 "ReadingParameters", readingParams);
12129e6c388aSLukasz Kazmierczak 
12139e6c388aSLukasz Kazmierczak             if (!success)
12149e6c388aSLukasz Kazmierczak             {
12159e6c388aSLukasz Kazmierczak                 messages::internalError(asyncResp->res);
12169e6c388aSLukasz Kazmierczak                 return;
12179e6c388aSLukasz Kazmierczak             }
12189e6c388aSLukasz Kazmierczak 
1219bd79bce8SPatrick Williams             auto updateMetricsReq =
1220bd79bce8SPatrick Williams                 std::make_shared<UpdateMetrics>(id, asyncResp);
12219e6c388aSLukasz Kazmierczak 
12229e6c388aSLukasz Kazmierczak             boost::container::flat_set<std::pair<std::string, std::string>>
12239e6c388aSLukasz Kazmierczak                 chassisSensors;
12249e6c388aSLukasz Kazmierczak 
12259e6c388aSLukasz Kazmierczak             size_t index = 0;
1226a64919e8SBoleslaw Ogonczyk Makowski             for (std::variant<nlohmann::json::object_t, std::nullptr_t>&
1227a64919e8SBoleslaw Ogonczyk Makowski                      metricVariant : redfishMetrics)
12289e6c388aSLukasz Kazmierczak             {
1229a64919e8SBoleslaw Ogonczyk Makowski                 nlohmann::json::object_t* metric =
1230a64919e8SBoleslaw Ogonczyk Makowski                     std::get_if<nlohmann::json::object_t>(&metricVariant);
1231a64919e8SBoleslaw Ogonczyk Makowski                 if (metric == nullptr)
1232a64919e8SBoleslaw Ogonczyk Makowski                 {
1233a64919e8SBoleslaw Ogonczyk Makowski                     index++;
1234a64919e8SBoleslaw Ogonczyk Makowski                     continue;
1235a64919e8SBoleslaw Ogonczyk Makowski                 }
1236a64919e8SBoleslaw Ogonczyk Makowski 
12379e6c388aSLukasz Kazmierczak                 AddReportArgs::MetricArgs metricArgs;
12389e6c388aSLukasz Kazmierczak                 std::vector<
12399e6c388aSLukasz Kazmierczak                     std::tuple<sdbusplus::message::object_path, std::string>>
12409e6c388aSLukasz Kazmierczak                     pathAndUri;
12419e6c388aSLukasz Kazmierczak 
12429e6c388aSLukasz Kazmierczak                 if (index < readingParams.size())
12439e6c388aSLukasz Kazmierczak                 {
12449e6c388aSLukasz Kazmierczak                     const ReadingParameters::value_type& existing =
12459e6c388aSLukasz Kazmierczak                         readingParams[index];
12469e6c388aSLukasz Kazmierczak 
1247*dac07cadSBoleslaw Ogonczyk Makowski                     if (metric->empty())
1248*dac07cadSBoleslaw Ogonczyk Makowski                     {
12499e6c388aSLukasz Kazmierczak                         pathAndUri = std::get<0>(existing);
1250*dac07cadSBoleslaw Ogonczyk Makowski                     }
12519e6c388aSLukasz Kazmierczak                     metricArgs.collectionFunction = std::get<1>(existing);
12529e6c388aSLukasz Kazmierczak                     metricArgs.collectionTimeScope = std::get<2>(existing);
12539e6c388aSLukasz Kazmierczak                     metricArgs.collectionDuration = std::get<3>(existing);
12549e6c388aSLukasz Kazmierczak                 }
12559e6c388aSLukasz Kazmierczak 
1256a64919e8SBoleslaw Ogonczyk Makowski                 if (!getUserMetric(asyncResp->res, *metric, metricArgs))
12579e6c388aSLukasz Kazmierczak                 {
12589e6c388aSLukasz Kazmierczak                     return;
12599e6c388aSLukasz Kazmierczak                 }
12609e6c388aSLukasz Kazmierczak 
12619e6c388aSLukasz Kazmierczak                 std::optional<IncorrectMetricUri> error =
12629e6c388aSLukasz Kazmierczak                     getChassisSensorNode(metricArgs.uris, chassisSensors);
12639e6c388aSLukasz Kazmierczak 
12649e6c388aSLukasz Kazmierczak                 if (error)
12659e6c388aSLukasz Kazmierczak                 {
12669e6c388aSLukasz Kazmierczak                     messages::propertyValueIncorrect(
12679e6c388aSLukasz Kazmierczak                         asyncResp->res, error->uri,
12689e6c388aSLukasz Kazmierczak                         "MetricProperties/" + std::to_string(error->index));
12699e6c388aSLukasz Kazmierczak                     return;
12709e6c388aSLukasz Kazmierczak                 }
12719e6c388aSLukasz Kazmierczak 
12729e6c388aSLukasz Kazmierczak                 updateMetricsReq->emplace(pathAndUri, metricArgs);
12739e6c388aSLukasz Kazmierczak                 index++;
12749e6c388aSLukasz Kazmierczak             }
12759e6c388aSLukasz Kazmierczak 
12769e6c388aSLukasz Kazmierczak             for (const auto& [chassis, sensorType] : chassisSensors)
12779e6c388aSLukasz Kazmierczak             {
12789e6c388aSLukasz Kazmierczak                 retrieveUriToDbusMap(
12799e6c388aSLukasz Kazmierczak                     chassis, sensorType,
12809e6c388aSLukasz Kazmierczak                     [asyncResp, updateMetricsReq](
12819e6c388aSLukasz Kazmierczak                         const boost::beast::http::status status,
12829e6c388aSLukasz Kazmierczak                         const std::map<std::string, std::string>& uriToDbus) {
12839e6c388aSLukasz Kazmierczak                         if (status != boost::beast::http::status::ok)
12849e6c388aSLukasz Kazmierczak                         {
12859e6c388aSLukasz Kazmierczak                             BMCWEB_LOG_ERROR(
12869e6c388aSLukasz Kazmierczak                                 "Failed to retrieve URI to dbus sensors map with err {}",
12879e6c388aSLukasz Kazmierczak                                 static_cast<unsigned>(status));
12889e6c388aSLukasz Kazmierczak                             return;
12899e6c388aSLukasz Kazmierczak                         }
12909e6c388aSLukasz Kazmierczak                         updateMetricsReq->insert(uriToDbus);
12919e6c388aSLukasz Kazmierczak                     });
12929e6c388aSLukasz Kazmierczak             }
12939e6c388aSLukasz Kazmierczak         });
12949e6c388aSLukasz Kazmierczak }
1295081ebf06SWludzik, Jozef 
12964220be3bSEd Tanous inline void handleMetricReportDefinitionCollectionHead(
12974220be3bSEd Tanous     App& app, const crow::Request& req,
12984220be3bSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12994220be3bSEd Tanous {
13004220be3bSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
13014220be3bSEd Tanous     {
13024220be3bSEd Tanous         return;
13034220be3bSEd Tanous     }
13044220be3bSEd Tanous     asyncResp->res.addHeader(
13054220be3bSEd Tanous         boost::beast::http::field::link,
13064220be3bSEd Tanous         "</redfish/v1/JsonSchemas/MetricReportDefinitionCollection/MetricReportDefinitionCollection.json>; rel=describedby");
13074220be3bSEd Tanous }
13084220be3bSEd Tanous 
13094220be3bSEd Tanous inline void handleMetricReportDefinitionCollectionGet(
1310fc0edbe3SEd Tanous     App& app, const crow::Request& req,
1311fc0edbe3SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1312fc0edbe3SEd Tanous {
1313fc0edbe3SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1314fc0edbe3SEd Tanous     {
1315fc0edbe3SEd Tanous         return;
1316fc0edbe3SEd Tanous     }
13179e6c388aSLukasz Kazmierczak     asyncResp->res.addHeader(
13189e6c388aSLukasz Kazmierczak         boost::beast::http::field::link,
13199e6c388aSLukasz Kazmierczak         "</redfish/v1/JsonSchemas/MetricReportDefinition/MetricReportDefinition.json>; rel=describedby");
1320fc0edbe3SEd Tanous 
1321fc0edbe3SEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
1322fc0edbe3SEd Tanous         "#MetricReportDefinitionCollection."
1323fc0edbe3SEd Tanous         "MetricReportDefinitionCollection";
1324fc0edbe3SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
1325fc0edbe3SEd Tanous         "/redfish/v1/TelemetryService/MetricReportDefinitions";
1326fc0edbe3SEd Tanous     asyncResp->res.jsonValue["Name"] = "Metric Definition Collection";
1327fc0edbe3SEd Tanous     constexpr std::array<std::string_view, 1> interfaces{
1328fc0edbe3SEd Tanous         telemetry::reportInterface};
1329fc0edbe3SEd Tanous     collection_util::getCollectionMembers(
1330fc0edbe3SEd Tanous         asyncResp,
1331fc0edbe3SEd Tanous         boost::urls::url(
1332fc0edbe3SEd Tanous             "/redfish/v1/TelemetryService/MetricReportDefinitions"),
1333fc0edbe3SEd Tanous         interfaces, "/xyz/openbmc_project/Telemetry/Reports/TelemetryService");
1334fc0edbe3SEd Tanous }
1335fc0edbe3SEd Tanous 
1336bd79bce8SPatrick Williams inline void handleReportPatch(
1337bd79bce8SPatrick Williams     App& app, const crow::Request& req,
1338bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, std::string_view id)
13399e6c388aSLukasz Kazmierczak {
13409e6c388aSLukasz Kazmierczak     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
13419e6c388aSLukasz Kazmierczak     {
13429e6c388aSLukasz Kazmierczak         return;
13439e6c388aSLukasz Kazmierczak     }
13449e6c388aSLukasz Kazmierczak 
13459e6c388aSLukasz Kazmierczak     std::optional<std::string> reportingTypeStr;
13469e6c388aSLukasz Kazmierczak     std::optional<std::string> reportUpdatesStr;
13479e6c388aSLukasz Kazmierczak     std::optional<bool> metricReportDefinitionEnabled;
1348a64919e8SBoleslaw Ogonczyk Makowski     std::optional<
1349a64919e8SBoleslaw Ogonczyk Makowski         std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
1350a64919e8SBoleslaw Ogonczyk Makowski         metrics;
13519e6c388aSLukasz Kazmierczak     std::optional<std::vector<std::string>> reportActionsStr;
1352b14f357fSEd Tanous     std::optional<std::string> scheduleDurationStr;
13539e6c388aSLukasz Kazmierczak 
1354afc474aeSMyung Bae     if (!json_util::readJsonPatch( //
1355afc474aeSMyung Bae             req, asyncResp->res, //
1356afc474aeSMyung Bae             "MetricReportDefinitionEnabled", metricReportDefinitionEnabled, //
1357afc474aeSMyung Bae             "MetricReportDefinitionType", reportingTypeStr, //
1358afc474aeSMyung Bae             "Metrics", metrics, //
1359afc474aeSMyung Bae             "ReportActions", reportActionsStr, //
1360afc474aeSMyung Bae             "ReportUpdates", reportUpdatesStr, //
1361afc474aeSMyung Bae             "Schedule/RecurrenceInterval", scheduleDurationStr //
1362afc474aeSMyung Bae             ))
13639e6c388aSLukasz Kazmierczak     {
13649e6c388aSLukasz Kazmierczak         return;
13659e6c388aSLukasz Kazmierczak     }
13669e6c388aSLukasz Kazmierczak 
13679e6c388aSLukasz Kazmierczak     if (metricReportDefinitionEnabled)
13689e6c388aSLukasz Kazmierczak     {
13699e6c388aSLukasz Kazmierczak         setReportEnabled(asyncResp, id, *metricReportDefinitionEnabled);
13709e6c388aSLukasz Kazmierczak     }
13719e6c388aSLukasz Kazmierczak 
13729e6c388aSLukasz Kazmierczak     if (reportUpdatesStr)
13739e6c388aSLukasz Kazmierczak     {
1374b4361d63SEd Tanous         setReportUpdates(asyncResp, id, *reportUpdatesStr);
13759e6c388aSLukasz Kazmierczak     }
13769e6c388aSLukasz Kazmierczak 
13779e6c388aSLukasz Kazmierczak     if (reportActionsStr)
13789e6c388aSLukasz Kazmierczak     {
1379b4361d63SEd Tanous         setReportActions(asyncResp, id, *reportActionsStr);
13809e6c388aSLukasz Kazmierczak     }
13819e6c388aSLukasz Kazmierczak 
1382b14f357fSEd Tanous     if (reportingTypeStr || scheduleDurationStr)
13839e6c388aSLukasz Kazmierczak     {
1384b4361d63SEd Tanous         setReportTypeAndInterval(asyncResp, id, reportingTypeStr,
1385b4361d63SEd Tanous                                  scheduleDurationStr);
13869e6c388aSLukasz Kazmierczak     }
13879e6c388aSLukasz Kazmierczak 
13889e6c388aSLukasz Kazmierczak     if (metrics)
13899e6c388aSLukasz Kazmierczak     {
1390ba498310SKrzysztof Grobelny         setReportMetrics(asyncResp, id, std::move(*metrics));
13919e6c388aSLukasz Kazmierczak     }
13929e6c388aSLukasz Kazmierczak }
13939e6c388aSLukasz Kazmierczak 
1394bd79bce8SPatrick Williams inline void handleReportDelete(
1395bd79bce8SPatrick Williams     App& app, const crow::Request& req,
1396bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, std::string_view id)
13979e6c388aSLukasz Kazmierczak {
13989e6c388aSLukasz Kazmierczak     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
13999e6c388aSLukasz Kazmierczak     {
14009e6c388aSLukasz Kazmierczak         return;
14019e6c388aSLukasz Kazmierczak     }
14029e6c388aSLukasz Kazmierczak 
14039e6c388aSLukasz Kazmierczak     const std::string reportPath = getDbusReportPath(id);
14049e6c388aSLukasz Kazmierczak 
14059e6c388aSLukasz Kazmierczak     crow::connections::systemBus->async_method_call(
14069e6c388aSLukasz Kazmierczak         [asyncResp,
14079e6c388aSLukasz Kazmierczak          reportId = std::string(id)](const boost::system::error_code& ec) {
14089e6c388aSLukasz Kazmierczak             if (!verifyCommonErrors(asyncResp->res, reportId, ec))
14099e6c388aSLukasz Kazmierczak             {
14109e6c388aSLukasz Kazmierczak                 return;
14119e6c388aSLukasz Kazmierczak             }
14129e6c388aSLukasz Kazmierczak             asyncResp->res.result(boost::beast::http::status::no_content);
14139e6c388aSLukasz Kazmierczak         },
14149e6c388aSLukasz Kazmierczak         service, reportPath, "xyz.openbmc_project.Object.Delete", "Delete");
14159e6c388aSLukasz Kazmierczak }
14169e6c388aSLukasz Kazmierczak } // namespace telemetry
14179e6c388aSLukasz Kazmierczak 
141895bdb5f0SEd Tanous inline void afterRetrieveUriToDbusMap(
141995bdb5f0SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
142095bdb5f0SEd Tanous     const std::shared_ptr<telemetry::AddReport>& addReportReq,
142195bdb5f0SEd Tanous     const boost::beast::http::status status,
142295bdb5f0SEd Tanous     const std::map<std::string, std::string>& uriToDbus)
142395bdb5f0SEd Tanous {
142495bdb5f0SEd Tanous     if (status != boost::beast::http::status::ok)
142595bdb5f0SEd Tanous     {
142695bdb5f0SEd Tanous         BMCWEB_LOG_ERROR(
142795bdb5f0SEd Tanous             "Failed to retrieve URI to dbus sensors map with err {}",
142895bdb5f0SEd Tanous             static_cast<unsigned>(status));
142995bdb5f0SEd Tanous         return;
143095bdb5f0SEd Tanous     }
143195bdb5f0SEd Tanous     addReportReq->insert(uriToDbus);
143295bdb5f0SEd Tanous }
143395bdb5f0SEd Tanous 
14349e6c388aSLukasz Kazmierczak inline void handleMetricReportDefinitionsPost(
14359e6c388aSLukasz Kazmierczak     App& app, const crow::Request& req,
14369e6c388aSLukasz Kazmierczak     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
14379e6c388aSLukasz Kazmierczak {
14389e6c388aSLukasz Kazmierczak     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
14399e6c388aSLukasz Kazmierczak     {
14409e6c388aSLukasz Kazmierczak         return;
14419e6c388aSLukasz Kazmierczak     }
14429e6c388aSLukasz Kazmierczak 
14439e6c388aSLukasz Kazmierczak     telemetry::AddReportArgs args;
14449e6c388aSLukasz Kazmierczak     if (!telemetry::getUserParameters(asyncResp->res, req, args))
14459e6c388aSLukasz Kazmierczak     {
14469e6c388aSLukasz Kazmierczak         return;
14479e6c388aSLukasz Kazmierczak     }
14489e6c388aSLukasz Kazmierczak 
14499e6c388aSLukasz Kazmierczak     boost::container::flat_set<std::pair<std::string, std::string>>
14509e6c388aSLukasz Kazmierczak         chassisSensors;
14519e6c388aSLukasz Kazmierczak     if (!telemetry::getChassisSensorNodeFromMetrics(asyncResp, args.metrics,
14529e6c388aSLukasz Kazmierczak                                                     chassisSensors))
14539e6c388aSLukasz Kazmierczak     {
14549e6c388aSLukasz Kazmierczak         return;
14559e6c388aSLukasz Kazmierczak     }
14569e6c388aSLukasz Kazmierczak 
1457bd79bce8SPatrick Williams     auto addReportReq =
1458bd79bce8SPatrick Williams         std::make_shared<telemetry::AddReport>(std::move(args), asyncResp);
14599e6c388aSLukasz Kazmierczak     for (const auto& [chassis, sensorType] : chassisSensors)
14609e6c388aSLukasz Kazmierczak     {
146195bdb5f0SEd Tanous         retrieveUriToDbusMap(chassis, sensorType,
146295bdb5f0SEd Tanous                              std::bind_front(afterRetrieveUriToDbusMap,
146395bdb5f0SEd Tanous                                              asyncResp, addReportReq));
14649e6c388aSLukasz Kazmierczak     }
14659e6c388aSLukasz Kazmierczak }
14669e6c388aSLukasz Kazmierczak 
14679e6c388aSLukasz Kazmierczak inline void
14684220be3bSEd Tanous     handleMetricReportHead(App& app, const crow::Request& req,
14694220be3bSEd Tanous                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
14704220be3bSEd Tanous                            const std::string& /*id*/)
14714220be3bSEd Tanous {
14724220be3bSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
14734220be3bSEd Tanous     {
14744220be3bSEd Tanous         return;
14754220be3bSEd Tanous     }
14764220be3bSEd Tanous     asyncResp->res.addHeader(
14774220be3bSEd Tanous         boost::beast::http::field::link,
14784220be3bSEd Tanous         "</redfish/v1/JsonSchemas/MetricReport/MetricReport.json>; rel=describedby");
14794220be3bSEd Tanous }
14804220be3bSEd Tanous 
1481bd79bce8SPatrick Williams inline void handleMetricReportGet(
1482bd79bce8SPatrick Williams     App& app, const crow::Request& req,
1483bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
148486a5ac98SEd Tanous {
148586a5ac98SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
148686a5ac98SEd Tanous     {
148786a5ac98SEd Tanous         return;
148886a5ac98SEd Tanous     }
14894220be3bSEd Tanous     asyncResp->res.addHeader(
14904220be3bSEd Tanous         boost::beast::http::field::link,
14914220be3bSEd Tanous         "</redfish/v1/JsonSchemas/MetricReport/MetricReport.json>; rel=describedby");
149286a5ac98SEd Tanous 
1493deae6a78SEd Tanous     dbus::utility::getAllProperties(
1494deae6a78SEd Tanous         telemetry::service, telemetry::getDbusReportPath(id),
1495deae6a78SEd Tanous         telemetry::reportInterface,
149686a5ac98SEd Tanous         [asyncResp, id](const boost::system::error_code& ec,
149786a5ac98SEd Tanous                         const dbus::utility::DBusPropertiesMap& properties) {
14989e6c388aSLukasz Kazmierczak             if (!redfish::telemetry::verifyCommonErrors(asyncResp->res, id, ec))
149986a5ac98SEd Tanous             {
150086a5ac98SEd Tanous                 return;
150186a5ac98SEd Tanous             }
150286a5ac98SEd Tanous 
150386a5ac98SEd Tanous             telemetry::fillReportDefinition(asyncResp, id, properties);
150486a5ac98SEd Tanous         });
150586a5ac98SEd Tanous }
150686a5ac98SEd Tanous 
1507dd1c4a9cSSzymon Dompke inline void handleMetricReportDelete(
1508dd1c4a9cSSzymon Dompke     App& app, const crow::Request& req,
1509dd1c4a9cSSzymon Dompke     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
1510dd1c4a9cSSzymon Dompke 
1511dd1c4a9cSSzymon Dompke {
1512dd1c4a9cSSzymon Dompke     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1513dd1c4a9cSSzymon Dompke     {
1514dd1c4a9cSSzymon Dompke         return;
1515dd1c4a9cSSzymon Dompke     }
1516dd1c4a9cSSzymon Dompke 
1517dd1c4a9cSSzymon Dompke     const std::string reportPath = telemetry::getDbusReportPath(id);
1518dd1c4a9cSSzymon Dompke 
1519dd1c4a9cSSzymon Dompke     crow::connections::systemBus->async_method_call(
1520dd1c4a9cSSzymon Dompke         [asyncResp, id](const boost::system::error_code& ec) {
1521dd1c4a9cSSzymon Dompke             /*
1522dd1c4a9cSSzymon Dompke              * boost::system::errc and std::errc are missing value
1523dd1c4a9cSSzymon Dompke              * for EBADR error that is defined in Linux.
1524dd1c4a9cSSzymon Dompke              */
1525dd1c4a9cSSzymon Dompke             if (ec.value() == EBADR)
1526dd1c4a9cSSzymon Dompke             {
1527bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res,
1528bd79bce8SPatrick Williams                                            "MetricReportDefinition", id);
1529dd1c4a9cSSzymon Dompke                 return;
1530dd1c4a9cSSzymon Dompke             }
1531dd1c4a9cSSzymon Dompke 
1532dd1c4a9cSSzymon Dompke             if (ec)
1533dd1c4a9cSSzymon Dompke             {
153462598e31SEd Tanous                 BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
1535dd1c4a9cSSzymon Dompke                 messages::internalError(asyncResp->res);
1536dd1c4a9cSSzymon Dompke                 return;
1537dd1c4a9cSSzymon Dompke             }
1538dd1c4a9cSSzymon Dompke 
1539dd1c4a9cSSzymon Dompke             asyncResp->res.result(boost::beast::http::status::no_content);
1540dd1c4a9cSSzymon Dompke         },
1541dd1c4a9cSSzymon Dompke         telemetry::service, reportPath, "xyz.openbmc_project.Object.Delete",
1542dd1c4a9cSSzymon Dompke         "Delete");
1543dd1c4a9cSSzymon Dompke }
1544dd1c4a9cSSzymon Dompke 
15457e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportDefinitionCollection(App& app)
1546081ebf06SWludzik, Jozef {
15477e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/")
15484220be3bSEd Tanous         .privileges(redfish::privileges::headMetricReportDefinitionCollection)
15494220be3bSEd Tanous         .methods(boost::beast::http::verb::head)(std::bind_front(
15509e6c388aSLukasz Kazmierczak             telemetry::handleMetricReportDefinitionCollectionHead,
15519e6c388aSLukasz Kazmierczak             std::ref(app)));
15524220be3bSEd Tanous 
15534220be3bSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/")
1554ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportDefinitionCollection)
15554220be3bSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
15569e6c388aSLukasz Kazmierczak             telemetry::handleMetricReportDefinitionCollectionGet,
15579e6c388aSLukasz Kazmierczak             std::ref(app)));
15584dbb8aeaSWludzik, Jozef 
15597e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/")
1560ed398213SEd Tanous         .privileges(redfish::privileges::postMetricReportDefinitionCollection)
1561002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
15629e6c388aSLukasz Kazmierczak             std::bind_front(handleMetricReportDefinitionsPost, std::ref(app)));
1563081ebf06SWludzik, Jozef }
1564081ebf06SWludzik, Jozef 
15657e860f15SJohn Edward Broadbent inline void requestRoutesMetricReportDefinition(App& app)
1566081ebf06SWludzik, Jozef {
15677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
15687e860f15SJohn Edward Broadbent                  "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/")
1569ed398213SEd Tanous         .privileges(redfish::privileges::getMetricReportDefinition)
15704220be3bSEd Tanous         .methods(boost::beast::http::verb::head)(
15714220be3bSEd Tanous             std::bind_front(handleMetricReportHead, std::ref(app)));
15724220be3bSEd Tanous 
15734220be3bSEd Tanous     BMCWEB_ROUTE(app,
15744220be3bSEd Tanous                  "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/")
15754220be3bSEd Tanous         .privileges(redfish::privileges::getMetricReportDefinition)
15767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
157786a5ac98SEd Tanous             std::bind_front(handleMetricReportGet, std::ref(app)));
1578479e899dSKrzysztof Grobelny 
15797e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
15807e860f15SJohn Edward Broadbent                  "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/")
15819e6c388aSLukasz Kazmierczak         .privileges(redfish::privileges::deleteMetricReportDefinition)
15827e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
1583dd1c4a9cSSzymon Dompke             std::bind_front(handleMetricReportDelete, std::ref(app)));
15849e6c388aSLukasz Kazmierczak 
15859e6c388aSLukasz Kazmierczak     BMCWEB_ROUTE(app,
15869e6c388aSLukasz Kazmierczak                  "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/")
15879e6c388aSLukasz Kazmierczak         .privileges(redfish::privileges::patchMetricReportDefinition)
15889e6c388aSLukasz Kazmierczak         .methods(boost::beast::http::verb::patch)(
15899e6c388aSLukasz Kazmierczak             std::bind_front(telemetry::handleReportPatch, std::ref(app)));
15904dbb8aeaSWludzik, Jozef }
1591081ebf06SWludzik, Jozef } // namespace redfish
1592