| Gunnar Mills | 5ffbc9a | 2025-04-24 08:41:49 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | // SPDX-FileCopyrightText: Copyright 2020 Intel Corporation |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 4 | #include "subscription.hpp" |
| 5 | |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 6 | #include "dbus_singleton.hpp" |
| Alexander Hansen | b80ba2e | 2024-11-18 12:24:35 +0100 | [diff] [blame] | 7 | #include "event_log.hpp" |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 8 | #include "event_logs_object_type.hpp" |
| 9 | #include "event_matches_filter.hpp" |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 10 | #include "event_service_store.hpp" |
| 11 | #include "filter_expr_executor.hpp" |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 12 | #include "heartbeat_messages.hpp" |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 13 | #include "http_client.hpp" |
| 14 | #include "http_response.hpp" |
| 15 | #include "logging.hpp" |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 16 | #include "server_sent_event.hpp" |
| 17 | #include "ssl_key_handler.hpp" |
| Ed Tanous | f86cdd7 | 2025-08-12 17:50:13 -0700 | [diff] [blame] | 18 | #include "telemetry_readings.hpp" |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 19 | #include "utils/time_utils.hpp" |
| 20 | |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 21 | #include <boost/asio/error.hpp> |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 22 | #include <boost/asio/io_context.hpp> |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 23 | #include <boost/asio/steady_timer.hpp> |
| Ed Tanous | 4ac7894 | 2024-12-02 08:25:38 -0800 | [diff] [blame] | 24 | #include <boost/beast/http/field.hpp> |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 25 | #include <boost/beast/http/verb.hpp> |
| 26 | #include <boost/system/errc.hpp> |
| 27 | #include <boost/url/format.hpp> |
| 28 | #include <boost/url/url_view_base.hpp> |
| 29 | #include <nlohmann/json.hpp> |
| 30 | |
| 31 | #include <algorithm> |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 32 | #include <chrono> |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 33 | #include <cstdint> |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 34 | #include <format> |
| 35 | #include <functional> |
| 36 | #include <memory> |
| 37 | #include <span> |
| 38 | #include <string> |
| 39 | #include <string_view> |
| 40 | #include <utility> |
| 41 | #include <vector> |
| 42 | |
| 43 | namespace redfish |
| 44 | { |
| 45 | |
| 46 | Subscription::Subscription( |
| 47 | std::shared_ptr<persistent_data::UserSubscription> userSubIn, |
| 48 | const boost::urls::url_view_base& url, boost::asio::io_context& ioc) : |
| 49 | userSub{std::move(userSubIn)}, |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 50 | policy(std::make_shared<crow::ConnectionPolicy>()), hbTimer(ioc) |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 51 | { |
| 52 | userSub->destinationUrl = url; |
| 53 | client.emplace(ioc, policy); |
| 54 | // Subscription constructor |
| 55 | policy->invalidResp = retryRespHandler; |
| 56 | } |
| 57 | |
| 58 | Subscription::Subscription(crow::sse_socket::Connection& connIn) : |
| 59 | userSub{std::make_shared<persistent_data::UserSubscription>()}, |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 60 | sseConn(&connIn), hbTimer(crow::connections::systemBus->get_io_context()) |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 61 | {} |
| 62 | |
| 63 | // callback for subscription sendData |
| Myung Bae | 99ff0dd | 2025-04-22 14:38:36 -0500 | [diff] [blame] | 64 | void Subscription::resHandler(const std::shared_ptr<Subscription>& /*self*/, |
| 65 | const crow::Response& res) |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 66 | { |
| 67 | BMCWEB_LOG_DEBUG("Response handled with return code: {}", res.resultInt()); |
| 68 | |
| 69 | if (!client) |
| 70 | { |
| 71 | BMCWEB_LOG_ERROR( |
| 72 | "Http client wasn't filled but http client callback was called."); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (userSub->retryPolicy != "TerminateAfterRetries") |
| 77 | { |
| 78 | return; |
| 79 | } |
| 80 | if (client->isTerminated()) |
| 81 | { |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 82 | hbTimer.cancel(); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 83 | if (deleter) |
| 84 | { |
| 85 | BMCWEB_LOG_INFO("Subscription {} is deleted after MaxRetryAttempts", |
| 86 | userSub->id); |
| 87 | deleter(); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 92 | void Subscription::sendHeartbeatEvent() |
| 93 | { |
| 94 | // send the heartbeat message |
| 95 | nlohmann::json eventMessage = messages::redfishServiceFunctional(); |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 96 | eventMessage["EventTimestamp"] = time_utils::getDateTimeOffsetNow().first; |
| 97 | eventMessage["OriginOfCondition"] = |
| 98 | std::format("/redfish/v1/EventService/Subscriptions/{}", userSub->id); |
| 99 | eventMessage["MemberId"] = "0"; |
| 100 | |
| 101 | nlohmann::json::array_t eventRecord; |
| 102 | eventRecord.emplace_back(std::move(eventMessage)); |
| 103 | |
| 104 | nlohmann::json msgJson; |
| 105 | msgJson["@odata.type"] = "#Event.v1_4_0.Event"; |
| 106 | msgJson["Name"] = "Heartbeat"; |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 107 | msgJson["Events"] = std::move(eventRecord); |
| 108 | |
| 109 | std::string strMsg = |
| 110 | msgJson.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 111 | |
| 112 | // Note, eventId here is always zero, because this is a a per subscription |
| 113 | // event and doesn't have an "ID" |
| 114 | uint64_t eventId = 0; |
| 115 | sendEventToSubscriber(eventId, std::move(strMsg)); |
| Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void Subscription::scheduleNextHeartbeatEvent() |
| 119 | { |
| 120 | hbTimer.expires_after(std::chrono::minutes(userSub->hbIntervalMinutes)); |
| 121 | hbTimer.async_wait( |
| 122 | std::bind_front(&Subscription::onHbTimeout, this, weak_from_this())); |
| 123 | } |
| 124 | |
| 125 | void Subscription::heartbeatParametersChanged() |
| 126 | { |
| 127 | hbTimer.cancel(); |
| 128 | |
| 129 | if (userSub->sendHeartbeat) |
| 130 | { |
| 131 | scheduleNextHeartbeatEvent(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void Subscription::onHbTimeout(const std::weak_ptr<Subscription>& weakSelf, |
| 136 | const boost::system::error_code& ec) |
| 137 | { |
| 138 | if (ec == boost::asio::error::operation_aborted) |
| 139 | { |
| 140 | BMCWEB_LOG_DEBUG("heartbeat timer async_wait is aborted"); |
| 141 | return; |
| 142 | } |
| 143 | if (ec == boost::system::errc::operation_canceled) |
| 144 | { |
| 145 | BMCWEB_LOG_DEBUG("heartbeat timer async_wait canceled"); |
| 146 | return; |
| 147 | } |
| 148 | if (ec) |
| 149 | { |
| 150 | BMCWEB_LOG_CRITICAL("heartbeat timer async_wait failed: {}", ec); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | std::shared_ptr<Subscription> self = weakSelf.lock(); |
| 155 | if (!self) |
| 156 | { |
| 157 | BMCWEB_LOG_CRITICAL("onHbTimeout failed on Subscription"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Timer expired. |
| 162 | sendHeartbeatEvent(); |
| 163 | |
| 164 | // reschedule heartbeat timer |
| 165 | scheduleNextHeartbeatEvent(); |
| 166 | } |
| 167 | |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 168 | bool Subscription::sendEventToSubscriber(uint64_t eventId, std::string&& msg) |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 169 | { |
| 170 | persistent_data::EventServiceConfig eventServiceConfig = |
| 171 | persistent_data::EventServiceStore::getInstance() |
| 172 | .getEventServiceConfig(); |
| 173 | if (!eventServiceConfig.enabled) |
| 174 | { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | if (client) |
| 179 | { |
| Ed Tanous | 4ac7894 | 2024-12-02 08:25:38 -0800 | [diff] [blame] | 180 | boost::beast::http::fields httpHeadersCopy(userSub->httpHeaders); |
| 181 | httpHeadersCopy.set(boost::beast::http::field::content_type, |
| 182 | "application/json"); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 183 | client->sendDataWithCallback( |
| 184 | std::move(msg), userSub->destinationUrl, |
| 185 | static_cast<ensuressl::VerifyCertificate>( |
| 186 | userSub->verifyCertificate), |
| Ed Tanous | 4ac7894 | 2024-12-02 08:25:38 -0800 | [diff] [blame] | 187 | httpHeadersCopy, boost::beast::http::verb::post, |
| Myung Bae | 99ff0dd | 2025-04-22 14:38:36 -0500 | [diff] [blame] | 188 | std::bind_front(&Subscription::resHandler, this, |
| 189 | shared_from_this())); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
| 193 | if (sseConn != nullptr) |
| 194 | { |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 195 | sseConn->sendSseEvent(std::to_string(eventId), msg); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 200 | void Subscription::filterAndSendEventLogs( |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 201 | uint64_t eventId, const std::vector<EventLogObjectsType>& eventRecords) |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 202 | { |
| 203 | nlohmann::json::array_t logEntryArray; |
| 204 | for (const EventLogObjectsType& logEntry : eventRecords) |
| 205 | { |
| Igor Kanyuka | 0309c21 | 2025-01-10 03:38:25 -0800 | [diff] [blame] | 206 | BMCWEB_LOG_DEBUG("Processing logEntry: {}, {} '{}'", logEntry.id, |
| 207 | logEntry.timestamp, logEntry.messageId); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 208 | std::vector<std::string_view> messageArgsView( |
| 209 | logEntry.messageArgs.begin(), logEntry.messageArgs.end()); |
| 210 | |
| 211 | nlohmann::json::object_t bmcLogEntry; |
| 212 | if (event_log::formatEventLogEntry( |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 213 | eventId, logEntry.id, logEntry.messageId, messageArgsView, |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 214 | logEntry.timestamp, userSub->customText, bmcLogEntry) != 0) |
| 215 | { |
| Igor Kanyuka | 0309c21 | 2025-01-10 03:38:25 -0800 | [diff] [blame] | 216 | BMCWEB_LOG_WARNING("Read eventLog entry failed"); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 217 | continue; |
| 218 | } |
| 219 | |
| 220 | if (!eventMatchesFilter(*userSub, bmcLogEntry, "")) |
| 221 | { |
| 222 | BMCWEB_LOG_DEBUG("Event {} did not match the filter", |
| 223 | nlohmann::json(bmcLogEntry).dump()); |
| 224 | continue; |
| 225 | } |
| 226 | |
| 227 | if (filter) |
| 228 | { |
| 229 | if (!memberMatches(bmcLogEntry, *filter)) |
| 230 | { |
| 231 | BMCWEB_LOG_DEBUG("Filter didn't match"); |
| 232 | continue; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | logEntryArray.emplace_back(std::move(bmcLogEntry)); |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 237 | eventId++; |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (logEntryArray.empty()) |
| 241 | { |
| 242 | BMCWEB_LOG_DEBUG("No log entries available to be transferred."); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | nlohmann::json msg; |
| 247 | msg["@odata.type"] = "#Event.v1_4_0.Event"; |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 248 | msg["Id"] = std::to_string(eventId); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 249 | msg["Name"] = "Event Log"; |
| 250 | msg["Events"] = std::move(logEntryArray); |
| 251 | std::string strMsg = |
| 252 | msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 253 | sendEventToSubscriber(eventId, std::move(strMsg)); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 256 | void Subscription::filterAndSendReports(uint64_t eventId, |
| 257 | const std::string& reportId, |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 258 | const telemetry::TimestampReadings& var) |
| 259 | { |
| 260 | boost::urls::url mrdUri = boost::urls::format( |
| 261 | "/redfish/v1/TelemetryService/MetricReportDefinitions/{}", reportId); |
| 262 | |
| 263 | // Empty list means no filter. Send everything. |
| 264 | if (!userSub->metricReportDefinitions.empty()) |
| 265 | { |
| 266 | if (std::ranges::find(userSub->metricReportDefinitions, |
| 267 | mrdUri.buffer()) == |
| 268 | userSub->metricReportDefinitions.end()) |
| 269 | { |
| 270 | return; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | nlohmann::json msg; |
| 275 | if (!telemetry::fillReport(msg, reportId, var)) |
| 276 | { |
| 277 | BMCWEB_LOG_ERROR("Failed to fill the MetricReport for DBus " |
| 278 | "Report with id {}", |
| 279 | reportId); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | // Context is set by user during Event subscription and it must be |
| 284 | // set for MetricReport response. |
| 285 | if (!userSub->customText.empty()) |
| 286 | { |
| 287 | msg["Context"] = userSub->customText; |
| 288 | } |
| 289 | |
| 290 | std::string strMsg = |
| 291 | msg.dump(2, ' ', true, nlohmann::json::error_handler_t::replace); |
| Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 292 | sendEventToSubscriber(eventId, std::move(strMsg)); |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void Subscription::updateRetryConfig(uint32_t retryAttempts, |
| 296 | uint32_t retryTimeoutInterval) |
| 297 | { |
| 298 | if (policy == nullptr) |
| 299 | { |
| 300 | BMCWEB_LOG_DEBUG("Retry policy was nullptr, ignoring set"); |
| 301 | return; |
| 302 | } |
| 303 | policy->maxRetryAttempts = retryAttempts; |
| 304 | policy->retryIntervalSecs = std::chrono::seconds(retryTimeoutInterval); |
| 305 | } |
| 306 | |
| Alexander Hansen | 02c1e29 | 2024-11-15 14:30:40 +0100 | [diff] [blame] | 307 | bool Subscription::matchSseId(const crow::sse_socket::Connection& thisConn) |
| 308 | { |
| 309 | return &thisConn == sseConn; |
| 310 | } |
| 311 | |
| 312 | // Check used to indicate what response codes are valid as part of our retry |
| 313 | // policy. 2XX is considered acceptable |
| 314 | boost::system::error_code Subscription::retryRespHandler(unsigned int respCode) |
| 315 | { |
| 316 | BMCWEB_LOG_DEBUG("Checking response code validity for SubscriptionEvent"); |
| 317 | if ((respCode < 200) || (respCode >= 300)) |
| 318 | { |
| 319 | return boost::system::errc::make_error_code( |
| 320 | boost::system::errc::result_out_of_range); |
| 321 | } |
| 322 | |
| 323 | // Return 0 if the response code is valid |
| 324 | return boost::system::errc::make_error_code(boost::system::errc::success); |
| 325 | } |
| 326 | |
| 327 | } // namespace redfish |