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