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