blob: 02885d2d7467901075e71c469095b1e34e6de312 [file] [log] [blame]
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +02001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "app.hpp"
Ed Tanous539d8c62024-06-19 14:38:27 -07004#include "generated/enums/metric_definition.hpp"
Szymon Dompkedd1c4a92022-03-04 13:11:38 +01005#include "generated/enums/resource.hpp"
6#include "generated/enums/triggers.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007#include "query.hpp"
8#include "registries/privilege_registry.hpp"
Szymon Dompkedd1c4a92022-03-04 13:11:38 +01009#include "sensors.hpp"
10#include "utility.hpp"
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +020011#include "utils/collection.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012#include "utils/dbus_utils.hpp"
Ed Tanous5b904292024-04-16 11:10:17 -070013#include "utils/json_utils.hpp"
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +020014#include "utils/telemetry_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080015#include "utils/time_utils.hpp"
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +020016
Ed Tanousef4c65b2023-04-24 15:28:50 -070017#include <boost/url/format.hpp>
Krzysztof Grobelny89474492022-09-06 16:30:38 +020018#include <sdbusplus/asio/property.hpp>
19#include <sdbusplus/unpack_properties.hpp>
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +020020
George Liu7a1dbc42022-12-07 16:03:22 +080021#include <array>
22#include <string_view>
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020023#include <tuple>
24#include <variant>
25#include <vector>
26
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +020027namespace redfish
28{
29namespace telemetry
30{
31constexpr const char* triggerInterface =
32 "xyz.openbmc_project.Telemetry.Trigger";
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +020033
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020034using NumericThresholdParams =
35 std::tuple<std::string, uint64_t, std::string, double>;
36
37using DiscreteThresholdParams =
38 std::tuple<std::string, std::string, uint64_t, std::string>;
39
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010040using TriggerThresholdParams =
41 std::variant<std::vector<NumericThresholdParams>,
42 std::vector<DiscreteThresholdParams>>;
43
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020044using TriggerThresholdParamsExt =
45 std::variant<std::monostate, std::vector<NumericThresholdParams>,
46 std::vector<DiscreteThresholdParams>>;
47
48using TriggerSensorsParams =
49 std::vector<std::pair<sdbusplus::message::object_path, std::string>>;
50
51using TriggerGetParamsVariant =
52 std::variant<std::monostate, bool, std::string, TriggerThresholdParamsExt,
Szymon Dompke3f215c92022-02-22 13:58:00 +010053 TriggerSensorsParams, std::vector<std::string>,
54 std::vector<sdbusplus::message::object_path>>;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020055
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010056inline triggers::TriggerActionEnum
57 toRedfishTriggerAction(std::string_view dbusValue)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020058{
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010059 if (dbusValue ==
60 "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.UpdateReport")
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020061 {
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010062 return triggers::TriggerActionEnum::RedfishMetricReport;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020063 }
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010064 if (dbusValue ==
65 "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToRedfishEventLog")
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020066 {
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010067 return triggers::TriggerActionEnum::RedfishEvent;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020068 }
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010069 if (dbusValue ==
70 "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToJournal")
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020071 {
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010072 return triggers::TriggerActionEnum::LogToLogService;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020073 }
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010074 return triggers::TriggerActionEnum::Invalid;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +020075}
76
Szymon Dompkedd1c4a92022-03-04 13:11:38 +010077inline std::string toDbusTriggerAction(std::string_view redfishValue)
78{
79 if (redfishValue == "RedfishMetricReport")
80 {
81 return "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.UpdateReport";
82 }
83 if (redfishValue == "RedfishEvent")
84 {
85 return "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToRedfishEventLog";
86 }
87 if (redfishValue == "LogToLogService")
88 {
89 return "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToJournal";
90 }
91 return "";
92}
93
94inline std::string toDbusSeverity(std::string_view redfishValue)
95{
96 if (redfishValue == "OK")
97 {
98 return "xyz.openbmc_project.Telemetry.Trigger.Severity.OK";
99 }
100 if (redfishValue == "Warning")
101 {
102 return "xyz.openbmc_project.Telemetry.Trigger.Severity.Warning";
103 }
104 if (redfishValue == "Critical")
105 {
106 return "xyz.openbmc_project.Telemetry.Trigger.Severity.Critical";
107 }
108 return "";
109}
110
111inline resource::Health toRedfishSeverity(std::string_view dbusValue)
112{
113 if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Severity.OK")
114 {
115 return resource::Health::OK;
116 }
117 if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Severity.Warning")
118 {
119 return resource::Health::Warning;
120 }
121 if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Severity.Critical")
122 {
123 return resource::Health::Critical;
124 }
125 return resource::Health::Invalid;
126}
127
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100128inline std::string toRedfishThresholdName(std::string_view dbusValue)
129{
130 if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical")
131 {
132 return "UpperCritical";
133 }
134
135 if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.LowerCritical")
136 {
137 return "LowerCritical";
138 }
139
140 if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning")
141 {
142 return "UpperWarning";
143 }
144
145 if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning")
146 {
147 return "LowerWarning";
148 }
149
150 return "";
151}
152
153inline std::string toDbusActivation(std::string_view redfishValue)
154{
155 if (redfishValue == "Either")
156 {
157 return "xyz.openbmc_project.Telemetry.Trigger.Direction.Either";
158 }
159
160 if (redfishValue == "Decreasing")
161 {
162 return "xyz.openbmc_project.Telemetry.Trigger.Direction.Decreasing";
163 }
164
165 if (redfishValue == "Increasing")
166 {
167 return "xyz.openbmc_project.Telemetry.Trigger.Direction.Increasing";
168 }
169
170 return "";
171}
172
173inline triggers::ThresholdActivation
174 toRedfishActivation(std::string_view dbusValue)
175{
176 if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Direction.Either")
177 {
178 return triggers::ThresholdActivation::Either;
179 }
180
181 if (dbusValue ==
182 "xyz.openbmc_project.Telemetry.Trigger.Direction.Decreasing")
183 {
184 return triggers::ThresholdActivation::Decreasing;
185 }
186
187 if (dbusValue ==
188 "xyz.openbmc_project.Telemetry.Trigger.Direction.Increasing")
189 {
190 return triggers::ThresholdActivation::Increasing;
191 }
192
193 return triggers::ThresholdActivation::Invalid;
194}
195
196enum class MetricType
197{
198 Discrete,
199 Numeric
200};
201
202enum class DiscreteCondition
203{
204 Specified,
205 Changed
206};
207
208struct Context
209{
210 std::string id;
211 std::string name;
212 std::vector<std::string> actions;
213 std::vector<std::pair<sdbusplus::message::object_path, std::string>>
214 sensors;
215 std::vector<sdbusplus::message::object_path> reports;
216 TriggerThresholdParams thresholds;
217
218 std::optional<DiscreteCondition> discreteCondition;
219 std::optional<MetricType> metricType;
220 std::optional<std::vector<std::string>> metricProperties;
221};
222
223inline std::optional<sdbusplus::message::object_path>
224 getReportPathFromReportDefinitionUri(const std::string& uri)
225{
Ed Tanous6fd29552023-10-04 09:40:14 -0700226 boost::system::result<boost::urls::url_view> parsed =
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100227 boost::urls::parse_relative_ref(uri);
228
229 if (!parsed)
230 {
231 return std::nullopt;
232 }
233
234 std::string id;
235 if (!crow::utility::readUrlSegments(
236 *parsed, "redfish", "v1", "TelemetryService",
237 "MetricReportDefinitions", std::ref(id)))
238 {
239 return std::nullopt;
240 }
241
242 return sdbusplus::message::object_path(
243 "/xyz/openbmc_project/Telemetry/Reports") /
244 "TelemetryService" / id;
245}
246
247inline std::optional<MetricType> getMetricType(const std::string& metricType)
248{
249 if (metricType == "Discrete")
250 {
251 return MetricType::Discrete;
252 }
253 if (metricType == "Numeric")
254 {
255 return MetricType::Numeric;
256 }
257 return std::nullopt;
258}
259
260inline std::optional<DiscreteCondition>
261 getDiscreteCondition(const std::string& discreteTriggerCondition)
262{
263 if (discreteTriggerCondition == "Specified")
264 {
265 return DiscreteCondition::Specified;
266 }
267 if (discreteTriggerCondition == "Changed")
268 {
269 return DiscreteCondition::Changed;
270 }
271 return std::nullopt;
272}
273
Ed Tanous2932dcb2024-03-06 12:04:47 -0800274inline bool parseThreshold(crow::Response& res,
275 nlohmann::json::object_t& threshold,
276 std::string_view dbusThresholdName,
277 std::vector<NumericThresholdParams>& parsedParams)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100278{
Ed Tanous2932dcb2024-03-06 12:04:47 -0800279 double reading = 0.0;
280 std::string activation;
281 std::string dwellTimeStr;
282
283 if (!json_util::readJsonObject(threshold, res, "Reading", reading,
284 "Activation", activation, "DwellTime",
285 dwellTimeStr))
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100286 {
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100287 return false;
288 }
289
Ed Tanous2932dcb2024-03-06 12:04:47 -0800290 std::string dbusActivation = toDbusActivation(activation);
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100291
Ed Tanous2932dcb2024-03-06 12:04:47 -0800292 if (dbusActivation.empty())
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100293 {
Ed Tanous2932dcb2024-03-06 12:04:47 -0800294 messages::propertyValueIncorrect(res, "Activation", activation);
295 return false;
296 }
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100297
Ed Tanous2932dcb2024-03-06 12:04:47 -0800298 std::optional<std::chrono::milliseconds> dwellTime =
299 time_utils::fromDurationString(dwellTimeStr);
300 if (!dwellTime)
301 {
302 messages::propertyValueIncorrect(res, "DwellTime", dwellTimeStr);
303 return false;
304 }
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100305
Ed Tanous2932dcb2024-03-06 12:04:47 -0800306 parsedParams.emplace_back(dbusThresholdName,
307 static_cast<uint64_t>(dwellTime->count()),
308 dbusActivation, reading);
309 return true;
310}
311
312struct NumericThresholds
313{
314 std::optional<nlohmann::json::object_t> upperCritical;
315 std::optional<nlohmann::json::object_t> upperWarning;
316 std::optional<nlohmann::json::object_t> lowerWarning;
317 std::optional<nlohmann::json::object_t> lowerCritical;
318
319 bool any() const
320 {
321 return upperCritical || upperWarning || lowerWarning || lowerCritical;
322 }
323};
324
325inline bool parseNumericThresholds(crow::Response& res,
326 NumericThresholds& numericThresholds,
327 Context& ctx)
328{
329 std::vector<NumericThresholdParams> parsedParams;
330 if (numericThresholds.upperCritical)
331 {
332 if (!parseThreshold(
333 res, *numericThresholds.upperCritical,
334 "xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical",
335 parsedParams))
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100336 {
337 return false;
338 }
Ed Tanous2932dcb2024-03-06 12:04:47 -0800339 }
340 if (numericThresholds.upperWarning)
341 {
342 if (!parseThreshold(
343 res, *numericThresholds.upperWarning,
344 "xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning",
345 parsedParams))
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100346 {
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100347 return false;
348 }
Ed Tanous2932dcb2024-03-06 12:04:47 -0800349 }
350 if (numericThresholds.lowerWarning)
351 {
352 if (!parseThreshold(
353 res, *numericThresholds.lowerWarning,
354 "xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning",
355 parsedParams))
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100356 {
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100357 return false;
358 }
Ed Tanous2932dcb2024-03-06 12:04:47 -0800359 }
360 if (numericThresholds.lowerCritical)
361 {
362 if (!parseThreshold(
363 res, *numericThresholds.lowerCritical,
364 "xyz.openbmc_project.Telemetry.Trigger.Type.LowerCritical",
365 parsedParams))
366 {
367 return false;
368 }
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100369 }
370
371 ctx.thresholds = std::move(parsedParams);
372 return true;
373}
374
375inline bool parseDiscreteTriggers(
376 crow::Response& res,
Ed Tanous2932dcb2024-03-06 12:04:47 -0800377 std::optional<std::vector<nlohmann::json::object_t>>& discreteTriggers,
378 Context& ctx)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100379{
380 std::vector<DiscreteThresholdParams> parsedParams;
381 if (!discreteTriggers)
382 {
383 ctx.thresholds = std::move(parsedParams);
384 return true;
385 }
386
387 parsedParams.reserve(discreteTriggers->size());
Ed Tanous2932dcb2024-03-06 12:04:47 -0800388 for (nlohmann::json::object_t& thresholdInfo : *discreteTriggers)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100389 {
390 std::optional<std::string> name = "";
391 std::string value;
392 std::string dwellTimeStr;
393 std::string severity;
394
Ed Tanous2932dcb2024-03-06 12:04:47 -0800395 if (!json_util::readJsonObject(thresholdInfo, res, "Name", name,
396 "Value", value, "DwellTime",
397 dwellTimeStr, "Severity", severity))
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100398 {
399 return false;
400 }
401
402 std::optional<std::chrono::milliseconds> dwellTime =
403 time_utils::fromDurationString(dwellTimeStr);
404 if (!dwellTime)
405 {
406 messages::propertyValueIncorrect(res, "DwellTime", dwellTimeStr);
407 return false;
408 }
409
410 std::string dbusSeverity = toDbusSeverity(severity);
411 if (dbusSeverity.empty())
412 {
413 messages::propertyValueIncorrect(res, "Severity", severity);
414 return false;
415 }
416
417 parsedParams.emplace_back(*name, dbusSeverity,
418 static_cast<uint64_t>(dwellTime->count()),
419 value);
420 }
421
422 ctx.thresholds = std::move(parsedParams);
423 return true;
424}
425
426inline bool parseTriggerThresholds(
427 crow::Response& res,
Ed Tanous2932dcb2024-03-06 12:04:47 -0800428 std::optional<std::vector<nlohmann::json::object_t>>& discreteTriggers,
429 NumericThresholds& numericThresholds, Context& ctx)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100430{
Ed Tanous2932dcb2024-03-06 12:04:47 -0800431 if (discreteTriggers && numericThresholds.any())
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100432 {
433 messages::propertyValueConflict(res, "DiscreteTriggers",
434 "NumericThresholds");
435 messages::propertyValueConflict(res, "NumericThresholds",
436 "DiscreteTriggers");
437 return false;
438 }
439
440 if (ctx.discreteCondition)
441 {
Ed Tanous2932dcb2024-03-06 12:04:47 -0800442 if (numericThresholds.any())
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100443 {
444 messages::propertyValueConflict(res, "DiscreteTriggerCondition",
445 "NumericThresholds");
446 messages::propertyValueConflict(res, "NumericThresholds",
447 "DiscreteTriggerCondition");
448 return false;
449 }
450 }
451
452 if (ctx.metricType)
453 {
Ed Tanous2932dcb2024-03-06 12:04:47 -0800454 if (*ctx.metricType == MetricType::Discrete && numericThresholds.any())
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100455 {
456 messages::propertyValueConflict(res, "NumericThresholds",
457 "MetricType");
458 return false;
459 }
460 if (*ctx.metricType == MetricType::Numeric && discreteTriggers)
461 {
462 messages::propertyValueConflict(res, "DiscreteTriggers",
463 "MetricType");
464 return false;
465 }
466 if (*ctx.metricType == MetricType::Numeric && ctx.discreteCondition)
467 {
468 messages::propertyValueConflict(res, "DiscreteTriggers",
469 "DiscreteTriggerCondition");
470 return false;
471 }
472 }
473
474 if (discreteTriggers || ctx.discreteCondition ||
475 (ctx.metricType && *ctx.metricType == MetricType::Discrete))
476 {
477 if (ctx.discreteCondition)
478 {
479 if (*ctx.discreteCondition == DiscreteCondition::Specified &&
480 !discreteTriggers)
481 {
482 messages::createFailedMissingReqProperties(res,
483 "DiscreteTriggers");
484 return false;
485 }
486 if (discreteTriggers &&
487 ((*ctx.discreteCondition == DiscreteCondition::Specified &&
488 discreteTriggers->empty()) ||
489 (*ctx.discreteCondition == DiscreteCondition::Changed &&
490 !discreteTriggers->empty())))
491 {
492 messages::propertyValueConflict(res, "DiscreteTriggers",
493 "DiscreteTriggerCondition");
494 return false;
495 }
496 }
497 if (!parseDiscreteTriggers(res, discreteTriggers, ctx))
498 {
499 return false;
500 }
501 }
Ed Tanous2932dcb2024-03-06 12:04:47 -0800502 else if (numericThresholds.any())
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100503 {
Ed Tanous2932dcb2024-03-06 12:04:47 -0800504 if (!parseNumericThresholds(res, numericThresholds, ctx))
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100505 {
506 return false;
507 }
508 }
509 else
510 {
511 messages::createFailedMissingReqProperties(
512 res, "'DiscreteTriggers', 'NumericThresholds', "
513 "'DiscreteTriggerCondition' or 'MetricType'");
514 return false;
515 }
516 return true;
517}
518
Ed Tanous2932dcb2024-03-06 12:04:47 -0800519inline bool parseLinks(crow::Response& res,
520 const std::vector<std::string>& metricReportDefinitions,
521 Context& ctx)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100522{
Ed Tanous2932dcb2024-03-06 12:04:47 -0800523 ctx.reports.reserve(metricReportDefinitions.size());
524 for (const std::string& reportDefinionUri : metricReportDefinitions)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100525 {
Ed Tanous2932dcb2024-03-06 12:04:47 -0800526 std::optional<sdbusplus::message::object_path> reportPath =
527 getReportPathFromReportDefinitionUri(reportDefinionUri);
528 if (!reportPath)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100529 {
Ed Tanous2932dcb2024-03-06 12:04:47 -0800530 messages::propertyValueIncorrect(res, "MetricReportDefinitions",
531 reportDefinionUri);
532 return false;
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100533 }
Ed Tanous2932dcb2024-03-06 12:04:47 -0800534 ctx.reports.emplace_back(*reportPath);
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100535 }
536 return true;
537}
538
539inline bool parseMetricProperties(crow::Response& res, Context& ctx)
540{
541 if (!ctx.metricProperties)
542 {
543 return true;
544 }
545
546 ctx.sensors.reserve(ctx.metricProperties->size());
547
548 size_t uriIdx = 0;
549 for (const std::string& uriStr : *ctx.metricProperties)
550 {
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700551 boost::system::result<boost::urls::url> uri =
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100552 boost::urls::parse_relative_ref(uriStr);
553 if (!uri)
554 {
555 messages::propertyValueIncorrect(
556 res, "MetricProperties/" + std::to_string(uriIdx), uriStr);
557 return false;
558 }
559 std::string chassisName;
560 std::string sensorName;
561 if (!crow::utility::readUrlSegments(*uri, "redfish", "v1", "Chassis",
562 std::ref(chassisName), "Sensors",
563 std::ref(sensorName)))
564 {
565 messages::propertyValueIncorrect(
566 res, "MetricProperties/" + std::to_string(uriIdx), uriStr);
567 return false;
568 }
569
570 std::pair<std::string, std::string> split =
571 splitSensorNameAndType(sensorName);
572 if (split.first.empty() || split.second.empty())
573 {
574 messages::propertyValueIncorrect(
575 res, "MetricProperties/" + std::to_string(uriIdx), uriStr);
576 return false;
577 }
578
579 std::string sensorPath = "/xyz/openbmc_project/sensors/" + split.first +
580 '/' + split.second;
581
582 ctx.sensors.emplace_back(sensorPath, uriStr);
583 uriIdx++;
584 }
585 return true;
586}
587
588inline bool parsePostTriggerParams(crow::Response& res,
589 const crow::Request& req, Context& ctx)
590{
591 std::optional<std::string> id = "";
592 std::optional<std::string> name = "";
593 std::optional<std::string> metricType;
594 std::optional<std::vector<std::string>> triggerActions;
595 std::optional<std::string> discreteTriggerCondition;
Ed Tanous2932dcb2024-03-06 12:04:47 -0800596 std::optional<std::vector<nlohmann::json::object_t>> discreteTriggers;
597 std::optional<std::vector<std::string>> metricReportDefinitions;
598 NumericThresholds thresholds;
599 // clang-format off
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100600 if (!json_util::readJsonPatch(
Ed Tanous2932dcb2024-03-06 12:04:47 -0800601 req, res,
602 "Id", id,
603 "Name", name,
604 "MetricType", metricType,
605 "TriggerActions", triggerActions,
606 "DiscreteTriggerCondition", discreteTriggerCondition,
607 "DiscreteTriggers", discreteTriggers,
608 "NumericThresholds/UpperCritical", thresholds.upperCritical,
609 "NumericThresholds/UpperWarning", thresholds.upperWarning,
610 "NumericThresholds/LowerWarning", thresholds.lowerWarning,
611 "NumericThresholds/LowerCritical", thresholds.lowerCritical,
612 "MetricProperties", ctx.metricProperties,
613 "Links/MetricReportDefinitions", metricReportDefinitions)
614 )
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100615 {
616 return false;
617 }
Ed Tanous2932dcb2024-03-06 12:04:47 -0800618 // clang-format on
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100619
620 ctx.id = *id;
621 ctx.name = *name;
622
623 if (metricType)
624 {
Ed Tanousd5736ef2023-07-06 10:37:23 -0700625 ctx.metricType = getMetricType(*metricType);
626 if (!ctx.metricType)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100627 {
628 messages::propertyValueIncorrect(res, "MetricType", *metricType);
629 return false;
630 }
631 }
632
633 if (discreteTriggerCondition)
634 {
Ed Tanousd5736ef2023-07-06 10:37:23 -0700635 ctx.discreteCondition = getDiscreteCondition(*discreteTriggerCondition);
636 if (!ctx.discreteCondition)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100637 {
638 messages::propertyValueIncorrect(res, "DiscreteTriggerCondition",
639 *discreteTriggerCondition);
640 return false;
641 }
642 }
643
644 if (triggerActions)
645 {
646 ctx.actions.reserve(triggerActions->size());
647 for (const std::string& action : *triggerActions)
648 {
649 std::string dbusAction = toDbusTriggerAction(action);
650
651 if (dbusAction.empty())
652 {
653 messages::propertyValueNotInList(res, action, "TriggerActions");
654 return false;
655 }
656
657 ctx.actions.emplace_back(dbusAction);
658 }
659 }
660 if (!parseMetricProperties(res, ctx))
661 {
662 return false;
663 }
664
Ed Tanous2932dcb2024-03-06 12:04:47 -0800665 if (!parseTriggerThresholds(res, discreteTriggers, thresholds, ctx))
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100666 {
667 return false;
668 }
669
Ed Tanous2932dcb2024-03-06 12:04:47 -0800670 if (metricReportDefinitions)
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100671 {
Ed Tanous2932dcb2024-03-06 12:04:47 -0800672 if (!parseLinks(res, *metricReportDefinitions, ctx))
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100673 {
674 return false;
675 }
676 }
677 return true;
678}
679
680inline void afterCreateTrigger(
681 const boost::system::error_code& ec, const std::string& dbusPath,
682 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
683{
684 if (ec == boost::system::errc::file_exists)
685 {
686 messages::resourceAlreadyExists(asyncResp->res, "Trigger", "Id", id);
687 return;
688 }
689 if (ec == boost::system::errc::too_many_files_open)
690 {
691 messages::createLimitReachedForResource(asyncResp->res);
692 return;
693 }
694 if (ec)
695 {
696 messages::internalError(asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -0700697 BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100698 return;
699 }
700
701 const std::optional<std::string>& triggerId =
702 getTriggerIdFromDbusPath(dbusPath);
703 if (!triggerId)
704 {
705 messages::internalError(asyncResp->res);
Ed Tanous62598e32023-07-17 17:06:25 -0700706 BMCWEB_LOG_ERROR("Unknown data returned by "
707 "AddTrigger DBus method");
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100708 return;
709 }
710
711 messages::created(asyncResp->res);
712 boost::urls::url locationUrl = boost::urls::format(
713 "/redfish/v1/TelemetryService/Triggers/{}", *triggerId);
714 asyncResp->res.addHeader("Location", locationUrl.buffer());
715}
716
717inline std::optional<nlohmann::json::array_t>
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200718 getTriggerActions(const std::vector<std::string>& dbusActions)
719{
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100720 nlohmann::json::array_t triggerActions;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200721 for (const std::string& dbusAction : dbusActions)
722 {
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100723 triggers::TriggerActionEnum redfishAction =
724 toRedfishTriggerAction(dbusAction);
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200725
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100726 if (redfishAction == triggers::TriggerActionEnum::Invalid)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200727 {
728 return std::nullopt;
729 }
730
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100731 triggerActions.emplace_back(redfishAction);
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200732 }
733
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100734 return triggerActions;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200735}
736
Szymon Dompke3f215c92022-02-22 13:58:00 +0100737inline std::optional<nlohmann::json::array_t>
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200738 getDiscreteTriggers(const TriggerThresholdParamsExt& thresholdParams)
739{
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100740 nlohmann::json::array_t triggers;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200741 const std::vector<DiscreteThresholdParams>* discreteParams =
742 std::get_if<std::vector<DiscreteThresholdParams>>(&thresholdParams);
743
Ed Tanouse662eae2022-01-25 10:39:19 -0800744 if (discreteParams == nullptr)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200745 {
746 return std::nullopt;
747 }
748
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200749 for (const auto& [name, severity, dwellTime, value] : *discreteParams)
750 {
751 std::optional<std::string> duration =
752 time_utils::toDurationStringFromUint(dwellTime);
753
754 if (!duration)
755 {
756 return std::nullopt;
757 }
Ed Tanous613dabe2022-07-09 11:17:36 -0700758 nlohmann::json::object_t trigger;
759 trigger["Name"] = name;
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100760 trigger["Severity"] = toRedfishSeverity(severity);
Ed Tanous613dabe2022-07-09 11:17:36 -0700761 trigger["DwellTime"] = *duration;
762 trigger["Value"] = value;
Patrick Williamsad539542023-05-12 10:10:08 -0500763 triggers.emplace_back(std::move(trigger));
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200764 }
765
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100766 return triggers;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200767}
768
769inline std::optional<nlohmann::json>
770 getNumericThresholds(const TriggerThresholdParamsExt& thresholdParams)
771{
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100772 nlohmann::json::object_t thresholds;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200773 const std::vector<NumericThresholdParams>* numericParams =
774 std::get_if<std::vector<NumericThresholdParams>>(&thresholdParams);
775
Ed Tanouse662eae2022-01-25 10:39:19 -0800776 if (numericParams == nullptr)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200777 {
778 return std::nullopt;
779 }
780
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200781 for (const auto& [type, dwellTime, activation, reading] : *numericParams)
782 {
783 std::optional<std::string> duration =
784 time_utils::toDurationStringFromUint(dwellTime);
785
786 if (!duration)
787 {
788 return std::nullopt;
789 }
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100790 nlohmann::json& threshold = thresholds[toRedfishThresholdName(type)];
Ed Tanous14766872022-03-15 10:44:42 -0700791 threshold["Reading"] = reading;
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100792 threshold["Activation"] = toRedfishActivation(activation);
Ed Tanous14766872022-03-15 10:44:42 -0700793 threshold["DwellTime"] = *duration;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200794 }
795
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100796 return thresholds;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200797}
798
Szymon Dompke3f215c92022-02-22 13:58:00 +0100799inline std::optional<nlohmann::json> getMetricReportDefinitions(
800 const std::vector<sdbusplus::message::object_path>& reportPaths)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200801{
802 nlohmann::json reports = nlohmann::json::array();
Szymon Dompke3f215c92022-02-22 13:58:00 +0100803
804 for (const sdbusplus::message::object_path& path : reportPaths)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200805 {
Szymon Dompke3f215c92022-02-22 13:58:00 +0100806 std::string reportId = path.filename();
807 if (reportId.empty())
808 {
809 {
Ed Tanous62598e32023-07-17 17:06:25 -0700810 BMCWEB_LOG_ERROR("Property Reports contains invalid value: {}",
811 path.str);
Szymon Dompke3f215c92022-02-22 13:58:00 +0100812 return std::nullopt;
813 }
814 }
815
Ed Tanous14766872022-03-15 10:44:42 -0700816 nlohmann::json::object_t report;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700817 report["@odata.id"] = boost::urls::format(
818 "/redfish/v1/TelemetryService/MetricReportDefinitions/{}",
819 reportId);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500820 reports.emplace_back(std::move(report));
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200821 }
822
Szymon Dompke3f215c92022-02-22 13:58:00 +0100823 return {std::move(reports)};
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200824}
825
826inline std::vector<std::string>
827 getMetricProperties(const TriggerSensorsParams& sensors)
828{
829 std::vector<std::string> metricProperties;
830 metricProperties.reserve(sensors.size());
831 for (const auto& [_, metadata] : sensors)
832 {
833 metricProperties.emplace_back(metadata);
834 }
835
836 return metricProperties;
837}
838
839inline bool fillTrigger(
840 nlohmann::json& json, const std::string& id,
841 const std::vector<std::pair<std::string, TriggerGetParamsVariant>>&
842 properties)
843{
844 const std::string* name = nullptr;
845 const bool* discrete = nullptr;
846 const TriggerSensorsParams* sensors = nullptr;
Szymon Dompke3f215c92022-02-22 13:58:00 +0100847 const std::vector<sdbusplus::message::object_path>* reports = nullptr;
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200848 const std::vector<std::string>* triggerActions = nullptr;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200849 const TriggerThresholdParamsExt* thresholds = nullptr;
850
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200851 const bool success = sdbusplus::unpackPropertiesNoThrow(
852 dbus_utils::UnpackErrorPrinter(), properties, "Name", name, "Discrete",
853 discrete, "Sensors", sensors, "Reports", reports, "TriggerActions",
854 triggerActions, "Thresholds", thresholds);
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200855
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200856 if (!success)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200857 {
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200858 return false;
859 }
860
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200861 if (triggerActions != nullptr)
Szymon Dompke3f215c92022-02-22 13:58:00 +0100862 {
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100863 std::optional<nlohmann::json::array_t> redfishTriggerActions =
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200864 getTriggerActions(*triggerActions);
865 if (!redfishTriggerActions)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200866 {
Ed Tanous62598e32023-07-17 17:06:25 -0700867 BMCWEB_LOG_ERROR(
868 "Property TriggerActions is invalid in Trigger: {}", id);
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200869 return false;
870 }
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100871 json["TriggerActions"] = *redfishTriggerActions;
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200872 }
873
874 if (reports != nullptr)
875 {
876 std::optional<nlohmann::json> linkedReports =
877 getMetricReportDefinitions(*reports);
878 if (!linkedReports)
879 {
Ed Tanous62598e32023-07-17 17:06:25 -0700880 BMCWEB_LOG_ERROR("Property Reports is invalid in Trigger: {}", id);
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200881 return false;
882 }
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200883 json["Links"]["MetricReportDefinitions"] = *linkedReports;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200884 }
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200885
886 if (discrete != nullptr)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200887 {
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200888 if (*discrete)
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200889 {
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200890 std::optional<nlohmann::json::array_t> discreteTriggers =
891 getDiscreteTriggers(*thresholds);
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200892
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200893 if (!discreteTriggers)
894 {
Ed Tanous62598e32023-07-17 17:06:25 -0700895 BMCWEB_LOG_ERROR("Property Thresholds is invalid for discrete "
896 "triggers in Trigger: {}",
897 id);
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200898 return false;
899 }
900
901 json["DiscreteTriggers"] = *discreteTriggers;
902 json["DiscreteTriggerCondition"] =
903 discreteTriggers->empty() ? "Changed" : "Specified";
Ed Tanous539d8c62024-06-19 14:38:27 -0700904 json["MetricType"] = metric_definition::MetricType::Discrete;
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200905 }
906 else
907 {
908 std::optional<nlohmann::json> numericThresholds =
909 getNumericThresholds(*thresholds);
910
911 if (!numericThresholds)
912 {
Ed Tanous62598e32023-07-17 17:06:25 -0700913 BMCWEB_LOG_ERROR("Property Thresholds is invalid for numeric "
914 "thresholds in Trigger: {}",
915 id);
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200916 return false;
917 }
918
919 json["NumericThresholds"] = *numericThresholds;
Ed Tanous539d8c62024-06-19 14:38:27 -0700920 json["MetricType"] = metric_definition::MetricType::Numeric;
Krzysztof Grobelny89474492022-09-06 16:30:38 +0200921 }
922 }
923
924 if (name != nullptr)
925 {
926 json["Name"] = *name;
927 }
928
929 if (sensors != nullptr)
930 {
931 json["MetricProperties"] = getMetricProperties(*sensors);
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200932 }
933
Szymon Dompke3f215c92022-02-22 13:58:00 +0100934 json["@odata.type"] = "#Triggers.v1_2_0.Triggers";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700935 json["@odata.id"] =
936 boost::urls::format("/redfish/v1/TelemetryService/Triggers/{}", id);
Szymon Dompke3f215c92022-02-22 13:58:00 +0100937 json["Id"] = id;
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +0200938
939 return true;
940}
941
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100942inline void handleTriggerCollectionPost(
943 App& app, const crow::Request& req,
944 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
945{
946 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
947 {
948 return;
949 }
950
951 telemetry::Context ctx;
952 if (!telemetry::parsePostTriggerParams(asyncResp->res, req, ctx))
953 {
954 return;
955 }
956
957 crow::connections::systemBus->async_method_call(
958 [asyncResp, id = ctx.id](const boost::system::error_code& ec,
959 const std::string& dbusPath) {
960 afterCreateTrigger(ec, dbusPath, asyncResp, id);
Patrick Williams5a39f772023-10-20 11:20:21 -0500961 },
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100962 service, "/xyz/openbmc_project/Telemetry/Triggers",
963 "xyz.openbmc_project.Telemetry.TriggerManager", "AddTrigger",
964 "TelemetryService/" + ctx.id, ctx.name, ctx.actions, ctx.sensors,
965 ctx.reports, ctx.thresholds);
966}
967
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +0200968} // namespace telemetry
969
970inline void requestRoutesTriggerCollection(App& app)
971{
972 BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/")
973 .privileges(redfish::privileges::getTriggersCollection)
974 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700975 [&app](const crow::Request& req,
976 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000977 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700978 {
979 return;
980 }
981 asyncResp->res.jsonValue["@odata.type"] =
982 "#TriggersCollection.TriggersCollection";
Willy Tuae9031f2022-09-27 05:48:07 +0000983 asyncResp->res.jsonValue["@odata.id"] =
984 "/redfish/v1/TelemetryService/Triggers";
Ed Tanous002d39b2022-05-31 08:59:27 -0700985 asyncResp->res.jsonValue["Name"] = "Triggers Collection";
George Liu7a1dbc42022-12-07 16:03:22 +0800986 constexpr std::array<std::string_view, 1> interfaces{
987 telemetry::triggerInterface};
Ed Tanous002d39b2022-05-31 08:59:27 -0700988 collection_util::getCollectionMembers(
Willy Tuae9031f2022-09-27 05:48:07 +0000989 asyncResp,
990 boost::urls::url("/redfish/v1/TelemetryService/Triggers"),
991 interfaces,
Ed Tanous002d39b2022-05-31 08:59:27 -0700992 "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService");
Patrick Williams5a39f772023-10-20 11:20:21 -0500993 });
Szymon Dompkedd1c4a92022-03-04 13:11:38 +0100994
995 BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/")
996 .privileges(redfish::privileges::postTriggersCollection)
997 .methods(boost::beast::http::verb::post)(std::bind_front(
998 telemetry::handleTriggerCollectionPost, std::ref(app)));
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +0200999}
1000
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +02001001inline void requestRoutesTrigger(App& app)
1002{
1003 BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/<str>/")
1004 .privileges(redfish::privileges::getTriggers)
1005 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001006 [&app](const crow::Request& req,
1007 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1008 const std::string& id) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001009 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001010 {
1011 return;
1012 }
Krzysztof Grobelny89474492022-09-06 16:30:38 +02001013 sdbusplus::asio::getAllProperties(
1014 *crow::connections::systemBus, telemetry::service,
1015 telemetry::getDbusTriggerPath(id), telemetry::triggerInterface,
Ed Tanous002d39b2022-05-31 08:59:27 -07001016 [asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001017 id](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001018 const std::vector<std::pair<
1019 std::string, telemetry::TriggerGetParamsVariant>>& ret) {
1020 if (ec.value() == EBADR ||
1021 ec == boost::system::errc::host_unreachable)
1022 {
1023 messages::resourceNotFound(asyncResp->res, "Triggers", id);
1024 return;
1025 }
1026 if (ec)
1027 {
Ed Tanous62598e32023-07-17 17:06:25 -07001028 BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001029 messages::internalError(asyncResp->res);
1030 return;
1031 }
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +02001032
Ed Tanous002d39b2022-05-31 08:59:27 -07001033 if (!telemetry::fillTrigger(asyncResp->res.jsonValue, id, ret))
1034 {
1035 messages::internalError(asyncResp->res);
1036 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001037 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001038 });
Szymon Dompke163994a2021-08-12 17:30:23 +02001039
1040 BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/<str>/")
1041 .privileges(redfish::privileges::deleteTriggers)
1042 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001043 [&app](const crow::Request& req,
1044 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1045 const std::string& id) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001046 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001047 {
1048 return;
1049 }
1050 const std::string triggerPath = telemetry::getDbusTriggerPath(id);
Szymon Dompke163994a2021-08-12 17:30:23 +02001051
Ed Tanous002d39b2022-05-31 08:59:27 -07001052 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001053 [asyncResp, id](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001054 if (ec.value() == EBADR)
1055 {
1056 messages::resourceNotFound(asyncResp->res, "Triggers", id);
1057 return;
1058 }
Szymon Dompke163994a2021-08-12 17:30:23 +02001059
Ed Tanous002d39b2022-05-31 08:59:27 -07001060 if (ec)
1061 {
Ed Tanous62598e32023-07-17 17:06:25 -07001062 BMCWEB_LOG_ERROR("respHandler DBus error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001063 messages::internalError(asyncResp->res);
1064 return;
1065 }
Szymon Dompke163994a2021-08-12 17:30:23 +02001066
Ed Tanous002d39b2022-05-31 08:59:27 -07001067 asyncResp->res.result(boost::beast::http::status::no_content);
Patrick Williams5a39f772023-10-20 11:20:21 -05001068 },
Ed Tanous002d39b2022-05-31 08:59:27 -07001069 telemetry::service, triggerPath,
1070 "xyz.openbmc_project.Object.Delete", "Delete");
Patrick Williams5a39f772023-10-20 11:20:21 -05001071 });
Lukasz Kazmierczak1b7e6962021-08-02 13:40:27 +02001072}
1073
Lukasz Kazmierczak07148cf2021-08-02 11:08:53 +02001074} // namespace redfish