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