blob: 5f57ff761ac616d7d33309187ea9bed240335958 [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080017#include "app.hpp"
AppaRao Pulib52664e2020-04-09 21:36:51 +053018#include "event_service_manager.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080019#include "http/utility.hpp"
20#include "logging.hpp"
21#include "query.hpp"
22#include "registries/privilege_registry.hpp"
AppaRao Pulie5aaf042020-03-20 01:05:52 +053023
Ed Tanous601c71a2021-09-08 16:40:12 -070024#include <boost/beast/http/fields.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070025
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 Tanous002d39b2022-05-31 08:59:27 -070052 .methods(boost::beast::http::verb::get)(
53 [&app](const crow::Request& req,
54 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +000055 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -070056 {
57 return;
58 }
Ed Tanous14766872022-03-15 10:44:42 -070059
Ed Tanous002d39b2022-05-31 08:59:27 -070060 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/EventService";
61 asyncResp->res.jsonValue["@odata.type"] =
62 "#EventService.v1_5_0.EventService";
63 asyncResp->res.jsonValue["Id"] = "EventService";
64 asyncResp->res.jsonValue["Name"] = "Event Service";
65 asyncResp->res.jsonValue["Subscriptions"]["@odata.id"] =
66 "/redfish/v1/EventService/Subscriptions";
67 asyncResp->res
68 .jsonValue["Actions"]["#EventService.SubmitTestEvent"]["target"] =
69 "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent";
AppaRao Pulie5aaf042020-03-20 01:05:52 +053070
Ed Tanous002d39b2022-05-31 08:59:27 -070071 const persistent_data::EventServiceConfig eventServiceConfig =
72 persistent_data::EventServiceStore::getInstance()
73 .getEventServiceConfig();
zhanghch058d1b46d2021-04-01 11:18:24 +080074
Ed Tanous002d39b2022-05-31 08:59:27 -070075 asyncResp->res.jsonValue["Status"]["State"] =
76 (eventServiceConfig.enabled ? "Enabled" : "Disabled");
77 asyncResp->res.jsonValue["ServiceEnabled"] = eventServiceConfig.enabled;
78 asyncResp->res.jsonValue["DeliveryRetryAttempts"] =
79 eventServiceConfig.retryAttempts;
80 asyncResp->res.jsonValue["DeliveryRetryIntervalSeconds"] =
81 eventServiceConfig.retryTimeoutInterval;
82 asyncResp->res.jsonValue["EventFormatTypes"] = supportedEvtFormatTypes;
83 asyncResp->res.jsonValue["RegistryPrefixes"] = supportedRegPrefixes;
84 asyncResp->res.jsonValue["ResourceTypes"] = supportedResourceTypes;
AppaRao Pulie5aaf042020-03-20 01:05:52 +053085
Ed Tanous613dabe2022-07-09 11:17:36 -070086 nlohmann::json::object_t supportedSSEFilters;
87 supportedSSEFilters["EventFormatType"] = true;
88 supportedSSEFilters["MessageId"] = true;
89 supportedSSEFilters["MetricReportDefinition"] = true;
90 supportedSSEFilters["RegistryPrefix"] = true;
91 supportedSSEFilters["OriginResource"] = false;
92 supportedSSEFilters["ResourceType"] = false;
AppaRao Puli7d1cc382020-05-16 02:42:22 +053093
Ed Tanous002d39b2022-05-31 08:59:27 -070094 asyncResp->res.jsonValue["SSEFilterPropertiesSupported"] =
Ed Tanous613dabe2022-07-09 11:17:36 -070095 std::move(supportedSSEFilters);
George Liu0fda0f12021-11-16 10:06:17 +080096 });
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) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000103 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700104 {
105 return;
106 }
107 std::optional<bool> serviceEnabled;
108 std::optional<uint32_t> retryAttemps;
109 std::optional<uint32_t> retryInterval;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530110
Ed Tanous002d39b2022-05-31 08:59:27 -0700111 if (!json_util::readJsonPatch(
112 req, asyncResp->res, "ServiceEnabled", serviceEnabled,
113 "DeliveryRetryAttempts", retryAttemps,
114 "DeliveryRetryIntervalSeconds", retryInterval))
115 {
116 return;
117 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530118
Ed Tanous002d39b2022-05-31 08:59:27 -0700119 persistent_data::EventServiceConfig eventServiceConfig =
120 persistent_data::EventServiceStore::getInstance()
121 .getEventServiceConfig();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700122
Ed Tanous002d39b2022-05-31 08:59:27 -0700123 if (serviceEnabled)
124 {
125 eventServiceConfig.enabled = *serviceEnabled;
126 }
Sunitha Harishe56f2542020-07-22 02:38:59 -0500127
Ed Tanous002d39b2022-05-31 08:59:27 -0700128 if (retryAttemps)
129 {
130 // Supported range [1-3]
131 if ((*retryAttemps < 1) || (*retryAttemps > 3))
132 {
133 messages::queryParameterOutOfRange(
134 asyncResp->res, std::to_string(*retryAttemps),
135 "DeliveryRetryAttempts", "[1-3]");
136 }
137 else
138 {
139 eventServiceConfig.retryAttempts = *retryAttemps;
140 }
141 }
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530142
Ed Tanous002d39b2022-05-31 08:59:27 -0700143 if (retryInterval)
144 {
Gunnar Mills33a32b32022-11-17 14:29:07 -0600145 // Supported range [5 - 180]
146 if ((*retryInterval < 5) || (*retryInterval > 180))
Ed Tanous002d39b2022-05-31 08:59:27 -0700147 {
148 messages::queryParameterOutOfRange(
149 asyncResp->res, std::to_string(*retryInterval),
Gunnar Mills33a32b32022-11-17 14:29:07 -0600150 "DeliveryRetryIntervalSeconds", "[5-180]");
Ed Tanous002d39b2022-05-31 08:59:27 -0700151 }
152 else
153 {
154 eventServiceConfig.retryTimeoutInterval = *retryInterval;
155 }
156 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700157
Ed Tanous002d39b2022-05-31 08:59:27 -0700158 EventServiceManager::getInstance().setEventServiceConfig(
159 eventServiceConfig);
160 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700161}
162
163inline void requestRoutesSubmitTestEvent(App& app)
164{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165 BMCWEB_ROUTE(
166 app, "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent/")
Ed Tanoused398212021-06-09 17:05:54 -0700167 .privileges(redfish::privileges::postEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700168 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700169 [&app](const crow::Request& req,
170 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000171 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700172 {
173 return;
174 }
175 if (!EventServiceManager::getInstance().sendTestEventLog())
176 {
177 messages::serviceDisabled(asyncResp->res,
178 "/redfish/v1/EventService/");
179 return;
180 }
181 asyncResp->res.result(boost::beast::http::status::no_content);
182 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700183}
184
185inline void requestRoutesEventDestinationCollection(App& app)
186{
Gayathri Leburu1ebe3e42022-02-09 10:45:19 +0000187 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/")
Ed Tanoused398212021-06-09 17:05:54 -0700188 .privileges(redfish::privileges::getEventDestinationCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700189 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700190 [&app](const crow::Request& req,
191 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000192 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700193 {
194 return;
195 }
196 asyncResp->res.jsonValue["@odata.type"] =
197 "#EventDestinationCollection.EventDestinationCollection";
198 asyncResp->res.jsonValue["@odata.id"] =
199 "/redfish/v1/EventService/Subscriptions";
200 asyncResp->res.jsonValue["Name"] = "Event Destination Collections";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700201
Ed Tanous002d39b2022-05-31 08:59:27 -0700202 nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700203
Ed Tanous002d39b2022-05-31 08:59:27 -0700204 std::vector<std::string> subscripIds =
205 EventServiceManager::getInstance().getAllIDs();
206 memberArray = nlohmann::json::array();
207 asyncResp->res.jsonValue["Members@odata.count"] = subscripIds.size();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700208
Ed Tanous002d39b2022-05-31 08:59:27 -0700209 for (const std::string& id : subscripIds)
210 {
211 nlohmann::json::object_t member;
Patrick Williams89492a12023-05-10 07:51:34 -0500212 member["@odata.id"] = "/redfish/v1/EventService/Subscriptions/" +
213 id;
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500214 memberArray.emplace_back(std::move(member));
Ed Tanous002d39b2022-05-31 08:59:27 -0700215 }
216 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700217 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/")
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500218 .privileges(redfish::privileges::postEventDestinationCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -0700219 .methods(boost::beast::http::verb::post)(
220 [&app](const crow::Request& req,
221 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000222 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700223 {
224 return;
225 }
226 if (EventServiceManager::getInstance().getNumberOfSubscriptions() >=
227 maxNoOfSubscriptions)
228 {
229 messages::eventSubscriptionLimitExceeded(asyncResp->res);
230 return;
231 }
232 std::string destUrl;
233 std::string protocol;
234 std::optional<std::string> context;
235 std::optional<std::string> subscriptionType;
236 std::optional<std::string> eventFormatType2;
237 std::optional<std::string> retryPolicy;
238 std::optional<std::vector<std::string>> msgIds;
239 std::optional<std::vector<std::string>> regPrefixes;
240 std::optional<std::vector<std::string>> resTypes;
241 std::optional<std::vector<nlohmann::json>> headers;
242 std::optional<std::vector<nlohmann::json>> mrdJsonArray;
Ed Tanousfffb8c12022-02-07 23:53:03 -0800243
Ed Tanous002d39b2022-05-31 08:59:27 -0700244 if (!json_util::readJsonPatch(
245 req, asyncResp->res, "Destination", destUrl, "Context", context,
246 "Protocol", protocol, "SubscriptionType", subscriptionType,
247 "EventFormatType", eventFormatType2, "HttpHeaders", headers,
248 "RegistryPrefixes", regPrefixes, "MessageIds", msgIds,
249 "DeliveryRetryPolicy", retryPolicy, "MetricReportDefinitions",
250 mrdJsonArray, "ResourceTypes", resTypes))
251 {
252 return;
253 }
254
255 if (regPrefixes && msgIds)
256 {
257 if (!regPrefixes->empty() && !msgIds->empty())
Ed Tanousfffb8c12022-02-07 23:53:03 -0800258 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700259 messages::propertyValueConflict(asyncResp->res, "MessageIds",
260 "RegistryPrefixes");
Ed Tanousfffb8c12022-02-07 23:53:03 -0800261 return;
262 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700263 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800264
Ed Tanous002d39b2022-05-31 08:59:27 -0700265 std::string host;
266 std::string urlProto;
267 uint16_t port = 0;
268 std::string path;
269
270 if (!crow::utility::validateAndSplitUrl(destUrl, urlProto, host, port,
271 path))
272 {
273 BMCWEB_LOG_WARNING
274 << "Failed to validate and split destination url";
275 messages::propertyValueFormatError(asyncResp->res, destUrl,
276 "Destination");
277 return;
278 }
279
280 if (path.empty())
281 {
282 path = "/";
283 }
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700284 std::shared_ptr<Subscription> subValue = std::make_shared<Subscription>(
285 host, port, path, urlProto, app.ioContext());
Ed Tanous002d39b2022-05-31 08:59:27 -0700286
287 subValue->destinationUrl = destUrl;
288
289 if (subscriptionType)
290 {
291 if (*subscriptionType != "RedfishEvent")
Ed Tanousfffb8c12022-02-07 23:53:03 -0800292 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700293 messages::propertyValueNotInList(
294 asyncResp->res, *subscriptionType, "SubscriptionType");
295 return;
296 }
297 subValue->subscriptionType = *subscriptionType;
298 }
299 else
300 {
301 subValue->subscriptionType = "RedfishEvent"; // Default
302 }
303
304 if (protocol != "Redfish")
305 {
306 messages::propertyValueNotInList(asyncResp->res, protocol,
307 "Protocol");
308 return;
309 }
310 subValue->protocol = protocol;
311
312 if (eventFormatType2)
313 {
314 if (std::find(supportedEvtFormatTypes.begin(),
315 supportedEvtFormatTypes.end(),
316 *eventFormatType2) == supportedEvtFormatTypes.end())
317 {
318 messages::propertyValueNotInList(
319 asyncResp->res, *eventFormatType2, "EventFormatType");
320 return;
321 }
322 subValue->eventFormatType = *eventFormatType2;
323 }
324 else
325 {
326 // If not specified, use default "Event"
327 subValue->eventFormatType = "Event";
328 }
329
330 if (context)
331 {
332 subValue->customText = *context;
333 }
334
335 if (headers)
336 {
337 for (const nlohmann::json& headerChunk : *headers)
338 {
339 for (const auto& item : headerChunk.items())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700340 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700341 const std::string* value =
342 item.value().get_ptr<const std::string*>();
343 if (value == nullptr)
344 {
345 messages::propertyValueFormatError(
346 asyncResp->res, item.value().dump(2, 1),
347 "HttpHeaders/" + item.key());
348 return;
349 }
350 subValue->httpHeaders.set(item.key(), *value);
351 }
352 }
353 }
354
355 if (regPrefixes)
356 {
357 for (const std::string& it : *regPrefixes)
358 {
359 if (std::find(supportedRegPrefixes.begin(),
360 supportedRegPrefixes.end(),
361 it) == supportedRegPrefixes.end())
362 {
363 messages::propertyValueNotInList(asyncResp->res, it,
364 "RegistryPrefixes");
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530365 return;
366 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800367 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700368 subValue->registryPrefixes = *regPrefixes;
369 }
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530370
Ed Tanous002d39b2022-05-31 08:59:27 -0700371 if (resTypes)
372 {
373 for (const std::string& it : *resTypes)
Ed Tanousfffb8c12022-02-07 23:53:03 -0800374 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700375 if (std::find(supportedResourceTypes.begin(),
376 supportedResourceTypes.end(),
377 it) == supportedResourceTypes.end())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700378 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700379 messages::propertyValueNotInList(asyncResp->res, it,
380 "ResourceTypes");
AppaRao Puli144b6312020-08-03 22:23:12 +0530381 return;
382 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700383 }
384 subValue->resourceTypes = *resTypes;
385 }
386
387 if (msgIds)
388 {
389 std::vector<std::string> registryPrefix;
390
391 // If no registry prefixes are mentioned, consider all
392 // supported prefixes
393 if (subValue->registryPrefixes.empty())
394 {
395 registryPrefix.assign(supportedRegPrefixes.begin(),
396 supportedRegPrefixes.end());
Ed Tanousfffb8c12022-02-07 23:53:03 -0800397 }
398 else
399 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700400 registryPrefix = subValue->registryPrefixes;
Ed Tanousfffb8c12022-02-07 23:53:03 -0800401 }
AppaRao Puli156d6b02020-04-25 06:04:05 +0530402
Ed Tanous002d39b2022-05-31 08:59:27 -0700403 for (const std::string& id : *msgIds)
Ed Tanousfffb8c12022-02-07 23:53:03 -0800404 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700405 bool validId = false;
Ed Tanousfffb8c12022-02-07 23:53:03 -0800406
Ed Tanous002d39b2022-05-31 08:59:27 -0700407 // Check for Message ID in each of the selected Registry
408 for (const std::string& it : registryPrefix)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700409 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700410 const std::span<const redfish::registries::MessageEntry>
411 registry =
412 redfish::registries::getRegistryFromPrefix(it);
413
414 if (std::any_of(
415 registry.begin(), registry.end(),
416 [&id](const redfish::registries::MessageEntry&
417 messageEntry) {
418 return id == messageEntry.first;
419 }))
420 {
421 validId = true;
422 break;
423 }
424 }
425
426 if (!validId)
427 {
428 messages::propertyValueNotInList(asyncResp->res, id,
429 "MessageIds");
Ed Tanousfffb8c12022-02-07 23:53:03 -0800430 return;
431 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800432 }
433
Ed Tanous002d39b2022-05-31 08:59:27 -0700434 subValue->registryMsgIds = *msgIds;
435 }
436
437 if (retryPolicy)
438 {
439 if (std::find(supportedRetryPolicies.begin(),
440 supportedRetryPolicies.end(),
441 *retryPolicy) == supportedRetryPolicies.end())
Ed Tanousfffb8c12022-02-07 23:53:03 -0800442 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700443 messages::propertyValueNotInList(asyncResp->res, *retryPolicy,
444 "DeliveryRetryPolicy");
Ed Tanousfffb8c12022-02-07 23:53:03 -0800445 return;
446 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700447 subValue->retryPolicy = *retryPolicy;
448 }
449 else
450 {
451 // Default "TerminateAfterRetries"
452 subValue->retryPolicy = "TerminateAfterRetries";
453 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800454
Ed Tanous002d39b2022-05-31 08:59:27 -0700455 if (mrdJsonArray)
456 {
457 for (nlohmann::json& mrdObj : *mrdJsonArray)
458 {
459 std::string mrdUri;
460
461 if (!json_util::readJson(mrdObj, asyncResp->res, "@odata.id",
462 mrdUri))
463
464 {
465 return;
466 }
467 subValue->metricReportDefinitions.emplace_back(mrdUri);
468 }
469 }
470
471 std::string id =
472 EventServiceManager::getInstance().addSubscription(subValue);
473 if (id.empty())
474 {
475 messages::internalError(asyncResp->res);
476 return;
477 }
478
479 messages::created(asyncResp->res);
480 asyncResp->res.addHeader(
481 "Location", "/redfish/v1/EventService/Subscriptions/" + id);
Ed Tanousfffb8c12022-02-07 23:53:03 -0800482 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700483}
484
485inline void requestRoutesEventDestination(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530486{
Ravi Teja9d41aec2021-07-23 01:57:01 -0500487 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700488 .privileges(redfish::privileges::getEventDestination)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700489 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700490 [&app](const crow::Request& req,
491 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
492 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000493 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700494 {
495 return;
496 }
497 std::shared_ptr<Subscription> subValue =
498 EventServiceManager::getInstance().getSubscription(param);
499 if (subValue == nullptr)
500 {
501 asyncResp->res.result(boost::beast::http::status::not_found);
502 return;
503 }
504 const std::string& id = param;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530505
Ed Tanous002d39b2022-05-31 08:59:27 -0700506 asyncResp->res.jsonValue["@odata.type"] =
507 "#EventDestination.v1_7_0.EventDestination";
508 asyncResp->res.jsonValue["Protocol"] = "Redfish";
509 asyncResp->res.jsonValue["@odata.id"] =
510 "/redfish/v1/EventService/Subscriptions/" + id;
511 asyncResp->res.jsonValue["Id"] = id;
512 asyncResp->res.jsonValue["Name"] = "Event Destination " + id;
513 asyncResp->res.jsonValue["Destination"] = subValue->destinationUrl;
514 asyncResp->res.jsonValue["Context"] = subValue->customText;
515 asyncResp->res.jsonValue["SubscriptionType"] =
516 subValue->subscriptionType;
517 asyncResp->res.jsonValue["HttpHeaders"] = nlohmann::json::array();
518 asyncResp->res.jsonValue["EventFormatType"] = subValue->eventFormatType;
519 asyncResp->res.jsonValue["RegistryPrefixes"] =
520 subValue->registryPrefixes;
521 asyncResp->res.jsonValue["ResourceTypes"] = subValue->resourceTypes;
zhanghch058d1b46d2021-04-01 11:18:24 +0800522
Ed Tanous002d39b2022-05-31 08:59:27 -0700523 asyncResp->res.jsonValue["MessageIds"] = subValue->registryMsgIds;
524 asyncResp->res.jsonValue["DeliveryRetryPolicy"] = subValue->retryPolicy;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530525
Ed Tanous002d39b2022-05-31 08:59:27 -0700526 nlohmann::json::array_t mrdJsonArray;
527 for (const auto& mdrUri : subValue->metricReportDefinitions)
528 {
529 nlohmann::json::object_t mdr;
530 mdr["@odata.id"] = mdrUri;
531 mrdJsonArray.emplace_back(std::move(mdr));
532 }
533 asyncResp->res.jsonValue["MetricReportDefinitions"] = mrdJsonArray;
534 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500535 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700536 // The below privilege is wrong, it should be ConfigureManager OR
537 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500538 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700539 //.privileges(redfish::privileges::patchEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700540 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700541 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700542 [&app](const crow::Request& req,
543 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
544 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000545 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700546 {
547 return;
548 }
549 std::shared_ptr<Subscription> subValue =
550 EventServiceManager::getInstance().getSubscription(param);
551 if (subValue == nullptr)
552 {
553 asyncResp->res.result(boost::beast::http::status::not_found);
554 return;
555 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530556
Ed Tanous002d39b2022-05-31 08:59:27 -0700557 std::optional<std::string> context;
558 std::optional<std::string> retryPolicy;
559 std::optional<std::vector<nlohmann::json>> headers;
Sunitha Harishe56f2542020-07-22 02:38:59 -0500560
Ed Tanous002d39b2022-05-31 08:59:27 -0700561 if (!json_util::readJsonPatch(req, asyncResp->res, "Context", context,
562 "DeliveryRetryPolicy", retryPolicy,
563 "HttpHeaders", headers))
564 {
565 return;
566 }
AppaRao Puli144b6312020-08-03 22:23:12 +0530567
Ed Tanous002d39b2022-05-31 08:59:27 -0700568 if (context)
569 {
570 subValue->customText = *context;
571 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530572
Ed Tanous002d39b2022-05-31 08:59:27 -0700573 if (headers)
574 {
575 boost::beast::http::fields fields;
576 for (const nlohmann::json& headerChunk : *headers)
577 {
Patrick Williams62bafc02022-09-08 17:35:35 -0500578 for (const auto& it : headerChunk.items())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700579 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700580 const std::string* value =
581 it.value().get_ptr<const std::string*>();
582 if (value == nullptr)
Ed Tanous601c71a2021-09-08 16:40:12 -0700583 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700584 messages::propertyValueFormatError(
585 asyncResp->res, it.value().dump(2, ' ', true),
586 "HttpHeaders/" + it.key());
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700587 return;
588 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700589 fields.set(it.key(), *value);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700590 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700591 }
592 subValue->httpHeaders = fields;
593 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530594
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 if (retryPolicy)
596 {
597 if (std::find(supportedRetryPolicies.begin(),
598 supportedRetryPolicies.end(),
599 *retryPolicy) == supportedRetryPolicies.end())
600 {
601 messages::propertyValueNotInList(asyncResp->res, *retryPolicy,
602 "DeliveryRetryPolicy");
603 return;
604 }
605 subValue->retryPolicy = *retryPolicy;
Ed Tanous002d39b2022-05-31 08:59:27 -0700606 }
607
608 EventServiceManager::getInstance().updateSubscriptionData();
609 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500610 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700611 // The below privilege is wrong, it should be ConfigureManager OR
612 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500613 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700614 //.privileges(redfish::privileges::deleteEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700615 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700616 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700617 [&app](const crow::Request& req,
618 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
619 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000620 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700621 {
622 return;
623 }
624 if (!EventServiceManager::getInstance().isSubscriptionExist(param))
625 {
626 asyncResp->res.result(boost::beast::http::status::not_found);
627 return;
628 }
629 EventServiceManager::getInstance().deleteSubscription(param);
630 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700631}
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530632
633} // namespace redfish