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