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