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 | } |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 60 | asyncResp->res.jsonValue = { |
| 61 | {"@odata.type", "#EventService.v1_5_0.EventService"}, |
| 62 | {"Id", "EventService"}, |
| 63 | {"Name", "Event Service"}, |
| 64 | {"Subscriptions", |
| 65 | {{"@odata.id", "/redfish/v1/EventService/Subscriptions"}}}, |
| 66 | {"Actions", |
| 67 | {{"#EventService.SubmitTestEvent", |
| 68 | {{"target", |
| 69 | "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent"}}}}}, |
| 70 | {"@odata.id", "/redfish/v1/EventService"}}; |
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 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 198 | asyncResp->res.jsonValue = { |
| 199 | {"@odata.type", |
| 200 | "#EventDestinationCollection.EventDestinationCollection"}, |
| 201 | {"@odata.id", "/redfish/v1/EventService/Subscriptions"}, |
| 202 | {"Name", "Event Destination Collections"}}; |
| 203 | |
| 204 | nlohmann::json& memberArray = |
| 205 | asyncResp->res.jsonValue["Members"]; |
| 206 | |
| 207 | std::vector<std::string> subscripIds = |
| 208 | EventServiceManager::getInstance().getAllIDs(); |
| 209 | memberArray = nlohmann::json::array(); |
| 210 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 211 | subscripIds.size(); |
| 212 | |
| 213 | for (const std::string& id : subscripIds) |
| 214 | { |
| 215 | memberArray.push_back( |
| 216 | {{"@odata.id", |
| 217 | "/redfish/v1/EventService/Subscriptions/" + id}}); |
| 218 | } |
| 219 | }); |
| 220 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/") |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 221 | .privileges(redfish::privileges::postEventDestinationCollection) |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 222 | .methods( |
| 223 | boost::beast::http::verb:: |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 224 | post)([&app]( |
| 225 | const crow::Request& req, |
| 226 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 227 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 228 | { |
| 229 | return; |
| 230 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 231 | if (EventServiceManager::getInstance().getNumberOfSubscriptions() >= |
| 232 | maxNoOfSubscriptions) |
| 233 | { |
| 234 | messages::eventSubscriptionLimitExceeded(asyncResp->res); |
| 235 | return; |
| 236 | } |
| 237 | std::string destUrl; |
| 238 | std::string protocol; |
| 239 | std::optional<std::string> context; |
| 240 | std::optional<std::string> subscriptionType; |
| 241 | std::optional<std::string> eventFormatType2; |
| 242 | std::optional<std::string> retryPolicy; |
| 243 | std::optional<std::vector<std::string>> msgIds; |
| 244 | std::optional<std::vector<std::string>> regPrefixes; |
| 245 | std::optional<std::vector<std::string>> resTypes; |
| 246 | std::optional<std::vector<nlohmann::json>> headers; |
| 247 | std::optional<std::vector<nlohmann::json>> mrdJsonArray; |
| 248 | |
| 249 | if (!json_util::readJsonPatch( |
| 250 | req, asyncResp->res, "Destination", destUrl, "Context", |
| 251 | context, "Protocol", protocol, "SubscriptionType", |
| 252 | subscriptionType, "EventFormatType", eventFormatType2, |
| 253 | "HttpHeaders", headers, "RegistryPrefixes", regPrefixes, |
| 254 | "MessageIds", msgIds, "DeliveryRetryPolicy", retryPolicy, |
| 255 | "MetricReportDefinitions", mrdJsonArray, "ResourceTypes", |
| 256 | resTypes)) |
| 257 | { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | if (regPrefixes && msgIds) |
| 262 | { |
| 263 | if (!regPrefixes->empty() && !msgIds->empty()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 264 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 265 | messages::propertyValueConflict( |
| 266 | asyncResp->res, "MessageIds", "RegistryPrefixes"); |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 267 | return; |
| 268 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 269 | } |
P Dheeraj Srujan Kumar | b304bd7 | 2021-01-29 17:46:35 +0530 | [diff] [blame] | 270 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 271 | std::string host; |
| 272 | std::string urlProto; |
| 273 | uint16_t port = 0; |
| 274 | std::string path; |
| 275 | |
| 276 | if (!crow::utility::validateAndSplitUrl(destUrl, urlProto, host, |
| 277 | port, path)) |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 278 | { |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 279 | BMCWEB_LOG_WARNING |
| 280 | << "Failed to validate and split destination url"; |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 281 | messages::propertyValueFormatError(asyncResp->res, destUrl, |
| 282 | "Destination"); |
| 283 | return; |
| 284 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 285 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 286 | if (path.empty()) |
| 287 | { |
| 288 | path = "/"; |
| 289 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 290 | std::shared_ptr<Subscription> subValue = |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 291 | std::make_shared<Subscription>(host, port, path, urlProto); |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 292 | |
| 293 | subValue->destinationUrl = destUrl; |
| 294 | |
| 295 | if (subscriptionType) |
| 296 | { |
| 297 | if (*subscriptionType != "RedfishEvent") |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 298 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 299 | messages::propertyValueNotInList( |
| 300 | asyncResp->res, *subscriptionType, "SubscriptionType"); |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 301 | return; |
| 302 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 303 | subValue->subscriptionType = *subscriptionType; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | subValue->subscriptionType = "RedfishEvent"; // Default |
| 308 | } |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 309 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 310 | if (protocol != "Redfish") |
| 311 | { |
| 312 | messages::propertyValueNotInList(asyncResp->res, protocol, |
| 313 | "Protocol"); |
| 314 | return; |
| 315 | } |
| 316 | subValue->protocol = protocol; |
| 317 | |
| 318 | if (eventFormatType2) |
| 319 | { |
| 320 | if (std::find(supportedEvtFormatTypes.begin(), |
| 321 | supportedEvtFormatTypes.end(), |
| 322 | *eventFormatType2) == |
| 323 | supportedEvtFormatTypes.end()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 324 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 325 | messages::propertyValueNotInList( |
| 326 | asyncResp->res, *eventFormatType2, "EventFormatType"); |
| 327 | return; |
| 328 | } |
| 329 | subValue->eventFormatType = *eventFormatType2; |
| 330 | } |
| 331 | else |
| 332 | { |
| 333 | // If not specified, use default "Event" |
| 334 | subValue->eventFormatType = "Event"; |
| 335 | } |
| 336 | |
| 337 | if (context) |
| 338 | { |
| 339 | subValue->customText = *context; |
| 340 | } |
| 341 | |
| 342 | if (headers) |
| 343 | { |
| 344 | for (const nlohmann::json& headerChunk : *headers) |
| 345 | { |
| 346 | for (const auto& item : headerChunk.items()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 347 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 348 | const std::string* value = |
| 349 | item.value().get_ptr<const std::string*>(); |
| 350 | if (value == nullptr) |
| 351 | { |
| 352 | messages::propertyValueFormatError( |
| 353 | asyncResp->res, item.value().dump(2, 1), |
| 354 | "HttpHeaders/" + item.key()); |
| 355 | return; |
| 356 | } |
| 357 | subValue->httpHeaders.set(item.key(), *value); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if (regPrefixes) |
| 363 | { |
| 364 | for (const std::string& it : *regPrefixes) |
| 365 | { |
| 366 | if (std::find(supportedRegPrefixes.begin(), |
| 367 | supportedRegPrefixes.end(), |
| 368 | it) == supportedRegPrefixes.end()) |
| 369 | { |
| 370 | messages::propertyValueNotInList(asyncResp->res, it, |
| 371 | "RegistryPrefixes"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 372 | return; |
| 373 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 374 | } |
| 375 | subValue->registryPrefixes = *regPrefixes; |
| 376 | } |
| 377 | |
| 378 | if (resTypes) |
| 379 | { |
| 380 | for (const std::string& it : *resTypes) |
| 381 | { |
| 382 | if (std::find(supportedResourceTypes.begin(), |
| 383 | supportedResourceTypes.end(), |
| 384 | it) == supportedResourceTypes.end()) |
| 385 | { |
| 386 | messages::propertyValueNotInList(asyncResp->res, it, |
| 387 | "ResourceTypes"); |
| 388 | return; |
| 389 | } |
| 390 | } |
| 391 | subValue->resourceTypes = *resTypes; |
| 392 | } |
| 393 | |
| 394 | if (msgIds) |
| 395 | { |
| 396 | std::vector<std::string> registryPrefix; |
| 397 | |
| 398 | // If no registry prefixes are mentioned, consider all |
| 399 | // supported prefixes |
| 400 | if (subValue->registryPrefixes.empty()) |
| 401 | { |
| 402 | registryPrefix.assign(supportedRegPrefixes.begin(), |
| 403 | supportedRegPrefixes.end()); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 404 | } |
| 405 | else |
| 406 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 407 | registryPrefix = subValue->registryPrefixes; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 408 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 409 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 410 | for (const std::string& id : *msgIds) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 411 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 412 | bool validId = false; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 413 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 414 | // Check for Message ID in each of the selected Registry |
| 415 | for (const std::string& it : registryPrefix) |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 416 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 417 | const std::span<const redfish::registries::MessageEntry> |
| 418 | registry = |
| 419 | redfish::registries::getRegistryFromPrefix(it); |
| 420 | |
| 421 | if (std::any_of( |
| 422 | registry.begin(), registry.end(), |
| 423 | [&id](const redfish::registries::MessageEntry& |
| 424 | messageEntry) { |
Ed Tanous | 55f79e6 | 2022-01-25 11:26:16 -0800 | [diff] [blame] | 425 | return id == messageEntry.first; |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 426 | })) |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 427 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 428 | validId = true; |
| 429 | break; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 433 | if (!validId) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 434 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 435 | messages::propertyValueNotInList(asyncResp->res, id, |
| 436 | "MessageIds"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 437 | return; |
| 438 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 441 | subValue->registryMsgIds = *msgIds; |
| 442 | } |
| 443 | |
| 444 | if (retryPolicy) |
| 445 | { |
| 446 | if (std::find(supportedRetryPolicies.begin(), |
| 447 | supportedRetryPolicies.end(), |
| 448 | *retryPolicy) == supportedRetryPolicies.end()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 449 | { |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 450 | messages::propertyValueNotInList( |
| 451 | asyncResp->res, *retryPolicy, "DeliveryRetryPolicy"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 452 | return; |
| 453 | } |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 454 | subValue->retryPolicy = *retryPolicy; |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | // Default "TerminateAfterRetries" |
| 459 | subValue->retryPolicy = "TerminateAfterRetries"; |
| 460 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 461 | |
Ed Tanous | fffb8c1 | 2022-02-07 23:53:03 -0800 | [diff] [blame] | 462 | if (mrdJsonArray) |
| 463 | { |
| 464 | for (nlohmann::json& mrdObj : *mrdJsonArray) |
| 465 | { |
| 466 | std::string mrdUri; |
| 467 | |
| 468 | if (!json_util::readJson(mrdObj, asyncResp->res, |
| 469 | "@odata.id", mrdUri)) |
| 470 | |
| 471 | { |
| 472 | return; |
| 473 | } |
| 474 | subValue->metricReportDefinitions.emplace_back(mrdUri); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | std::string id = |
| 479 | EventServiceManager::getInstance().addSubscription(subValue); |
| 480 | if (id.empty()) |
| 481 | { |
| 482 | messages::internalError(asyncResp->res); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | messages::created(asyncResp->res); |
| 487 | asyncResp->res.addHeader( |
| 488 | "Location", "/redfish/v1/EventService/Subscriptions/" + id); |
| 489 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | inline void requestRoutesEventDestination(App& app) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 493 | { |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 494 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 495 | .privileges(redfish::privileges::getEventDestination) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 496 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 497 | [&app](const crow::Request& req, |
| 498 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 499 | const std::string& param) { |
| 500 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 501 | { |
| 502 | return; |
| 503 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 504 | std::shared_ptr<Subscription> subValue = |
| 505 | EventServiceManager::getInstance().getSubscription(param); |
| 506 | if (subValue == nullptr) |
| 507 | { |
| 508 | asyncResp->res.result( |
| 509 | boost::beast::http::status::not_found); |
| 510 | return; |
| 511 | } |
| 512 | const std::string& id = param; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 513 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 514 | asyncResp->res.jsonValue = { |
| 515 | {"@odata.type", |
| 516 | "#EventDestination.v1_7_0.EventDestination"}, |
| 517 | {"Protocol", "Redfish"}}; |
| 518 | asyncResp->res.jsonValue["@odata.id"] = |
| 519 | "/redfish/v1/EventService/Subscriptions/" + id; |
| 520 | asyncResp->res.jsonValue["Id"] = id; |
| 521 | asyncResp->res.jsonValue["Name"] = "Event Destination " + id; |
| 522 | asyncResp->res.jsonValue["Destination"] = |
| 523 | subValue->destinationUrl; |
| 524 | asyncResp->res.jsonValue["Context"] = subValue->customText; |
| 525 | asyncResp->res.jsonValue["SubscriptionType"] = |
| 526 | subValue->subscriptionType; |
Ed Tanous | ad22fef | 2021-09-13 13:07:32 -0700 | [diff] [blame] | 527 | asyncResp->res.jsonValue["HttpHeaders"] = |
| 528 | nlohmann::json::array(); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 529 | asyncResp->res.jsonValue["EventFormatType"] = |
| 530 | subValue->eventFormatType; |
| 531 | asyncResp->res.jsonValue["RegistryPrefixes"] = |
| 532 | subValue->registryPrefixes; |
| 533 | asyncResp->res.jsonValue["ResourceTypes"] = |
| 534 | subValue->resourceTypes; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 535 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 536 | asyncResp->res.jsonValue["MessageIds"] = |
| 537 | subValue->registryMsgIds; |
| 538 | asyncResp->res.jsonValue["DeliveryRetryPolicy"] = |
| 539 | subValue->retryPolicy; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 540 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 541 | std::vector<nlohmann::json> mrdJsonArray; |
| 542 | for (const auto& mdrUri : subValue->metricReportDefinitions) |
| 543 | { |
| 544 | mrdJsonArray.push_back({{"@odata.id", mdrUri}}); |
| 545 | } |
| 546 | asyncResp->res.jsonValue["MetricReportDefinitions"] = |
| 547 | mrdJsonArray; |
| 548 | }); |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 549 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 550 | // The below privilege is wrong, it should be ConfigureManager OR |
| 551 | // ConfigureSelf |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 552 | // https://github.com/openbmc/bmcweb/issues/220 |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 553 | //.privileges(redfish::privileges::patchEventDestination) |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 554 | .privileges({{"ConfigureManager"}}) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 555 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 556 | [&app](const crow::Request& req, |
| 557 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 558 | const std::string& param) { |
| 559 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 560 | { |
| 561 | return; |
| 562 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 563 | std::shared_ptr<Subscription> subValue = |
| 564 | EventServiceManager::getInstance().getSubscription(param); |
| 565 | if (subValue == nullptr) |
| 566 | { |
| 567 | asyncResp->res.result( |
| 568 | boost::beast::http::status::not_found); |
| 569 | return; |
| 570 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 571 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 572 | std::optional<std::string> context; |
| 573 | std::optional<std::string> retryPolicy; |
| 574 | std::optional<std::vector<nlohmann::json>> headers; |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 575 | |
Willy Tu | 15ed678 | 2021-12-14 11:03:16 -0800 | [diff] [blame] | 576 | if (!json_util::readJsonPatch(req, asyncResp->res, "Context", |
| 577 | context, "DeliveryRetryPolicy", |
| 578 | retryPolicy, "HttpHeaders", |
| 579 | headers)) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 580 | { |
| 581 | return; |
| 582 | } |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 583 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 584 | if (context) |
| 585 | { |
| 586 | subValue->customText = *context; |
| 587 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 588 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 589 | if (headers) |
| 590 | { |
Ed Tanous | 601c71a | 2021-09-08 16:40:12 -0700 | [diff] [blame] | 591 | boost::beast::http::fields fields; |
| 592 | for (const nlohmann::json& headerChunk : *headers) |
| 593 | { |
| 594 | for (auto& it : headerChunk.items()) |
| 595 | { |
| 596 | const std::string* value = |
| 597 | it.value().get_ptr<const std::string*>(); |
| 598 | if (value == nullptr) |
| 599 | { |
| 600 | messages::propertyValueFormatError( |
| 601 | asyncResp->res, |
| 602 | it.value().dump(2, ' ', true), |
| 603 | "HttpHeaders/" + it.key()); |
| 604 | return; |
| 605 | } |
| 606 | fields.set(it.key(), *value); |
| 607 | } |
| 608 | } |
| 609 | subValue->httpHeaders = fields; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 610 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 611 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 612 | if (retryPolicy) |
| 613 | { |
| 614 | if (std::find(supportedRetryPolicies.begin(), |
| 615 | supportedRetryPolicies.end(), |
| 616 | *retryPolicy) == supportedRetryPolicies.end()) |
| 617 | { |
| 618 | messages::propertyValueNotInList(asyncResp->res, |
| 619 | *retryPolicy, |
| 620 | "DeliveryRetryPolicy"); |
| 621 | return; |
| 622 | } |
| 623 | subValue->retryPolicy = *retryPolicy; |
| 624 | subValue->updateRetryPolicy(); |
| 625 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 626 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 627 | EventServiceManager::getInstance().updateSubscriptionData(); |
| 628 | }); |
Ravi Teja | 9d41aec | 2021-07-23 01:57:01 -0500 | [diff] [blame] | 629 | BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 630 | // The below privilege is wrong, it should be ConfigureManager OR |
| 631 | // ConfigureSelf |
Abhishek Patel | 7eeafa7 | 2021-07-28 10:59:16 -0500 | [diff] [blame] | 632 | // https://github.com/openbmc/bmcweb/issues/220 |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 633 | //.privileges(redfish::privileges::deleteEventDestination) |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 634 | .privileges({{"ConfigureManager"}}) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 635 | .methods(boost::beast::http::verb::delete_)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 636 | [&app](const crow::Request& req, |
| 637 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 638 | const std::string& param) { |
| 639 | if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) |
| 640 | { |
| 641 | return; |
| 642 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 643 | if (!EventServiceManager::getInstance().isSubscriptionExist( |
| 644 | param)) |
| 645 | { |
| 646 | asyncResp->res.result( |
| 647 | boost::beast::http::status::not_found); |
| 648 | return; |
| 649 | } |
| 650 | EventServiceManager::getInstance().deleteSubscription(param); |
| 651 | }); |
| 652 | } |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 653 | |
| 654 | } // namespace redfish |