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