Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | // SPDX-FileCopyrightText: Copyright 2020 Intel Corporation |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 4 | #pragma once |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "app.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 6 | #include "async_resp.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 7 | #include "dbus_utility.hpp" |
| 8 | #include "error_messages.hpp" |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 9 | #include "event_service_manager.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 10 | #include "event_service_store.hpp" |
| 11 | #include "generated/enums/event_destination.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 12 | #include "http/utility.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 13 | #include "http_request.hpp" |
Ed Tanous | 9838eb2 | 2025-01-29 16:24:42 -0800 | [diff] [blame] | 14 | #include "io_context_singleton.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 15 | #include "logging.hpp" |
| 16 | #include "query.hpp" |
Alexander Hansen | d109e2b | 2024-11-18 14:38:06 +0100 | [diff] [blame] | 17 | #include "registries.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 18 | #include "registries/privilege_registry.hpp" |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 19 | #include "snmp_trap_event_clients.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 20 | #include "subscription.hpp" |
Alexander Hansen | d109e2b | 2024-11-18 14:38:06 +0100 | [diff] [blame] | 21 | #include "utils/json_utils.hpp" |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 22 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 23 | #include <asm-generic/errno.h> |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 24 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 25 | #include <boost/beast/http/fields.hpp> |
| 26 | #include <boost/beast/http/status.hpp> |
| 27 | #include <boost/beast/http/verb.hpp> |
| 28 | #include <boost/system/error_code.hpp> |
| 29 | #include <boost/system/result.hpp> |
| 30 | #include <boost/url/format.hpp> |
| 31 | #include <boost/url/parse.hpp> |
| 32 | #include <boost/url/url.hpp> |
| 33 | #include <sdbusplus/message/native_types.hpp> |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <array> |
| 37 | #include <cerrno> |
| 38 | #include <cstddef> |
| 39 | #include <cstdint> |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 40 | #include <memory> |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 41 | #include <optional> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 42 | #include <ranges> |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 43 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 44 | #include <utility> |
Ed Tanous | a14c911 | 2024-09-04 10:46:47 -0700 | [diff] [blame] | 45 | #include <vector> |
Patrick Williams | 1e270c5 | 2021-12-04 06:06:56 -0600 | [diff] [blame] | 46 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 47 | namespace redfish |
| 48 | { |
| 49 | |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 50 | static constexpr const std::array<const char*, 2> supportedEvtFormatTypes = { |
| 51 | eventFormatType, metricReportFormatType}; |
Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 52 | static constexpr const std::array<const char*, 4> supportedRegPrefixes = { |
| 53 | "Base", "OpenBMC", "TaskEvent", "HeartbeatEvent"}; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 54 | static constexpr const std::array<const char*, 3> supportedRetryPolicies = { |
| 55 | "TerminateAfterRetries", "SuspendRetries", "RetryForever"}; |
| 56 | |
Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 57 | static constexpr const std::array<const char*, 2> supportedResourceTypes = { |
| 58 | "Task", "Heartbeat"}; |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 59 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 60 | inline void requestRoutesEventService(App& app) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 61 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 62 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 63 | .privileges(redfish::privileges::getEventService) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 64 | .methods( |
| 65 | boost::beast::http::verb:: |
| 66 | get)([&app]( |
| 67 | const crow::Request& req, |
| 68 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 69 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 70 | { |
| 71 | return; |
| 72 | } |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 73 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 74 | asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/EventService"; |
| 75 | asyncResp->res.jsonValue["@odata.type"] = |
| 76 | "#EventService.v1_5_0.EventService"; |
| 77 | asyncResp->res.jsonValue["Id"] = "EventService"; |
| 78 | asyncResp->res.jsonValue["Name"] = "Event Service"; |
| 79 | asyncResp->res.jsonValue["ServerSentEventUri"] = |
| 80 | "/redfish/v1/EventService/SSE"; |
AppaRao Puli | 5e44e3d | 2021-03-16 15:37:24 +0000 | [diff] [blame] | 81 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 82 | asyncResp->res.jsonValue["Subscriptions"]["@odata.id"] = |
| 83 | "/redfish/v1/EventService/Subscriptions"; |
| 84 | asyncResp->res.jsonValue["Actions"]["#EventService.SubmitTestEvent"] |
| 85 | ["target"] = |
| 86 | "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent"; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 87 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 88 | const persistent_data::EventServiceConfig eventServiceConfig = |
| 89 | persistent_data::EventServiceStore::getInstance() |
| 90 | .getEventServiceConfig(); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 91 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 92 | asyncResp->res.jsonValue["Status"]["State"] = |
| 93 | (eventServiceConfig.enabled ? "Enabled" : "Disabled"); |
| 94 | asyncResp->res.jsonValue["ServiceEnabled"] = |
| 95 | eventServiceConfig.enabled; |
| 96 | asyncResp->res.jsonValue["DeliveryRetryAttempts"] = |
| 97 | eventServiceConfig.retryAttempts; |
| 98 | asyncResp->res.jsonValue["DeliveryRetryIntervalSeconds"] = |
| 99 | eventServiceConfig.retryTimeoutInterval; |
| 100 | asyncResp->res.jsonValue["EventFormatTypes"] = |
| 101 | supportedEvtFormatTypes; |
| 102 | asyncResp->res.jsonValue["RegistryPrefixes"] = supportedRegPrefixes; |
| 103 | asyncResp->res.jsonValue["ResourceTypes"] = supportedResourceTypes; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 104 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 105 | nlohmann::json::object_t supportedSSEFilters; |
| 106 | supportedSSEFilters["EventFormatType"] = true; |
| 107 | supportedSSEFilters["MessageId"] = true; |
| 108 | supportedSSEFilters["MetricReportDefinition"] = true; |
| 109 | supportedSSEFilters["RegistryPrefix"] = true; |
| 110 | supportedSSEFilters["OriginResource"] = false; |
| 111 | supportedSSEFilters["ResourceType"] = false; |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 112 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 113 | asyncResp->res.jsonValue["SSEFilterPropertiesSupported"] = |
| 114 | std::move(supportedSSEFilters); |
| 115 | }); |
Ayushi Smriti | 07941a8 | 2020-05-21 15:55:34 +0530 | [diff] [blame] | 116 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 117 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 118 | .privileges(redfish::privileges::patchEventService) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 119 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 120 | [&app](const crow::Request& req, |
| 121 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 122 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 123 | { |
| 124 | return; |
| 125 | } |
| 126 | std::optional<bool> serviceEnabled; |
| 127 | std::optional<uint32_t> retryAttemps; |
| 128 | std::optional<uint32_t> retryInterval; |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 129 | if (!json_util::readJsonPatch( // |
| 130 | req, asyncResp->res, // |
| 131 | "DeliveryRetryAttempts", retryAttemps, // |
Myung Bae | afc474a | 2024-10-09 00:53:29 -0700 | [diff] [blame] | 132 | "DeliveryRetryIntervalSeconds", retryInterval, // |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 133 | "ServiceEnabled", serviceEnabled // |
Myung Bae | afc474a | 2024-10-09 00:53:29 -0700 | [diff] [blame] | 134 | )) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 135 | { |
| 136 | return; |
| 137 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 138 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 139 | persistent_data::EventServiceConfig eventServiceConfig = |
| 140 | persistent_data::EventServiceStore::getInstance() |
| 141 | .getEventServiceConfig(); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 142 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 143 | if (serviceEnabled) |
| 144 | { |
| 145 | eventServiceConfig.enabled = *serviceEnabled; |
| 146 | } |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 147 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 148 | if (retryAttemps) |
| 149 | { |
| 150 | // Supported range [1-3] |
| 151 | if ((*retryAttemps < 1) || (*retryAttemps > 3)) |
| 152 | { |
| 153 | messages::queryParameterOutOfRange( |
| 154 | asyncResp->res, std::to_string(*retryAttemps), |
| 155 | "DeliveryRetryAttempts", "[1-3]"); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | eventServiceConfig.retryAttempts = *retryAttemps; |
| 160 | } |
| 161 | } |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 162 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 163 | if (retryInterval) |
| 164 | { |
| 165 | // Supported range [5 - 180] |
| 166 | if ((*retryInterval < 5) || (*retryInterval > 180)) |
| 167 | { |
| 168 | messages::queryParameterOutOfRange( |
| 169 | asyncResp->res, std::to_string(*retryInterval), |
| 170 | "DeliveryRetryIntervalSeconds", "[5-180]"); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | eventServiceConfig.retryTimeoutInterval = |
| 175 | *retryInterval; |
| 176 | } |
| 177 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 178 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 179 | EventServiceManager::getInstance().setEventServiceConfig( |
| 180 | eventServiceConfig); |
| 181 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | inline void requestRoutesSubmitTestEvent(App& app) |
| 185 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 186 | BMCWEB_ROUTE( |
| 187 | app, "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 188 | .privileges(redfish::privileges::postEventService) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 189 | .methods(boost::beast::http::verb::post)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 190 | [&app](const crow::Request& req, |
| 191 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 192 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 193 | { |
| 194 | return; |
| 195 | } |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 196 | |
Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 197 | // From the Redfish spec on EventId |
| 198 | // A service can ignore this value and replace it with its own. |
| 199 | // note that this parameter is intentionally ignored |
| 200 | |
| 201 | std::optional<std::string> eventId; |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 202 | TestEvent testEvent; |
| 203 | // clang-format off |
| 204 | if (!json_util::readJsonAction( |
| 205 | req, asyncResp->res, |
| 206 | "EventGroupId", testEvent.eventGroupId, |
Ed Tanous | 4a19a7b | 2025-01-27 10:44:15 -0800 | [diff] [blame] | 207 | "EventId", eventId, |
Chandramohan Harkude | 81ee0e7 | 2024-12-20 19:22:12 +0530 | [diff] [blame] | 208 | "EventTimestamp", testEvent.eventTimestamp, |
| 209 | "Message", testEvent.message, |
| 210 | "MessageArgs", testEvent.messageArgs, |
| 211 | "MessageId", testEvent.messageId, |
| 212 | "OriginOfCondition", testEvent.originOfCondition, |
| 213 | "Resolution", testEvent.resolution, |
| 214 | "Severity", testEvent.severity)) |
| 215 | { |
| 216 | return; |
| 217 | } |
| 218 | // clang-format on |
| 219 | |
| 220 | if (!EventServiceManager::getInstance().sendTestEventLog( |
| 221 | testEvent)) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 222 | { |
| 223 | messages::serviceDisabled(asyncResp->res, |
| 224 | "/redfish/v1/EventService/"); |
| 225 | return; |
| 226 | } |
| 227 | asyncResp->res.result(boost::beast::http::status::no_content); |
| 228 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 231 | inline void doSubscriptionCollection( |
Ed Tanous | e81de51 | 2023-06-27 17:07:00 -0700 | [diff] [blame] | 232 | const boost::system::error_code& ec, |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 233 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 234 | const dbus::utility::ManagedObjectType& resp) |
| 235 | { |
| 236 | if (ec) |
| 237 | { |
Ed Tanous | 1306101 | 2023-07-25 11:12:19 -0700 | [diff] [blame] | 238 | if (ec.value() == EBADR || ec.value() == EHOSTUNREACH) |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 239 | { |
| 240 | // This is an optional process so just return if it isn't there |
| 241 | return; |
| 242 | } |
| 243 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 244 | BMCWEB_LOG_ERROR("D-Bus response error on GetManagedObjects {}", ec); |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 245 | messages::internalError(asyncResp->res); |
| 246 | return; |
| 247 | } |
| 248 | nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"]; |
| 249 | for (const auto& objpath : resp) |
| 250 | { |
| 251 | sdbusplus::message::object_path path(objpath.first); |
| 252 | const std::string snmpId = path.filename(); |
| 253 | if (snmpId.empty()) |
| 254 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 255 | BMCWEB_LOG_ERROR("The SNMP client ID is wrong"); |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 256 | messages::internalError(asyncResp->res); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | getSnmpSubscriptionList(asyncResp, snmpId, memberArray); |
| 261 | } |
| 262 | } |
| 263 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 264 | inline void requestRoutesEventDestinationCollection(App& app) |
| 265 | { |
Gayathri Leburu | 1ebe3e4 | 2022-02-09 10:45:19 +0000 | [diff] [blame] | 266 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 267 | .privileges(redfish::privileges::getEventDestinationCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 268 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 269 | [&app](const crow::Request& req, |
| 270 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 271 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 272 | { |
| 273 | return; |
| 274 | } |
| 275 | asyncResp->res.jsonValue["@odata.type"] = |
| 276 | "#EventDestinationCollection.EventDestinationCollection"; |
| 277 | asyncResp->res.jsonValue["@odata.id"] = |
| 278 | "/redfish/v1/EventService/Subscriptions"; |
| 279 | asyncResp->res.jsonValue["Name"] = |
| 280 | "Event Destination Collections"; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 281 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 282 | nlohmann::json& memberArray = |
| 283 | asyncResp->res.jsonValue["Members"]; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 284 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 285 | std::vector<std::string> subscripIds = |
| 286 | EventServiceManager::getInstance().getAllIDs(); |
| 287 | memberArray = nlohmann::json::array(); |
| 288 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 289 | subscripIds.size(); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 290 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 291 | for (const std::string& id : subscripIds) |
| 292 | { |
| 293 | nlohmann::json::object_t member; |
| 294 | member["@odata.id"] = boost::urls::format( |
| 295 | "/redfish/v1/EventService/Subscriptions/{}" + id); |
| 296 | memberArray.emplace_back(std::move(member)); |
| 297 | } |
Ed Tanous | 177612a | 2025-02-14 15:16:09 -0800 | [diff] [blame] | 298 | dbus::utility::async_method_call( |
| 299 | asyncResp, |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 300 | [asyncResp](const boost::system::error_code& ec, |
| 301 | const dbus::utility::ManagedObjectType& resp) { |
| 302 | doSubscriptionCollection(ec, asyncResp, resp); |
| 303 | }, |
| 304 | "xyz.openbmc_project.Network.SNMP", |
| 305 | "/xyz/openbmc_project/network/snmp/manager", |
| 306 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 307 | }); |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 308 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 309 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/") |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 310 | .privileges(redfish::privileges::postEventDestinationCollection) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 311 | .methods( |
| 312 | boost::beast::http::verb:: |
| 313 | post)([&app]( |
| 314 | const crow::Request& req, |
| 315 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 316 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 317 | { |
| 318 | return; |
| 319 | } |
| 320 | if (EventServiceManager::getInstance().getNumberOfSubscriptions() >= |
| 321 | maxNoOfSubscriptions) |
| 322 | { |
| 323 | messages::eventSubscriptionLimitExceeded(asyncResp->res); |
| 324 | return; |
| 325 | } |
| 326 | std::string destUrl; |
| 327 | std::string protocol; |
| 328 | std::optional<bool> verifyCertificate; |
| 329 | std::optional<std::string> context; |
| 330 | std::optional<std::string> subscriptionType; |
| 331 | std::optional<std::string> eventFormatType2; |
| 332 | std::optional<std::string> retryPolicy; |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 333 | std::optional<bool> sendHeartbeat; |
| 334 | std::optional<uint64_t> hbIntervalMinutes; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 335 | std::optional<std::vector<std::string>> msgIds; |
| 336 | std::optional<std::vector<std::string>> regPrefixes; |
Ed Tanous | a14c911 | 2024-09-04 10:46:47 -0700 | [diff] [blame] | 337 | std::optional<std::vector<std::string>> originResources; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 338 | std::optional<std::vector<std::string>> resTypes; |
| 339 | std::optional<std::vector<nlohmann::json::object_t>> headers; |
| 340 | std::optional<std::vector<nlohmann::json::object_t>> mrdJsonArray; |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 341 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 342 | if (!json_util::readJsonPatch( // |
| 343 | req, asyncResp->res, // |
| 344 | "Context", context, // |
| 345 | "DeliveryRetryPolicy", retryPolicy, // |
| 346 | "Destination", destUrl, // |
| 347 | "EventFormatType", eventFormatType2, // |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 348 | "HeartbeatIntervalMinutes", hbIntervalMinutes, // |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 349 | "HttpHeaders", headers, // |
| 350 | "MessageIds", msgIds, // |
| 351 | "MetricReportDefinitions", mrdJsonArray, // |
| 352 | "OriginResources", originResources, // |
| 353 | "Protocol", protocol, // |
| 354 | "RegistryPrefixes", regPrefixes, // |
| 355 | "ResourceTypes", resTypes, // |
| 356 | "SendHeartbeat", sendHeartbeat, // |
| 357 | "SubscriptionType", subscriptionType, // |
| 358 | "VerifyCertificate", verifyCertificate // |
Myung Bae | afc474a | 2024-10-09 00:53:29 -0700 | [diff] [blame] | 359 | )) |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 360 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 361 | return; |
| 362 | } |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 363 | // clang-format on |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 364 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 365 | // https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers |
| 366 | static constexpr const uint16_t maxDestinationSize = 2000; |
| 367 | if (destUrl.size() > maxDestinationSize) |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 368 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 369 | messages::stringValueTooLong(asyncResp->res, "Destination", |
| 370 | maxDestinationSize); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 371 | return; |
| 372 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 373 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 374 | if (regPrefixes && msgIds) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 375 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 376 | if (!regPrefixes->empty() && !msgIds->empty()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 377 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 378 | messages::propertyValueConflict( |
| 379 | asyncResp->res, "MessageIds", "RegistryPrefixes"); |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 380 | return; |
| 381 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 382 | } |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 383 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 384 | boost::system::result<boost::urls::url> url = |
| 385 | boost::urls::parse_absolute_uri(destUrl); |
| 386 | if (!url) |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 387 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 388 | BMCWEB_LOG_WARNING( |
| 389 | "Failed to validate and split destination url"); |
| 390 | messages::propertyValueFormatError(asyncResp->res, destUrl, |
| 391 | "Destination"); |
| 392 | return; |
| 393 | } |
| 394 | url->normalize(); |
George Liu | b07942e | 2024-11-01 09:59:40 +0800 | [diff] [blame] | 395 | |
| 396 | // port_number returns zero if it is not a valid representable port |
| 397 | if (url->has_port() && url->port_number() == 0) |
| 398 | { |
| 399 | BMCWEB_LOG_WARNING("{} is an invalid port in destination url", |
| 400 | url->port()); |
| 401 | messages::propertyValueFormatError(asyncResp->res, destUrl, |
| 402 | "Destination"); |
| 403 | return; |
| 404 | } |
| 405 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 406 | crow::utility::setProtocolDefaults(*url, protocol); |
| 407 | crow::utility::setPortDefaults(*url); |
| 408 | |
| 409 | if (url->path().empty()) |
| 410 | { |
| 411 | url->set_path("/"); |
| 412 | } |
| 413 | |
| 414 | if (url->has_userinfo()) |
| 415 | { |
| 416 | messages::propertyValueFormatError(asyncResp->res, destUrl, |
| 417 | "Destination"); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | if (protocol == "SNMPv2c") |
| 422 | { |
| 423 | if (context) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 424 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 425 | messages::propertyValueConflict(asyncResp->res, "Context", |
| 426 | "Protocol"); |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 427 | return; |
| 428 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 429 | if (eventFormatType2) |
| 430 | { |
| 431 | messages::propertyValueConflict( |
| 432 | asyncResp->res, "EventFormatType", "Protocol"); |
| 433 | return; |
| 434 | } |
| 435 | if (retryPolicy) |
| 436 | { |
| 437 | messages::propertyValueConflict(asyncResp->res, |
| 438 | "RetryPolicy", "Protocol"); |
| 439 | return; |
| 440 | } |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 441 | if (sendHeartbeat) |
| 442 | { |
| 443 | messages::propertyValueConflict( |
| 444 | asyncResp->res, "SendHeartbeat", "Protocol"); |
| 445 | return; |
| 446 | } |
| 447 | if (hbIntervalMinutes) |
| 448 | { |
| 449 | messages::propertyValueConflict( |
| 450 | asyncResp->res, "HeartbeatIntervalMinutes", "Protocol"); |
| 451 | return; |
| 452 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 453 | if (msgIds) |
| 454 | { |
| 455 | messages::propertyValueConflict(asyncResp->res, |
| 456 | "MessageIds", "Protocol"); |
| 457 | return; |
| 458 | } |
| 459 | if (regPrefixes) |
| 460 | { |
| 461 | messages::propertyValueConflict( |
| 462 | asyncResp->res, "RegistryPrefixes", "Protocol"); |
| 463 | return; |
| 464 | } |
| 465 | if (resTypes) |
| 466 | { |
| 467 | messages::propertyValueConflict( |
| 468 | asyncResp->res, "ResourceTypes", "Protocol"); |
| 469 | return; |
| 470 | } |
| 471 | if (headers) |
| 472 | { |
| 473 | messages::propertyValueConflict(asyncResp->res, |
| 474 | "HttpHeaders", "Protocol"); |
| 475 | return; |
| 476 | } |
| 477 | if (mrdJsonArray) |
| 478 | { |
| 479 | messages::propertyValueConflict( |
| 480 | asyncResp->res, "MetricReportDefinitions", "Protocol"); |
| 481 | return; |
| 482 | } |
| 483 | if (url->scheme() != "snmp") |
| 484 | { |
| 485 | messages::propertyValueConflict(asyncResp->res, |
| 486 | "Destination", "Protocol"); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | addSnmpTrapClient(asyncResp, url->host_address(), |
| 491 | url->port_number()); |
| 492 | return; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 493 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 494 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 495 | std::shared_ptr<Subscription> subValue = |
Myung Bae | 21a94d5 | 2024-10-14 15:02:57 -0700 | [diff] [blame] | 496 | std::make_shared<Subscription>( |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 497 | std::make_shared<persistent_data::UserSubscription>(), *url, |
Ed Tanous | 9838eb2 | 2025-01-29 16:24:42 -0800 | [diff] [blame] | 498 | getIoContext()); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 499 | |
| 500 | if (subscriptionType) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 501 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 502 | if (*subscriptionType != "RedfishEvent") |
| 503 | { |
| 504 | messages::propertyValueNotInList( |
| 505 | asyncResp->res, *subscriptionType, "SubscriptionType"); |
| 506 | return; |
| 507 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 508 | subValue->userSub->subscriptionType = *subscriptionType; |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 509 | } |
| 510 | else |
| 511 | { |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 512 | // Default |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 513 | subValue->userSub->subscriptionType = "RedfishEvent"; |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 514 | } |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 515 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 516 | if (protocol != "Redfish") |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 517 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 518 | messages::propertyValueNotInList(asyncResp->res, protocol, |
| 519 | "Protocol"); |
| 520 | return; |
| 521 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 522 | subValue->userSub->protocol = protocol; |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 523 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 524 | if (verifyCertificate) |
| 525 | { |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 526 | subValue->userSub->verifyCertificate = *verifyCertificate; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | if (eventFormatType2) |
| 530 | { |
| 531 | if (std::ranges::find(supportedEvtFormatTypes, |
| 532 | *eventFormatType2) == |
| 533 | supportedEvtFormatTypes.end()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 534 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 535 | messages::propertyValueNotInList( |
| 536 | asyncResp->res, *eventFormatType2, "EventFormatType"); |
| 537 | return; |
| 538 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 539 | subValue->userSub->eventFormatType = *eventFormatType2; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 540 | } |
| 541 | else |
| 542 | { |
| 543 | // If not specified, use default "Event" |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 544 | subValue->userSub->eventFormatType = "Event"; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 545 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 546 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 547 | if (context) |
| 548 | { |
| 549 | // This value is selected arbitrarily. |
| 550 | constexpr const size_t maxContextSize = 256; |
| 551 | if (context->size() > maxContextSize) |
| 552 | { |
| 553 | messages::stringValueTooLong(asyncResp->res, "Context", |
| 554 | maxContextSize); |
| 555 | return; |
| 556 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 557 | subValue->userSub->customText = *context; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | if (headers) |
| 561 | { |
| 562 | size_t cumulativeLen = 0; |
| 563 | |
| 564 | for (const nlohmann::json::object_t& headerChunk : *headers) |
| 565 | { |
| 566 | for (const auto& item : headerChunk) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 567 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 568 | const std::string* value = |
| 569 | item.second.get_ptr<const std::string*>(); |
| 570 | if (value == nullptr) |
| 571 | { |
| 572 | messages::propertyValueFormatError( |
| 573 | asyncResp->res, item.second, |
| 574 | "HttpHeaders/" + item.first); |
| 575 | return; |
| 576 | } |
| 577 | // Adding a new json value is the size of the key, + |
| 578 | // the size of the value + 2 * 2 quotes for each, + |
| 579 | // the colon and space between. example: |
| 580 | // "key": "value" |
| 581 | cumulativeLen += item.first.size() + value->size() + 6; |
| 582 | // This value is selected to mirror http_connection.hpp |
| 583 | constexpr const uint16_t maxHeaderSizeED = 8096; |
| 584 | if (cumulativeLen > maxHeaderSizeED) |
| 585 | { |
| 586 | messages::arraySizeTooLong( |
| 587 | asyncResp->res, "HttpHeaders", maxHeaderSizeED); |
| 588 | return; |
| 589 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 590 | subValue->userSub->httpHeaders.set(item.first, *value); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | if (regPrefixes) |
| 596 | { |
| 597 | for (const std::string& it : *regPrefixes) |
| 598 | { |
| 599 | if (std::ranges::find(supportedRegPrefixes, it) == |
| 600 | supportedRegPrefixes.end()) |
| 601 | { |
| 602 | messages::propertyValueNotInList(asyncResp->res, it, |
| 603 | "RegistryPrefixes"); |
| 604 | return; |
| 605 | } |
| 606 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 607 | subValue->userSub->registryPrefixes = *regPrefixes; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 608 | } |
| 609 | |
Ed Tanous | a14c911 | 2024-09-04 10:46:47 -0700 | [diff] [blame] | 610 | if (originResources) |
| 611 | { |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 612 | subValue->userSub->originResources = *originResources; |
Ed Tanous | a14c911 | 2024-09-04 10:46:47 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 615 | if (resTypes) |
| 616 | { |
| 617 | for (const std::string& it : *resTypes) |
| 618 | { |
| 619 | if (std::ranges::find(supportedResourceTypes, it) == |
| 620 | supportedResourceTypes.end()) |
| 621 | { |
| 622 | messages::propertyValueNotInList(asyncResp->res, it, |
| 623 | "ResourceTypes"); |
| 624 | return; |
| 625 | } |
| 626 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 627 | subValue->userSub->resourceTypes = *resTypes; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | if (msgIds) |
| 631 | { |
| 632 | std::vector<std::string> registryPrefix; |
| 633 | |
| 634 | // If no registry prefixes are mentioned, consider all |
| 635 | // supported prefixes |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 636 | if (subValue->userSub->registryPrefixes.empty()) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 637 | { |
| 638 | registryPrefix.assign(supportedRegPrefixes.begin(), |
| 639 | supportedRegPrefixes.end()); |
| 640 | } |
| 641 | else |
| 642 | { |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 643 | registryPrefix = subValue->userSub->registryPrefixes; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | for (const std::string& id : *msgIds) |
| 647 | { |
| 648 | bool validId = false; |
| 649 | |
| 650 | // Check for Message ID in each of the selected Registry |
| 651 | for (const std::string& it : registryPrefix) |
| 652 | { |
Patrick Williams | 4a102cd | 2025-02-27 14:52:54 -0500 | [diff] [blame^] | 653 | const registries::MessageEntries registry = |
| 654 | redfish::registries::getRegistryMessagesFromPrefix( |
| 655 | it); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 656 | |
| 657 | if (std::ranges::any_of( |
| 658 | registry, |
| 659 | [&id](const redfish::registries::MessageEntry& |
| 660 | messageEntry) { |
| 661 | return id == messageEntry.first; |
| 662 | })) |
| 663 | { |
| 664 | validId = true; |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (!validId) |
| 670 | { |
| 671 | messages::propertyValueNotInList(asyncResp->res, id, |
| 672 | "MessageIds"); |
| 673 | return; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 677 | subValue->userSub->registryMsgIds = *msgIds; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | if (retryPolicy) |
| 681 | { |
| 682 | if (std::ranges::find(supportedRetryPolicies, *retryPolicy) == |
| 683 | supportedRetryPolicies.end()) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 684 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 685 | messages::propertyValueNotInList( |
| 686 | asyncResp->res, *retryPolicy, "DeliveryRetryPolicy"); |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 687 | return; |
| 688 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 689 | subValue->userSub->retryPolicy = *retryPolicy; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 690 | } |
| 691 | else |
| 692 | { |
| 693 | // Default "TerminateAfterRetries" |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 694 | subValue->userSub->retryPolicy = "TerminateAfterRetries"; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 695 | } |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 696 | if (sendHeartbeat) |
| 697 | { |
| 698 | subValue->userSub->sendHeartbeat = *sendHeartbeat; |
| 699 | } |
| 700 | if (hbIntervalMinutes) |
| 701 | { |
| 702 | if (*hbIntervalMinutes < 1 || *hbIntervalMinutes > 65535) |
| 703 | { |
| 704 | messages::propertyValueOutOfRange( |
| 705 | asyncResp->res, *hbIntervalMinutes, |
| 706 | "HeartbeatIntervalMinutes"); |
| 707 | return; |
| 708 | } |
| 709 | subValue->userSub->hbIntervalMinutes = *hbIntervalMinutes; |
| 710 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 711 | |
| 712 | if (mrdJsonArray) |
| 713 | { |
| 714 | for (nlohmann::json::object_t& mrdObj : *mrdJsonArray) |
| 715 | { |
| 716 | std::string mrdUri; |
| 717 | |
| 718 | if (!json_util::readJsonObject(mrdObj, asyncResp->res, |
| 719 | "@odata.id", mrdUri)) |
| 720 | |
| 721 | { |
| 722 | return; |
| 723 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 724 | subValue->userSub->metricReportDefinitions.emplace_back( |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 725 | mrdUri); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 726 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 729 | std::string id = |
| 730 | EventServiceManager::getInstance().addPushSubscription( |
| 731 | subValue); |
| 732 | if (id.empty()) |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 733 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 734 | messages::internalError(asyncResp->res); |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 735 | return; |
| 736 | } |
| 737 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 738 | messages::created(asyncResp->res); |
| 739 | asyncResp->res.addHeader( |
| 740 | "Location", "/redfish/v1/EventService/Subscriptions/" + id); |
Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 741 | |
| 742 | // schedule a heartbeat |
| 743 | if (subValue->userSub->sendHeartbeat) |
| 744 | { |
| 745 | subValue->scheduleNextHeartbeatEvent(); |
| 746 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 747 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | inline void requestRoutesEventDestination(App& app) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 751 | { |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 752 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 753 | .privileges(redfish::privileges::getEventDestination) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 754 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 755 | [&app](const crow::Request& req, |
| 756 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 757 | const std::string& param) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 758 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 759 | { |
| 760 | return; |
| 761 | } |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 762 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 763 | if (param.starts_with("snmp")) |
| 764 | { |
| 765 | getSnmpTrapClient(asyncResp, param); |
| 766 | return; |
| 767 | } |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 768 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 769 | std::shared_ptr<Subscription> subValue = |
| 770 | EventServiceManager::getInstance().getSubscription(param); |
| 771 | if (subValue == nullptr) |
| 772 | { |
| 773 | asyncResp->res.result( |
| 774 | boost::beast::http::status::not_found); |
| 775 | return; |
| 776 | } |
| 777 | const std::string& id = param; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 778 | |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 779 | const persistent_data::UserSubscription& userSub = |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 780 | *subValue->userSub; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 781 | |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 782 | nlohmann::json& jVal = asyncResp->res.jsonValue; |
| 783 | jVal["@odata.type"] = |
| 784 | "#EventDestination.v1_14_1.EventDestination"; |
| 785 | jVal["Protocol"] = |
| 786 | event_destination::EventDestinationProtocol::Redfish; |
| 787 | jVal["@odata.id"] = boost::urls::format( |
| 788 | "/redfish/v1/EventService/Subscriptions/{}", id); |
| 789 | jVal["Id"] = id; |
| 790 | jVal["Name"] = "Event Destination " + id; |
| 791 | jVal["Destination"] = userSub.destinationUrl; |
| 792 | jVal["Context"] = userSub.customText; |
| 793 | jVal["SubscriptionType"] = userSub.subscriptionType; |
| 794 | jVal["HttpHeaders"] = nlohmann::json::array(); |
| 795 | jVal["EventFormatType"] = userSub.eventFormatType; |
| 796 | jVal["RegistryPrefixes"] = userSub.registryPrefixes; |
| 797 | jVal["ResourceTypes"] = userSub.resourceTypes; |
| 798 | |
| 799 | jVal["MessageIds"] = userSub.registryMsgIds; |
| 800 | jVal["DeliveryRetryPolicy"] = userSub.retryPolicy; |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 801 | jVal["SendHeartbeat"] = userSub.sendHeartbeat; |
| 802 | jVal["HeartbeatIntervalMinutes"] = userSub.hbIntervalMinutes; |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 803 | jVal["VerifyCertificate"] = userSub.verifyCertificate; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 804 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 805 | nlohmann::json::array_t mrdJsonArray; |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 806 | for (const auto& mdrUri : userSub.metricReportDefinitions) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 807 | { |
| 808 | nlohmann::json::object_t mdr; |
| 809 | mdr["@odata.id"] = mdrUri; |
| 810 | mrdJsonArray.emplace_back(std::move(mdr)); |
| 811 | } |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 812 | jVal["MetricReportDefinitions"] = mrdJsonArray; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 813 | }); |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 814 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 815 | // The below privilege is wrong, it should be ConfigureManager OR |
| 816 | // ConfigureSelf |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 817 | // https://github.com/openbmc/bmcweb/issues/220 |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 818 | //.privileges(redfish::privileges::patchEventDestination) |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 819 | .privileges({{"ConfigureManager"}}) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 820 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 821 | [&app](const crow::Request& req, |
| 822 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 823 | const std::string& param) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 824 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 825 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 826 | return; |
| 827 | } |
| 828 | std::shared_ptr<Subscription> subValue = |
| 829 | EventServiceManager::getInstance().getSubscription(param); |
| 830 | if (subValue == nullptr) |
| 831 | { |
| 832 | asyncResp->res.result( |
| 833 | boost::beast::http::status::not_found); |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | std::optional<std::string> context; |
| 838 | std::optional<std::string> retryPolicy; |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 839 | std::optional<bool> sendHeartbeat; |
| 840 | std::optional<uint64_t> hbIntervalMinutes; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 841 | std::optional<bool> verifyCertificate; |
| 842 | std::optional<std::vector<nlohmann::json::object_t>> headers; |
| 843 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 844 | if (!json_util::readJsonPatch( // |
| 845 | req, asyncResp->res, // |
| 846 | "Context", context, // |
| 847 | "DeliveryRetryPolicy", retryPolicy, // |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 848 | "HeartbeatIntervalMinutes", hbIntervalMinutes, // |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 849 | "HttpHeaders", headers, // |
| 850 | "SendHeartbeat", sendHeartbeat, // |
| 851 | "VerifyCertificate", verifyCertificate // |
Myung Bae | afc474a | 2024-10-09 00:53:29 -0700 | [diff] [blame] | 852 | )) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 853 | { |
| 854 | return; |
| 855 | } |
| 856 | |
| 857 | if (context) |
| 858 | { |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 859 | subValue->userSub->customText = *context; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | if (headers) |
| 863 | { |
| 864 | boost::beast::http::fields fields; |
| 865 | for (const nlohmann::json::object_t& headerChunk : *headers) |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 866 | { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 867 | for (const auto& it : headerChunk) |
| 868 | { |
| 869 | const std::string* value = |
| 870 | it.second.get_ptr<const std::string*>(); |
| 871 | if (value == nullptr) |
| 872 | { |
| 873 | messages::propertyValueFormatError( |
| 874 | asyncResp->res, it.second, |
| 875 | "HttpHeaders/" + it.first); |
| 876 | return; |
| 877 | } |
| 878 | fields.set(it.first, *value); |
| 879 | } |
| 880 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 881 | subValue->userSub->httpHeaders = std::move(fields); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | if (retryPolicy) |
| 885 | { |
| 886 | if (std::ranges::find(supportedRetryPolicies, |
| 887 | *retryPolicy) == |
| 888 | supportedRetryPolicies.end()) |
| 889 | { |
| 890 | messages::propertyValueNotInList(asyncResp->res, |
| 891 | *retryPolicy, |
| 892 | "DeliveryRetryPolicy"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 893 | return; |
| 894 | } |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 895 | subValue->userSub->retryPolicy = *retryPolicy; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 896 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 897 | |
Myung Bae | 5064a25 | 2024-10-04 09:34:25 -0700 | [diff] [blame] | 898 | if (sendHeartbeat) |
| 899 | { |
| 900 | subValue->userSub->sendHeartbeat = *sendHeartbeat; |
| 901 | } |
| 902 | if (hbIntervalMinutes) |
| 903 | { |
| 904 | if (*hbIntervalMinutes < 1 || *hbIntervalMinutes > 65535) |
| 905 | { |
| 906 | messages::propertyValueOutOfRange( |
| 907 | asyncResp->res, *hbIntervalMinutes, |
| 908 | "HeartbeatIntervalMinutes"); |
| 909 | return; |
| 910 | } |
| 911 | subValue->userSub->hbIntervalMinutes = *hbIntervalMinutes; |
| 912 | } |
| 913 | |
Myung Bae | fb54610 | 2024-10-29 10:21:26 -0500 | [diff] [blame] | 914 | if (hbIntervalMinutes || sendHeartbeat) |
| 915 | { |
| 916 | // if Heartbeat interval or send heart were changed, cancel |
| 917 | // the heartbeat timer if running and start a new heartbeat |
| 918 | // if needed |
| 919 | subValue->heartbeatParametersChanged(); |
| 920 | } |
| 921 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 922 | if (verifyCertificate) |
| 923 | { |
Myung Bae | 5fe4ef3 | 2024-10-19 09:56:02 -0400 | [diff] [blame] | 924 | subValue->userSub->verifyCertificate = *verifyCertificate; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 925 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 926 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 927 | EventServiceManager::getInstance().updateSubscriptionData(); |
| 928 | }); |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 929 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 930 | // The below privilege is wrong, it should be ConfigureManager OR |
| 931 | // ConfigureSelf |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 932 | // https://github.com/openbmc/bmcweb/issues/220 |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 933 | //.privileges(redfish::privileges::deleteEventDestination) |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 934 | .privileges({{"ConfigureManager"}}) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 935 | .methods(boost::beast::http::verb::delete_)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 936 | [&app](const crow::Request& req, |
| 937 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 938 | const std::string& param) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 939 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 940 | { |
| 941 | return; |
| 942 | } |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 943 | EventServiceManager& event = EventServiceManager::getInstance(); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 944 | if (param.starts_with("snmp")) |
| 945 | { |
| 946 | deleteSnmpTrapClient(asyncResp, param); |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 947 | event.deleteSubscription(param); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 948 | return; |
| 949 | } |
Chicago Duan | 3d30708 | 2020-11-26 14:12:12 +0800 | [diff] [blame] | 950 | |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 951 | if (!event.deleteSubscription(param)) |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 952 | { |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 953 | messages::resourceNotFound(asyncResp->res, |
| 954 | "EventDestination", param); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 955 | return; |
| 956 | } |
Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 957 | messages::success(asyncResp->res); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 958 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 959 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 960 | |
| 961 | } // namespace redfish |