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