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 |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 17 | #include "event_service_manager.hpp" |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 18 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 19 | #include <app.hpp> |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 20 | #include <boost/beast/http/fields.hpp> |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 21 | #include <http/utility.hpp> |
| 22 | #include <logging.hpp> |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 23 | #include <query.hpp> |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 24 | #include <registries/privilege_registry.hpp> |
| 25 | |
Patrick Williams | 1e270c5 | 2021-12-04 06:06:56 -0600 | [diff] [blame] | 26 | #include <span> |
| 27 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 28 | namespace redfish |
| 29 | { |
| 30 | |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 31 | static constexpr const std::array<const char*, 2> supportedEvtFormatTypes = { |
| 32 | eventFormatType, metricReportFormatType}; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 33 | static constexpr const std::array<const char*, 3> supportedRegPrefixes = { |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 34 | "Base", "OpenBMC", "TaskEvent"}; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 35 | static constexpr const std::array<const char*, 3> supportedRetryPolicies = { |
| 36 | "TerminateAfterRetries", "SuspendRetries", "RetryForever"}; |
| 37 | |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 38 | #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE |
| 39 | static constexpr const std::array<const char*, 2> supportedResourceTypes = { |
| 40 | "IBMConfigFile", "Task"}; |
| 41 | #else |
| 42 | static constexpr const std::array<const char*, 1> supportedResourceTypes = { |
| 43 | "Task"}; |
| 44 | #endif |
| 45 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 46 | static constexpr const uint8_t maxNoOfSubscriptions = 20; |
| 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 | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 52 | .methods(boost::beast::http::verb::get)([&app](const crow::Request& req, |
| 53 | const std::shared_ptr< |
| 54 | bmcweb::AsyncResp>& |
| 55 | asyncResp) { |
| 56 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 57 | { |
| 58 | return; |
| 59 | } |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame^] | 60 | |
| 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"; |
| 66 | asyncResp->res.jsonValue["Subscriptions"]["@odata.id"] = |
| 67 | "/redfish/v1/EventService/Subscriptions"; |
| 68 | asyncResp->res.jsonValue["Actions"]["#EventService.SubmitTestEvent"] |
| 69 | ["target"] = |
| 70 | "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent"; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 71 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 72 | const persistent_data::EventServiceConfig eventServiceConfig = |
| 73 | persistent_data::EventServiceStore::getInstance() |
| 74 | .getEventServiceConfig(); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 75 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 76 | asyncResp->res.jsonValue["Status"]["State"] = |
| 77 | (eventServiceConfig.enabled ? "Enabled" : "Disabled"); |
| 78 | asyncResp->res.jsonValue["ServiceEnabled"] = |
| 79 | eventServiceConfig.enabled; |
| 80 | asyncResp->res.jsonValue["DeliveryRetryAttempts"] = |
| 81 | eventServiceConfig.retryAttempts; |
| 82 | asyncResp->res.jsonValue["DeliveryRetryIntervalSeconds"] = |
| 83 | eventServiceConfig.retryTimeoutInterval; |
| 84 | asyncResp->res.jsonValue["EventFormatTypes"] = |
| 85 | 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 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 89 | nlohmann::json supportedSSEFilters = { |
| 90 | {"EventFormatType", true}, {"MessageId", true}, |
| 91 | {"MetricReportDefinition", true}, {"RegistryPrefix", true}, |
| 92 | {"OriginResource", false}, {"ResourceType", false}}; |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 93 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 94 | asyncResp->res.jsonValue["SSEFilterPropertiesSupported"] = |
| 95 | supportedSSEFilters; |
| 96 | }); |
Ayushi Smriti | 07941a8 | 2020-05-21 15:55:34 +0530 | [diff] [blame] | 97 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 98 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 99 | .privileges(redfish::privileges::patchEventService) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 100 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 101 | [&app](const crow::Request& req, |
| 102 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 103 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 104 | { |
| 105 | return; |
| 106 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 107 | std::optional<bool> serviceEnabled; |
| 108 | std::optional<uint32_t> retryAttemps; |
| 109 | std::optional<uint32_t> retryInterval; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 110 | |
Willy Tu | 15ed678 | 2021-12-14 11:03:16 -0800 | [diff] [blame] | 111 | if (!json_util::readJsonPatch( |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 112 | req, asyncResp->res, "ServiceEnabled", serviceEnabled, |
| 113 | "DeliveryRetryAttempts", retryAttemps, |
| 114 | "DeliveryRetryIntervalSeconds", retryInterval)) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 115 | { |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 116 | return; |
| 117 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 118 | |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 119 | persistent_data::EventServiceConfig eventServiceConfig = |
| 120 | persistent_data::EventServiceStore::getInstance() |
| 121 | .getEventServiceConfig(); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 122 | |
| 123 | if (serviceEnabled) |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 124 | { |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 125 | eventServiceConfig.enabled = *serviceEnabled; |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 126 | } |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 127 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 128 | if (retryAttemps) |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 129 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 130 | // Supported range [1-3] |
| 131 | if ((*retryAttemps < 1) || (*retryAttemps > 3)) |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 132 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 133 | messages::queryParameterOutOfRange( |
| 134 | asyncResp->res, std::to_string(*retryAttemps), |
| 135 | "DeliveryRetryAttempts", "[1-3]"); |
| 136 | } |
| 137 | else |
| 138 | { |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 139 | eventServiceConfig.retryAttempts = *retryAttemps; |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 143 | if (retryInterval) |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 144 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 145 | // Supported range [30 - 180] |
| 146 | if ((*retryInterval < 30) || (*retryInterval > 180)) |
| 147 | { |
| 148 | messages::queryParameterOutOfRange( |
| 149 | asyncResp->res, std::to_string(*retryInterval), |
| 150 | "DeliveryRetryIntervalSeconds", "[30-180]"); |
| 151 | } |
| 152 | else |
| 153 | { |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 154 | eventServiceConfig.retryTimeoutInterval = |
| 155 | *retryInterval; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | EventServiceManager::getInstance().setEventServiceConfig( |
JunLin Chen | 28afb49 | 2021-02-24 17:13:29 +0800 | [diff] [blame] | 160 | eventServiceConfig); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 161 | }); |
| 162 | } |
| 163 | |
| 164 | inline void requestRoutesSubmitTestEvent(App& app) |
| 165 | { |
| 166 | |
| 167 | BMCWEB_ROUTE( |
| 168 | app, "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 169 | .privileges(redfish::privileges::postEventService) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 170 | .methods(boost::beast::http::verb::post)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 171 | [&app](const crow::Request& req, |
| 172 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 173 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 174 | { |
| 175 | return; |
| 176 | } |
sunharis_in | 6ba8c82 | 2021-07-06 06:06:58 -0500 | [diff] [blame] | 177 | if (!EventServiceManager::getInstance().sendTestEventLog()) |
| 178 | { |
| 179 | messages::serviceDisabled(asyncResp->res, |
| 180 | "/redfish/v1/EventService/"); |
| 181 | return; |
| 182 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 183 | asyncResp->res.result(boost::beast::http::status::no_content); |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | inline void requestRoutesEventDestinationCollection(App& app) |
| 188 | { |
Gayathri Leburu | 1ebe3e4 | 2022-02-09 10:45:19 +0000 | [diff] [blame] | 189 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 190 | .privileges(redfish::privileges::getEventDestinationCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 191 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 192 | [&app](const crow::Request& req, |
| 193 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 194 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 195 | { |
| 196 | return; |
| 197 | } |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame^] | 198 | asyncResp->res.jsonValue["@odata.type"] = |
| 199 | "#EventDestinationCollection.EventDestinationCollection"; |
| 200 | asyncResp->res.jsonValue["@odata.id"] = |
| 201 | "/redfish/v1/EventService/Subscriptions"; |
| 202 | asyncResp->res.jsonValue["Name"] = |
| 203 | "Event Destination Collections"; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 204 | |
| 205 | nlohmann::json& memberArray = |
| 206 | asyncResp->res.jsonValue["Members"]; |
| 207 | |
| 208 | std::vector<std::string> subscripIds = |
| 209 | EventServiceManager::getInstance().getAllIDs(); |
| 210 | memberArray = nlohmann::json::array(); |
| 211 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 212 | subscripIds.size(); |
| 213 | |
| 214 | for (const std::string& id : subscripIds) |
| 215 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame^] | 216 | nlohmann::json::object_t member; |
| 217 | member["@odata.id"] = |
| 218 | "/redfish/v1/EventService/Subscriptions/" + id; |
| 219 | memberArray.push_back(std::move(member)); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 220 | } |
| 221 | }); |
| 222 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/") |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 223 | .privileges(redfish::privileges::postEventDestinationCollection) |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 224 | .methods( |
| 225 | boost::beast::http::verb:: |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 226 | post)([&app]( |
| 227 | const crow::Request& req, |
| 228 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 229 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 230 | { |
| 231 | return; |
| 232 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 233 | if (EventServiceManager::getInstance().getNumberOfSubscriptions() >= |
| 234 | maxNoOfSubscriptions) |
| 235 | { |
| 236 | messages::eventSubscriptionLimitExceeded(asyncResp->res); |
| 237 | return; |
| 238 | } |
| 239 | std::string destUrl; |
| 240 | std::string protocol; |
| 241 | std::optional<std::string> context; |
| 242 | std::optional<std::string> subscriptionType; |
| 243 | std::optional<std::string> eventFormatType2; |
| 244 | std::optional<std::string> retryPolicy; |
| 245 | std::optional<std::vector<std::string>> msgIds; |
| 246 | std::optional<std::vector<std::string>> regPrefixes; |
| 247 | std::optional<std::vector<std::string>> resTypes; |
| 248 | std::optional<std::vector<nlohmann::json>> headers; |
| 249 | std::optional<std::vector<nlohmann::json>> mrdJsonArray; |
| 250 | |
| 251 | if (!json_util::readJsonPatch( |
| 252 | req, asyncResp->res, "Destination", destUrl, "Context", |
| 253 | context, "Protocol", protocol, "SubscriptionType", |
| 254 | subscriptionType, "EventFormatType", eventFormatType2, |
| 255 | "HttpHeaders", headers, "RegistryPrefixes", regPrefixes, |
| 256 | "MessageIds", msgIds, "DeliveryRetryPolicy", retryPolicy, |
| 257 | "MetricReportDefinitions", mrdJsonArray, "ResourceTypes", |
| 258 | resTypes)) |
| 259 | { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | if (regPrefixes && msgIds) |
| 264 | { |
| 265 | if (!regPrefixes->empty() && !msgIds->empty()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 266 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 267 | messages::propertyValueConflict( |
| 268 | asyncResp->res, "MessageIds", "RegistryPrefixes"); |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 269 | return; |
| 270 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 271 | } |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 272 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 273 | std::string host; |
| 274 | std::string urlProto; |
| 275 | uint16_t port = 0; |
| 276 | std::string path; |
| 277 | |
| 278 | if (!crow::utility::validateAndSplitUrl(destUrl, urlProto, host, |
| 279 | port, path)) |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 280 | { |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 281 | BMCWEB_LOG_WARNING |
| 282 | << "Failed to validate and split destination url"; |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 283 | messages::propertyValueFormatError(asyncResp->res, destUrl, |
| 284 | "Destination"); |
| 285 | return; |
| 286 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 287 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 288 | if (path.empty()) |
| 289 | { |
| 290 | path = "/"; |
| 291 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 292 | std::shared_ptr<Subscription> subValue = |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 293 | std::make_shared<Subscription>(host, port, path, urlProto); |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 294 | |
| 295 | subValue->destinationUrl = destUrl; |
| 296 | |
| 297 | if (subscriptionType) |
| 298 | { |
| 299 | if (*subscriptionType != "RedfishEvent") |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 300 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 301 | messages::propertyValueNotInList( |
| 302 | asyncResp->res, *subscriptionType, "SubscriptionType"); |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 303 | return; |
| 304 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 305 | subValue->subscriptionType = *subscriptionType; |
| 306 | } |
| 307 | else |
| 308 | { |
| 309 | subValue->subscriptionType = "RedfishEvent"; // Default |
| 310 | } |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 311 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 312 | if (protocol != "Redfish") |
| 313 | { |
| 314 | messages::propertyValueNotInList(asyncResp->res, protocol, |
| 315 | "Protocol"); |
| 316 | return; |
| 317 | } |
| 318 | subValue->protocol = protocol; |
| 319 | |
| 320 | if (eventFormatType2) |
| 321 | { |
| 322 | if (std::find(supportedEvtFormatTypes.begin(), |
| 323 | supportedEvtFormatTypes.end(), |
| 324 | *eventFormatType2) == |
| 325 | supportedEvtFormatTypes.end()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 326 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 327 | messages::propertyValueNotInList( |
| 328 | asyncResp->res, *eventFormatType2, "EventFormatType"); |
| 329 | return; |
| 330 | } |
| 331 | subValue->eventFormatType = *eventFormatType2; |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | // If not specified, use default "Event" |
| 336 | subValue->eventFormatType = "Event"; |
| 337 | } |
| 338 | |
| 339 | if (context) |
| 340 | { |
| 341 | subValue->customText = *context; |
| 342 | } |
| 343 | |
| 344 | if (headers) |
| 345 | { |
| 346 | for (const nlohmann::json& headerChunk : *headers) |
| 347 | { |
| 348 | for (const auto& item : headerChunk.items()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 349 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 350 | const std::string* value = |
| 351 | item.value().get_ptr<const std::string*>(); |
| 352 | if (value == nullptr) |
| 353 | { |
| 354 | messages::propertyValueFormatError( |
| 355 | asyncResp->res, item.value().dump(2, 1), |
| 356 | "HttpHeaders/" + item.key()); |
| 357 | return; |
| 358 | } |
| 359 | subValue->httpHeaders.set(item.key(), *value); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (regPrefixes) |
| 365 | { |
| 366 | for (const std::string& it : *regPrefixes) |
| 367 | { |
| 368 | if (std::find(supportedRegPrefixes.begin(), |
| 369 | supportedRegPrefixes.end(), |
| 370 | it) == supportedRegPrefixes.end()) |
| 371 | { |
| 372 | messages::propertyValueNotInList(asyncResp->res, it, |
| 373 | "RegistryPrefixes"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 374 | return; |
| 375 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 376 | } |
| 377 | subValue->registryPrefixes = *regPrefixes; |
| 378 | } |
| 379 | |
| 380 | if (resTypes) |
| 381 | { |
| 382 | for (const std::string& it : *resTypes) |
| 383 | { |
| 384 | if (std::find(supportedResourceTypes.begin(), |
| 385 | supportedResourceTypes.end(), |
| 386 | it) == supportedResourceTypes.end()) |
| 387 | { |
| 388 | messages::propertyValueNotInList(asyncResp->res, it, |
| 389 | "ResourceTypes"); |
| 390 | return; |
| 391 | } |
| 392 | } |
| 393 | subValue->resourceTypes = *resTypes; |
| 394 | } |
| 395 | |
| 396 | if (msgIds) |
| 397 | { |
| 398 | std::vector<std::string> registryPrefix; |
| 399 | |
| 400 | // If no registry prefixes are mentioned, consider all |
| 401 | // supported prefixes |
| 402 | if (subValue->registryPrefixes.empty()) |
| 403 | { |
| 404 | registryPrefix.assign(supportedRegPrefixes.begin(), |
| 405 | supportedRegPrefixes.end()); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 406 | } |
| 407 | else |
| 408 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 409 | registryPrefix = subValue->registryPrefixes; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 410 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 411 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 412 | for (const std::string& id : *msgIds) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 413 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 414 | bool validId = false; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 415 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 416 | // Check for Message ID in each of the selected Registry |
| 417 | for (const std::string& it : registryPrefix) |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 418 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 419 | const std::span<const redfish::registries::MessageEntry> |
| 420 | registry = |
| 421 | redfish::registries::getRegistryFromPrefix(it); |
| 422 | |
| 423 | if (std::any_of( |
| 424 | registry.begin(), registry.end(), |
| 425 | [&id](const redfish::registries::MessageEntry& |
| 426 | messageEntry) { |
Ed Tanous | 55f79e6 | 2022-01-25 11:26:16 -0800 | [diff] [blame] | 427 | return id == messageEntry.first; |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 428 | })) |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 429 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 430 | validId = true; |
| 431 | break; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 435 | if (!validId) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 436 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 437 | messages::propertyValueNotInList(asyncResp->res, id, |
| 438 | "MessageIds"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 439 | return; |
| 440 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 443 | subValue->registryMsgIds = *msgIds; |
| 444 | } |
| 445 | |
| 446 | if (retryPolicy) |
| 447 | { |
| 448 | if (std::find(supportedRetryPolicies.begin(), |
| 449 | supportedRetryPolicies.end(), |
| 450 | *retryPolicy) == supportedRetryPolicies.end()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 451 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 452 | messages::propertyValueNotInList( |
| 453 | asyncResp->res, *retryPolicy, "DeliveryRetryPolicy"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 454 | return; |
| 455 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 456 | subValue->retryPolicy = *retryPolicy; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | // Default "TerminateAfterRetries" |
| 461 | subValue->retryPolicy = "TerminateAfterRetries"; |
| 462 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 463 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 464 | if (mrdJsonArray) |
| 465 | { |
| 466 | for (nlohmann::json& mrdObj : *mrdJsonArray) |
| 467 | { |
| 468 | std::string mrdUri; |
| 469 | |
| 470 | if (!json_util::readJson(mrdObj, asyncResp->res, |
| 471 | "@odata.id", mrdUri)) |
| 472 | |
| 473 | { |
| 474 | return; |
| 475 | } |
| 476 | subValue->metricReportDefinitions.emplace_back(mrdUri); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | std::string id = |
| 481 | EventServiceManager::getInstance().addSubscription(subValue); |
| 482 | if (id.empty()) |
| 483 | { |
| 484 | messages::internalError(asyncResp->res); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | messages::created(asyncResp->res); |
| 489 | asyncResp->res.addHeader( |
| 490 | "Location", "/redfish/v1/EventService/Subscriptions/" + id); |
| 491 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | inline void requestRoutesEventDestination(App& app) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 495 | { |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 496 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 497 | .privileges(redfish::privileges::getEventDestination) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 498 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 499 | [&app](const crow::Request& req, |
| 500 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 501 | const std::string& param) { |
| 502 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 503 | { |
| 504 | return; |
| 505 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 506 | std::shared_ptr<Subscription> subValue = |
| 507 | EventServiceManager::getInstance().getSubscription(param); |
| 508 | if (subValue == nullptr) |
| 509 | { |
| 510 | asyncResp->res.result( |
| 511 | boost::beast::http::status::not_found); |
| 512 | return; |
| 513 | } |
| 514 | const std::string& id = param; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 515 | |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame^] | 516 | asyncResp->res.jsonValue["@odata.type"] = |
| 517 | "#EventDestination.v1_7_0.EventDestination"; |
| 518 | asyncResp->res.jsonValue["Protocol"] = "Redfish"; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 519 | asyncResp->res.jsonValue["@odata.id"] = |
| 520 | "/redfish/v1/EventService/Subscriptions/" + id; |
| 521 | asyncResp->res.jsonValue["Id"] = id; |
| 522 | asyncResp->res.jsonValue["Name"] = "Event Destination " + id; |
| 523 | asyncResp->res.jsonValue["Destination"] = |
| 524 | subValue->destinationUrl; |
| 525 | asyncResp->res.jsonValue["Context"] = subValue->customText; |
| 526 | asyncResp->res.jsonValue["SubscriptionType"] = |
| 527 | subValue->subscriptionType; |
Ed Tanous | ad22fef | 2021-09-13 13:07:32 -0700 | [diff] [blame] | 528 | asyncResp->res.jsonValue["HttpHeaders"] = |
| 529 | nlohmann::json::array(); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 530 | asyncResp->res.jsonValue["EventFormatType"] = |
| 531 | subValue->eventFormatType; |
| 532 | asyncResp->res.jsonValue["RegistryPrefixes"] = |
| 533 | subValue->registryPrefixes; |
| 534 | asyncResp->res.jsonValue["ResourceTypes"] = |
| 535 | subValue->resourceTypes; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 536 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 537 | asyncResp->res.jsonValue["MessageIds"] = |
| 538 | subValue->registryMsgIds; |
| 539 | asyncResp->res.jsonValue["DeliveryRetryPolicy"] = |
| 540 | subValue->retryPolicy; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 541 | |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame^] | 542 | nlohmann::json::array_t mrdJsonArray; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 543 | for (const auto& mdrUri : subValue->metricReportDefinitions) |
| 544 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame^] | 545 | nlohmann::json::object_t mdr; |
| 546 | mdr["@odata.id"] = mdrUri; |
| 547 | mrdJsonArray.emplace_back(std::move(mdr)); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 548 | } |
| 549 | asyncResp->res.jsonValue["MetricReportDefinitions"] = |
| 550 | mrdJsonArray; |
| 551 | }); |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 552 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 553 | // The below privilege is wrong, it should be ConfigureManager OR |
| 554 | // ConfigureSelf |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 555 | // https://github.com/openbmc/bmcweb/issues/220 |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 556 | //.privileges(redfish::privileges::patchEventDestination) |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 557 | .privileges({{"ConfigureManager"}}) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 558 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 559 | [&app](const crow::Request& req, |
| 560 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 561 | const std::string& param) { |
| 562 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 563 | { |
| 564 | return; |
| 565 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 566 | std::shared_ptr<Subscription> subValue = |
| 567 | EventServiceManager::getInstance().getSubscription(param); |
| 568 | if (subValue == nullptr) |
| 569 | { |
| 570 | asyncResp->res.result( |
| 571 | boost::beast::http::status::not_found); |
| 572 | return; |
| 573 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 574 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 575 | std::optional<std::string> context; |
| 576 | std::optional<std::string> retryPolicy; |
| 577 | std::optional<std::vector<nlohmann::json>> headers; |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 578 | |
Willy Tu | 15ed678 | 2021-12-14 11:03:16 -0800 | [diff] [blame] | 579 | if (!json_util::readJsonPatch(req, asyncResp->res, "Context", |
| 580 | context, "DeliveryRetryPolicy", |
| 581 | retryPolicy, "HttpHeaders", |
| 582 | headers)) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 583 | { |
| 584 | return; |
| 585 | } |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 586 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 587 | if (context) |
| 588 | { |
| 589 | subValue->customText = *context; |
| 590 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 591 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 592 | if (headers) |
| 593 | { |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 594 | boost::beast::http::fields fields; |
| 595 | for (const nlohmann::json& headerChunk : *headers) |
| 596 | { |
| 597 | for (auto& it : headerChunk.items()) |
| 598 | { |
| 599 | const std::string* value = |
| 600 | it.value().get_ptr<const std::string*>(); |
| 601 | if (value == nullptr) |
| 602 | { |
| 603 | messages::propertyValueFormatError( |
| 604 | asyncResp->res, |
| 605 | it.value().dump(2, ' ', true), |
| 606 | "HttpHeaders/" + it.key()); |
| 607 | return; |
| 608 | } |
| 609 | fields.set(it.key(), *value); |
| 610 | } |
| 611 | } |
| 612 | subValue->httpHeaders = fields; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 613 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 614 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 615 | if (retryPolicy) |
| 616 | { |
| 617 | if (std::find(supportedRetryPolicies.begin(), |
| 618 | supportedRetryPolicies.end(), |
| 619 | *retryPolicy) == supportedRetryPolicies.end()) |
| 620 | { |
| 621 | messages::propertyValueNotInList(asyncResp->res, |
| 622 | *retryPolicy, |
| 623 | "DeliveryRetryPolicy"); |
| 624 | return; |
| 625 | } |
| 626 | subValue->retryPolicy = *retryPolicy; |
| 627 | subValue->updateRetryPolicy(); |
| 628 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 629 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 630 | EventServiceManager::getInstance().updateSubscriptionData(); |
| 631 | }); |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 632 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 633 | // The below privilege is wrong, it should be ConfigureManager OR |
| 634 | // ConfigureSelf |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 635 | // https://github.com/openbmc/bmcweb/issues/220 |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 636 | //.privileges(redfish::privileges::deleteEventDestination) |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 637 | .privileges({{"ConfigureManager"}}) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 638 | .methods(boost::beast::http::verb::delete_)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 639 | [&app](const crow::Request& req, |
| 640 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 641 | const std::string& param) { |
| 642 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 643 | { |
| 644 | return; |
| 645 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 646 | if (!EventServiceManager::getInstance().isSubscriptionExist( |
| 647 | param)) |
| 648 | { |
| 649 | asyncResp->res.result( |
| 650 | boost::beast::http::status::not_found); |
| 651 | return; |
| 652 | } |
| 653 | EventServiceManager::getInstance().deleteSubscription(param); |
| 654 | }); |
| 655 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 656 | |
| 657 | } // namespace redfish |