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