1 #pragma once 2 3 #include "sensors.hpp" 4 #include "utils/telemetry_utils.hpp" 5 #include "utils/time_utils.hpp" 6 7 #include <app.hpp> 8 #include <boost/container/flat_map.hpp> 9 10 #include <tuple> 11 #include <variant> 12 13 namespace redfish 14 { 15 16 namespace telemetry 17 { 18 19 using ReadingParameters = 20 std::vector<std::tuple<sdbusplus::message::object_path, std::string, 21 std::string, std::string>>; 22 23 inline void fillReportDefinition( 24 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id, 25 const std::vector< 26 std::pair<std::string, std::variant<std::string, bool, uint64_t, 27 ReadingParameters>>>& ret) 28 { 29 asyncResp->res.jsonValue["@odata.type"] = 30 "#MetricReportDefinition.v1_3_0.MetricReportDefinition"; 31 asyncResp->res.jsonValue["@odata.id"] = 32 telemetry::metricReportDefinitionUri + id; 33 asyncResp->res.jsonValue["Id"] = id; 34 asyncResp->res.jsonValue["Name"] = id; 35 asyncResp->res.jsonValue["MetricReport"]["@odata.id"] = 36 telemetry::metricReportUri + id; 37 asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 38 asyncResp->res.jsonValue["ReportUpdates"] = "Overwrite"; 39 40 const bool* emitsReadingsUpdate = nullptr; 41 const bool* logToMetricReportsCollection = nullptr; 42 const ReadingParameters* readingParams = nullptr; 43 const std::string* reportingType = nullptr; 44 const uint64_t* interval = nullptr; 45 for (const auto& [key, var] : ret) 46 { 47 if (key == "EmitsReadingsUpdate") 48 { 49 emitsReadingsUpdate = std::get_if<bool>(&var); 50 } 51 else if (key == "LogToMetricReportsCollection") 52 { 53 logToMetricReportsCollection = std::get_if<bool>(&var); 54 } 55 else if (key == "ReadingParameters") 56 { 57 readingParams = std::get_if<ReadingParameters>(&var); 58 } 59 else if (key == "ReportingType") 60 { 61 reportingType = std::get_if<std::string>(&var); 62 } 63 else if (key == "Interval") 64 { 65 interval = std::get_if<uint64_t>(&var); 66 } 67 } 68 if (!emitsReadingsUpdate || !logToMetricReportsCollection || 69 !readingParams || !reportingType || !interval) 70 { 71 BMCWEB_LOG_ERROR << "Property type mismatch or property is missing"; 72 messages::internalError(asyncResp->res); 73 return; 74 } 75 76 std::vector<std::string> redfishReportActions; 77 redfishReportActions.reserve(2); 78 if (*emitsReadingsUpdate) 79 { 80 redfishReportActions.emplace_back("RedfishEvent"); 81 } 82 if (*logToMetricReportsCollection) 83 { 84 redfishReportActions.emplace_back("LogToMetricReportsCollection"); 85 } 86 87 nlohmann::json metrics = nlohmann::json::array(); 88 for (auto& [sensorPath, operationType, id, metadata] : *readingParams) 89 { 90 metrics.push_back({ 91 {"MetricId", id}, 92 {"MetricProperties", {metadata}}, 93 }); 94 } 95 asyncResp->res.jsonValue["Metrics"] = metrics; 96 asyncResp->res.jsonValue["MetricReportDefinitionType"] = *reportingType; 97 asyncResp->res.jsonValue["ReportActions"] = redfishReportActions; 98 asyncResp->res.jsonValue["Schedule"]["RecurrenceInterval"] = 99 time_utils::toDurationString(std::chrono::milliseconds(*interval)); 100 } 101 102 struct AddReportArgs 103 { 104 std::string name; 105 std::string reportingType; 106 bool emitsReadingsUpdate = false; 107 bool logToMetricReportsCollection = false; 108 uint64_t interval = 0; 109 std::vector<std::pair<std::string, std::vector<std::string>>> metrics; 110 }; 111 112 inline bool toDbusReportActions(crow::Response& res, 113 std::vector<std::string>& actions, 114 AddReportArgs& args) 115 { 116 size_t index = 0; 117 for (auto& action : actions) 118 { 119 if (action == "RedfishEvent") 120 { 121 args.emitsReadingsUpdate = true; 122 } 123 else if (action == "LogToMetricReportsCollection") 124 { 125 args.logToMetricReportsCollection = true; 126 } 127 else 128 { 129 messages::propertyValueNotInList( 130 res, action, "ReportActions/" + std::to_string(index)); 131 return false; 132 } 133 index++; 134 } 135 return true; 136 } 137 138 inline bool getUserParameters(crow::Response& res, const crow::Request& req, 139 AddReportArgs& args) 140 { 141 std::vector<nlohmann::json> metrics; 142 std::vector<std::string> reportActions; 143 std::optional<nlohmann::json> schedule; 144 if (!json_util::readJson(req, res, "Id", args.name, "Metrics", metrics, 145 "MetricReportDefinitionType", args.reportingType, 146 "ReportActions", reportActions, "Schedule", 147 schedule)) 148 { 149 return false; 150 } 151 152 constexpr const char* allowedCharactersInName = 153 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; 154 if (args.name.empty() || args.name.find_first_not_of( 155 allowedCharactersInName) != std::string::npos) 156 { 157 BMCWEB_LOG_ERROR << "Failed to match " << args.name 158 << " with allowed character " 159 << allowedCharactersInName; 160 messages::propertyValueIncorrect(res, "Id", args.name); 161 return false; 162 } 163 164 if (args.reportingType != "Periodic" && args.reportingType != "OnRequest") 165 { 166 messages::propertyValueNotInList(res, args.reportingType, 167 "MetricReportDefinitionType"); 168 return false; 169 } 170 171 if (!toDbusReportActions(res, reportActions, args)) 172 { 173 return false; 174 } 175 176 if (args.reportingType == "Periodic") 177 { 178 if (!schedule) 179 { 180 messages::createFailedMissingReqProperties(res, "Schedule"); 181 return false; 182 } 183 184 std::string durationStr; 185 if (!json_util::readJson(*schedule, res, "RecurrenceInterval", 186 durationStr)) 187 { 188 return false; 189 } 190 191 std::optional<std::chrono::milliseconds> durationNum = 192 time_utils::fromDurationString(durationStr); 193 if (!durationNum) 194 { 195 messages::propertyValueIncorrect(res, "RecurrenceInterval", 196 durationStr); 197 return false; 198 } 199 args.interval = static_cast<uint64_t>(durationNum->count()); 200 } 201 202 args.metrics.reserve(metrics.size()); 203 for (auto& m : metrics) 204 { 205 std::string id; 206 std::vector<std::string> uris; 207 if (!json_util::readJson(m, res, "MetricId", id, "MetricProperties", 208 uris)) 209 { 210 return false; 211 } 212 213 args.metrics.emplace_back(std::move(id), std::move(uris)); 214 } 215 216 return true; 217 } 218 219 inline bool getChassisSensorNode( 220 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 221 const std::vector<std::pair<std::string, std::vector<std::string>>>& 222 metrics, 223 boost::container::flat_set<std::pair<std::string, std::string>>& matched) 224 { 225 for (const auto& [id, uris] : metrics) 226 { 227 for (size_t i = 0; i < uris.size(); i++) 228 { 229 const std::string& uri = uris[i]; 230 std::string chassis; 231 std::string node; 232 233 if (!boost::starts_with(uri, "/redfish/v1/Chassis/") || 234 !dbus::utility::getNthStringFromPath(uri, 3, chassis) || 235 !dbus::utility::getNthStringFromPath(uri, 4, node)) 236 { 237 BMCWEB_LOG_ERROR << "Failed to get chassis and sensor Node " 238 "from " 239 << uri; 240 messages::propertyValueIncorrect(asyncResp->res, uri, 241 "MetricProperties/" + 242 std::to_string(i)); 243 return false; 244 } 245 246 if (boost::ends_with(node, "#")) 247 { 248 node.pop_back(); 249 } 250 251 matched.emplace(std::move(chassis), std::move(node)); 252 } 253 } 254 return true; 255 } 256 257 class AddReport 258 { 259 public: 260 AddReport(AddReportArgs argsIn, 261 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) : 262 asyncResp(asyncResp), 263 args{std::move(argsIn)} 264 {} 265 ~AddReport() 266 { 267 if (asyncResp->res.result() != boost::beast::http::status::ok) 268 { 269 return; 270 } 271 272 telemetry::ReadingParameters readingParams; 273 readingParams.reserve(args.metrics.size()); 274 275 for (const auto& [id, uris] : args.metrics) 276 { 277 for (size_t i = 0; i < uris.size(); i++) 278 { 279 const std::string& uri = uris[i]; 280 auto el = uriToDbus.find(uri); 281 if (el == uriToDbus.end()) 282 { 283 BMCWEB_LOG_ERROR << "Failed to find DBus sensor " 284 "corresponding to URI " 285 << uri; 286 messages::propertyValueNotInList(asyncResp->res, uri, 287 "MetricProperties/" + 288 std::to_string(i)); 289 return; 290 } 291 292 const std::string& dbusPath = el->second; 293 readingParams.emplace_back(dbusPath, "SINGLE", id, uri); 294 } 295 } 296 const std::shared_ptr<bmcweb::AsyncResp> aResp = asyncResp; 297 crow::connections::systemBus->async_method_call( 298 [aResp, name = args.name, uriToDbus = std::move(uriToDbus)]( 299 const boost::system::error_code ec, const std::string&) { 300 if (ec == boost::system::errc::file_exists) 301 { 302 messages::resourceAlreadyExists( 303 aResp->res, "MetricReportDefinition", "Id", name); 304 return; 305 } 306 if (ec == boost::system::errc::too_many_files_open) 307 { 308 messages::createLimitReachedForResource(aResp->res); 309 return; 310 } 311 if (ec == boost::system::errc::argument_list_too_long) 312 { 313 nlohmann::json metricProperties = nlohmann::json::array(); 314 for (const auto& [uri, _] : uriToDbus) 315 { 316 metricProperties.emplace_back(uri); 317 } 318 messages::propertyValueIncorrect( 319 aResp->res, metricProperties, "MetricProperties"); 320 return; 321 } 322 if (ec) 323 { 324 messages::internalError(aResp->res); 325 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec; 326 return; 327 } 328 329 messages::created(aResp->res); 330 }, 331 telemetry::service, "/xyz/openbmc_project/Telemetry/Reports", 332 "xyz.openbmc_project.Telemetry.ReportManager", "AddReport", 333 "TelemetryService/" + args.name, args.reportingType, 334 args.emitsReadingsUpdate, args.logToMetricReportsCollection, 335 args.interval, readingParams); 336 } 337 338 void insert(const boost::container::flat_map<std::string, std::string>& el) 339 { 340 uriToDbus.insert(el.begin(), el.end()); 341 } 342 343 private: 344 const std::shared_ptr<bmcweb::AsyncResp> asyncResp; 345 AddReportArgs args; 346 boost::container::flat_map<std::string, std::string> uriToDbus{}; 347 }; 348 } // namespace telemetry 349 350 inline void requestRoutesMetricReportDefinitionCollection(App& app) 351 { 352 BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/") 353 .privileges({"Login"}) 354 .methods(boost::beast::http::verb::get)( 355 [](const crow::Request&, 356 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 357 asyncResp->res.jsonValue["@odata.type"] = 358 "#MetricReportDefinitionCollection." 359 "MetricReportDefinitionCollection"; 360 asyncResp->res.jsonValue["@odata.id"] = 361 "/redfish/v1/TelemetryService/MetricReportDefinitions"; 362 asyncResp->res.jsonValue["Name"] = 363 "Metric Definition Collection"; 364 365 telemetry::getReportCollection( 366 asyncResp, telemetry::metricReportDefinitionUri); 367 }); 368 369 BMCWEB_ROUTE(app, "/redfish/v1/TelemetryService/MetricReportDefinitions/") 370 .privileges({"ConfigureManager"}) 371 .methods(boost::beast::http::verb::post)( 372 [](const crow::Request& req, 373 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 374 telemetry::AddReportArgs args; 375 if (!telemetry::getUserParameters(asyncResp->res, req, args)) 376 { 377 return; 378 } 379 380 boost::container::flat_set<std::pair<std::string, std::string>> 381 chassisSensors; 382 if (!telemetry::getChassisSensorNode(asyncResp, args.metrics, 383 chassisSensors)) 384 { 385 return; 386 } 387 388 auto addReportReq = std::make_shared<telemetry::AddReport>( 389 std::move(args), asyncResp); 390 for (const auto& [chassis, sensorType] : chassisSensors) 391 { 392 retrieveUriToDbusMap( 393 chassis, sensorType, 394 [asyncResp, addReportReq]( 395 const boost::beast::http::status status, 396 const boost::container::flat_map< 397 std::string, std::string>& uriToDbus) { 398 if (status != boost::beast::http::status::ok) 399 { 400 BMCWEB_LOG_ERROR 401 << "Failed to retrieve URI to dbus " 402 "sensors map with err " 403 << static_cast<unsigned>(status); 404 return; 405 } 406 addReportReq->insert(uriToDbus); 407 }); 408 } 409 }); 410 } 411 412 inline void requestRoutesMetricReportDefinition(App& app) 413 { 414 BMCWEB_ROUTE(app, 415 "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/") 416 .privileges({"Login"}) 417 .methods(boost::beast::http::verb::get)( 418 [](const crow::Request&, 419 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 420 const std::string& id) { 421 crow::connections::systemBus->async_method_call( 422 [asyncResp, id]( 423 const boost::system::error_code ec, 424 const std::vector<std::pair< 425 std::string, 426 std::variant<std::string, bool, uint64_t, 427 telemetry::ReadingParameters>>>& ret) { 428 if (ec.value() == EBADR || 429 ec == boost::system::errc::host_unreachable) 430 { 431 messages::resourceNotFound( 432 asyncResp->res, "MetricReportDefinition", id); 433 return; 434 } 435 if (ec) 436 { 437 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec; 438 messages::internalError(asyncResp->res); 439 return; 440 } 441 442 telemetry::fillReportDefinition(asyncResp, id, ret); 443 }, 444 telemetry::service, telemetry::getDbusReportPath(id), 445 "org.freedesktop.DBus.Properties", "GetAll", 446 telemetry::reportInterface); 447 }); 448 BMCWEB_ROUTE(app, 449 "/redfish/v1/TelemetryService/MetricReportDefinitions/<str>/") 450 .privileges({"ConfigureManager"}) 451 .methods(boost::beast::http::verb::delete_)( 452 [](const crow::Request&, 453 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 454 const std::string& id) 455 456 { 457 const std::string reportPath = telemetry::getDbusReportPath(id); 458 459 crow::connections::systemBus->async_method_call( 460 [asyncResp, id](const boost::system::error_code ec) { 461 /* 462 * boost::system::errc and std::errc are missing value 463 * for EBADR error that is defined in Linux. 464 */ 465 if (ec.value() == EBADR) 466 { 467 messages::resourceNotFound( 468 asyncResp->res, "MetricReportDefinition", id); 469 return; 470 } 471 472 if (ec) 473 { 474 BMCWEB_LOG_ERROR << "respHandler DBus error " << ec; 475 messages::internalError(asyncResp->res); 476 return; 477 } 478 479 asyncResp->res.result( 480 boost::beast::http::status::no_content); 481 }, 482 telemetry::service, reportPath, 483 "xyz.openbmc_project.Object.Delete", "Delete"); 484 }); 485 } 486 } // namespace redfish 487