blob: bcb30aa79d2bcfb05836d3687d194c8470c4e6db [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 Tanoused398212021-06-09 17:05:54 -070021#include <registries/privilege_registry.hpp>
22
Patrick Williams1e270c52021-12-04 06:06:56 -060023#include <span>
24
AppaRao Pulie5aaf042020-03-20 01:05:52 +053025namespace redfish
26{
27
AppaRao Puli156d6b02020-04-25 06:04:05 +053028static constexpr const std::array<const char*, 2> supportedEvtFormatTypes = {
29 eventFormatType, metricReportFormatType};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053030static constexpr const std::array<const char*, 3> supportedRegPrefixes = {
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +053031 "Base", "OpenBMC", "TaskEvent"};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053032static constexpr const std::array<const char*, 3> supportedRetryPolicies = {
33 "TerminateAfterRetries", "SuspendRetries", "RetryForever"};
34
Sunitha Harishe56f2542020-07-22 02:38:59 -050035#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
36static constexpr const std::array<const char*, 2> supportedResourceTypes = {
37 "IBMConfigFile", "Task"};
38#else
39static constexpr const std::array<const char*, 1> supportedResourceTypes = {
40 "Task"};
41#endif
42
AppaRao Pulie5aaf042020-03-20 01:05:52 +053043static constexpr const uint8_t maxNoOfSubscriptions = 20;
44
John Edward Broadbent7e860f12021-04-08 15:57:16 -070045inline void requestRoutesEventService(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +053046{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070047 BMCWEB_ROUTE(app, "/redfish/v1/EventService/")
Ed Tanoused398212021-06-09 17:05:54 -070048 .privileges(redfish::privileges::getEventService)
George Liu0fda0f12021-11-16 10:06:17 +080049 .methods(
50 boost::beast::http::verb::
51 get)([](const crow::Request&,
52 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
53 asyncResp->res.jsonValue = {
54 {"@odata.type", "#EventService.v1_5_0.EventService"},
55 {"Id", "EventService"},
56 {"Name", "Event Service"},
57 {"Subscriptions",
58 {{"@odata.id", "/redfish/v1/EventService/Subscriptions"}}},
59 {"Actions",
60 {{"#EventService.SubmitTestEvent",
61 {{"target",
62 "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent"}}}}},
63 {"@odata.id", "/redfish/v1/EventService"}};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053064
George Liu0fda0f12021-11-16 10:06:17 +080065 const persistent_data::EventServiceConfig eventServiceConfig =
66 persistent_data::EventServiceStore::getInstance()
67 .getEventServiceConfig();
zhanghch058d1b46d2021-04-01 11:18:24 +080068
George Liu0fda0f12021-11-16 10:06:17 +080069 asyncResp->res.jsonValue["Status"]["State"] =
70 (eventServiceConfig.enabled ? "Enabled" : "Disabled");
71 asyncResp->res.jsonValue["ServiceEnabled"] =
72 eventServiceConfig.enabled;
73 asyncResp->res.jsonValue["DeliveryRetryAttempts"] =
74 eventServiceConfig.retryAttempts;
75 asyncResp->res.jsonValue["DeliveryRetryIntervalSeconds"] =
76 eventServiceConfig.retryTimeoutInterval;
77 asyncResp->res.jsonValue["EventFormatTypes"] =
78 supportedEvtFormatTypes;
79 asyncResp->res.jsonValue["RegistryPrefixes"] = supportedRegPrefixes;
80 asyncResp->res.jsonValue["ResourceTypes"] = supportedResourceTypes;
AppaRao Pulie5aaf042020-03-20 01:05:52 +053081
George Liu0fda0f12021-11-16 10:06:17 +080082 nlohmann::json supportedSSEFilters = {
83 {"EventFormatType", true}, {"MessageId", true},
84 {"MetricReportDefinition", true}, {"RegistryPrefix", true},
85 {"OriginResource", false}, {"ResourceType", false}};
AppaRao Puli7d1cc382020-05-16 02:42:22 +053086
George Liu0fda0f12021-11-16 10:06:17 +080087 asyncResp->res.jsonValue["SSEFilterPropertiesSupported"] =
88 supportedSSEFilters;
89 });
Ayushi Smriti07941a82020-05-21 15:55:34 +053090
John Edward Broadbent7e860f12021-04-08 15:57:16 -070091 BMCWEB_ROUTE(app, "/redfish/v1/EventService/")
Ed Tanoused398212021-06-09 17:05:54 -070092 .privileges(redfish::privileges::patchEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070093 .methods(boost::beast::http::verb::patch)(
94 [](const crow::Request& req,
95 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ayushi Smriti07941a82020-05-21 15:55:34 +053096
AppaRao Pulie5aaf042020-03-20 01:05:52 +053097 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -070098 std::optional<bool> serviceEnabled;
99 std::optional<uint32_t> retryAttemps;
100 std::optional<uint32_t> retryInterval;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530101
Willy Tu15ed6782021-12-14 11:03:16 -0800102 if (!json_util::readJsonPatch(
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700103 req, asyncResp->res, "ServiceEnabled", serviceEnabled,
104 "DeliveryRetryAttempts", retryAttemps,
105 "DeliveryRetryIntervalSeconds", retryInterval))
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530106 {
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530107 return;
108 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530109
JunLin Chen28afb492021-02-24 17:13:29 +0800110 persistent_data::EventServiceConfig eventServiceConfig =
111 persistent_data::EventServiceStore::getInstance()
112 .getEventServiceConfig();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700113
114 if (serviceEnabled)
Sunitha Harishe56f2542020-07-22 02:38:59 -0500115 {
JunLin Chen28afb492021-02-24 17:13:29 +0800116 eventServiceConfig.enabled = *serviceEnabled;
Sunitha Harishe56f2542020-07-22 02:38:59 -0500117 }
Sunitha Harishe56f2542020-07-22 02:38:59 -0500118
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700119 if (retryAttemps)
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530120 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700121 // Supported range [1-3]
122 if ((*retryAttemps < 1) || (*retryAttemps > 3))
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530123 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700124 messages::queryParameterOutOfRange(
125 asyncResp->res, std::to_string(*retryAttemps),
126 "DeliveryRetryAttempts", "[1-3]");
127 }
128 else
129 {
JunLin Chen28afb492021-02-24 17:13:29 +0800130 eventServiceConfig.retryAttempts = *retryAttemps;
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530131 }
132 }
133
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700134 if (retryInterval)
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530135 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700136 // Supported range [30 - 180]
137 if ((*retryInterval < 30) || (*retryInterval > 180))
138 {
139 messages::queryParameterOutOfRange(
140 asyncResp->res, std::to_string(*retryInterval),
141 "DeliveryRetryIntervalSeconds", "[30-180]");
142 }
143 else
144 {
JunLin Chen28afb492021-02-24 17:13:29 +0800145 eventServiceConfig.retryTimeoutInterval =
146 *retryInterval;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700147 }
148 }
149
150 EventServiceManager::getInstance().setEventServiceConfig(
JunLin Chen28afb492021-02-24 17:13:29 +0800151 eventServiceConfig);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700152 });
153}
154
155inline void requestRoutesSubmitTestEvent(App& app)
156{
157
158 BMCWEB_ROUTE(
159 app, "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent/")
Ed Tanoused398212021-06-09 17:05:54 -0700160 .privileges(redfish::privileges::postEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700161 .methods(boost::beast::http::verb::post)(
162 [](const crow::Request&,
163 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
sunharis_in6ba8c822021-07-06 06:06:58 -0500164 if (!EventServiceManager::getInstance().sendTestEventLog())
165 {
166 messages::serviceDisabled(asyncResp->res,
167 "/redfish/v1/EventService/");
168 return;
169 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170 asyncResp->res.result(boost::beast::http::status::no_content);
171 });
172}
173
174inline void requestRoutesEventDestinationCollection(App& app)
175{
Gayathri Leburu1ebe3e42022-02-09 10:45:19 +0000176 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/")
Ed Tanoused398212021-06-09 17:05:54 -0700177 .privileges(redfish::privileges::getEventDestinationCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700178 .methods(boost::beast::http::verb::get)(
179 [](const crow::Request&,
180 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
181 asyncResp->res.jsonValue = {
182 {"@odata.type",
183 "#EventDestinationCollection.EventDestinationCollection"},
184 {"@odata.id", "/redfish/v1/EventService/Subscriptions"},
185 {"Name", "Event Destination Collections"}};
186
187 nlohmann::json& memberArray =
188 asyncResp->res.jsonValue["Members"];
189
190 std::vector<std::string> subscripIds =
191 EventServiceManager::getInstance().getAllIDs();
192 memberArray = nlohmann::json::array();
193 asyncResp->res.jsonValue["Members@odata.count"] =
194 subscripIds.size();
195
196 for (const std::string& id : subscripIds)
197 {
198 memberArray.push_back(
199 {{"@odata.id",
200 "/redfish/v1/EventService/Subscriptions/" + id}});
201 }
202 });
203 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/")
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500204 .privileges(redfish::privileges::postEventDestinationCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700205 .methods(boost::beast::http::verb::post)(
206 [](const crow::Request& req,
207 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
208 if (EventServiceManager::getInstance()
209 .getNumberOfSubscriptions() >= maxNoOfSubscriptions)
210 {
211 messages::eventSubscriptionLimitExceeded(asyncResp->res);
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530212 return;
213 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700214 std::string destUrl;
215 std::string protocol;
216 std::optional<std::string> context;
217 std::optional<std::string> subscriptionType;
218 std::optional<std::string> eventFormatType2;
219 std::optional<std::string> retryPolicy;
220 std::optional<std::vector<std::string>> msgIds;
221 std::optional<std::vector<std::string>> regPrefixes;
222 std::optional<std::vector<std::string>> resTypes;
223 std::optional<std::vector<nlohmann::json>> headers;
224 std::optional<std::vector<nlohmann::json>> mrdJsonArray;
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530225
Willy Tu15ed6782021-12-14 11:03:16 -0800226 if (!json_util::readJsonPatch(
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700227 req, asyncResp->res, "Destination", destUrl, "Context",
228 context, "Protocol", protocol, "SubscriptionType",
229 subscriptionType, "EventFormatType", eventFormatType2,
230 "HttpHeaders", headers, "RegistryPrefixes", regPrefixes,
231 "MessageIds", msgIds, "DeliveryRetryPolicy",
232 retryPolicy, "MetricReportDefinitions", mrdJsonArray,
233 "ResourceTypes", resTypes))
AppaRao Puli144b6312020-08-03 22:23:12 +0530234 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700235 return;
236 }
237
238 if (regPrefixes && msgIds)
239 {
Ed Tanous26f69762022-01-25 09:49:11 -0800240 if (!regPrefixes->empty() && !msgIds->empty())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700241 {
Ed Tanous0a4304c2022-02-07 13:30:00 -0800242 messages::propertyValueConflict(
243 asyncResp->res, "MessageIds", "RegistryPrefixes");
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700244 return;
245 }
246 }
247
248 // Validate the URL using regex expression
249 // Format: <protocol>://<host>:<port>/<uri>
250 // protocol: http/https
251 // host: Exclude ' ', ':', '#', '?'
252 // port: Empty or numeric value with ':' separator.
253 // uri: Start with '/' and Exclude '#', ' '
254 // Can include query params(ex: '/event?test=1')
255 // TODO: Need to validate hostname extensively(as per rfc)
256 const std::regex urlRegex(
257 "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/"
258 "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)");
259 std::cmatch match;
260 if (!std::regex_match(destUrl.c_str(), match, urlRegex))
261 {
262 messages::propertyValueFormatError(asyncResp->res, destUrl,
263 "Destination");
264 return;
265 }
266
267 std::string uriProto =
268 std::string(match[1].first, match[1].second);
269 if (uriProto == "http")
270 {
271#ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
272 messages::propertyValueFormatError(asyncResp->res, destUrl,
273 "Destination");
274 return;
275#endif
276 }
277
278 std::string host = std::string(match[2].first, match[2].second);
279 std::string port = std::string(match[3].first, match[3].second);
280 std::string path = std::string(match[4].first, match[4].second);
281 if (port.empty())
282 {
283 if (uriProto == "http")
284 {
285 port = "80";
286 }
287 else
288 {
289 port = "443";
290 }
291 }
292 if (path.empty())
293 {
294 path = "/";
295 }
296
297 std::shared_ptr<Subscription> subValue =
298 std::make_shared<Subscription>(host, port, path, uriProto);
299
300 subValue->destinationUrl = destUrl;
301
302 if (subscriptionType)
303 {
304 if (*subscriptionType != "RedfishEvent")
305 {
306 messages::propertyValueNotInList(asyncResp->res,
307 *subscriptionType,
308 "SubscriptionType");
309 return;
310 }
311 subValue->subscriptionType = *subscriptionType;
AppaRao Puli144b6312020-08-03 22:23:12 +0530312 }
313 else
314 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700315 subValue->subscriptionType = "RedfishEvent"; // Default
316 }
317
318 if (protocol != "Redfish")
319 {
320 messages::propertyValueNotInList(asyncResp->res, protocol,
321 "Protocol");
AppaRao Puli144b6312020-08-03 22:23:12 +0530322 return;
323 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700324 subValue->protocol = protocol;
AppaRao Puli156d6b02020-04-25 06:04:05 +0530325
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700326 if (eventFormatType2)
327 {
328 if (std::find(supportedEvtFormatTypes.begin(),
329 supportedEvtFormatTypes.end(),
330 *eventFormatType2) ==
331 supportedEvtFormatTypes.end())
332 {
333 messages::propertyValueNotInList(asyncResp->res,
334 *eventFormatType2,
335 "EventFormatType");
336 return;
337 }
338 subValue->eventFormatType = *eventFormatType2;
339 }
340 else
341 {
342 // If not specified, use default "Event"
343 subValue->eventFormatType = "Event";
344 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530345
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700346 if (context)
347 {
348 subValue->customText = *context;
349 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530350
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700351 if (headers)
352 {
Ed Tanous601c71a2021-09-08 16:40:12 -0700353 for (const nlohmann::json& headerChunk : *headers)
354 {
355 for (const auto& item : headerChunk.items())
356 {
357 const std::string* value =
358 item.value().get_ptr<const std::string*>();
359 if (value == nullptr)
360 {
361 messages::propertyValueFormatError(
Ed Tanouse662eae2022-01-25 10:39:19 -0800362 asyncResp->res, item.value().dump(2, 1),
Ed Tanous601c71a2021-09-08 16:40:12 -0700363 "HttpHeaders/" + item.key());
364 return;
365 }
366 subValue->httpHeaders.set(item.key(), *value);
367 }
368 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700369 }
370
371 if (regPrefixes)
372 {
373 for (const std::string& it : *regPrefixes)
374 {
375 if (std::find(supportedRegPrefixes.begin(),
376 supportedRegPrefixes.end(),
377 it) == supportedRegPrefixes.end())
378 {
379 messages::propertyValueNotInList(
380 asyncResp->res, it, "RegistryPrefixes");
381 return;
382 }
383 }
384 subValue->registryPrefixes = *regPrefixes;
385 }
386
387 if (resTypes)
388 {
389 for (const std::string& it : *resTypes)
390 {
391 if (std::find(supportedResourceTypes.begin(),
392 supportedResourceTypes.end(),
393 it) == supportedResourceTypes.end())
394 {
395 messages::propertyValueNotInList(asyncResp->res, it,
396 "ResourceTypes");
397 return;
398 }
399 }
400 subValue->resourceTypes = *resTypes;
401 }
402
403 if (msgIds)
404 {
405 std::vector<std::string> registryPrefix;
406
407 // If no registry prefixes are mentioned, consider all
408 // supported prefixes
409 if (subValue->registryPrefixes.empty())
410 {
411 registryPrefix.assign(supportedRegPrefixes.begin(),
412 supportedRegPrefixes.end());
413 }
414 else
415 {
416 registryPrefix = subValue->registryPrefixes;
417 }
418
419 for (const std::string& id : *msgIds)
420 {
421 bool validId = false;
422
423 // Check for Message ID in each of the selected Registry
424 for (const std::string& it : registryPrefix)
425 {
Ed Tanous26702d02021-11-03 15:02:33 -0700426 const std::span<
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700427 const redfish::message_registries::MessageEntry>
428 registry = redfish::message_registries::
429 getRegistryFromPrefix(it);
430
431 if (std::any_of(
Ed Tanous26702d02021-11-03 15:02:33 -0700432 registry.begin(), registry.end(),
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700433 [&id](const redfish::message_registries::
434 MessageEntry& messageEntry) {
Ed Tanouse662eae2022-01-25 10:39:19 -0800435 return id.compare(messageEntry.first) ==
436 0;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700437 }))
438 {
439 validId = true;
440 break;
441 }
442 }
443
444 if (!validId)
445 {
446 messages::propertyValueNotInList(asyncResp->res, id,
447 "MessageIds");
448 return;
449 }
450 }
451
452 subValue->registryMsgIds = *msgIds;
453 }
454
455 if (retryPolicy)
456 {
457 if (std::find(supportedRetryPolicies.begin(),
458 supportedRetryPolicies.end(),
459 *retryPolicy) == supportedRetryPolicies.end())
460 {
461 messages::propertyValueNotInList(asyncResp->res,
462 *retryPolicy,
463 "DeliveryRetryPolicy");
464 return;
465 }
466 subValue->retryPolicy = *retryPolicy;
467 }
468 else
469 {
470 // Default "TerminateAfterRetries"
471 subValue->retryPolicy = "TerminateAfterRetries";
472 }
473
474 if (mrdJsonArray)
475 {
476 for (nlohmann::json& mrdObj : *mrdJsonArray)
477 {
478 std::string mrdUri;
Willy Tuea2e6ee2022-01-31 15:38:48 -0800479
480 if (!json_util::readJson(mrdObj, asyncResp->res,
481 "@odata.id", mrdUri))
482
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700483 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700484 return;
485 }
Willy Tuea2e6ee2022-01-31 15:38:48 -0800486 subValue->metricReportDefinitions.emplace_back(mrdUri);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700487 }
488 }
489
490 std::string id =
491 EventServiceManager::getInstance().addSubscription(
492 subValue);
493 if (id.empty())
494 {
495 messages::internalError(asyncResp->res);
496 return;
497 }
498
499 messages::created(asyncResp->res);
500 asyncResp->res.addHeader(
501 "Location", "/redfish/v1/EventService/Subscriptions/" + id);
502 });
503}
504
505inline void requestRoutesEventDestination(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530506{
Ravi Teja9d41aec2021-07-23 01:57:01 -0500507 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700508 .privileges(redfish::privileges::getEventDestination)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700509 .methods(boost::beast::http::verb::get)(
510 [](const crow::Request&,
511 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
512 const std::string& param) {
513 std::shared_ptr<Subscription> subValue =
514 EventServiceManager::getInstance().getSubscription(param);
515 if (subValue == nullptr)
516 {
517 asyncResp->res.result(
518 boost::beast::http::status::not_found);
519 return;
520 }
521 const std::string& id = param;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530522
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700523 asyncResp->res.jsonValue = {
524 {"@odata.type",
525 "#EventDestination.v1_7_0.EventDestination"},
526 {"Protocol", "Redfish"}};
527 asyncResp->res.jsonValue["@odata.id"] =
528 "/redfish/v1/EventService/Subscriptions/" + id;
529 asyncResp->res.jsonValue["Id"] = id;
530 asyncResp->res.jsonValue["Name"] = "Event Destination " + id;
531 asyncResp->res.jsonValue["Destination"] =
532 subValue->destinationUrl;
533 asyncResp->res.jsonValue["Context"] = subValue->customText;
534 asyncResp->res.jsonValue["SubscriptionType"] =
535 subValue->subscriptionType;
Ed Tanousad22fef2021-09-13 13:07:32 -0700536 asyncResp->res.jsonValue["HttpHeaders"] =
537 nlohmann::json::array();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700538 asyncResp->res.jsonValue["EventFormatType"] =
539 subValue->eventFormatType;
540 asyncResp->res.jsonValue["RegistryPrefixes"] =
541 subValue->registryPrefixes;
542 asyncResp->res.jsonValue["ResourceTypes"] =
543 subValue->resourceTypes;
zhanghch058d1b46d2021-04-01 11:18:24 +0800544
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700545 asyncResp->res.jsonValue["MessageIds"] =
546 subValue->registryMsgIds;
547 asyncResp->res.jsonValue["DeliveryRetryPolicy"] =
548 subValue->retryPolicy;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530549
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700550 std::vector<nlohmann::json> mrdJsonArray;
551 for (const auto& mdrUri : subValue->metricReportDefinitions)
552 {
553 mrdJsonArray.push_back({{"@odata.id", mdrUri}});
554 }
555 asyncResp->res.jsonValue["MetricReportDefinitions"] =
556 mrdJsonArray;
557 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500558 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700559 // The below privilege is wrong, it should be ConfigureManager OR
560 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500561 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700562 //.privileges(redfish::privileges::patchEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700563 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700564 .methods(boost::beast::http::verb::patch)(
565 [](const crow::Request& req,
566 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
567 const std::string& param) {
568 std::shared_ptr<Subscription> subValue =
569 EventServiceManager::getInstance().getSubscription(param);
570 if (subValue == nullptr)
571 {
572 asyncResp->res.result(
573 boost::beast::http::status::not_found);
574 return;
575 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530576
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700577 std::optional<std::string> context;
578 std::optional<std::string> retryPolicy;
579 std::optional<std::vector<nlohmann::json>> headers;
Sunitha Harishe56f2542020-07-22 02:38:59 -0500580
Willy Tu15ed6782021-12-14 11:03:16 -0800581 if (!json_util::readJsonPatch(req, asyncResp->res, "Context",
582 context, "DeliveryRetryPolicy",
583 retryPolicy, "HttpHeaders",
584 headers))
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700585 {
586 return;
587 }
AppaRao Puli144b6312020-08-03 22:23:12 +0530588
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700589 if (context)
590 {
591 subValue->customText = *context;
592 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530593
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700594 if (headers)
595 {
Ed Tanous601c71a2021-09-08 16:40:12 -0700596 boost::beast::http::fields fields;
597 for (const nlohmann::json& headerChunk : *headers)
598 {
599 for (auto& it : headerChunk.items())
600 {
601 const std::string* value =
602 it.value().get_ptr<const std::string*>();
603 if (value == nullptr)
604 {
605 messages::propertyValueFormatError(
606 asyncResp->res,
607 it.value().dump(2, ' ', true),
608 "HttpHeaders/" + it.key());
609 return;
610 }
611 fields.set(it.key(), *value);
612 }
613 }
614 subValue->httpHeaders = fields;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700615 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800616
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700617 if (retryPolicy)
618 {
619 if (std::find(supportedRetryPolicies.begin(),
620 supportedRetryPolicies.end(),
621 *retryPolicy) == supportedRetryPolicies.end())
622 {
623 messages::propertyValueNotInList(asyncResp->res,
624 *retryPolicy,
625 "DeliveryRetryPolicy");
626 return;
627 }
628 subValue->retryPolicy = *retryPolicy;
629 subValue->updateRetryPolicy();
630 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530631
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700632 EventServiceManager::getInstance().updateSubscriptionData();
633 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500634 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700635 // The below privilege is wrong, it should be ConfigureManager OR
636 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500637 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700638 //.privileges(redfish::privileges::deleteEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700639 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700640 .methods(boost::beast::http::verb::delete_)(
641 [](const crow::Request&,
642 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
643 const std::string& param) {
644 if (!EventServiceManager::getInstance().isSubscriptionExist(
645 param))
646 {
647 asyncResp->res.result(
648 boost::beast::http::status::not_found);
649 return;
650 }
651 EventServiceManager::getInstance().deleteSubscription(param);
652 });
653}
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530654
655} // namespace redfish