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 | |
| 19 | namespace redfish |
| 20 | { |
| 21 | |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 22 | static constexpr const std::array<const char*, 2> supportedEvtFormatTypes = { |
| 23 | eventFormatType, metricReportFormatType}; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 24 | static constexpr const std::array<const char*, 3> supportedRegPrefixes = { |
| 25 | "Base", "OpenBMC", "Task"}; |
| 26 | static constexpr const std::array<const char*, 3> supportedRetryPolicies = { |
| 27 | "TerminateAfterRetries", "SuspendRetries", "RetryForever"}; |
| 28 | |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 29 | #ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE |
| 30 | static constexpr const std::array<const char*, 2> supportedResourceTypes = { |
| 31 | "IBMConfigFile", "Task"}; |
| 32 | #else |
| 33 | static constexpr const std::array<const char*, 1> supportedResourceTypes = { |
| 34 | "Task"}; |
| 35 | #endif |
| 36 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 37 | static constexpr const uint8_t maxNoOfSubscriptions = 20; |
| 38 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 39 | class EventService : public Node |
| 40 | { |
| 41 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 42 | EventService(App& app) : Node(app, "/redfish/v1/EventService/") |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 43 | { |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 44 | entityPrivileges = { |
| 45 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 46 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 47 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 48 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 49 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 50 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 51 | } |
| 52 | |
| 53 | private: |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 54 | void doGet(crow::Response& res, const crow::Request&, |
| 55 | const std::vector<std::string>&) override |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 56 | { |
| 57 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 58 | res.jsonValue = { |
| 59 | {"@odata.type", "#EventService.v1_5_0.EventService"}, |
| 60 | {"Id", "EventService"}, |
| 61 | {"Name", "Event Service"}, |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 62 | {"Subscriptions", |
| 63 | {{"@odata.id", "/redfish/v1/EventService/Subscriptions"}}}, |
AppaRao Puli | 0b4bdd9 | 2020-04-14 17:57:45 +0530 | [diff] [blame] | 64 | {"Actions", |
| 65 | {{"#EventService.SubmitTestEvent", |
| 66 | {{"target", "/redfish/v1/EventService/Actions/" |
| 67 | "EventService.SubmitTestEvent"}}}}}, |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 68 | {"@odata.id", "/redfish/v1/EventService"}}; |
| 69 | |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 70 | const auto& [enabled, retryAttempts, retryTimeoutInterval] = |
| 71 | EventServiceManager::getInstance().getEventServiceConfig(); |
| 72 | |
| 73 | asyncResp->res.jsonValue["Status"]["State"] = |
| 74 | (enabled ? "Enabled" : "Disabled"); |
| 75 | asyncResp->res.jsonValue["ServiceEnabled"] = enabled; |
| 76 | asyncResp->res.jsonValue["DeliveryRetryAttempts"] = retryAttempts; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 77 | asyncResp->res.jsonValue["DeliveryRetryIntervalSeconds"] = |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 78 | retryTimeoutInterval; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 79 | asyncResp->res.jsonValue["EventFormatTypes"] = supportedEvtFormatTypes; |
| 80 | asyncResp->res.jsonValue["RegistryPrefixes"] = supportedRegPrefixes; |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 81 | asyncResp->res.jsonValue["ResourceTypes"] = supportedResourceTypes; |
Ayushi Smriti | 07941a8 | 2020-05-21 15:55:34 +0530 | [diff] [blame] | 82 | |
| 83 | nlohmann::json supportedSSEFilters = { |
| 84 | {"EventFormatType", true}, {"MessageId", true}, |
| 85 | {"MetricReportDefinition", true}, {"RegistryPrefix", true}, |
| 86 | {"OriginResource", false}, {"ResourceType", false}}; |
| 87 | |
| 88 | asyncResp->res.jsonValue["SSEFilterPropertiesSupported"] = |
| 89 | supportedSSEFilters; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void doPatch(crow::Response& res, const crow::Request& req, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 93 | const std::vector<std::string>&) override |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 94 | { |
| 95 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 96 | |
| 97 | std::optional<bool> serviceEnabled; |
| 98 | std::optional<uint32_t> retryAttemps; |
| 99 | std::optional<uint32_t> retryInterval; |
| 100 | |
| 101 | if (!json_util::readJson(req, res, "ServiceEnabled", serviceEnabled, |
| 102 | "DeliveryRetryAttempts", retryAttemps, |
| 103 | "DeliveryRetryIntervalSeconds", retryInterval)) |
| 104 | { |
| 105 | return; |
| 106 | } |
| 107 | |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 108 | auto [enabled, retryCount, retryTimeoutInterval] = |
| 109 | EventServiceManager::getInstance().getEventServiceConfig(); |
| 110 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 111 | if (serviceEnabled) |
| 112 | { |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 113 | enabled = *serviceEnabled; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (retryAttemps) |
| 117 | { |
| 118 | // Supported range [1-3] |
| 119 | if ((*retryAttemps < 1) || (*retryAttemps > 3)) |
| 120 | { |
| 121 | messages::queryParameterOutOfRange( |
| 122 | asyncResp->res, std::to_string(*retryAttemps), |
| 123 | "DeliveryRetryAttempts", "[1-3]"); |
| 124 | } |
| 125 | else |
| 126 | { |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 127 | retryCount = *retryAttemps; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | if (retryInterval) |
| 132 | { |
| 133 | // Supported range [30 - 180] |
| 134 | if ((*retryInterval < 30) || (*retryInterval > 180)) |
| 135 | { |
| 136 | messages::queryParameterOutOfRange( |
| 137 | asyncResp->res, std::to_string(*retryInterval), |
| 138 | "DeliveryRetryIntervalSeconds", "[30-180]"); |
| 139 | } |
| 140 | else |
| 141 | { |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 142 | retryTimeoutInterval = *retryInterval; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
AppaRao Puli | 7d1cc38 | 2020-05-16 02:42:22 +0530 | [diff] [blame] | 146 | EventServiceManager::getInstance().setEventServiceConfig( |
| 147 | std::make_tuple(enabled, retryCount, retryTimeoutInterval)); |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 148 | } |
| 149 | }; |
| 150 | |
AppaRao Puli | 0b4bdd9 | 2020-04-14 17:57:45 +0530 | [diff] [blame] | 151 | class SubmitTestEvent : public Node |
| 152 | { |
| 153 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 154 | SubmitTestEvent(App& app) : |
AppaRao Puli | 0b4bdd9 | 2020-04-14 17:57:45 +0530 | [diff] [blame] | 155 | Node(app, |
| 156 | "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent/") |
| 157 | { |
| 158 | entityPrivileges = { |
| 159 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 160 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 161 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 162 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 163 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 164 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 165 | } |
| 166 | |
| 167 | private: |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 168 | void doPost(crow::Response& res, const crow::Request&, |
| 169 | const std::vector<std::string>&) override |
AppaRao Puli | 0b4bdd9 | 2020-04-14 17:57:45 +0530 | [diff] [blame] | 170 | { |
| 171 | EventServiceManager::getInstance().sendTestEventLog(); |
| 172 | res.result(boost::beast::http::status::no_content); |
| 173 | res.end(); |
| 174 | } |
| 175 | }; |
| 176 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 177 | class EventDestinationCollection : public Node |
| 178 | { |
| 179 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 180 | EventDestinationCollection(App& app) : |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 181 | Node(app, "/redfish/v1/EventService/Subscriptions/") |
| 182 | { |
| 183 | entityPrivileges = { |
| 184 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 185 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 186 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 187 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 188 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 189 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 190 | } |
| 191 | |
| 192 | private: |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 193 | void doGet(crow::Response& res, const crow::Request&, |
| 194 | const std::vector<std::string>&) override |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 195 | { |
| 196 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 197 | |
| 198 | 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 = asyncResp->res.jsonValue["Members"]; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 205 | |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 206 | std::vector<std::string> subscripIds = |
| 207 | EventServiceManager::getInstance().getAllIDs(); |
| 208 | memberArray = nlohmann::json::array(); |
| 209 | asyncResp->res.jsonValue["Members@odata.count"] = subscripIds.size(); |
| 210 | |
| 211 | for (const std::string& id : subscripIds) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 212 | { |
| 213 | memberArray.push_back( |
| 214 | {{"@odata.id", |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 215 | "/redfish/v1/EventService/Subscriptions/" + id}}); |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | void doPost(crow::Response& res, const crow::Request& req, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 220 | const std::vector<std::string>&) override |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 221 | { |
| 222 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 223 | |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 224 | if (EventServiceManager::getInstance().getNumberOfSubscriptions() >= |
| 225 | maxNoOfSubscriptions) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 226 | { |
| 227 | messages::eventSubscriptionLimitExceeded(asyncResp->res); |
| 228 | return; |
| 229 | } |
| 230 | std::string destUrl; |
| 231 | std::string protocol; |
| 232 | std::optional<std::string> context; |
| 233 | std::optional<std::string> subscriptionType; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 234 | std::optional<std::string> eventFormatType2; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 235 | std::optional<std::string> retryPolicy; |
| 236 | std::optional<std::vector<std::string>> msgIds; |
| 237 | std::optional<std::vector<std::string>> regPrefixes; |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 238 | std::optional<std::vector<std::string>> resTypes; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 239 | std::optional<std::vector<nlohmann::json>> headers; |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 240 | std::optional<std::vector<nlohmann::json>> mrdJsonArray; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 241 | |
| 242 | if (!json_util::readJson( |
| 243 | req, res, "Destination", destUrl, "Context", context, |
| 244 | "Protocol", protocol, "SubscriptionType", subscriptionType, |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 245 | "EventFormatType", eventFormatType2, "HttpHeaders", headers, |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 246 | "RegistryPrefixes", regPrefixes, "MessageIds", msgIds, |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 247 | "DeliveryRetryPolicy", retryPolicy, "MetricReportDefinitions", |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 248 | mrdJsonArray, "ResourceTypes", resTypes)) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 249 | { |
| 250 | return; |
| 251 | } |
| 252 | |
AppaRao Puli | dd28ba8 | 2020-09-08 01:53:21 +0530 | [diff] [blame] | 253 | if (regPrefixes && msgIds) |
| 254 | { |
| 255 | if (regPrefixes->size() && msgIds->size()) |
| 256 | { |
| 257 | messages::mutualExclusiveProperties( |
| 258 | asyncResp->res, "RegistryPrefixes", "MessageIds"); |
| 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 263 | // Validate the URL using regex expression |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 264 | // Format: <protocol>://<host>:<port>/<uri> |
| 265 | // protocol: http/https |
| 266 | // host: Exclude ' ', ':', '#', '?' |
Gunnar Mills | 4e0453b | 2020-07-08 14:00:30 -0500 | [diff] [blame] | 267 | // port: Empty or numeric value with ':' separator. |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 268 | // uri: Start with '/' and Exclude '#', ' ' |
| 269 | // Can include query params(ex: '/event?test=1') |
| 270 | // TODO: Need to validate hostname extensively(as per rfc) |
| 271 | const std::regex urlRegex( |
| 272 | "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/" |
| 273 | "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)"); |
| 274 | std::cmatch match; |
| 275 | if (!std::regex_match(destUrl.c_str(), match, urlRegex)) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 276 | { |
| 277 | messages::propertyValueFormatError(asyncResp->res, destUrl, |
| 278 | "Destination"); |
| 279 | return; |
| 280 | } |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 281 | |
| 282 | std::string uriProto = std::string(match[1].first, match[1].second); |
| 283 | if (uriProto == "http") |
| 284 | { |
| 285 | #ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING |
| 286 | messages::propertyValueFormatError(asyncResp->res, destUrl, |
| 287 | "Destination"); |
| 288 | return; |
| 289 | #endif |
| 290 | } |
| 291 | |
| 292 | std::string host = std::string(match[2].first, match[2].second); |
| 293 | std::string port = std::string(match[3].first, match[3].second); |
| 294 | std::string path = std::string(match[4].first, match[4].second); |
| 295 | if (port.empty()) |
| 296 | { |
| 297 | if (uriProto == "http") |
| 298 | { |
| 299 | port = "80"; |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | port = "443"; |
| 304 | } |
| 305 | } |
| 306 | if (path.empty()) |
| 307 | { |
| 308 | path = "/"; |
| 309 | } |
| 310 | |
| 311 | std::shared_ptr<Subscription> subValue = |
| 312 | std::make_shared<Subscription>(host, port, path, uriProto); |
| 313 | |
| 314 | subValue->destinationUrl = destUrl; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 315 | |
| 316 | if (subscriptionType) |
| 317 | { |
| 318 | if (*subscriptionType != "RedfishEvent") |
| 319 | { |
| 320 | messages::propertyValueNotInList( |
| 321 | asyncResp->res, *subscriptionType, "SubscriptionType"); |
| 322 | return; |
| 323 | } |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 324 | subValue->subscriptionType = *subscriptionType; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | { |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 328 | subValue->subscriptionType = "RedfishEvent"; // Default |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | if (protocol != "Redfish") |
| 332 | { |
| 333 | messages::propertyValueNotInList(asyncResp->res, protocol, |
| 334 | "Protocol"); |
| 335 | return; |
| 336 | } |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 337 | subValue->protocol = protocol; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 338 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 339 | if (eventFormatType2) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 340 | { |
| 341 | if (std::find(supportedEvtFormatTypes.begin(), |
| 342 | supportedEvtFormatTypes.end(), |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 343 | *eventFormatType2) == supportedEvtFormatTypes.end()) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 344 | { |
| 345 | messages::propertyValueNotInList( |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 346 | asyncResp->res, *eventFormatType2, "EventFormatType"); |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 347 | return; |
| 348 | } |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 349 | subValue->eventFormatType = *eventFormatType2; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 350 | } |
| 351 | else |
| 352 | { |
| 353 | // If not specified, use default "Event" |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 354 | subValue->eventFormatType = "Event"; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | if (context) |
| 358 | { |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 359 | subValue->customText = *context; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (headers) |
| 363 | { |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 364 | subValue->httpHeaders = *headers; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | if (regPrefixes) |
| 368 | { |
| 369 | for (const std::string& it : *regPrefixes) |
| 370 | { |
| 371 | if (std::find(supportedRegPrefixes.begin(), |
| 372 | supportedRegPrefixes.end(), |
| 373 | it) == supportedRegPrefixes.end()) |
| 374 | { |
| 375 | messages::propertyValueNotInList(asyncResp->res, it, |
| 376 | "RegistryPrefixes"); |
| 377 | return; |
| 378 | } |
| 379 | } |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 380 | subValue->registryPrefixes = *regPrefixes; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 381 | } |
| 382 | |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 383 | if (resTypes) |
| 384 | { |
| 385 | for (const std::string& it : *resTypes) |
| 386 | { |
| 387 | if (std::find(supportedResourceTypes.begin(), |
| 388 | supportedResourceTypes.end(), |
| 389 | it) == supportedResourceTypes.end()) |
| 390 | { |
| 391 | messages::propertyValueNotInList(asyncResp->res, it, |
| 392 | "ResourceTypes"); |
| 393 | return; |
| 394 | } |
| 395 | } |
| 396 | subValue->resourceTypes = *resTypes; |
| 397 | } |
| 398 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 399 | if (msgIds) |
| 400 | { |
| 401 | // Do we need to loop-up MessageRegistry and validate |
| 402 | // data for authenticity??? Not mandate, i believe. |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 403 | subValue->registryMsgIds = *msgIds; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | if (retryPolicy) |
| 407 | { |
| 408 | if (std::find(supportedRetryPolicies.begin(), |
| 409 | supportedRetryPolicies.end(), |
| 410 | *retryPolicy) == supportedRetryPolicies.end()) |
| 411 | { |
| 412 | messages::propertyValueNotInList(asyncResp->res, *retryPolicy, |
| 413 | "DeliveryRetryPolicy"); |
| 414 | return; |
| 415 | } |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 416 | subValue->retryPolicy = *retryPolicy; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 417 | } |
| 418 | else |
| 419 | { |
| 420 | // Default "TerminateAfterRetries" |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 421 | subValue->retryPolicy = "TerminateAfterRetries"; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 422 | } |
| 423 | |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 424 | if (mrdJsonArray) |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 425 | { |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 426 | for (nlohmann::json& mrdObj : *mrdJsonArray) |
| 427 | { |
| 428 | std::string mrdUri; |
| 429 | if (json_util::getValueFromJsonObject(mrdObj, "@odata.id", |
| 430 | mrdUri)) |
| 431 | { |
| 432 | subValue->metricReportDefinitions.emplace_back(mrdUri); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | messages::propertyValueFormatError( |
| 437 | asyncResp->res, mrdObj.dump(), |
| 438 | "MetricReportDefinitions"); |
| 439 | return; |
| 440 | } |
| 441 | } |
AppaRao Puli | 156d6b0 | 2020-04-25 06:04:05 +0530 | [diff] [blame] | 442 | } |
| 443 | |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 444 | std::string id = |
| 445 | EventServiceManager::getInstance().addSubscription(subValue); |
| 446 | if (id.empty()) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 447 | { |
| 448 | messages::internalError(asyncResp->res); |
| 449 | return; |
| 450 | } |
| 451 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 452 | messages::created(asyncResp->res); |
| 453 | asyncResp->res.addHeader( |
| 454 | "Location", "/redfish/v1/EventService/Subscriptions/" + id); |
| 455 | } |
| 456 | }; |
| 457 | |
| 458 | class EventDestination : public Node |
| 459 | { |
| 460 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 461 | EventDestination(App& app) : |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 462 | Node(app, "/redfish/v1/EventService/Subscriptions/<str>/", |
| 463 | std::string()) |
| 464 | { |
| 465 | entityPrivileges = { |
| 466 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 467 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 468 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 469 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 470 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 471 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 472 | } |
| 473 | |
| 474 | private: |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 475 | void doGet(crow::Response& res, const crow::Request&, |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 476 | const std::vector<std::string>& params) override |
| 477 | { |
| 478 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 479 | if (params.size() != 1) |
| 480 | { |
| 481 | messages::internalError(asyncResp->res); |
| 482 | return; |
| 483 | } |
| 484 | |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 485 | std::shared_ptr<Subscription> subValue = |
| 486 | EventServiceManager::getInstance().getSubscription(params[0]); |
| 487 | if (subValue == nullptr) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 488 | { |
| 489 | res.result(boost::beast::http::status::not_found); |
| 490 | res.end(); |
| 491 | return; |
| 492 | } |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 493 | const std::string& id = params[0]; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 494 | |
| 495 | res.jsonValue = { |
| 496 | {"@odata.type", "#EventDestination.v1_7_0.EventDestination"}, |
| 497 | {"Protocol", "Redfish"}}; |
| 498 | asyncResp->res.jsonValue["@odata.id"] = |
| 499 | "/redfish/v1/EventService/Subscriptions/" + id; |
| 500 | asyncResp->res.jsonValue["Id"] = id; |
| 501 | asyncResp->res.jsonValue["Name"] = "Event Destination " + id; |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 502 | asyncResp->res.jsonValue["Destination"] = subValue->destinationUrl; |
| 503 | asyncResp->res.jsonValue["Context"] = subValue->customText; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 504 | asyncResp->res.jsonValue["SubscriptionType"] = |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 505 | subValue->subscriptionType; |
| 506 | asyncResp->res.jsonValue["HttpHeaders"] = subValue->httpHeaders; |
| 507 | asyncResp->res.jsonValue["EventFormatType"] = subValue->eventFormatType; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 508 | asyncResp->res.jsonValue["RegistryPrefixes"] = |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 509 | subValue->registryPrefixes; |
Sunitha Harish | e56f254 | 2020-07-22 02:38:59 -0500 | [diff] [blame] | 510 | asyncResp->res.jsonValue["ResourceTypes"] = subValue->resourceTypes; |
| 511 | |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 512 | asyncResp->res.jsonValue["MessageIds"] = subValue->registryMsgIds; |
| 513 | asyncResp->res.jsonValue["DeliveryRetryPolicy"] = subValue->retryPolicy; |
AppaRao Puli | 144b631 | 2020-08-03 22:23:12 +0530 | [diff] [blame] | 514 | |
| 515 | std::vector<nlohmann::json> mrdJsonArray; |
| 516 | for (const auto& mdrUri : subValue->metricReportDefinitions) |
| 517 | { |
| 518 | mrdJsonArray.push_back({{"@odata.id", mdrUri}}); |
| 519 | } |
| 520 | asyncResp->res.jsonValue["MetricReportDefinitions"] = mrdJsonArray; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | void doPatch(crow::Response& res, const crow::Request& req, |
| 524 | const std::vector<std::string>& params) override |
| 525 | { |
| 526 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 527 | if (params.size() != 1) |
| 528 | { |
| 529 | messages::internalError(asyncResp->res); |
| 530 | return; |
| 531 | } |
| 532 | |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 533 | std::shared_ptr<Subscription> subValue = |
| 534 | EventServiceManager::getInstance().getSubscription(params[0]); |
| 535 | if (subValue == nullptr) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 536 | { |
| 537 | res.result(boost::beast::http::status::not_found); |
| 538 | res.end(); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | std::optional<std::string> context; |
| 543 | std::optional<std::string> retryPolicy; |
| 544 | std::optional<std::vector<nlohmann::json>> headers; |
| 545 | |
| 546 | if (!json_util::readJson(req, res, "Context", context, |
| 547 | "DeliveryRetryPolicy", retryPolicy, |
| 548 | "HttpHeaders", headers)) |
| 549 | { |
| 550 | return; |
| 551 | } |
| 552 | |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 553 | if (context) |
| 554 | { |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 555 | subValue->customText = *context; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (headers) |
| 559 | { |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 560 | subValue->httpHeaders = *headers; |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | if (retryPolicy) |
| 564 | { |
| 565 | if (std::find(supportedRetryPolicies.begin(), |
| 566 | supportedRetryPolicies.end(), |
| 567 | *retryPolicy) == supportedRetryPolicies.end()) |
| 568 | { |
| 569 | messages::propertyValueNotInList(asyncResp->res, *retryPolicy, |
| 570 | "DeliveryRetryPolicy"); |
| 571 | return; |
| 572 | } |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 573 | subValue->retryPolicy = *retryPolicy; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 574 | subValue->updateRetryPolicy(); |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 575 | } |
| 576 | |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 577 | EventServiceManager::getInstance().updateSubscriptionData(); |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 578 | } |
| 579 | |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 580 | void doDelete(crow::Response& res, const crow::Request&, |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 581 | const std::vector<std::string>& params) override |
| 582 | { |
| 583 | auto asyncResp = std::make_shared<AsyncResp>(res); |
| 584 | |
| 585 | if (params.size() != 1) |
| 586 | { |
| 587 | messages::internalError(asyncResp->res); |
| 588 | return; |
| 589 | } |
| 590 | |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 591 | if (!EventServiceManager::getInstance().isSubscriptionExist(params[0])) |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 592 | { |
| 593 | res.result(boost::beast::http::status::not_found); |
| 594 | res.end(); |
| 595 | return; |
| 596 | } |
AppaRao Puli | b52664e | 2020-04-09 21:36:51 +0530 | [diff] [blame] | 597 | EventServiceManager::getInstance().deleteSubscription(params[0]); |
AppaRao Puli | e5aaf04 | 2020-03-20 01:05:52 +0530 | [diff] [blame] | 598 | } |
| 599 | }; |
| 600 | |
| 601 | } // namespace redfish |