blob: 2b63955009699e969e8ca77b07d252729dee6363 [file] [log] [blame]
AppaRao Pulie5aaf042020-03-20 01:05:52 +05301/*
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 Pulib52664e2020-04-09 21:36:51 +053017#include "event_service_manager.hpp"
AppaRao Pulie5aaf042020-03-20 01:05:52 +053018
John Edward Broadbent7e860f12021-04-08 15:57:16 -070019#include <app.hpp>
Ed Tanous601c71a2021-09-08 16:40:12 -070020#include <boost/beast/http/fields.hpp>
Ed Tanouseb1c47d2022-02-09 11:47:27 -080021#include <http/utility.hpp>
22#include <logging.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070023#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
25
Patrick Williams1e270c52021-12-04 06:06:56 -060026#include <span>
27
AppaRao Pulie5aaf042020-03-20 01:05:52 +053028namespace redfish
29{
30
AppaRao Puli156d6b02020-04-25 06:04:05 +053031static constexpr const std::array<const char*, 2> supportedEvtFormatTypes = {
32 eventFormatType, metricReportFormatType};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053033static constexpr const std::array<const char*, 3> supportedRegPrefixes = {
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +053034 "Base", "OpenBMC", "TaskEvent"};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053035static constexpr const std::array<const char*, 3> supportedRetryPolicies = {
36 "TerminateAfterRetries", "SuspendRetries", "RetryForever"};
37
Sunitha Harishe56f2542020-07-22 02:38:59 -050038#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
39static constexpr const std::array<const char*, 2> supportedResourceTypes = {
40 "IBMConfigFile", "Task"};
41#else
42static constexpr const std::array<const char*, 1> supportedResourceTypes = {
43 "Task"};
44#endif
45
AppaRao Pulie5aaf042020-03-20 01:05:52 +053046static constexpr const uint8_t maxNoOfSubscriptions = 20;
47
John Edward Broadbent7e860f12021-04-08 15:57:16 -070048inline void requestRoutesEventService(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +053049{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070050 BMCWEB_ROUTE(app, "/redfish/v1/EventService/")
Ed Tanoused398212021-06-09 17:05:54 -070051 .privileges(redfish::privileges::getEventService)
Ed Tanous45ca1b82022-03-25 13:07:27 -070052 .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 Liu0fda0f12021-11-16 10:06:17 +080060 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 Pulie5aaf042020-03-20 01:05:52 +053071
George Liu0fda0f12021-11-16 10:06:17 +080072 const persistent_data::EventServiceConfig eventServiceConfig =
73 persistent_data::EventServiceStore::getInstance()
74 .getEventServiceConfig();
zhanghch058d1b46d2021-04-01 11:18:24 +080075
George Liu0fda0f12021-11-16 10:06:17 +080076 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 Pulie5aaf042020-03-20 01:05:52 +053088
George Liu0fda0f12021-11-16 10:06:17 +080089 nlohmann::json supportedSSEFilters = {
90 {"EventFormatType", true}, {"MessageId", true},
91 {"MetricReportDefinition", true}, {"RegistryPrefix", true},
92 {"OriginResource", false}, {"ResourceType", false}};
AppaRao Puli7d1cc382020-05-16 02:42:22 +053093
George Liu0fda0f12021-11-16 10:06:17 +080094 asyncResp->res.jsonValue["SSEFilterPropertiesSupported"] =
95 supportedSSEFilters;
96 });
Ayushi Smriti07941a82020-05-21 15:55:34 +053097
John Edward Broadbent7e860f12021-04-08 15:57:16 -070098 BMCWEB_ROUTE(app, "/redfish/v1/EventService/")
Ed Tanoused398212021-06-09 17:05:54 -070099 .privileges(redfish::privileges::patchEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700100 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700101 [&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 Broadbent7e860f12021-04-08 15:57:16 -0700107 std::optional<bool> serviceEnabled;
108 std::optional<uint32_t> retryAttemps;
109 std::optional<uint32_t> retryInterval;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530110
Willy Tu15ed6782021-12-14 11:03:16 -0800111 if (!json_util::readJsonPatch(
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700112 req, asyncResp->res, "ServiceEnabled", serviceEnabled,
113 "DeliveryRetryAttempts", retryAttemps,
114 "DeliveryRetryIntervalSeconds", retryInterval))
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530115 {
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530116 return;
117 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530118
JunLin Chen28afb492021-02-24 17:13:29 +0800119 persistent_data::EventServiceConfig eventServiceConfig =
120 persistent_data::EventServiceStore::getInstance()
121 .getEventServiceConfig();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700122
123 if (serviceEnabled)
Sunitha Harishe56f2542020-07-22 02:38:59 -0500124 {
JunLin Chen28afb492021-02-24 17:13:29 +0800125 eventServiceConfig.enabled = *serviceEnabled;
Sunitha Harishe56f2542020-07-22 02:38:59 -0500126 }
Sunitha Harishe56f2542020-07-22 02:38:59 -0500127
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700128 if (retryAttemps)
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530129 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700130 // Supported range [1-3]
131 if ((*retryAttemps < 1) || (*retryAttemps > 3))
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530132 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700133 messages::queryParameterOutOfRange(
134 asyncResp->res, std::to_string(*retryAttemps),
135 "DeliveryRetryAttempts", "[1-3]");
136 }
137 else
138 {
JunLin Chen28afb492021-02-24 17:13:29 +0800139 eventServiceConfig.retryAttempts = *retryAttemps;
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530140 }
141 }
142
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700143 if (retryInterval)
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530144 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700145 // 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 Chen28afb492021-02-24 17:13:29 +0800154 eventServiceConfig.retryTimeoutInterval =
155 *retryInterval;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700156 }
157 }
158
159 EventServiceManager::getInstance().setEventServiceConfig(
JunLin Chen28afb492021-02-24 17:13:29 +0800160 eventServiceConfig);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700161 });
162}
163
164inline void requestRoutesSubmitTestEvent(App& app)
165{
166
167 BMCWEB_ROUTE(
168 app, "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent/")
Ed Tanoused398212021-06-09 17:05:54 -0700169 .privileges(redfish::privileges::postEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700171 [&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_in6ba8c822021-07-06 06:06:58 -0500177 if (!EventServiceManager::getInstance().sendTestEventLog())
178 {
179 messages::serviceDisabled(asyncResp->res,
180 "/redfish/v1/EventService/");
181 return;
182 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700183 asyncResp->res.result(boost::beast::http::status::no_content);
184 });
185}
186
187inline void requestRoutesEventDestinationCollection(App& app)
188{
Gayathri Leburu1ebe3e42022-02-09 10:45:19 +0000189 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/")
Ed Tanoused398212021-06-09 17:05:54 -0700190 .privileges(redfish::privileges::getEventDestinationCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700191 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700192 [&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 Broadbent7e860f12021-04-08 15:57:16 -0700198 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 Patel7eeafa72021-07-28 10:59:16 -0500221 .privileges(redfish::privileges::postEventDestinationCollection)
Ed Tanousfffb8c12022-02-07 23:53:03 -0800222 .methods(
223 boost::beast::http::verb::
Ed Tanous45ca1b82022-03-25 13:07:27 -0700224 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 Tanousfffb8c12022-02-07 23:53:03 -0800231 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 Broadbent7e860f12021-04-08 15:57:16 -0700264 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800265 messages::propertyValueConflict(
266 asyncResp->res, "MessageIds", "RegistryPrefixes");
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530267 return;
268 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800269 }
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530270
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800271 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 Tanousfffb8c12022-02-07 23:53:03 -0800278 {
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800279 BMCWEB_LOG_WARNING
280 << "Failed to validate and split destination url";
Ed Tanousfffb8c12022-02-07 23:53:03 -0800281 messages::propertyValueFormatError(asyncResp->res, destUrl,
282 "Destination");
283 return;
284 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700285
Ed Tanousfffb8c12022-02-07 23:53:03 -0800286 if (path.empty())
287 {
288 path = "/";
289 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800290 std::shared_ptr<Subscription> subValue =
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800291 std::make_shared<Subscription>(host, port, path, urlProto);
Ed Tanousfffb8c12022-02-07 23:53:03 -0800292
293 subValue->destinationUrl = destUrl;
294
295 if (subscriptionType)
296 {
297 if (*subscriptionType != "RedfishEvent")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700298 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800299 messages::propertyValueNotInList(
300 asyncResp->res, *subscriptionType, "SubscriptionType");
AppaRao Puli144b6312020-08-03 22:23:12 +0530301 return;
302 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800303 subValue->subscriptionType = *subscriptionType;
304 }
305 else
306 {
307 subValue->subscriptionType = "RedfishEvent"; // Default
308 }
AppaRao Puli156d6b02020-04-25 06:04:05 +0530309
Ed Tanousfffb8c12022-02-07 23:53:03 -0800310 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 Broadbent7e860f12021-04-08 15:57:16 -0700324 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800325 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 Broadbent7e860f12021-04-08 15:57:16 -0700347 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800348 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 Broadbent7e860f12021-04-08 15:57:16 -0700372 return;
373 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800374 }
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 Broadbent7e860f12021-04-08 15:57:16 -0700404 }
405 else
406 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800407 registryPrefix = subValue->registryPrefixes;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700408 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530409
Ed Tanousfffb8c12022-02-07 23:53:03 -0800410 for (const std::string& id : *msgIds)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700411 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800412 bool validId = false;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530413
Ed Tanousfffb8c12022-02-07 23:53:03 -0800414 // Check for Message ID in each of the selected Registry
415 for (const std::string& it : registryPrefix)
Ed Tanous601c71a2021-09-08 16:40:12 -0700416 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800417 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 Tanous55f79e62022-01-25 11:26:16 -0800425 return id == messageEntry.first;
Ed Tanousfffb8c12022-02-07 23:53:03 -0800426 }))
Ed Tanous601c71a2021-09-08 16:40:12 -0700427 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800428 validId = true;
429 break;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700430 }
431 }
432
Ed Tanousfffb8c12022-02-07 23:53:03 -0800433 if (!validId)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700434 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800435 messages::propertyValueNotInList(asyncResp->res, id,
436 "MessageIds");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700437 return;
438 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700439 }
440
Ed Tanousfffb8c12022-02-07 23:53:03 -0800441 subValue->registryMsgIds = *msgIds;
442 }
443
444 if (retryPolicy)
445 {
446 if (std::find(supportedRetryPolicies.begin(),
447 supportedRetryPolicies.end(),
448 *retryPolicy) == supportedRetryPolicies.end())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700449 {
Ed Tanousfffb8c12022-02-07 23:53:03 -0800450 messages::propertyValueNotInList(
451 asyncResp->res, *retryPolicy, "DeliveryRetryPolicy");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700452 return;
453 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800454 subValue->retryPolicy = *retryPolicy;
455 }
456 else
457 {
458 // Default "TerminateAfterRetries"
459 subValue->retryPolicy = "TerminateAfterRetries";
460 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700461
Ed Tanousfffb8c12022-02-07 23:53:03 -0800462 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 Broadbent7e860f12021-04-08 15:57:16 -0700490}
491
492inline void requestRoutesEventDestination(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530493{
Ravi Teja9d41aec2021-07-23 01:57:01 -0500494 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700495 .privileges(redfish::privileges::getEventDestination)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700496 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700497 [&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 Broadbent7e860f12021-04-08 15:57:16 -0700504 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 Pulie5aaf042020-03-20 01:05:52 +0530513
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700514 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 Tanousad22fef2021-09-13 13:07:32 -0700527 asyncResp->res.jsonValue["HttpHeaders"] =
528 nlohmann::json::array();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700529 asyncResp->res.jsonValue["EventFormatType"] =
530 subValue->eventFormatType;
531 asyncResp->res.jsonValue["RegistryPrefixes"] =
532 subValue->registryPrefixes;
533 asyncResp->res.jsonValue["ResourceTypes"] =
534 subValue->resourceTypes;
zhanghch058d1b46d2021-04-01 11:18:24 +0800535
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700536 asyncResp->res.jsonValue["MessageIds"] =
537 subValue->registryMsgIds;
538 asyncResp->res.jsonValue["DeliveryRetryPolicy"] =
539 subValue->retryPolicy;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530540
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700541 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 Teja9d41aec2021-07-23 01:57:01 -0500549 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700550 // The below privilege is wrong, it should be ConfigureManager OR
551 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500552 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700553 //.privileges(redfish::privileges::patchEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700554 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700555 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700556 [&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 Broadbent7e860f12021-04-08 15:57:16 -0700563 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 Pulie5aaf042020-03-20 01:05:52 +0530571
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700572 std::optional<std::string> context;
573 std::optional<std::string> retryPolicy;
574 std::optional<std::vector<nlohmann::json>> headers;
Sunitha Harishe56f2542020-07-22 02:38:59 -0500575
Willy Tu15ed6782021-12-14 11:03:16 -0800576 if (!json_util::readJsonPatch(req, asyncResp->res, "Context",
577 context, "DeliveryRetryPolicy",
578 retryPolicy, "HttpHeaders",
579 headers))
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700580 {
581 return;
582 }
AppaRao Puli144b6312020-08-03 22:23:12 +0530583
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700584 if (context)
585 {
586 subValue->customText = *context;
587 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530588
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700589 if (headers)
590 {
Ed Tanous601c71a2021-09-08 16:40:12 -0700591 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 Broadbent7e860f12021-04-08 15:57:16 -0700610 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800611
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700612 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 Pulie5aaf042020-03-20 01:05:52 +0530626
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700627 EventServiceManager::getInstance().updateSubscriptionData();
628 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500629 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700630 // The below privilege is wrong, it should be ConfigureManager OR
631 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500632 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700633 //.privileges(redfish::privileges::deleteEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700634 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700635 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700636 [&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 Broadbent7e860f12021-04-08 15:57:16 -0700643 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 Pulie5aaf042020-03-20 01:05:52 +0530653
654} // namespace redfish