Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "app.hpp" |
Ed Tanous | 539d8c6 | 2024-06-19 14:38:27 -0700 | [diff] [blame^] | 4 | #include "generated/enums/metric_definition.hpp" |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 5 | #include "generated/enums/resource.hpp" |
| 6 | #include "generated/enums/triggers.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 7 | #include "query.hpp" |
| 8 | #include "registries/privilege_registry.hpp" |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 9 | #include "sensors.hpp" |
| 10 | #include "utility.hpp" |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 11 | #include "utils/collection.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 12 | #include "utils/dbus_utils.hpp" |
Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 13 | #include "utils/json_utils.hpp" |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 14 | #include "utils/telemetry_utils.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 15 | #include "utils/time_utils.hpp" |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 16 | |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 17 | #include <boost/url/format.hpp> |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 18 | #include <sdbusplus/asio/property.hpp> |
| 19 | #include <sdbusplus/unpack_properties.hpp> |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 20 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 21 | #include <array> |
| 22 | #include <string_view> |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 23 | #include <tuple> |
| 24 | #include <variant> |
| 25 | #include <vector> |
| 26 | |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 27 | namespace redfish |
| 28 | { |
| 29 | namespace telemetry |
| 30 | { |
| 31 | constexpr const char* triggerInterface = |
| 32 | "xyz.openbmc_project.Telemetry.Trigger"; |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 33 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 34 | using NumericThresholdParams = |
| 35 | std::tuple<std::string, uint64_t, std::string, double>; |
| 36 | |
| 37 | using DiscreteThresholdParams = |
| 38 | std::tuple<std::string, std::string, uint64_t, std::string>; |
| 39 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 40 | using TriggerThresholdParams = |
| 41 | std::variant<std::vector<NumericThresholdParams>, |
| 42 | std::vector<DiscreteThresholdParams>>; |
| 43 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 44 | using TriggerThresholdParamsExt = |
| 45 | std::variant<std::monostate, std::vector<NumericThresholdParams>, |
| 46 | std::vector<DiscreteThresholdParams>>; |
| 47 | |
| 48 | using TriggerSensorsParams = |
| 49 | std::vector<std::pair<sdbusplus::message::object_path, std::string>>; |
| 50 | |
| 51 | using TriggerGetParamsVariant = |
| 52 | std::variant<std::monostate, bool, std::string, TriggerThresholdParamsExt, |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 53 | TriggerSensorsParams, std::vector<std::string>, |
| 54 | std::vector<sdbusplus::message::object_path>>; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 55 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 56 | inline triggers::TriggerActionEnum |
| 57 | toRedfishTriggerAction(std::string_view dbusValue) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 58 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 59 | if (dbusValue == |
| 60 | "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.UpdateReport") |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 61 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 62 | return triggers::TriggerActionEnum::RedfishMetricReport; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 63 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 64 | if (dbusValue == |
| 65 | "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToRedfishEventLog") |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 66 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 67 | return triggers::TriggerActionEnum::RedfishEvent; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 68 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 69 | if (dbusValue == |
| 70 | "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToJournal") |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 71 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 72 | return triggers::TriggerActionEnum::LogToLogService; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 73 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 74 | return triggers::TriggerActionEnum::Invalid; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 77 | inline 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 | |
| 94 | inline 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 | |
| 111 | inline 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 Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 128 | inline 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 | |
| 153 | inline 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 | |
| 173 | inline 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 | |
| 196 | enum class MetricType |
| 197 | { |
| 198 | Discrete, |
| 199 | Numeric |
| 200 | }; |
| 201 | |
| 202 | enum class DiscreteCondition |
| 203 | { |
| 204 | Specified, |
| 205 | Changed |
| 206 | }; |
| 207 | |
| 208 | struct 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 | |
| 223 | inline std::optional<sdbusplus::message::object_path> |
| 224 | getReportPathFromReportDefinitionUri(const std::string& uri) |
| 225 | { |
Ed Tanous | 6fd2955 | 2023-10-04 09:40:14 -0700 | [diff] [blame] | 226 | boost::system::result<boost::urls::url_view> parsed = |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 227 | 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 | |
| 247 | inline 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 | |
| 260 | inline 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 Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 274 | inline bool parseThreshold(crow::Response& res, |
| 275 | nlohmann::json::object_t& threshold, |
| 276 | std::string_view dbusThresholdName, |
| 277 | std::vector<NumericThresholdParams>& parsedParams) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 278 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 279 | 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 Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 286 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 290 | std::string dbusActivation = toDbusActivation(activation); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 291 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 292 | if (dbusActivation.empty()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 293 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 294 | messages::propertyValueIncorrect(res, "Activation", activation); |
| 295 | return false; |
| 296 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 297 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 298 | 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 Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 305 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 306 | parsedParams.emplace_back(dbusThresholdName, |
| 307 | static_cast<uint64_t>(dwellTime->count()), |
| 308 | dbusActivation, reading); |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | struct 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 | |
| 325 | inline 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 Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 336 | { |
| 337 | return false; |
| 338 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 339 | } |
| 340 | if (numericThresholds.upperWarning) |
| 341 | { |
| 342 | if (!parseThreshold( |
| 343 | res, *numericThresholds.upperWarning, |
| 344 | "xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning", |
| 345 | parsedParams)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 346 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 347 | return false; |
| 348 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 349 | } |
| 350 | if (numericThresholds.lowerWarning) |
| 351 | { |
| 352 | if (!parseThreshold( |
| 353 | res, *numericThresholds.lowerWarning, |
| 354 | "xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning", |
| 355 | parsedParams)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 356 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 357 | return false; |
| 358 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 359 | } |
| 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 Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | ctx.thresholds = std::move(parsedParams); |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | inline bool parseDiscreteTriggers( |
| 376 | crow::Response& res, |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 377 | std::optional<std::vector<nlohmann::json::object_t>>& discreteTriggers, |
| 378 | Context& ctx) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 379 | { |
| 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 Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 388 | for (nlohmann::json::object_t& thresholdInfo : *discreteTriggers) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 389 | { |
| 390 | std::optional<std::string> name = ""; |
| 391 | std::string value; |
| 392 | std::string dwellTimeStr; |
| 393 | std::string severity; |
| 394 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 395 | if (!json_util::readJsonObject(thresholdInfo, res, "Name", name, |
| 396 | "Value", value, "DwellTime", |
| 397 | dwellTimeStr, "Severity", severity)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 398 | { |
| 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 | |
| 426 | inline bool parseTriggerThresholds( |
| 427 | crow::Response& res, |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 428 | std::optional<std::vector<nlohmann::json::object_t>>& discreteTriggers, |
| 429 | NumericThresholds& numericThresholds, Context& ctx) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 430 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 431 | if (discreteTriggers && numericThresholds.any()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 432 | { |
| 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 Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 442 | if (numericThresholds.any()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 443 | { |
| 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 Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 454 | if (*ctx.metricType == MetricType::Discrete && numericThresholds.any()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 455 | { |
| 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 Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 502 | else if (numericThresholds.any()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 503 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 504 | if (!parseNumericThresholds(res, numericThresholds, ctx)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 505 | { |
| 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 Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 519 | inline bool parseLinks(crow::Response& res, |
| 520 | const std::vector<std::string>& metricReportDefinitions, |
| 521 | Context& ctx) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 522 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 523 | ctx.reports.reserve(metricReportDefinitions.size()); |
| 524 | for (const std::string& reportDefinionUri : metricReportDefinitions) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 525 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 526 | std::optional<sdbusplus::message::object_path> reportPath = |
| 527 | getReportPathFromReportDefinitionUri(reportDefinionUri); |
| 528 | if (!reportPath) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 529 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 530 | messages::propertyValueIncorrect(res, "MetricReportDefinitions", |
| 531 | reportDefinionUri); |
| 532 | return false; |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 533 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 534 | ctx.reports.emplace_back(*reportPath); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 535 | } |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | inline 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 Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 551 | boost::system::result<boost::urls::url> uri = |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 552 | 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 | |
| 588 | inline 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 Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 596 | 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 Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 600 | if (!json_util::readJsonPatch( |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 601 | 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 Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 615 | { |
| 616 | return false; |
| 617 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 618 | // clang-format on |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 619 | |
| 620 | ctx.id = *id; |
| 621 | ctx.name = *name; |
| 622 | |
| 623 | if (metricType) |
| 624 | { |
Ed Tanous | d5736ef | 2023-07-06 10:37:23 -0700 | [diff] [blame] | 625 | ctx.metricType = getMetricType(*metricType); |
| 626 | if (!ctx.metricType) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 627 | { |
| 628 | messages::propertyValueIncorrect(res, "MetricType", *metricType); |
| 629 | return false; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | if (discreteTriggerCondition) |
| 634 | { |
Ed Tanous | d5736ef | 2023-07-06 10:37:23 -0700 | [diff] [blame] | 635 | ctx.discreteCondition = getDiscreteCondition(*discreteTriggerCondition); |
| 636 | if (!ctx.discreteCondition) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 637 | { |
| 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 Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 665 | if (!parseTriggerThresholds(res, discreteTriggers, thresholds, ctx)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 666 | { |
| 667 | return false; |
| 668 | } |
| 669 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 670 | if (metricReportDefinitions) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 671 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 672 | if (!parseLinks(res, *metricReportDefinitions, ctx)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 673 | { |
| 674 | return false; |
| 675 | } |
| 676 | } |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | inline 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 Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 697 | BMCWEB_LOG_ERROR("respHandler DBus error {}", ec); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | |
| 701 | const std::optional<std::string>& triggerId = |
| 702 | getTriggerIdFromDbusPath(dbusPath); |
| 703 | if (!triggerId) |
| 704 | { |
| 705 | messages::internalError(asyncResp->res); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 706 | BMCWEB_LOG_ERROR("Unknown data returned by " |
| 707 | "AddTrigger DBus method"); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 708 | 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 | |
| 717 | inline std::optional<nlohmann::json::array_t> |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 718 | getTriggerActions(const std::vector<std::string>& dbusActions) |
| 719 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 720 | nlohmann::json::array_t triggerActions; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 721 | for (const std::string& dbusAction : dbusActions) |
| 722 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 723 | triggers::TriggerActionEnum redfishAction = |
| 724 | toRedfishTriggerAction(dbusAction); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 725 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 726 | if (redfishAction == triggers::TriggerActionEnum::Invalid) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 727 | { |
| 728 | return std::nullopt; |
| 729 | } |
| 730 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 731 | triggerActions.emplace_back(redfishAction); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 732 | } |
| 733 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 734 | return triggerActions; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 737 | inline std::optional<nlohmann::json::array_t> |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 738 | getDiscreteTriggers(const TriggerThresholdParamsExt& thresholdParams) |
| 739 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 740 | nlohmann::json::array_t triggers; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 741 | const std::vector<DiscreteThresholdParams>* discreteParams = |
| 742 | std::get_if<std::vector<DiscreteThresholdParams>>(&thresholdParams); |
| 743 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 744 | if (discreteParams == nullptr) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 745 | { |
| 746 | return std::nullopt; |
| 747 | } |
| 748 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 749 | 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 Tanous | 613dabe | 2022-07-09 11:17:36 -0700 | [diff] [blame] | 758 | nlohmann::json::object_t trigger; |
| 759 | trigger["Name"] = name; |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 760 | trigger["Severity"] = toRedfishSeverity(severity); |
Ed Tanous | 613dabe | 2022-07-09 11:17:36 -0700 | [diff] [blame] | 761 | trigger["DwellTime"] = *duration; |
| 762 | trigger["Value"] = value; |
Patrick Williams | ad53954 | 2023-05-12 10:10:08 -0500 | [diff] [blame] | 763 | triggers.emplace_back(std::move(trigger)); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 766 | return triggers; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | inline std::optional<nlohmann::json> |
| 770 | getNumericThresholds(const TriggerThresholdParamsExt& thresholdParams) |
| 771 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 772 | nlohmann::json::object_t thresholds; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 773 | const std::vector<NumericThresholdParams>* numericParams = |
| 774 | std::get_if<std::vector<NumericThresholdParams>>(&thresholdParams); |
| 775 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 776 | if (numericParams == nullptr) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 777 | { |
| 778 | return std::nullopt; |
| 779 | } |
| 780 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 781 | 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 Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 790 | nlohmann::json& threshold = thresholds[toRedfishThresholdName(type)]; |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 791 | threshold["Reading"] = reading; |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 792 | threshold["Activation"] = toRedfishActivation(activation); |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 793 | threshold["DwellTime"] = *duration; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 794 | } |
| 795 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 796 | return thresholds; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 799 | inline std::optional<nlohmann::json> getMetricReportDefinitions( |
| 800 | const std::vector<sdbusplus::message::object_path>& reportPaths) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 801 | { |
| 802 | nlohmann::json reports = nlohmann::json::array(); |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 803 | |
| 804 | for (const sdbusplus::message::object_path& path : reportPaths) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 805 | { |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 806 | std::string reportId = path.filename(); |
| 807 | if (reportId.empty()) |
| 808 | { |
| 809 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 810 | BMCWEB_LOG_ERROR("Property Reports contains invalid value: {}", |
| 811 | path.str); |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 812 | return std::nullopt; |
| 813 | } |
| 814 | } |
| 815 | |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 816 | nlohmann::json::object_t report; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 817 | report["@odata.id"] = boost::urls::format( |
| 818 | "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", |
| 819 | reportId); |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 820 | reports.emplace_back(std::move(report)); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 821 | } |
| 822 | |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 823 | return {std::move(reports)}; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | inline 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 | |
| 839 | inline 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 Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 847 | const std::vector<sdbusplus::message::object_path>* reports = nullptr; |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 848 | const std::vector<std::string>* triggerActions = nullptr; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 849 | const TriggerThresholdParamsExt* thresholds = nullptr; |
| 850 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 851 | 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 Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 855 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 856 | if (!success) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 857 | { |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 858 | return false; |
| 859 | } |
| 860 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 861 | if (triggerActions != nullptr) |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 862 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 863 | std::optional<nlohmann::json::array_t> redfishTriggerActions = |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 864 | getTriggerActions(*triggerActions); |
| 865 | if (!redfishTriggerActions) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 866 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 867 | BMCWEB_LOG_ERROR( |
| 868 | "Property TriggerActions is invalid in Trigger: {}", id); |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 869 | return false; |
| 870 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 871 | json["TriggerActions"] = *redfishTriggerActions; |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | if (reports != nullptr) |
| 875 | { |
| 876 | std::optional<nlohmann::json> linkedReports = |
| 877 | getMetricReportDefinitions(*reports); |
| 878 | if (!linkedReports) |
| 879 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 880 | BMCWEB_LOG_ERROR("Property Reports is invalid in Trigger: {}", id); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 881 | return false; |
| 882 | } |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 883 | json["Links"]["MetricReportDefinitions"] = *linkedReports; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 884 | } |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 885 | |
| 886 | if (discrete != nullptr) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 887 | { |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 888 | if (*discrete) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 889 | { |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 890 | std::optional<nlohmann::json::array_t> discreteTriggers = |
| 891 | getDiscreteTriggers(*thresholds); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 892 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 893 | if (!discreteTriggers) |
| 894 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 895 | BMCWEB_LOG_ERROR("Property Thresholds is invalid for discrete " |
| 896 | "triggers in Trigger: {}", |
| 897 | id); |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 898 | return false; |
| 899 | } |
| 900 | |
| 901 | json["DiscreteTriggers"] = *discreteTriggers; |
| 902 | json["DiscreteTriggerCondition"] = |
| 903 | discreteTriggers->empty() ? "Changed" : "Specified"; |
Ed Tanous | 539d8c6 | 2024-06-19 14:38:27 -0700 | [diff] [blame^] | 904 | json["MetricType"] = metric_definition::MetricType::Discrete; |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 905 | } |
| 906 | else |
| 907 | { |
| 908 | std::optional<nlohmann::json> numericThresholds = |
| 909 | getNumericThresholds(*thresholds); |
| 910 | |
| 911 | if (!numericThresholds) |
| 912 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 913 | BMCWEB_LOG_ERROR("Property Thresholds is invalid for numeric " |
| 914 | "thresholds in Trigger: {}", |
| 915 | id); |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 916 | return false; |
| 917 | } |
| 918 | |
| 919 | json["NumericThresholds"] = *numericThresholds; |
Ed Tanous | 539d8c6 | 2024-06-19 14:38:27 -0700 | [diff] [blame^] | 920 | json["MetricType"] = metric_definition::MetricType::Numeric; |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
| 924 | if (name != nullptr) |
| 925 | { |
| 926 | json["Name"] = *name; |
| 927 | } |
| 928 | |
| 929 | if (sensors != nullptr) |
| 930 | { |
| 931 | json["MetricProperties"] = getMetricProperties(*sensors); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 932 | } |
| 933 | |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 934 | json["@odata.type"] = "#Triggers.v1_2_0.Triggers"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 935 | json["@odata.id"] = |
| 936 | boost::urls::format("/redfish/v1/TelemetryService/Triggers/{}", id); |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 937 | json["Id"] = id; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 938 | |
| 939 | return true; |
| 940 | } |
| 941 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 942 | inline 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 Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 961 | }, |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 962 | 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 Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 968 | } // namespace telemetry |
| 969 | |
| 970 | inline 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 Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 975 | [&app](const crow::Request& req, |
| 976 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 977 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 978 | { |
| 979 | return; |
| 980 | } |
| 981 | asyncResp->res.jsonValue["@odata.type"] = |
| 982 | "#TriggersCollection.TriggersCollection"; |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 983 | asyncResp->res.jsonValue["@odata.id"] = |
| 984 | "/redfish/v1/TelemetryService/Triggers"; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 985 | asyncResp->res.jsonValue["Name"] = "Triggers Collection"; |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 986 | constexpr std::array<std::string_view, 1> interfaces{ |
| 987 | telemetry::triggerInterface}; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 988 | collection_util::getCollectionMembers( |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 989 | asyncResp, |
| 990 | boost::urls::url("/redfish/v1/TelemetryService/Triggers"), |
| 991 | interfaces, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 992 | "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService"); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 993 | }); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 994 | |
| 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 Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 1001 | inline 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 Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 1006 | [&app](const crow::Request& req, |
| 1007 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1008 | const std::string& id) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1009 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1010 | { |
| 1011 | return; |
| 1012 | } |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 1013 | sdbusplus::asio::getAllProperties( |
| 1014 | *crow::connections::systemBus, telemetry::service, |
| 1015 | telemetry::getDbusTriggerPath(id), telemetry::triggerInterface, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1016 | [asyncResp, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1017 | id](const boost::system::error_code& ec, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1018 | 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 Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1028 | BMCWEB_LOG_ERROR("respHandler DBus error {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1029 | messages::internalError(asyncResp->res); |
| 1030 | return; |
| 1031 | } |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 1032 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1033 | if (!telemetry::fillTrigger(asyncResp->res.jsonValue, id, ret)) |
| 1034 | { |
| 1035 | messages::internalError(asyncResp->res); |
| 1036 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1037 | }); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 1038 | }); |
Szymon Dompke | 163994a | 2021-08-12 17:30:23 +0200 | [diff] [blame] | 1039 | |
| 1040 | BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/<str>/") |
| 1041 | .privileges(redfish::privileges::deleteTriggers) |
| 1042 | .methods(boost::beast::http::verb::delete_)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 1043 | [&app](const crow::Request& req, |
| 1044 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1045 | const std::string& id) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1046 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1047 | { |
| 1048 | return; |
| 1049 | } |
| 1050 | const std::string triggerPath = telemetry::getDbusTriggerPath(id); |
Szymon Dompke | 163994a | 2021-08-12 17:30:23 +0200 | [diff] [blame] | 1051 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1052 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1053 | [asyncResp, id](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1054 | if (ec.value() == EBADR) |
| 1055 | { |
| 1056 | messages::resourceNotFound(asyncResp->res, "Triggers", id); |
| 1057 | return; |
| 1058 | } |
Szymon Dompke | 163994a | 2021-08-12 17:30:23 +0200 | [diff] [blame] | 1059 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1060 | if (ec) |
| 1061 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1062 | BMCWEB_LOG_ERROR("respHandler DBus error {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1063 | messages::internalError(asyncResp->res); |
| 1064 | return; |
| 1065 | } |
Szymon Dompke | 163994a | 2021-08-12 17:30:23 +0200 | [diff] [blame] | 1066 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1067 | asyncResp->res.result(boost::beast::http::status::no_content); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 1068 | }, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1069 | telemetry::service, triggerPath, |
| 1070 | "xyz.openbmc_project.Object.Delete", "Delete"); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 1071 | }); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 1074 | } // namespace redfish |