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