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" |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 4 | #include "generated/enums/resource.hpp" |
| 5 | #include "generated/enums/triggers.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 6 | #include "query.hpp" |
| 7 | #include "registries/privilege_registry.hpp" |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 8 | #include "sensors.hpp" |
| 9 | #include "utility.hpp" |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 10 | #include "utils/collection.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 11 | #include "utils/dbus_utils.hpp" |
Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 12 | #include "utils/json_utils.hpp" |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 13 | #include "utils/telemetry_utils.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 14 | #include "utils/time_utils.hpp" |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 15 | |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 16 | #include <boost/url/format.hpp> |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 17 | #include <sdbusplus/asio/property.hpp> |
| 18 | #include <sdbusplus/unpack_properties.hpp> |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 19 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 20 | #include <array> |
| 21 | #include <string_view> |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 22 | #include <tuple> |
| 23 | #include <variant> |
| 24 | #include <vector> |
| 25 | |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 26 | namespace redfish |
| 27 | { |
| 28 | namespace telemetry |
| 29 | { |
| 30 | constexpr const char* triggerInterface = |
| 31 | "xyz.openbmc_project.Telemetry.Trigger"; |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 32 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 33 | using NumericThresholdParams = |
| 34 | std::tuple<std::string, uint64_t, std::string, double>; |
| 35 | |
| 36 | using DiscreteThresholdParams = |
| 37 | std::tuple<std::string, std::string, uint64_t, std::string>; |
| 38 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 39 | using TriggerThresholdParams = |
| 40 | std::variant<std::vector<NumericThresholdParams>, |
| 41 | std::vector<DiscreteThresholdParams>>; |
| 42 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 43 | using TriggerThresholdParamsExt = |
| 44 | std::variant<std::monostate, std::vector<NumericThresholdParams>, |
| 45 | std::vector<DiscreteThresholdParams>>; |
| 46 | |
| 47 | using TriggerSensorsParams = |
| 48 | std::vector<std::pair<sdbusplus::message::object_path, std::string>>; |
| 49 | |
| 50 | using TriggerGetParamsVariant = |
| 51 | std::variant<std::monostate, bool, std::string, TriggerThresholdParamsExt, |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 52 | TriggerSensorsParams, std::vector<std::string>, |
| 53 | std::vector<sdbusplus::message::object_path>>; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 54 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 55 | inline triggers::TriggerActionEnum |
| 56 | toRedfishTriggerAction(std::string_view dbusValue) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 57 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 58 | if (dbusValue == |
| 59 | "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.UpdateReport") |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 60 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 61 | return triggers::TriggerActionEnum::RedfishMetricReport; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 62 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 63 | if (dbusValue == |
| 64 | "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToRedfishEventLog") |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 65 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 66 | return triggers::TriggerActionEnum::RedfishEvent; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 67 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 68 | if (dbusValue == |
| 69 | "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToJournal") |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 70 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 71 | return triggers::TriggerActionEnum::LogToLogService; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 72 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 73 | return triggers::TriggerActionEnum::Invalid; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 76 | inline std::string toDbusTriggerAction(std::string_view redfishValue) |
| 77 | { |
| 78 | if (redfishValue == "RedfishMetricReport") |
| 79 | { |
| 80 | return "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.UpdateReport"; |
| 81 | } |
| 82 | if (redfishValue == "RedfishEvent") |
| 83 | { |
| 84 | return "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToRedfishEventLog"; |
| 85 | } |
| 86 | if (redfishValue == "LogToLogService") |
| 87 | { |
| 88 | return "xyz.openbmc_project.Telemetry.Trigger.TriggerAction.LogToJournal"; |
| 89 | } |
| 90 | return ""; |
| 91 | } |
| 92 | |
| 93 | inline std::string toDbusSeverity(std::string_view redfishValue) |
| 94 | { |
| 95 | if (redfishValue == "OK") |
| 96 | { |
| 97 | return "xyz.openbmc_project.Telemetry.Trigger.Severity.OK"; |
| 98 | } |
| 99 | if (redfishValue == "Warning") |
| 100 | { |
| 101 | return "xyz.openbmc_project.Telemetry.Trigger.Severity.Warning"; |
| 102 | } |
| 103 | if (redfishValue == "Critical") |
| 104 | { |
| 105 | return "xyz.openbmc_project.Telemetry.Trigger.Severity.Critical"; |
| 106 | } |
| 107 | return ""; |
| 108 | } |
| 109 | |
| 110 | inline resource::Health toRedfishSeverity(std::string_view dbusValue) |
| 111 | { |
| 112 | if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Severity.OK") |
| 113 | { |
| 114 | return resource::Health::OK; |
| 115 | } |
| 116 | if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Severity.Warning") |
| 117 | { |
| 118 | return resource::Health::Warning; |
| 119 | } |
| 120 | if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Severity.Critical") |
| 121 | { |
| 122 | return resource::Health::Critical; |
| 123 | } |
| 124 | return resource::Health::Invalid; |
| 125 | } |
| 126 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 127 | inline std::string toRedfishThresholdName(std::string_view dbusValue) |
| 128 | { |
| 129 | if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical") |
| 130 | { |
| 131 | return "UpperCritical"; |
| 132 | } |
| 133 | |
| 134 | if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.LowerCritical") |
| 135 | { |
| 136 | return "LowerCritical"; |
| 137 | } |
| 138 | |
| 139 | if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning") |
| 140 | { |
| 141 | return "UpperWarning"; |
| 142 | } |
| 143 | |
| 144 | if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning") |
| 145 | { |
| 146 | return "LowerWarning"; |
| 147 | } |
| 148 | |
| 149 | return ""; |
| 150 | } |
| 151 | |
| 152 | inline std::string toDbusActivation(std::string_view redfishValue) |
| 153 | { |
| 154 | if (redfishValue == "Either") |
| 155 | { |
| 156 | return "xyz.openbmc_project.Telemetry.Trigger.Direction.Either"; |
| 157 | } |
| 158 | |
| 159 | if (redfishValue == "Decreasing") |
| 160 | { |
| 161 | return "xyz.openbmc_project.Telemetry.Trigger.Direction.Decreasing"; |
| 162 | } |
| 163 | |
| 164 | if (redfishValue == "Increasing") |
| 165 | { |
| 166 | return "xyz.openbmc_project.Telemetry.Trigger.Direction.Increasing"; |
| 167 | } |
| 168 | |
| 169 | return ""; |
| 170 | } |
| 171 | |
| 172 | inline triggers::ThresholdActivation |
| 173 | toRedfishActivation(std::string_view dbusValue) |
| 174 | { |
| 175 | if (dbusValue == "xyz.openbmc_project.Telemetry.Trigger.Direction.Either") |
| 176 | { |
| 177 | return triggers::ThresholdActivation::Either; |
| 178 | } |
| 179 | |
| 180 | if (dbusValue == |
| 181 | "xyz.openbmc_project.Telemetry.Trigger.Direction.Decreasing") |
| 182 | { |
| 183 | return triggers::ThresholdActivation::Decreasing; |
| 184 | } |
| 185 | |
| 186 | if (dbusValue == |
| 187 | "xyz.openbmc_project.Telemetry.Trigger.Direction.Increasing") |
| 188 | { |
| 189 | return triggers::ThresholdActivation::Increasing; |
| 190 | } |
| 191 | |
| 192 | return triggers::ThresholdActivation::Invalid; |
| 193 | } |
| 194 | |
| 195 | enum class MetricType |
| 196 | { |
| 197 | Discrete, |
| 198 | Numeric |
| 199 | }; |
| 200 | |
| 201 | enum class DiscreteCondition |
| 202 | { |
| 203 | Specified, |
| 204 | Changed |
| 205 | }; |
| 206 | |
| 207 | struct Context |
| 208 | { |
| 209 | std::string id; |
| 210 | std::string name; |
| 211 | std::vector<std::string> actions; |
| 212 | std::vector<std::pair<sdbusplus::message::object_path, std::string>> |
| 213 | sensors; |
| 214 | std::vector<sdbusplus::message::object_path> reports; |
| 215 | TriggerThresholdParams thresholds; |
| 216 | |
| 217 | std::optional<DiscreteCondition> discreteCondition; |
| 218 | std::optional<MetricType> metricType; |
| 219 | std::optional<std::vector<std::string>> metricProperties; |
| 220 | }; |
| 221 | |
| 222 | inline std::optional<sdbusplus::message::object_path> |
| 223 | getReportPathFromReportDefinitionUri(const std::string& uri) |
| 224 | { |
Ed Tanous | 6fd2955 | 2023-10-04 09:40:14 -0700 | [diff] [blame] | 225 | boost::system::result<boost::urls::url_view> parsed = |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 226 | boost::urls::parse_relative_ref(uri); |
| 227 | |
| 228 | if (!parsed) |
| 229 | { |
| 230 | return std::nullopt; |
| 231 | } |
| 232 | |
| 233 | std::string id; |
| 234 | if (!crow::utility::readUrlSegments( |
| 235 | *parsed, "redfish", "v1", "TelemetryService", |
| 236 | "MetricReportDefinitions", std::ref(id))) |
| 237 | { |
| 238 | return std::nullopt; |
| 239 | } |
| 240 | |
| 241 | return sdbusplus::message::object_path( |
| 242 | "/xyz/openbmc_project/Telemetry/Reports") / |
| 243 | "TelemetryService" / id; |
| 244 | } |
| 245 | |
| 246 | inline std::optional<MetricType> getMetricType(const std::string& metricType) |
| 247 | { |
| 248 | if (metricType == "Discrete") |
| 249 | { |
| 250 | return MetricType::Discrete; |
| 251 | } |
| 252 | if (metricType == "Numeric") |
| 253 | { |
| 254 | return MetricType::Numeric; |
| 255 | } |
| 256 | return std::nullopt; |
| 257 | } |
| 258 | |
| 259 | inline std::optional<DiscreteCondition> |
| 260 | getDiscreteCondition(const std::string& discreteTriggerCondition) |
| 261 | { |
| 262 | if (discreteTriggerCondition == "Specified") |
| 263 | { |
| 264 | return DiscreteCondition::Specified; |
| 265 | } |
| 266 | if (discreteTriggerCondition == "Changed") |
| 267 | { |
| 268 | return DiscreteCondition::Changed; |
| 269 | } |
| 270 | return std::nullopt; |
| 271 | } |
| 272 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 273 | inline bool parseThreshold(crow::Response& res, |
| 274 | nlohmann::json::object_t& threshold, |
| 275 | std::string_view dbusThresholdName, |
| 276 | std::vector<NumericThresholdParams>& parsedParams) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 277 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 278 | double reading = 0.0; |
| 279 | std::string activation; |
| 280 | std::string dwellTimeStr; |
| 281 | |
| 282 | if (!json_util::readJsonObject(threshold, res, "Reading", reading, |
| 283 | "Activation", activation, "DwellTime", |
| 284 | dwellTimeStr)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 285 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 289 | std::string dbusActivation = toDbusActivation(activation); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 290 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 291 | if (dbusActivation.empty()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 292 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 293 | messages::propertyValueIncorrect(res, "Activation", activation); |
| 294 | return false; |
| 295 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 296 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 297 | std::optional<std::chrono::milliseconds> dwellTime = |
| 298 | time_utils::fromDurationString(dwellTimeStr); |
| 299 | if (!dwellTime) |
| 300 | { |
| 301 | messages::propertyValueIncorrect(res, "DwellTime", dwellTimeStr); |
| 302 | return false; |
| 303 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 304 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 305 | parsedParams.emplace_back(dbusThresholdName, |
| 306 | static_cast<uint64_t>(dwellTime->count()), |
| 307 | dbusActivation, reading); |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | struct NumericThresholds |
| 312 | { |
| 313 | std::optional<nlohmann::json::object_t> upperCritical; |
| 314 | std::optional<nlohmann::json::object_t> upperWarning; |
| 315 | std::optional<nlohmann::json::object_t> lowerWarning; |
| 316 | std::optional<nlohmann::json::object_t> lowerCritical; |
| 317 | |
| 318 | bool any() const |
| 319 | { |
| 320 | return upperCritical || upperWarning || lowerWarning || lowerCritical; |
| 321 | } |
| 322 | }; |
| 323 | |
| 324 | inline bool parseNumericThresholds(crow::Response& res, |
| 325 | NumericThresholds& numericThresholds, |
| 326 | Context& ctx) |
| 327 | { |
| 328 | std::vector<NumericThresholdParams> parsedParams; |
| 329 | if (numericThresholds.upperCritical) |
| 330 | { |
| 331 | if (!parseThreshold( |
| 332 | res, *numericThresholds.upperCritical, |
| 333 | "xyz.openbmc_project.Telemetry.Trigger.Type.UpperCritical", |
| 334 | parsedParams)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 335 | { |
| 336 | return false; |
| 337 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 338 | } |
| 339 | if (numericThresholds.upperWarning) |
| 340 | { |
| 341 | if (!parseThreshold( |
| 342 | res, *numericThresholds.upperWarning, |
| 343 | "xyz.openbmc_project.Telemetry.Trigger.Type.UpperWarning", |
| 344 | parsedParams)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 345 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 346 | return false; |
| 347 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 348 | } |
| 349 | if (numericThresholds.lowerWarning) |
| 350 | { |
| 351 | if (!parseThreshold( |
| 352 | res, *numericThresholds.lowerWarning, |
| 353 | "xyz.openbmc_project.Telemetry.Trigger.Type.LowerWarning", |
| 354 | parsedParams)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 355 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 356 | return false; |
| 357 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 358 | } |
| 359 | if (numericThresholds.lowerCritical) |
| 360 | { |
| 361 | if (!parseThreshold( |
| 362 | res, *numericThresholds.lowerCritical, |
| 363 | "xyz.openbmc_project.Telemetry.Trigger.Type.LowerCritical", |
| 364 | parsedParams)) |
| 365 | { |
| 366 | return false; |
| 367 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | ctx.thresholds = std::move(parsedParams); |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | inline bool parseDiscreteTriggers( |
| 375 | crow::Response& res, |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 376 | std::optional<std::vector<nlohmann::json::object_t>>& discreteTriggers, |
| 377 | Context& ctx) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 378 | { |
| 379 | std::vector<DiscreteThresholdParams> parsedParams; |
| 380 | if (!discreteTriggers) |
| 381 | { |
| 382 | ctx.thresholds = std::move(parsedParams); |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | parsedParams.reserve(discreteTriggers->size()); |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 387 | for (nlohmann::json::object_t& thresholdInfo : *discreteTriggers) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 388 | { |
| 389 | std::optional<std::string> name = ""; |
| 390 | std::string value; |
| 391 | std::string dwellTimeStr; |
| 392 | std::string severity; |
| 393 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 394 | if (!json_util::readJsonObject(thresholdInfo, res, "Name", name, |
| 395 | "Value", value, "DwellTime", |
| 396 | dwellTimeStr, "Severity", severity)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 397 | { |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | std::optional<std::chrono::milliseconds> dwellTime = |
| 402 | time_utils::fromDurationString(dwellTimeStr); |
| 403 | if (!dwellTime) |
| 404 | { |
| 405 | messages::propertyValueIncorrect(res, "DwellTime", dwellTimeStr); |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | std::string dbusSeverity = toDbusSeverity(severity); |
| 410 | if (dbusSeverity.empty()) |
| 411 | { |
| 412 | messages::propertyValueIncorrect(res, "Severity", severity); |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | parsedParams.emplace_back(*name, dbusSeverity, |
| 417 | static_cast<uint64_t>(dwellTime->count()), |
| 418 | value); |
| 419 | } |
| 420 | |
| 421 | ctx.thresholds = std::move(parsedParams); |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | inline bool parseTriggerThresholds( |
| 426 | crow::Response& res, |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 427 | std::optional<std::vector<nlohmann::json::object_t>>& discreteTriggers, |
| 428 | NumericThresholds& numericThresholds, Context& ctx) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 429 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 430 | if (discreteTriggers && numericThresholds.any()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 431 | { |
| 432 | messages::propertyValueConflict(res, "DiscreteTriggers", |
| 433 | "NumericThresholds"); |
| 434 | messages::propertyValueConflict(res, "NumericThresholds", |
| 435 | "DiscreteTriggers"); |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | if (ctx.discreteCondition) |
| 440 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 441 | if (numericThresholds.any()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 442 | { |
| 443 | messages::propertyValueConflict(res, "DiscreteTriggerCondition", |
| 444 | "NumericThresholds"); |
| 445 | messages::propertyValueConflict(res, "NumericThresholds", |
| 446 | "DiscreteTriggerCondition"); |
| 447 | return false; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | if (ctx.metricType) |
| 452 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 453 | if (*ctx.metricType == MetricType::Discrete && numericThresholds.any()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 454 | { |
| 455 | messages::propertyValueConflict(res, "NumericThresholds", |
| 456 | "MetricType"); |
| 457 | return false; |
| 458 | } |
| 459 | if (*ctx.metricType == MetricType::Numeric && discreteTriggers) |
| 460 | { |
| 461 | messages::propertyValueConflict(res, "DiscreteTriggers", |
| 462 | "MetricType"); |
| 463 | return false; |
| 464 | } |
| 465 | if (*ctx.metricType == MetricType::Numeric && ctx.discreteCondition) |
| 466 | { |
| 467 | messages::propertyValueConflict(res, "DiscreteTriggers", |
| 468 | "DiscreteTriggerCondition"); |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (discreteTriggers || ctx.discreteCondition || |
| 474 | (ctx.metricType && *ctx.metricType == MetricType::Discrete)) |
| 475 | { |
| 476 | if (ctx.discreteCondition) |
| 477 | { |
| 478 | if (*ctx.discreteCondition == DiscreteCondition::Specified && |
| 479 | !discreteTriggers) |
| 480 | { |
| 481 | messages::createFailedMissingReqProperties(res, |
| 482 | "DiscreteTriggers"); |
| 483 | return false; |
| 484 | } |
| 485 | if (discreteTriggers && |
| 486 | ((*ctx.discreteCondition == DiscreteCondition::Specified && |
| 487 | discreteTriggers->empty()) || |
| 488 | (*ctx.discreteCondition == DiscreteCondition::Changed && |
| 489 | !discreteTriggers->empty()))) |
| 490 | { |
| 491 | messages::propertyValueConflict(res, "DiscreteTriggers", |
| 492 | "DiscreteTriggerCondition"); |
| 493 | return false; |
| 494 | } |
| 495 | } |
| 496 | if (!parseDiscreteTriggers(res, discreteTriggers, ctx)) |
| 497 | { |
| 498 | return false; |
| 499 | } |
| 500 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 501 | else if (numericThresholds.any()) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 502 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 503 | if (!parseNumericThresholds(res, numericThresholds, ctx)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 504 | { |
| 505 | return false; |
| 506 | } |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | messages::createFailedMissingReqProperties( |
| 511 | res, "'DiscreteTriggers', 'NumericThresholds', " |
| 512 | "'DiscreteTriggerCondition' or 'MetricType'"); |
| 513 | return false; |
| 514 | } |
| 515 | return true; |
| 516 | } |
| 517 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 518 | inline bool parseLinks(crow::Response& res, |
| 519 | const std::vector<std::string>& metricReportDefinitions, |
| 520 | Context& ctx) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 521 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 522 | ctx.reports.reserve(metricReportDefinitions.size()); |
| 523 | for (const std::string& reportDefinionUri : metricReportDefinitions) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 524 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 525 | std::optional<sdbusplus::message::object_path> reportPath = |
| 526 | getReportPathFromReportDefinitionUri(reportDefinionUri); |
| 527 | if (!reportPath) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 528 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 529 | messages::propertyValueIncorrect(res, "MetricReportDefinitions", |
| 530 | reportDefinionUri); |
| 531 | return false; |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 532 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 533 | ctx.reports.emplace_back(*reportPath); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 534 | } |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | inline bool parseMetricProperties(crow::Response& res, Context& ctx) |
| 539 | { |
| 540 | if (!ctx.metricProperties) |
| 541 | { |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | ctx.sensors.reserve(ctx.metricProperties->size()); |
| 546 | |
| 547 | size_t uriIdx = 0; |
| 548 | for (const std::string& uriStr : *ctx.metricProperties) |
| 549 | { |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 550 | boost::system::result<boost::urls::url> uri = |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 551 | boost::urls::parse_relative_ref(uriStr); |
| 552 | if (!uri) |
| 553 | { |
| 554 | messages::propertyValueIncorrect( |
| 555 | res, "MetricProperties/" + std::to_string(uriIdx), uriStr); |
| 556 | return false; |
| 557 | } |
| 558 | std::string chassisName; |
| 559 | std::string sensorName; |
| 560 | if (!crow::utility::readUrlSegments(*uri, "redfish", "v1", "Chassis", |
| 561 | std::ref(chassisName), "Sensors", |
| 562 | std::ref(sensorName))) |
| 563 | { |
| 564 | messages::propertyValueIncorrect( |
| 565 | res, "MetricProperties/" + std::to_string(uriIdx), uriStr); |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | std::pair<std::string, std::string> split = |
| 570 | splitSensorNameAndType(sensorName); |
| 571 | if (split.first.empty() || split.second.empty()) |
| 572 | { |
| 573 | messages::propertyValueIncorrect( |
| 574 | res, "MetricProperties/" + std::to_string(uriIdx), uriStr); |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | std::string sensorPath = "/xyz/openbmc_project/sensors/" + split.first + |
| 579 | '/' + split.second; |
| 580 | |
| 581 | ctx.sensors.emplace_back(sensorPath, uriStr); |
| 582 | uriIdx++; |
| 583 | } |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | inline bool parsePostTriggerParams(crow::Response& res, |
| 588 | const crow::Request& req, Context& ctx) |
| 589 | { |
| 590 | std::optional<std::string> id = ""; |
| 591 | std::optional<std::string> name = ""; |
| 592 | std::optional<std::string> metricType; |
| 593 | std::optional<std::vector<std::string>> triggerActions; |
| 594 | std::optional<std::string> discreteTriggerCondition; |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 595 | std::optional<std::vector<nlohmann::json::object_t>> discreteTriggers; |
| 596 | std::optional<std::vector<std::string>> metricReportDefinitions; |
| 597 | NumericThresholds thresholds; |
| 598 | // clang-format off |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 599 | if (!json_util::readJsonPatch( |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 600 | req, res, |
| 601 | "Id", id, |
| 602 | "Name", name, |
| 603 | "MetricType", metricType, |
| 604 | "TriggerActions", triggerActions, |
| 605 | "DiscreteTriggerCondition", discreteTriggerCondition, |
| 606 | "DiscreteTriggers", discreteTriggers, |
| 607 | "NumericThresholds/UpperCritical", thresholds.upperCritical, |
| 608 | "NumericThresholds/UpperWarning", thresholds.upperWarning, |
| 609 | "NumericThresholds/LowerWarning", thresholds.lowerWarning, |
| 610 | "NumericThresholds/LowerCritical", thresholds.lowerCritical, |
| 611 | "MetricProperties", ctx.metricProperties, |
| 612 | "Links/MetricReportDefinitions", metricReportDefinitions) |
| 613 | ) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 614 | { |
| 615 | return false; |
| 616 | } |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 617 | // clang-format on |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 618 | |
| 619 | ctx.id = *id; |
| 620 | ctx.name = *name; |
| 621 | |
| 622 | if (metricType) |
| 623 | { |
Ed Tanous | d5736ef | 2023-07-06 10:37:23 -0700 | [diff] [blame] | 624 | ctx.metricType = getMetricType(*metricType); |
| 625 | if (!ctx.metricType) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 626 | { |
| 627 | messages::propertyValueIncorrect(res, "MetricType", *metricType); |
| 628 | return false; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | if (discreteTriggerCondition) |
| 633 | { |
Ed Tanous | d5736ef | 2023-07-06 10:37:23 -0700 | [diff] [blame] | 634 | ctx.discreteCondition = getDiscreteCondition(*discreteTriggerCondition); |
| 635 | if (!ctx.discreteCondition) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 636 | { |
| 637 | messages::propertyValueIncorrect(res, "DiscreteTriggerCondition", |
| 638 | *discreteTriggerCondition); |
| 639 | return false; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | if (triggerActions) |
| 644 | { |
| 645 | ctx.actions.reserve(triggerActions->size()); |
| 646 | for (const std::string& action : *triggerActions) |
| 647 | { |
| 648 | std::string dbusAction = toDbusTriggerAction(action); |
| 649 | |
| 650 | if (dbusAction.empty()) |
| 651 | { |
| 652 | messages::propertyValueNotInList(res, action, "TriggerActions"); |
| 653 | return false; |
| 654 | } |
| 655 | |
| 656 | ctx.actions.emplace_back(dbusAction); |
| 657 | } |
| 658 | } |
| 659 | if (!parseMetricProperties(res, ctx)) |
| 660 | { |
| 661 | return false; |
| 662 | } |
| 663 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 664 | if (!parseTriggerThresholds(res, discreteTriggers, thresholds, ctx)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 665 | { |
| 666 | return false; |
| 667 | } |
| 668 | |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 669 | if (metricReportDefinitions) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 670 | { |
Ed Tanous | 2932dcb | 2024-03-06 12:04:47 -0800 | [diff] [blame] | 671 | if (!parseLinks(res, *metricReportDefinitions, ctx)) |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 672 | { |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | return true; |
| 677 | } |
| 678 | |
| 679 | inline void afterCreateTrigger( |
| 680 | const boost::system::error_code& ec, const std::string& dbusPath, |
| 681 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id) |
| 682 | { |
| 683 | if (ec == boost::system::errc::file_exists) |
| 684 | { |
| 685 | messages::resourceAlreadyExists(asyncResp->res, "Trigger", "Id", id); |
| 686 | return; |
| 687 | } |
| 688 | if (ec == boost::system::errc::too_many_files_open) |
| 689 | { |
| 690 | messages::createLimitReachedForResource(asyncResp->res); |
| 691 | return; |
| 692 | } |
| 693 | if (ec) |
| 694 | { |
| 695 | messages::internalError(asyncResp->res); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 696 | BMCWEB_LOG_ERROR("respHandler DBus error {}", ec); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 697 | return; |
| 698 | } |
| 699 | |
| 700 | const std::optional<std::string>& triggerId = |
| 701 | getTriggerIdFromDbusPath(dbusPath); |
| 702 | if (!triggerId) |
| 703 | { |
| 704 | messages::internalError(asyncResp->res); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 705 | BMCWEB_LOG_ERROR("Unknown data returned by " |
| 706 | "AddTrigger DBus method"); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | |
| 710 | messages::created(asyncResp->res); |
| 711 | boost::urls::url locationUrl = boost::urls::format( |
| 712 | "/redfish/v1/TelemetryService/Triggers/{}", *triggerId); |
| 713 | asyncResp->res.addHeader("Location", locationUrl.buffer()); |
| 714 | } |
| 715 | |
| 716 | inline std::optional<nlohmann::json::array_t> |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 717 | getTriggerActions(const std::vector<std::string>& dbusActions) |
| 718 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 719 | nlohmann::json::array_t triggerActions; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 720 | for (const std::string& dbusAction : dbusActions) |
| 721 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 722 | triggers::TriggerActionEnum redfishAction = |
| 723 | toRedfishTriggerAction(dbusAction); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 724 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 725 | if (redfishAction == triggers::TriggerActionEnum::Invalid) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 726 | { |
| 727 | return std::nullopt; |
| 728 | } |
| 729 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 730 | triggerActions.emplace_back(redfishAction); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 733 | return triggerActions; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 736 | inline std::optional<nlohmann::json::array_t> |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 737 | getDiscreteTriggers(const TriggerThresholdParamsExt& thresholdParams) |
| 738 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 739 | nlohmann::json::array_t triggers; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 740 | const std::vector<DiscreteThresholdParams>* discreteParams = |
| 741 | std::get_if<std::vector<DiscreteThresholdParams>>(&thresholdParams); |
| 742 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 743 | if (discreteParams == nullptr) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 744 | { |
| 745 | return std::nullopt; |
| 746 | } |
| 747 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 748 | for (const auto& [name, severity, dwellTime, value] : *discreteParams) |
| 749 | { |
| 750 | std::optional<std::string> duration = |
| 751 | time_utils::toDurationStringFromUint(dwellTime); |
| 752 | |
| 753 | if (!duration) |
| 754 | { |
| 755 | return std::nullopt; |
| 756 | } |
Ed Tanous | 613dabe | 2022-07-09 11:17:36 -0700 | [diff] [blame] | 757 | nlohmann::json::object_t trigger; |
| 758 | trigger["Name"] = name; |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 759 | trigger["Severity"] = toRedfishSeverity(severity); |
Ed Tanous | 613dabe | 2022-07-09 11:17:36 -0700 | [diff] [blame] | 760 | trigger["DwellTime"] = *duration; |
| 761 | trigger["Value"] = value; |
Patrick Williams | ad53954 | 2023-05-12 10:10:08 -0500 | [diff] [blame] | 762 | triggers.emplace_back(std::move(trigger)); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 763 | } |
| 764 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 765 | return triggers; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | inline std::optional<nlohmann::json> |
| 769 | getNumericThresholds(const TriggerThresholdParamsExt& thresholdParams) |
| 770 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 771 | nlohmann::json::object_t thresholds; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 772 | const std::vector<NumericThresholdParams>* numericParams = |
| 773 | std::get_if<std::vector<NumericThresholdParams>>(&thresholdParams); |
| 774 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 775 | if (numericParams == nullptr) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 776 | { |
| 777 | return std::nullopt; |
| 778 | } |
| 779 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 780 | for (const auto& [type, dwellTime, activation, reading] : *numericParams) |
| 781 | { |
| 782 | std::optional<std::string> duration = |
| 783 | time_utils::toDurationStringFromUint(dwellTime); |
| 784 | |
| 785 | if (!duration) |
| 786 | { |
| 787 | return std::nullopt; |
| 788 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 789 | nlohmann::json& threshold = thresholds[toRedfishThresholdName(type)]; |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 790 | threshold["Reading"] = reading; |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 791 | threshold["Activation"] = toRedfishActivation(activation); |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 792 | threshold["DwellTime"] = *duration; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 793 | } |
| 794 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 795 | return thresholds; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 796 | } |
| 797 | |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 798 | inline std::optional<nlohmann::json> getMetricReportDefinitions( |
| 799 | const std::vector<sdbusplus::message::object_path>& reportPaths) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 800 | { |
| 801 | nlohmann::json reports = nlohmann::json::array(); |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 802 | |
| 803 | for (const sdbusplus::message::object_path& path : reportPaths) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 804 | { |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 805 | std::string reportId = path.filename(); |
| 806 | if (reportId.empty()) |
| 807 | { |
| 808 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 809 | BMCWEB_LOG_ERROR("Property Reports contains invalid value: {}", |
| 810 | path.str); |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 811 | return std::nullopt; |
| 812 | } |
| 813 | } |
| 814 | |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 815 | nlohmann::json::object_t report; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 816 | report["@odata.id"] = boost::urls::format( |
| 817 | "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", |
| 818 | reportId); |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 819 | reports.emplace_back(std::move(report)); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 822 | return {std::move(reports)}; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | inline std::vector<std::string> |
| 826 | getMetricProperties(const TriggerSensorsParams& sensors) |
| 827 | { |
| 828 | std::vector<std::string> metricProperties; |
| 829 | metricProperties.reserve(sensors.size()); |
| 830 | for (const auto& [_, metadata] : sensors) |
| 831 | { |
| 832 | metricProperties.emplace_back(metadata); |
| 833 | } |
| 834 | |
| 835 | return metricProperties; |
| 836 | } |
| 837 | |
| 838 | inline bool fillTrigger( |
| 839 | nlohmann::json& json, const std::string& id, |
| 840 | const std::vector<std::pair<std::string, TriggerGetParamsVariant>>& |
| 841 | properties) |
| 842 | { |
| 843 | const std::string* name = nullptr; |
| 844 | const bool* discrete = nullptr; |
| 845 | const TriggerSensorsParams* sensors = nullptr; |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 846 | const std::vector<sdbusplus::message::object_path>* reports = nullptr; |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 847 | const std::vector<std::string>* triggerActions = nullptr; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 848 | const TriggerThresholdParamsExt* thresholds = nullptr; |
| 849 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 850 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 851 | dbus_utils::UnpackErrorPrinter(), properties, "Name", name, "Discrete", |
| 852 | discrete, "Sensors", sensors, "Reports", reports, "TriggerActions", |
| 853 | triggerActions, "Thresholds", thresholds); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 854 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 855 | if (!success) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 856 | { |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 857 | return false; |
| 858 | } |
| 859 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 860 | if (triggerActions != nullptr) |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 861 | { |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 862 | std::optional<nlohmann::json::array_t> redfishTriggerActions = |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 863 | getTriggerActions(*triggerActions); |
| 864 | if (!redfishTriggerActions) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 865 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 866 | BMCWEB_LOG_ERROR( |
| 867 | "Property TriggerActions is invalid in Trigger: {}", id); |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 868 | return false; |
| 869 | } |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 870 | json["TriggerActions"] = *redfishTriggerActions; |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | if (reports != nullptr) |
| 874 | { |
| 875 | std::optional<nlohmann::json> linkedReports = |
| 876 | getMetricReportDefinitions(*reports); |
| 877 | if (!linkedReports) |
| 878 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 879 | BMCWEB_LOG_ERROR("Property Reports is invalid in Trigger: {}", id); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 880 | return false; |
| 881 | } |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 882 | json["Links"]["MetricReportDefinitions"] = *linkedReports; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 883 | } |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 884 | |
| 885 | if (discrete != nullptr) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 886 | { |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 887 | if (*discrete) |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 888 | { |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 889 | std::optional<nlohmann::json::array_t> discreteTriggers = |
| 890 | getDiscreteTriggers(*thresholds); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 891 | |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 892 | if (!discreteTriggers) |
| 893 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 894 | BMCWEB_LOG_ERROR("Property Thresholds is invalid for discrete " |
| 895 | "triggers in Trigger: {}", |
| 896 | id); |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 897 | return false; |
| 898 | } |
| 899 | |
| 900 | json["DiscreteTriggers"] = *discreteTriggers; |
| 901 | json["DiscreteTriggerCondition"] = |
| 902 | discreteTriggers->empty() ? "Changed" : "Specified"; |
| 903 | json["MetricType"] = "Discrete"; |
| 904 | } |
| 905 | else |
| 906 | { |
| 907 | std::optional<nlohmann::json> numericThresholds = |
| 908 | getNumericThresholds(*thresholds); |
| 909 | |
| 910 | if (!numericThresholds) |
| 911 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 912 | BMCWEB_LOG_ERROR("Property Thresholds is invalid for numeric " |
| 913 | "thresholds in Trigger: {}", |
| 914 | id); |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 915 | return false; |
| 916 | } |
| 917 | |
| 918 | json["NumericThresholds"] = *numericThresholds; |
| 919 | json["MetricType"] = "Numeric"; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | if (name != nullptr) |
| 924 | { |
| 925 | json["Name"] = *name; |
| 926 | } |
| 927 | |
| 928 | if (sensors != nullptr) |
| 929 | { |
| 930 | json["MetricProperties"] = getMetricProperties(*sensors); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 933 | json["@odata.type"] = "#Triggers.v1_2_0.Triggers"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 934 | json["@odata.id"] = |
| 935 | boost::urls::format("/redfish/v1/TelemetryService/Triggers/{}", id); |
Szymon Dompke | 3f215c9 | 2022-02-22 13:58:00 +0100 | [diff] [blame] | 936 | json["Id"] = id; |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 937 | |
| 938 | return true; |
| 939 | } |
| 940 | |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 941 | inline void handleTriggerCollectionPost( |
| 942 | App& app, const crow::Request& req, |
| 943 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 944 | { |
| 945 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 946 | { |
| 947 | return; |
| 948 | } |
| 949 | |
| 950 | telemetry::Context ctx; |
| 951 | if (!telemetry::parsePostTriggerParams(asyncResp->res, req, ctx)) |
| 952 | { |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | crow::connections::systemBus->async_method_call( |
| 957 | [asyncResp, id = ctx.id](const boost::system::error_code& ec, |
| 958 | const std::string& dbusPath) { |
| 959 | afterCreateTrigger(ec, dbusPath, asyncResp, id); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 960 | }, |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 961 | service, "/xyz/openbmc_project/Telemetry/Triggers", |
| 962 | "xyz.openbmc_project.Telemetry.TriggerManager", "AddTrigger", |
| 963 | "TelemetryService/" + ctx.id, ctx.name, ctx.actions, ctx.sensors, |
| 964 | ctx.reports, ctx.thresholds); |
| 965 | } |
| 966 | |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 967 | } // namespace telemetry |
| 968 | |
| 969 | inline void requestRoutesTriggerCollection(App& app) |
| 970 | { |
| 971 | BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/") |
| 972 | .privileges(redfish::privileges::getTriggersCollection) |
| 973 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 974 | [&app](const crow::Request& req, |
| 975 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 976 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 977 | { |
| 978 | return; |
| 979 | } |
| 980 | asyncResp->res.jsonValue["@odata.type"] = |
| 981 | "#TriggersCollection.TriggersCollection"; |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 982 | asyncResp->res.jsonValue["@odata.id"] = |
| 983 | "/redfish/v1/TelemetryService/Triggers"; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 984 | asyncResp->res.jsonValue["Name"] = "Triggers Collection"; |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 985 | constexpr std::array<std::string_view, 1> interfaces{ |
| 986 | telemetry::triggerInterface}; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 987 | collection_util::getCollectionMembers( |
Willy Tu | ae9031f | 2022-09-27 05:48:07 +0000 | [diff] [blame] | 988 | asyncResp, |
| 989 | boost::urls::url("/redfish/v1/TelemetryService/Triggers"), |
| 990 | interfaces, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 991 | "/xyz/openbmc_project/Telemetry/Triggers/TelemetryService"); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 992 | }); |
Szymon Dompke | dd1c4a9 | 2022-03-04 13:11:38 +0100 | [diff] [blame] | 993 | |
| 994 | BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/") |
| 995 | .privileges(redfish::privileges::postTriggersCollection) |
| 996 | .methods(boost::beast::http::verb::post)(std::bind_front( |
| 997 | telemetry::handleTriggerCollectionPost, std::ref(app))); |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 1000 | inline void requestRoutesTrigger(App& app) |
| 1001 | { |
| 1002 | BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/<str>/") |
| 1003 | .privileges(redfish::privileges::getTriggers) |
| 1004 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 1005 | [&app](const crow::Request& req, |
| 1006 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1007 | const std::string& id) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1008 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1009 | { |
| 1010 | return; |
| 1011 | } |
Krzysztof Grobelny | 8947449 | 2022-09-06 16:30:38 +0200 | [diff] [blame] | 1012 | sdbusplus::asio::getAllProperties( |
| 1013 | *crow::connections::systemBus, telemetry::service, |
| 1014 | telemetry::getDbusTriggerPath(id), telemetry::triggerInterface, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1015 | [asyncResp, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1016 | id](const boost::system::error_code& ec, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1017 | const std::vector<std::pair< |
| 1018 | std::string, telemetry::TriggerGetParamsVariant>>& ret) { |
| 1019 | if (ec.value() == EBADR || |
| 1020 | ec == boost::system::errc::host_unreachable) |
| 1021 | { |
| 1022 | messages::resourceNotFound(asyncResp->res, "Triggers", id); |
| 1023 | return; |
| 1024 | } |
| 1025 | if (ec) |
| 1026 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1027 | BMCWEB_LOG_ERROR("respHandler DBus error {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1028 | messages::internalError(asyncResp->res); |
| 1029 | return; |
| 1030 | } |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 1031 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1032 | if (!telemetry::fillTrigger(asyncResp->res.jsonValue, id, ret)) |
| 1033 | { |
| 1034 | messages::internalError(asyncResp->res); |
| 1035 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1036 | }); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 1037 | }); |
Szymon Dompke | 163994a | 2021-08-12 17:30:23 +0200 | [diff] [blame] | 1038 | |
| 1039 | BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/Triggers/<str>/") |
| 1040 | .privileges(redfish::privileges::deleteTriggers) |
| 1041 | .methods(boost::beast::http::verb::delete_)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 1042 | [&app](const crow::Request& req, |
| 1043 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1044 | const std::string& id) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1045 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1046 | { |
| 1047 | return; |
| 1048 | } |
| 1049 | const std::string triggerPath = telemetry::getDbusTriggerPath(id); |
Szymon Dompke | 163994a | 2021-08-12 17:30:23 +0200 | [diff] [blame] | 1050 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1051 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1052 | [asyncResp, id](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1053 | if (ec.value() == EBADR) |
| 1054 | { |
| 1055 | messages::resourceNotFound(asyncResp->res, "Triggers", id); |
| 1056 | return; |
| 1057 | } |
Szymon Dompke | 163994a | 2021-08-12 17:30:23 +0200 | [diff] [blame] | 1058 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1059 | if (ec) |
| 1060 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 1061 | BMCWEB_LOG_ERROR("respHandler DBus error {}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1062 | messages::internalError(asyncResp->res); |
| 1063 | return; |
| 1064 | } |
Szymon Dompke | 163994a | 2021-08-12 17:30:23 +0200 | [diff] [blame] | 1065 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1066 | asyncResp->res.result(boost::beast::http::status::no_content); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 1067 | }, |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1068 | telemetry::service, triggerPath, |
| 1069 | "xyz.openbmc_project.Object.Delete", "Delete"); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 1070 | }); |
Lukasz Kazmierczak | 1b7e696 | 2021-08-02 13:40:27 +0200 | [diff] [blame] | 1071 | } |
| 1072 | |
Lukasz Kazmierczak | 07148cf | 2021-08-02 11:08:53 +0200 | [diff] [blame] | 1073 | } // namespace redfish |