blob: 67ad014690b1d84b9a126cc386b9ecc534fa717d [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 Tanoused398212021-06-09 17:05:54 -070020#include <registries/privilege_registry.hpp>
21
AppaRao Pulie5aaf042020-03-20 01:05:52 +053022namespace redfish
23{
24
AppaRao Puli156d6b02020-04-25 06:04:05 +053025static constexpr const std::array<const char*, 2> supportedEvtFormatTypes = {
26 eventFormatType, metricReportFormatType};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053027static constexpr const std::array<const char*, 3> supportedRegPrefixes = {
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +053028 "Base", "OpenBMC", "TaskEvent"};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053029static constexpr const std::array<const char*, 3> supportedRetryPolicies = {
30 "TerminateAfterRetries", "SuspendRetries", "RetryForever"};
31
Sunitha Harishe56f2542020-07-22 02:38:59 -050032#ifdef BMCWEB_ENABLE_IBM_MANAGEMENT_CONSOLE
33static constexpr const std::array<const char*, 2> supportedResourceTypes = {
34 "IBMConfigFile", "Task"};
35#else
36static constexpr const std::array<const char*, 1> supportedResourceTypes = {
37 "Task"};
38#endif
39
AppaRao Pulie5aaf042020-03-20 01:05:52 +053040static constexpr const uint8_t maxNoOfSubscriptions = 20;
41
John Edward Broadbent7e860f12021-04-08 15:57:16 -070042inline void requestRoutesEventService(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +053043{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070044 BMCWEB_ROUTE(app, "/redfish/v1/EventService/")
Ed Tanoused398212021-06-09 17:05:54 -070045 .privileges(redfish::privileges::getEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070046 .methods(boost::beast::http::verb::get)(
47 [](const crow::Request&,
48 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
49 asyncResp->res.jsonValue = {
50 {"@odata.type", "#EventService.v1_5_0.EventService"},
51 {"Id", "EventService"},
52 {"Name", "Event Service"},
53 {"Subscriptions",
54 {{"@odata.id", "/redfish/v1/EventService/Subscriptions"}}},
55 {"Actions",
56 {{"#EventService.SubmitTestEvent",
57 {{"target", "/redfish/v1/EventService/Actions/"
58 "EventService.SubmitTestEvent"}}}}},
59 {"@odata.id", "/redfish/v1/EventService"}};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053060
JunLin Chen28afb492021-02-24 17:13:29 +080061 const persistent_data::EventServiceConfig eventServiceConfig =
62 persistent_data::EventServiceStore::getInstance()
63 .getEventServiceConfig();
zhanghch058d1b46d2021-04-01 11:18:24 +080064
John Edward Broadbent7e860f12021-04-08 15:57:16 -070065 asyncResp->res.jsonValue["Status"]["State"] =
JunLin Chen28afb492021-02-24 17:13:29 +080066 (eventServiceConfig.enabled ? "Enabled" : "Disabled");
67 asyncResp->res.jsonValue["ServiceEnabled"] =
68 eventServiceConfig.enabled;
John Edward Broadbent7e860f12021-04-08 15:57:16 -070069 asyncResp->res.jsonValue["DeliveryRetryAttempts"] =
JunLin Chen28afb492021-02-24 17:13:29 +080070 eventServiceConfig.retryAttempts;
John Edward Broadbent7e860f12021-04-08 15:57:16 -070071 asyncResp->res.jsonValue["DeliveryRetryIntervalSeconds"] =
JunLin Chen28afb492021-02-24 17:13:29 +080072 eventServiceConfig.retryTimeoutInterval;
John Edward Broadbent7e860f12021-04-08 15:57:16 -070073 asyncResp->res.jsonValue["EventFormatTypes"] =
74 supportedEvtFormatTypes;
75 asyncResp->res.jsonValue["RegistryPrefixes"] =
76 supportedRegPrefixes;
77 asyncResp->res.jsonValue["ResourceTypes"] =
78 supportedResourceTypes;
AppaRao Pulie5aaf042020-03-20 01:05:52 +053079
John Edward Broadbent7e860f12021-04-08 15:57:16 -070080 nlohmann::json supportedSSEFilters = {
81 {"EventFormatType", true}, {"MessageId", true},
82 {"MetricReportDefinition", true}, {"RegistryPrefix", true},
83 {"OriginResource", false}, {"ResourceType", false}};
AppaRao Puli7d1cc382020-05-16 02:42:22 +053084
John Edward Broadbent7e860f12021-04-08 15:57:16 -070085 asyncResp->res.jsonValue["SSEFilterPropertiesSupported"] =
86 supportedSSEFilters;
87 });
Ayushi Smriti07941a82020-05-21 15:55:34 +053088
John Edward Broadbent7e860f12021-04-08 15:57:16 -070089 BMCWEB_ROUTE(app, "/redfish/v1/EventService/")
Ed Tanoused398212021-06-09 17:05:54 -070090 .privileges(redfish::privileges::patchEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070091 .methods(boost::beast::http::verb::patch)(
92 [](const crow::Request& req,
93 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ayushi Smriti07941a82020-05-21 15:55:34 +053094
AppaRao Pulie5aaf042020-03-20 01:05:52 +053095 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -070096 std::optional<bool> serviceEnabled;
97 std::optional<uint32_t> retryAttemps;
98 std::optional<uint32_t> retryInterval;
AppaRao Pulie5aaf042020-03-20 01:05:52 +053099
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700100 if (!json_util::readJson(
101 req, asyncResp->res, "ServiceEnabled", serviceEnabled,
102 "DeliveryRetryAttempts", retryAttemps,
103 "DeliveryRetryIntervalSeconds", retryInterval))
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530104 {
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530105 return;
106 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530107
JunLin Chen28afb492021-02-24 17:13:29 +0800108 persistent_data::EventServiceConfig eventServiceConfig =
109 persistent_data::EventServiceStore::getInstance()
110 .getEventServiceConfig();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700111
112 if (serviceEnabled)
Sunitha Harishe56f2542020-07-22 02:38:59 -0500113 {
JunLin Chen28afb492021-02-24 17:13:29 +0800114 eventServiceConfig.enabled = *serviceEnabled;
Sunitha Harishe56f2542020-07-22 02:38:59 -0500115 }
Sunitha Harishe56f2542020-07-22 02:38:59 -0500116
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700117 if (retryAttemps)
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530118 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700119 // Supported range [1-3]
120 if ((*retryAttemps < 1) || (*retryAttemps > 3))
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530121 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700122 messages::queryParameterOutOfRange(
123 asyncResp->res, std::to_string(*retryAttemps),
124 "DeliveryRetryAttempts", "[1-3]");
125 }
126 else
127 {
JunLin Chen28afb492021-02-24 17:13:29 +0800128 eventServiceConfig.retryAttempts = *retryAttemps;
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530129 }
130 }
131
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700132 if (retryInterval)
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530133 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700134 // Supported range [30 - 180]
135 if ((*retryInterval < 30) || (*retryInterval > 180))
136 {
137 messages::queryParameterOutOfRange(
138 asyncResp->res, std::to_string(*retryInterval),
139 "DeliveryRetryIntervalSeconds", "[30-180]");
140 }
141 else
142 {
JunLin Chen28afb492021-02-24 17:13:29 +0800143 eventServiceConfig.retryTimeoutInterval =
144 *retryInterval;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700145 }
146 }
147
148 EventServiceManager::getInstance().setEventServiceConfig(
JunLin Chen28afb492021-02-24 17:13:29 +0800149 eventServiceConfig);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700150 });
151}
152
153inline void requestRoutesSubmitTestEvent(App& app)
154{
155
156 BMCWEB_ROUTE(
157 app, "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent/")
Ed Tanoused398212021-06-09 17:05:54 -0700158 .privileges(redfish::privileges::postEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700159 .methods(boost::beast::http::verb::post)(
160 [](const crow::Request&,
161 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
162 EventServiceManager::getInstance().sendTestEventLog();
163 asyncResp->res.result(boost::beast::http::status::no_content);
164 });
165}
166
167inline void requestRoutesEventDestinationCollection(App& app)
168{
169 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions")
Ed Tanoused398212021-06-09 17:05:54 -0700170 .privileges(redfish::privileges::getEventDestinationCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700171 .methods(boost::beast::http::verb::get)(
172 [](const crow::Request&,
173 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
174 asyncResp->res.jsonValue = {
175 {"@odata.type",
176 "#EventDestinationCollection.EventDestinationCollection"},
177 {"@odata.id", "/redfish/v1/EventService/Subscriptions"},
178 {"Name", "Event Destination Collections"}};
179
180 nlohmann::json& memberArray =
181 asyncResp->res.jsonValue["Members"];
182
183 std::vector<std::string> subscripIds =
184 EventServiceManager::getInstance().getAllIDs();
185 memberArray = nlohmann::json::array();
186 asyncResp->res.jsonValue["Members@odata.count"] =
187 subscripIds.size();
188
189 for (const std::string& id : subscripIds)
190 {
191 memberArray.push_back(
192 {{"@odata.id",
193 "/redfish/v1/EventService/Subscriptions/" + id}});
194 }
195 });
196 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/")
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500197 .privileges(redfish::privileges::postEventDestinationCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700198 .methods(boost::beast::http::verb::post)(
199 [](const crow::Request& req,
200 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
201 if (EventServiceManager::getInstance()
202 .getNumberOfSubscriptions() >= maxNoOfSubscriptions)
203 {
204 messages::eventSubscriptionLimitExceeded(asyncResp->res);
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530205 return;
206 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700207 std::string destUrl;
208 std::string protocol;
209 std::optional<std::string> context;
210 std::optional<std::string> subscriptionType;
211 std::optional<std::string> eventFormatType2;
212 std::optional<std::string> retryPolicy;
213 std::optional<std::vector<std::string>> msgIds;
214 std::optional<std::vector<std::string>> regPrefixes;
215 std::optional<std::vector<std::string>> resTypes;
216 std::optional<std::vector<nlohmann::json>> headers;
217 std::optional<std::vector<nlohmann::json>> mrdJsonArray;
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530218
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700219 if (!json_util::readJson(
220 req, asyncResp->res, "Destination", destUrl, "Context",
221 context, "Protocol", protocol, "SubscriptionType",
222 subscriptionType, "EventFormatType", eventFormatType2,
223 "HttpHeaders", headers, "RegistryPrefixes", regPrefixes,
224 "MessageIds", msgIds, "DeliveryRetryPolicy",
225 retryPolicy, "MetricReportDefinitions", mrdJsonArray,
226 "ResourceTypes", resTypes))
AppaRao Puli144b6312020-08-03 22:23:12 +0530227 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700228 return;
229 }
230
231 if (regPrefixes && msgIds)
232 {
233 if (regPrefixes->size() && msgIds->size())
234 {
235 messages::mutualExclusiveProperties(
236 asyncResp->res, "RegistryPrefixes", "MessageIds");
237 return;
238 }
239 }
240
241 // Validate the URL using regex expression
242 // Format: <protocol>://<host>:<port>/<uri>
243 // protocol: http/https
244 // host: Exclude ' ', ':', '#', '?'
245 // port: Empty or numeric value with ':' separator.
246 // uri: Start with '/' and Exclude '#', ' '
247 // Can include query params(ex: '/event?test=1')
248 // TODO: Need to validate hostname extensively(as per rfc)
249 const std::regex urlRegex(
250 "(http|https)://([^/\\x20\\x3f\\x23\\x3a]+):?([0-9]*)(/"
251 "([^\\x20\\x23\\x3f]*\\x3f?([^\\x20\\x23\\x3f])*)?)");
252 std::cmatch match;
253 if (!std::regex_match(destUrl.c_str(), match, urlRegex))
254 {
255 messages::propertyValueFormatError(asyncResp->res, destUrl,
256 "Destination");
257 return;
258 }
259
260 std::string uriProto =
261 std::string(match[1].first, match[1].second);
262 if (uriProto == "http")
263 {
264#ifndef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
265 messages::propertyValueFormatError(asyncResp->res, destUrl,
266 "Destination");
267 return;
268#endif
269 }
270
271 std::string host = std::string(match[2].first, match[2].second);
272 std::string port = std::string(match[3].first, match[3].second);
273 std::string path = std::string(match[4].first, match[4].second);
274 if (port.empty())
275 {
276 if (uriProto == "http")
277 {
278 port = "80";
279 }
280 else
281 {
282 port = "443";
283 }
284 }
285 if (path.empty())
286 {
287 path = "/";
288 }
289
290 std::shared_ptr<Subscription> subValue =
291 std::make_shared<Subscription>(host, port, path, uriProto);
292
293 subValue->destinationUrl = destUrl;
294
295 if (subscriptionType)
296 {
297 if (*subscriptionType != "RedfishEvent")
298 {
299 messages::propertyValueNotInList(asyncResp->res,
300 *subscriptionType,
301 "SubscriptionType");
302 return;
303 }
304 subValue->subscriptionType = *subscriptionType;
AppaRao Puli144b6312020-08-03 22:23:12 +0530305 }
306 else
307 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700308 subValue->subscriptionType = "RedfishEvent"; // Default
309 }
310
311 if (protocol != "Redfish")
312 {
313 messages::propertyValueNotInList(asyncResp->res, protocol,
314 "Protocol");
AppaRao Puli144b6312020-08-03 22:23:12 +0530315 return;
316 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700317 subValue->protocol = protocol;
AppaRao Puli156d6b02020-04-25 06:04:05 +0530318
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700319 if (eventFormatType2)
320 {
321 if (std::find(supportedEvtFormatTypes.begin(),
322 supportedEvtFormatTypes.end(),
323 *eventFormatType2) ==
324 supportedEvtFormatTypes.end())
325 {
326 messages::propertyValueNotInList(asyncResp->res,
327 *eventFormatType2,
328 "EventFormatType");
329 return;
330 }
331 subValue->eventFormatType = *eventFormatType2;
332 }
333 else
334 {
335 // If not specified, use default "Event"
336 subValue->eventFormatType = "Event";
337 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530338
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700339 if (context)
340 {
341 subValue->customText = *context;
342 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530343
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700344 if (headers)
345 {
346 subValue->httpHeaders = *headers;
347 }
348
349 if (regPrefixes)
350 {
351 for (const std::string& it : *regPrefixes)
352 {
353 if (std::find(supportedRegPrefixes.begin(),
354 supportedRegPrefixes.end(),
355 it) == supportedRegPrefixes.end())
356 {
357 messages::propertyValueNotInList(
358 asyncResp->res, it, "RegistryPrefixes");
359 return;
360 }
361 }
362 subValue->registryPrefixes = *regPrefixes;
363 }
364
365 if (resTypes)
366 {
367 for (const std::string& it : *resTypes)
368 {
369 if (std::find(supportedResourceTypes.begin(),
370 supportedResourceTypes.end(),
371 it) == supportedResourceTypes.end())
372 {
373 messages::propertyValueNotInList(asyncResp->res, it,
374 "ResourceTypes");
375 return;
376 }
377 }
378 subValue->resourceTypes = *resTypes;
379 }
380
381 if (msgIds)
382 {
383 std::vector<std::string> registryPrefix;
384
385 // If no registry prefixes are mentioned, consider all
386 // supported prefixes
387 if (subValue->registryPrefixes.empty())
388 {
389 registryPrefix.assign(supportedRegPrefixes.begin(),
390 supportedRegPrefixes.end());
391 }
392 else
393 {
394 registryPrefix = subValue->registryPrefixes;
395 }
396
397 for (const std::string& id : *msgIds)
398 {
399 bool validId = false;
400
401 // Check for Message ID in each of the selected Registry
402 for (const std::string& it : registryPrefix)
403 {
404 const boost::beast::span<
405 const redfish::message_registries::MessageEntry>
406 registry = redfish::message_registries::
407 getRegistryFromPrefix(it);
408
409 if (std::any_of(
410 registry.cbegin(), registry.cend(),
411 [&id](const redfish::message_registries::
412 MessageEntry& messageEntry) {
413 return !id.compare(messageEntry.first);
414 }))
415 {
416 validId = true;
417 break;
418 }
419 }
420
421 if (!validId)
422 {
423 messages::propertyValueNotInList(asyncResp->res, id,
424 "MessageIds");
425 return;
426 }
427 }
428
429 subValue->registryMsgIds = *msgIds;
430 }
431
432 if (retryPolicy)
433 {
434 if (std::find(supportedRetryPolicies.begin(),
435 supportedRetryPolicies.end(),
436 *retryPolicy) == supportedRetryPolicies.end())
437 {
438 messages::propertyValueNotInList(asyncResp->res,
439 *retryPolicy,
440 "DeliveryRetryPolicy");
441 return;
442 }
443 subValue->retryPolicy = *retryPolicy;
444 }
445 else
446 {
447 // Default "TerminateAfterRetries"
448 subValue->retryPolicy = "TerminateAfterRetries";
449 }
450
451 if (mrdJsonArray)
452 {
453 for (nlohmann::json& mrdObj : *mrdJsonArray)
454 {
455 std::string mrdUri;
456 if (json_util::getValueFromJsonObject(
457 mrdObj, "@odata.id", mrdUri))
458 {
459 subValue->metricReportDefinitions.emplace_back(
460 mrdUri);
461 }
462 else
463 {
464 messages::propertyValueFormatError(
465 asyncResp->res,
466 mrdObj.dump(
467 2, ' ', true,
468 nlohmann::json::error_handler_t::replace),
469 "MetricReportDefinitions");
470 return;
471 }
472 }
473 }
474
475 std::string id =
476 EventServiceManager::getInstance().addSubscription(
477 subValue);
478 if (id.empty())
479 {
480 messages::internalError(asyncResp->res);
481 return;
482 }
483
484 messages::created(asyncResp->res);
485 asyncResp->res.addHeader(
486 "Location", "/redfish/v1/EventService/Subscriptions/" + id);
487 });
488}
489
490inline void requestRoutesEventDestination(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530491{
Ravi Teja9d41aec2021-07-23 01:57:01 -0500492 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700493 .privileges(redfish::privileges::getEventDestination)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700494 .methods(boost::beast::http::verb::get)(
495 [](const crow::Request&,
496 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
497 const std::string& param) {
498 std::shared_ptr<Subscription> subValue =
499 EventServiceManager::getInstance().getSubscription(param);
500 if (subValue == nullptr)
501 {
502 asyncResp->res.result(
503 boost::beast::http::status::not_found);
504 return;
505 }
506 const std::string& id = param;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530507
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700508 asyncResp->res.jsonValue = {
509 {"@odata.type",
510 "#EventDestination.v1_7_0.EventDestination"},
511 {"Protocol", "Redfish"}};
512 asyncResp->res.jsonValue["@odata.id"] =
513 "/redfish/v1/EventService/Subscriptions/" + id;
514 asyncResp->res.jsonValue["Id"] = id;
515 asyncResp->res.jsonValue["Name"] = "Event Destination " + id;
516 asyncResp->res.jsonValue["Destination"] =
517 subValue->destinationUrl;
518 asyncResp->res.jsonValue["Context"] = subValue->customText;
519 asyncResp->res.jsonValue["SubscriptionType"] =
520 subValue->subscriptionType;
521 asyncResp->res.jsonValue["HttpHeaders"] = subValue->httpHeaders;
522 asyncResp->res.jsonValue["EventFormatType"] =
523 subValue->eventFormatType;
524 asyncResp->res.jsonValue["RegistryPrefixes"] =
525 subValue->registryPrefixes;
526 asyncResp->res.jsonValue["ResourceTypes"] =
527 subValue->resourceTypes;
zhanghch058d1b46d2021-04-01 11:18:24 +0800528
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700529 asyncResp->res.jsonValue["MessageIds"] =
530 subValue->registryMsgIds;
531 asyncResp->res.jsonValue["DeliveryRetryPolicy"] =
532 subValue->retryPolicy;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530533
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700534 std::vector<nlohmann::json> mrdJsonArray;
535 for (const auto& mdrUri : subValue->metricReportDefinitions)
536 {
537 mrdJsonArray.push_back({{"@odata.id", mdrUri}});
538 }
539 asyncResp->res.jsonValue["MetricReportDefinitions"] =
540 mrdJsonArray;
541 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500542 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700543 // The below privilege is wrong, it should be ConfigureManager OR
544 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500545 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700546 //.privileges(redfish::privileges::patchEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700547 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700548 .methods(boost::beast::http::verb::patch)(
549 [](const crow::Request& req,
550 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
551 const std::string& param) {
552 std::shared_ptr<Subscription> subValue =
553 EventServiceManager::getInstance().getSubscription(param);
554 if (subValue == nullptr)
555 {
556 asyncResp->res.result(
557 boost::beast::http::status::not_found);
558 return;
559 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530560
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700561 std::optional<std::string> context;
562 std::optional<std::string> retryPolicy;
563 std::optional<std::vector<nlohmann::json>> headers;
Sunitha Harishe56f2542020-07-22 02:38:59 -0500564
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700565 if (!json_util::readJson(req, asyncResp->res, "Context",
566 context, "DeliveryRetryPolicy",
567 retryPolicy, "HttpHeaders", headers))
568 {
569 return;
570 }
AppaRao Puli144b6312020-08-03 22:23:12 +0530571
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700572 if (context)
573 {
574 subValue->customText = *context;
575 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530576
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700577 if (headers)
578 {
579 subValue->httpHeaders = *headers;
580 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800581
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700582 if (retryPolicy)
583 {
584 if (std::find(supportedRetryPolicies.begin(),
585 supportedRetryPolicies.end(),
586 *retryPolicy) == supportedRetryPolicies.end())
587 {
588 messages::propertyValueNotInList(asyncResp->res,
589 *retryPolicy,
590 "DeliveryRetryPolicy");
591 return;
592 }
593 subValue->retryPolicy = *retryPolicy;
594 subValue->updateRetryPolicy();
595 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530596
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700597 EventServiceManager::getInstance().updateSubscriptionData();
598 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500599 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700600 // The below privilege is wrong, it should be ConfigureManager OR
601 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500602 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700603 //.privileges(redfish::privileges::deleteEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700604 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700605 .methods(boost::beast::http::verb::delete_)(
606 [](const crow::Request&,
607 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
608 const std::string& param) {
609 if (!EventServiceManager::getInstance().isSubscriptionExist(
610 param))
611 {
612 asyncResp->res.result(
613 boost::beast::http::status::not_found);
614 return;
615 }
616 EventServiceManager::getInstance().deleteSubscription(param);
617 });
618}
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530619
620} // namespace redfish