blob: aebc2824b07c599cb828fefcf179f7634cd66122 [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"
Chicago Duan3d307082020-11-26 14:12:12 +080023#include "snmp_trap_event_clients.hpp"
AppaRao Pulie5aaf042020-03-20 01:05:52 +053024
Ed Tanous601c71a2021-09-08 16:40:12 -070025#include <boost/beast/http/fields.hpp>
Chicago Duan3d307082020-11-26 14:12:12 +080026#include <boost/system/error_code.hpp>
Ed Tanousa716aa72023-08-01 11:35:53 -070027#include <boost/url/parse.hpp>
Chicago Duan3d307082020-11-26 14:12:12 +080028#include <sdbusplus/unpack_properties.hpp>
29#include <utils/dbus_utils.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070030
Chicago Duan3d307082020-11-26 14:12:12 +080031#include <charconv>
32#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070033#include <ranges>
Patrick Williams1e270c52021-12-04 06:06:56 -060034#include <span>
Chicago Duan3d307082020-11-26 14:12:12 +080035#include <string>
Patrick Williams1e270c52021-12-04 06:06:56 -060036
AppaRao Pulie5aaf042020-03-20 01:05:52 +053037namespace redfish
38{
39
AppaRao Puli156d6b02020-04-25 06:04:05 +053040static constexpr const std::array<const char*, 2> supportedEvtFormatTypes = {
41 eventFormatType, metricReportFormatType};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053042static constexpr const std::array<const char*, 3> supportedRegPrefixes = {
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +053043 "Base", "OpenBMC", "TaskEvent"};
AppaRao Pulie5aaf042020-03-20 01:05:52 +053044static constexpr const std::array<const char*, 3> supportedRetryPolicies = {
45 "TerminateAfterRetries", "SuspendRetries", "RetryForever"};
46
Sunitha Harishe56f2542020-07-22 02:38:59 -050047static constexpr const std::array<const char*, 1> supportedResourceTypes = {
48 "Task"};
Sunitha Harishe56f2542020-07-22 02:38:59 -050049
John Edward Broadbent7e860f12021-04-08 15:57:16 -070050inline void requestRoutesEventService(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +053051{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070052 BMCWEB_ROUTE(app, "/redfish/v1/EventService/")
Ed Tanoused398212021-06-09 17:05:54 -070053 .privileges(redfish::privileges::getEventService)
Ed Tanous002d39b2022-05-31 08:59:27 -070054 .methods(boost::beast::http::verb::get)(
55 [&app](const crow::Request& req,
56 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +000057 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -070058 {
59 return;
60 }
Ed Tanous14766872022-03-15 10:44:42 -070061
Ed Tanous002d39b2022-05-31 08:59:27 -070062 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/EventService";
63 asyncResp->res.jsonValue["@odata.type"] =
64 "#EventService.v1_5_0.EventService";
65 asyncResp->res.jsonValue["Id"] = "EventService";
66 asyncResp->res.jsonValue["Name"] = "Event Service";
AppaRao Puli5e44e3d2021-03-16 15:37:24 +000067 asyncResp->res.jsonValue["ServerSentEventUri"] =
68 "/redfish/v1/EventService/SSE";
69
Ed Tanous002d39b2022-05-31 08:59:27 -070070 asyncResp->res.jsonValue["Subscriptions"]["@odata.id"] =
71 "/redfish/v1/EventService/Subscriptions";
72 asyncResp->res
73 .jsonValue["Actions"]["#EventService.SubmitTestEvent"]["target"] =
74 "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent";
AppaRao Pulie5aaf042020-03-20 01:05:52 +053075
Ed Tanous002d39b2022-05-31 08:59:27 -070076 const persistent_data::EventServiceConfig eventServiceConfig =
77 persistent_data::EventServiceStore::getInstance()
78 .getEventServiceConfig();
zhanghch058d1b46d2021-04-01 11:18:24 +080079
Ed Tanous002d39b2022-05-31 08:59:27 -070080 asyncResp->res.jsonValue["Status"]["State"] =
81 (eventServiceConfig.enabled ? "Enabled" : "Disabled");
82 asyncResp->res.jsonValue["ServiceEnabled"] = eventServiceConfig.enabled;
83 asyncResp->res.jsonValue["DeliveryRetryAttempts"] =
84 eventServiceConfig.retryAttempts;
85 asyncResp->res.jsonValue["DeliveryRetryIntervalSeconds"] =
86 eventServiceConfig.retryTimeoutInterval;
87 asyncResp->res.jsonValue["EventFormatTypes"] = supportedEvtFormatTypes;
88 asyncResp->res.jsonValue["RegistryPrefixes"] = supportedRegPrefixes;
89 asyncResp->res.jsonValue["ResourceTypes"] = supportedResourceTypes;
AppaRao Pulie5aaf042020-03-20 01:05:52 +053090
Ed Tanous613dabe2022-07-09 11:17:36 -070091 nlohmann::json::object_t supportedSSEFilters;
92 supportedSSEFilters["EventFormatType"] = true;
93 supportedSSEFilters["MessageId"] = true;
94 supportedSSEFilters["MetricReportDefinition"] = true;
95 supportedSSEFilters["RegistryPrefix"] = true;
96 supportedSSEFilters["OriginResource"] = false;
97 supportedSSEFilters["ResourceType"] = false;
AppaRao Puli7d1cc382020-05-16 02:42:22 +053098
Ed Tanous002d39b2022-05-31 08:59:27 -070099 asyncResp->res.jsonValue["SSEFilterPropertiesSupported"] =
Ed Tanous613dabe2022-07-09 11:17:36 -0700100 std::move(supportedSSEFilters);
Patrick Williams5a39f772023-10-20 11:20:21 -0500101 });
Ayushi Smriti07941a82020-05-21 15:55:34 +0530102
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700103 BMCWEB_ROUTE(app, "/redfish/v1/EventService/")
Ed Tanoused398212021-06-09 17:05:54 -0700104 .privileges(redfish::privileges::patchEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700105 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700106 [&app](const crow::Request& req,
107 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000108 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700109 {
110 return;
111 }
112 std::optional<bool> serviceEnabled;
113 std::optional<uint32_t> retryAttemps;
114 std::optional<uint32_t> retryInterval;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530115
Ed Tanous002d39b2022-05-31 08:59:27 -0700116 if (!json_util::readJsonPatch(
117 req, asyncResp->res, "ServiceEnabled", serviceEnabled,
118 "DeliveryRetryAttempts", retryAttemps,
119 "DeliveryRetryIntervalSeconds", retryInterval))
120 {
121 return;
122 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530123
Ed Tanous002d39b2022-05-31 08:59:27 -0700124 persistent_data::EventServiceConfig eventServiceConfig =
125 persistent_data::EventServiceStore::getInstance()
126 .getEventServiceConfig();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700127
Ed Tanous002d39b2022-05-31 08:59:27 -0700128 if (serviceEnabled)
129 {
130 eventServiceConfig.enabled = *serviceEnabled;
131 }
Sunitha Harishe56f2542020-07-22 02:38:59 -0500132
Ed Tanous002d39b2022-05-31 08:59:27 -0700133 if (retryAttemps)
134 {
135 // Supported range [1-3]
136 if ((*retryAttemps < 1) || (*retryAttemps > 3))
137 {
138 messages::queryParameterOutOfRange(
139 asyncResp->res, std::to_string(*retryAttemps),
140 "DeliveryRetryAttempts", "[1-3]");
141 }
142 else
143 {
144 eventServiceConfig.retryAttempts = *retryAttemps;
145 }
146 }
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530147
Ed Tanous002d39b2022-05-31 08:59:27 -0700148 if (retryInterval)
149 {
Gunnar Mills33a32b32022-11-17 14:29:07 -0600150 // Supported range [5 - 180]
151 if ((*retryInterval < 5) || (*retryInterval > 180))
Ed Tanous002d39b2022-05-31 08:59:27 -0700152 {
153 messages::queryParameterOutOfRange(
154 asyncResp->res, std::to_string(*retryInterval),
Gunnar Mills33a32b32022-11-17 14:29:07 -0600155 "DeliveryRetryIntervalSeconds", "[5-180]");
Ed Tanous002d39b2022-05-31 08:59:27 -0700156 }
157 else
158 {
159 eventServiceConfig.retryTimeoutInterval = *retryInterval;
160 }
161 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700162
Ed Tanous002d39b2022-05-31 08:59:27 -0700163 EventServiceManager::getInstance().setEventServiceConfig(
164 eventServiceConfig);
Patrick Williams5a39f772023-10-20 11:20:21 -0500165 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700166}
167
168inline void requestRoutesSubmitTestEvent(App& app)
169{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170 BMCWEB_ROUTE(
171 app, "/redfish/v1/EventService/Actions/EventService.SubmitTestEvent/")
Ed Tanoused398212021-06-09 17:05:54 -0700172 .privileges(redfish::privileges::postEventService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700173 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700174 [&app](const crow::Request& req,
175 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000176 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700177 {
178 return;
179 }
180 if (!EventServiceManager::getInstance().sendTestEventLog())
181 {
182 messages::serviceDisabled(asyncResp->res,
183 "/redfish/v1/EventService/");
184 return;
185 }
186 asyncResp->res.result(boost::beast::http::status::no_content);
Patrick Williams5a39f772023-10-20 11:20:21 -0500187 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700188}
189
Chicago Duan3d307082020-11-26 14:12:12 +0800190inline void doSubscriptionCollection(
Ed Tanouse81de512023-06-27 17:07:00 -0700191 const boost::system::error_code& ec,
Chicago Duan3d307082020-11-26 14:12:12 +0800192 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
193 const dbus::utility::ManagedObjectType& resp)
194{
195 if (ec)
196 {
Ed Tanous13061012023-07-25 11:12:19 -0700197 if (ec.value() == EBADR || ec.value() == EHOSTUNREACH)
Chicago Duan3d307082020-11-26 14:12:12 +0800198 {
199 // This is an optional process so just return if it isn't there
200 return;
201 }
202
Ed Tanous62598e32023-07-17 17:06:25 -0700203 BMCWEB_LOG_ERROR("D-Bus response error on GetManagedObjects {}", ec);
Chicago Duan3d307082020-11-26 14:12:12 +0800204 messages::internalError(asyncResp->res);
205 return;
206 }
207 nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
208 for (const auto& objpath : resp)
209 {
210 sdbusplus::message::object_path path(objpath.first);
211 const std::string snmpId = path.filename();
212 if (snmpId.empty())
213 {
Ed Tanous62598e32023-07-17 17:06:25 -0700214 BMCWEB_LOG_ERROR("The SNMP client ID is wrong");
Chicago Duan3d307082020-11-26 14:12:12 +0800215 messages::internalError(asyncResp->res);
216 return;
217 }
218
219 getSnmpSubscriptionList(asyncResp, snmpId, memberArray);
220 }
221}
222
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700223inline void requestRoutesEventDestinationCollection(App& app)
224{
Gayathri Leburu1ebe3e42022-02-09 10:45:19 +0000225 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/")
Ed Tanoused398212021-06-09 17:05:54 -0700226 .privileges(redfish::privileges::getEventDestinationCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700227 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700228 [&app](const crow::Request& req,
229 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000230 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700231 {
232 return;
233 }
234 asyncResp->res.jsonValue["@odata.type"] =
235 "#EventDestinationCollection.EventDestinationCollection";
236 asyncResp->res.jsonValue["@odata.id"] =
237 "/redfish/v1/EventService/Subscriptions";
238 asyncResp->res.jsonValue["Name"] = "Event Destination Collections";
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700239
Ed Tanous002d39b2022-05-31 08:59:27 -0700240 nlohmann::json& memberArray = asyncResp->res.jsonValue["Members"];
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700241
Ed Tanous002d39b2022-05-31 08:59:27 -0700242 std::vector<std::string> subscripIds =
243 EventServiceManager::getInstance().getAllIDs();
244 memberArray = nlohmann::json::array();
245 asyncResp->res.jsonValue["Members@odata.count"] = subscripIds.size();
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700246
Ed Tanous002d39b2022-05-31 08:59:27 -0700247 for (const std::string& id : subscripIds)
248 {
249 nlohmann::json::object_t member;
Chicago Duan3d307082020-11-26 14:12:12 +0800250 member["@odata.id"] = boost::urls::format(
251 "/redfish/v1/EventService/Subscriptions/{}" + id);
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500252 memberArray.emplace_back(std::move(member));
Ed Tanous002d39b2022-05-31 08:59:27 -0700253 }
Chicago Duan3d307082020-11-26 14:12:12 +0800254 crow::connections::systemBus->async_method_call(
Ed Tanouse81de512023-06-27 17:07:00 -0700255 [asyncResp](const boost::system::error_code& ec,
Chicago Duan3d307082020-11-26 14:12:12 +0800256 const dbus::utility::ManagedObjectType& resp) {
257 doSubscriptionCollection(ec, asyncResp, resp);
Patrick Williams5a39f772023-10-20 11:20:21 -0500258 },
Chicago Duan3d307082020-11-26 14:12:12 +0800259 "xyz.openbmc_project.Network.SNMP",
260 "/xyz/openbmc_project/network/snmp/manager",
261 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Patrick Williams5a39f772023-10-20 11:20:21 -0500262 });
Chicago Duan3d307082020-11-26 14:12:12 +0800263
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700264 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/")
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500265 .privileges(redfish::privileges::postEventDestinationCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -0700266 .methods(boost::beast::http::verb::post)(
267 [&app](const crow::Request& req,
268 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000269 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700270 {
271 return;
272 }
273 if (EventServiceManager::getInstance().getNumberOfSubscriptions() >=
274 maxNoOfSubscriptions)
275 {
276 messages::eventSubscriptionLimitExceeded(asyncResp->res);
277 return;
278 }
279 std::string destUrl;
280 std::string protocol;
281 std::optional<std::string> context;
282 std::optional<std::string> subscriptionType;
283 std::optional<std::string> eventFormatType2;
284 std::optional<std::string> retryPolicy;
285 std::optional<std::vector<std::string>> msgIds;
286 std::optional<std::vector<std::string>> regPrefixes;
287 std::optional<std::vector<std::string>> resTypes;
Ed Tanous78d4ec42024-03-06 17:36:33 -0800288 std::optional<std::vector<nlohmann::json::object_t>> headers;
289 std::optional<std::vector<nlohmann::json::object_t>> mrdJsonArray;
Ed Tanousfffb8c12022-02-07 23:53:03 -0800290
Ed Tanous002d39b2022-05-31 08:59:27 -0700291 if (!json_util::readJsonPatch(
292 req, asyncResp->res, "Destination", destUrl, "Context", context,
293 "Protocol", protocol, "SubscriptionType", subscriptionType,
294 "EventFormatType", eventFormatType2, "HttpHeaders", headers,
295 "RegistryPrefixes", regPrefixes, "MessageIds", msgIds,
296 "DeliveryRetryPolicy", retryPolicy, "MetricReportDefinitions",
297 mrdJsonArray, "ResourceTypes", resTypes))
298 {
299 return;
300 }
301
AppaRao Puli600af5f2021-10-06 21:51:16 +0000302 // https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
303 static constexpr const uint16_t maxDestinationSize = 2000;
304 if (destUrl.size() > maxDestinationSize)
305 {
306 messages::stringValueTooLong(asyncResp->res, "Destination",
307 maxDestinationSize);
308 return;
309 }
310
Ed Tanous002d39b2022-05-31 08:59:27 -0700311 if (regPrefixes && msgIds)
312 {
313 if (!regPrefixes->empty() && !msgIds->empty())
Ed Tanousfffb8c12022-02-07 23:53:03 -0800314 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700315 messages::propertyValueConflict(asyncResp->res, "MessageIds",
316 "RegistryPrefixes");
Ed Tanousfffb8c12022-02-07 23:53:03 -0800317 return;
318 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700319 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800320
Ed Tanous6fd29552023-10-04 09:40:14 -0700321 boost::system::result<boost::urls::url> url =
Ed Tanousa716aa72023-08-01 11:35:53 -0700322 boost::urls::parse_absolute_uri(destUrl);
323 if (!url)
Ed Tanous002d39b2022-05-31 08:59:27 -0700324 {
Ed Tanous62598e32023-07-17 17:06:25 -0700325 BMCWEB_LOG_WARNING("Failed to validate and split destination url");
Ed Tanous002d39b2022-05-31 08:59:27 -0700326 messages::propertyValueFormatError(asyncResp->res, destUrl,
327 "Destination");
328 return;
329 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700330 url->normalize();
331 crow::utility::setProtocolDefaults(*url, protocol);
332 crow::utility::setPortDefaults(*url);
333
334 if (url->path().empty())
335 {
336 url->set_path("/");
337 }
338
339 if (url->has_userinfo())
340 {
341 messages::propertyValueFormatError(asyncResp->res, destUrl,
342 "Destination");
343 return;
344 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700345
Chicago Duan3d307082020-11-26 14:12:12 +0800346 if (protocol == "SNMPv2c")
347 {
348 if (context)
349 {
350 messages::propertyValueConflict(asyncResp->res, "Context",
351 "Protocol");
352 return;
353 }
354 if (eventFormatType2)
355 {
356 messages::propertyValueConflict(asyncResp->res,
357 "EventFormatType", "Protocol");
358 return;
359 }
360 if (retryPolicy)
361 {
362 messages::propertyValueConflict(asyncResp->res, "RetryPolicy",
363 "Protocol");
364 return;
365 }
366 if (msgIds)
367 {
368 messages::propertyValueConflict(asyncResp->res, "MessageIds",
369 "Protocol");
370 return;
371 }
372 if (regPrefixes)
373 {
374 messages::propertyValueConflict(asyncResp->res,
375 "RegistryPrefixes", "Protocol");
376 return;
377 }
378 if (resTypes)
379 {
380 messages::propertyValueConflict(asyncResp->res, "ResourceTypes",
381 "Protocol");
382 return;
383 }
384 if (headers)
385 {
386 messages::propertyValueConflict(asyncResp->res, "HttpHeaders",
387 "Protocol");
388 return;
389 }
390 if (mrdJsonArray)
391 {
392 messages::propertyValueConflict(
393 asyncResp->res, "MetricReportDefinitions", "Protocol");
394 return;
395 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700396 if (url->scheme() != "snmp")
397 {
398 messages::propertyValueConflict(asyncResp->res, "Destination",
399 "Protocol");
400 return;
401 }
Chicago Duan3d307082020-11-26 14:12:12 +0800402
Ed Tanousa716aa72023-08-01 11:35:53 -0700403 addSnmpTrapClient(asyncResp, url->host_address(),
404 url->port_number());
Chicago Duan3d307082020-11-26 14:12:12 +0800405 return;
406 }
407
Ed Tanousa716aa72023-08-01 11:35:53 -0700408 std::shared_ptr<Subscription> subValue =
409 std::make_shared<Subscription>(*url, app.ioContext());
Chicago Duan3d307082020-11-26 14:12:12 +0800410
Ed Tanousa716aa72023-08-01 11:35:53 -0700411 subValue->destinationUrl = std::move(*url);
Ed Tanous002d39b2022-05-31 08:59:27 -0700412
413 if (subscriptionType)
414 {
415 if (*subscriptionType != "RedfishEvent")
Ed Tanousfffb8c12022-02-07 23:53:03 -0800416 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700417 messages::propertyValueNotInList(
418 asyncResp->res, *subscriptionType, "SubscriptionType");
419 return;
420 }
421 subValue->subscriptionType = *subscriptionType;
422 }
423 else
424 {
425 subValue->subscriptionType = "RedfishEvent"; // Default
426 }
427
428 if (protocol != "Redfish")
429 {
430 messages::propertyValueNotInList(asyncResp->res, protocol,
431 "Protocol");
432 return;
433 }
434 subValue->protocol = protocol;
435
436 if (eventFormatType2)
437 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700438 if (std::ranges::find(supportedEvtFormatTypes, *eventFormatType2) ==
439 supportedEvtFormatTypes.end())
Ed Tanous002d39b2022-05-31 08:59:27 -0700440 {
441 messages::propertyValueNotInList(
442 asyncResp->res, *eventFormatType2, "EventFormatType");
443 return;
444 }
445 subValue->eventFormatType = *eventFormatType2;
446 }
447 else
448 {
449 // If not specified, use default "Event"
450 subValue->eventFormatType = "Event";
451 }
452
453 if (context)
454 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800455 // This value is selected arbitrarily.
AppaRao Puli600af5f2021-10-06 21:51:16 +0000456 constexpr const size_t maxContextSize = 256;
457 if (context->size() > maxContextSize)
458 {
459 messages::stringValueTooLong(asyncResp->res, "Context",
460 maxContextSize);
461 return;
462 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700463 subValue->customText = *context;
464 }
465
466 if (headers)
467 {
AppaRao Puli600af5f2021-10-06 21:51:16 +0000468 size_t cumulativeLen = 0;
469
Ed Tanous78d4ec42024-03-06 17:36:33 -0800470 for (const nlohmann::json::object_t& headerChunk : *headers)
Ed Tanous002d39b2022-05-31 08:59:27 -0700471 {
Ed Tanous78d4ec42024-03-06 17:36:33 -0800472 for (const auto& item : headerChunk)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700473 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700474 const std::string* value =
Ed Tanous78d4ec42024-03-06 17:36:33 -0800475 item.second.get_ptr<const std::string*>();
Ed Tanous002d39b2022-05-31 08:59:27 -0700476 if (value == nullptr)
477 {
478 messages::propertyValueFormatError(
Ed Tanous78d4ec42024-03-06 17:36:33 -0800479 asyncResp->res, item.second,
480 "HttpHeaders/" + item.first);
Ed Tanous002d39b2022-05-31 08:59:27 -0700481 return;
482 }
Ed Tanous78d4ec42024-03-06 17:36:33 -0800483 // Adding a new json value is the size of the key, +
484 // the size of the value + 2 * 2 quotes for each, +
485 // the colon and space between. example:
486 // "key": "value"
487 cumulativeLen += item.first.size() + value->size() + 6;
488 // This value is selected to mirror http_connection.hpp
489 constexpr const uint16_t maxHeaderSizeED = 8096;
490 if (cumulativeLen > maxHeaderSizeED)
491 {
492 messages::arraySizeTooLong(
493 asyncResp->res, "HttpHeaders", maxHeaderSizeED);
494 return;
495 }
496 subValue->httpHeaders.set(item.first, *value);
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 }
498 }
499 }
500
501 if (regPrefixes)
502 {
503 for (const std::string& it : *regPrefixes)
504 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700505 if (std::ranges::find(supportedRegPrefixes, it) ==
506 supportedRegPrefixes.end())
Ed Tanous002d39b2022-05-31 08:59:27 -0700507 {
508 messages::propertyValueNotInList(asyncResp->res, it,
509 "RegistryPrefixes");
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530510 return;
511 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800512 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700513 subValue->registryPrefixes = *regPrefixes;
514 }
P Dheeraj Srujan Kumarb304bd72021-01-29 17:46:35 +0530515
Ed Tanous002d39b2022-05-31 08:59:27 -0700516 if (resTypes)
517 {
518 for (const std::string& it : *resTypes)
Ed Tanousfffb8c12022-02-07 23:53:03 -0800519 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700520 if (std::ranges::find(supportedResourceTypes, it) ==
521 supportedResourceTypes.end())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700522 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700523 messages::propertyValueNotInList(asyncResp->res, it,
524 "ResourceTypes");
AppaRao Puli144b6312020-08-03 22:23:12 +0530525 return;
526 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700527 }
528 subValue->resourceTypes = *resTypes;
529 }
530
531 if (msgIds)
532 {
533 std::vector<std::string> registryPrefix;
534
535 // If no registry prefixes are mentioned, consider all
536 // supported prefixes
537 if (subValue->registryPrefixes.empty())
538 {
539 registryPrefix.assign(supportedRegPrefixes.begin(),
540 supportedRegPrefixes.end());
Ed Tanousfffb8c12022-02-07 23:53:03 -0800541 }
542 else
543 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700544 registryPrefix = subValue->registryPrefixes;
Ed Tanousfffb8c12022-02-07 23:53:03 -0800545 }
AppaRao Puli156d6b02020-04-25 06:04:05 +0530546
Ed Tanous002d39b2022-05-31 08:59:27 -0700547 for (const std::string& id : *msgIds)
Ed Tanousfffb8c12022-02-07 23:53:03 -0800548 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700549 bool validId = false;
Ed Tanousfffb8c12022-02-07 23:53:03 -0800550
Ed Tanous002d39b2022-05-31 08:59:27 -0700551 // Check for Message ID in each of the selected Registry
552 for (const std::string& it : registryPrefix)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700553 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700554 const std::span<const redfish::registries::MessageEntry>
555 registry =
556 redfish::registries::getRegistryFromPrefix(it);
557
Ed Tanous3544d2a2023-08-06 18:12:20 -0700558 if (std::ranges::any_of(
559 registry,
Ed Tanous002d39b2022-05-31 08:59:27 -0700560 [&id](const redfish::registries::MessageEntry&
561 messageEntry) {
562 return id == messageEntry.first;
Patrick Williams5a39f772023-10-20 11:20:21 -0500563 }))
Ed Tanous002d39b2022-05-31 08:59:27 -0700564 {
565 validId = true;
566 break;
567 }
568 }
569
570 if (!validId)
571 {
572 messages::propertyValueNotInList(asyncResp->res, id,
573 "MessageIds");
Ed Tanousfffb8c12022-02-07 23:53:03 -0800574 return;
575 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800576 }
577
Ed Tanous002d39b2022-05-31 08:59:27 -0700578 subValue->registryMsgIds = *msgIds;
579 }
580
581 if (retryPolicy)
582 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700583 if (std::ranges::find(supportedRetryPolicies, *retryPolicy) ==
584 supportedRetryPolicies.end())
Ed Tanousfffb8c12022-02-07 23:53:03 -0800585 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 messages::propertyValueNotInList(asyncResp->res, *retryPolicy,
587 "DeliveryRetryPolicy");
Ed Tanousfffb8c12022-02-07 23:53:03 -0800588 return;
589 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 subValue->retryPolicy = *retryPolicy;
591 }
592 else
593 {
594 // Default "TerminateAfterRetries"
595 subValue->retryPolicy = "TerminateAfterRetries";
596 }
Ed Tanousfffb8c12022-02-07 23:53:03 -0800597
Ed Tanous002d39b2022-05-31 08:59:27 -0700598 if (mrdJsonArray)
599 {
Ed Tanous78d4ec42024-03-06 17:36:33 -0800600 for (nlohmann::json::object_t& mrdObj : *mrdJsonArray)
Ed Tanous002d39b2022-05-31 08:59:27 -0700601 {
602 std::string mrdUri;
603
Ed Tanous78d4ec42024-03-06 17:36:33 -0800604 if (!json_util::readJsonObject(mrdObj, asyncResp->res,
605 "@odata.id", mrdUri))
Ed Tanous002d39b2022-05-31 08:59:27 -0700606
607 {
608 return;
609 }
610 subValue->metricReportDefinitions.emplace_back(mrdUri);
611 }
612 }
613
614 std::string id =
615 EventServiceManager::getInstance().addSubscription(subValue);
616 if (id.empty())
617 {
618 messages::internalError(asyncResp->res);
619 return;
620 }
621
622 messages::created(asyncResp->res);
623 asyncResp->res.addHeader(
624 "Location", "/redfish/v1/EventService/Subscriptions/" + id);
Patrick Williams5a39f772023-10-20 11:20:21 -0500625 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700626}
627
628inline void requestRoutesEventDestination(App& app)
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530629{
Ravi Teja9d41aec2021-07-23 01:57:01 -0500630 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700631 .privileges(redfish::privileges::getEventDestination)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700632 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700633 [&app](const crow::Request& req,
634 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
635 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000636 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 {
638 return;
639 }
Chicago Duan3d307082020-11-26 14:12:12 +0800640
641 if (param.starts_with("snmp"))
642 {
643 getSnmpTrapClient(asyncResp, param);
644 return;
645 }
646
Ed Tanous002d39b2022-05-31 08:59:27 -0700647 std::shared_ptr<Subscription> subValue =
648 EventServiceManager::getInstance().getSubscription(param);
649 if (subValue == nullptr)
650 {
651 asyncResp->res.result(boost::beast::http::status::not_found);
652 return;
653 }
654 const std::string& id = param;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530655
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 asyncResp->res.jsonValue["@odata.type"] =
Chicago Duan3d307082020-11-26 14:12:12 +0800657 "#EventDestination.v1_8_0.EventDestination";
Ed Tanous002d39b2022-05-31 08:59:27 -0700658 asyncResp->res.jsonValue["Protocol"] = "Redfish";
Ed Tanous3b327802023-08-14 09:23:43 -0700659 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
660 "/redfish/v1/EventService/Subscriptions/{}", id);
Ed Tanous002d39b2022-05-31 08:59:27 -0700661 asyncResp->res.jsonValue["Id"] = id;
662 asyncResp->res.jsonValue["Name"] = "Event Destination " + id;
663 asyncResp->res.jsonValue["Destination"] = subValue->destinationUrl;
664 asyncResp->res.jsonValue["Context"] = subValue->customText;
665 asyncResp->res.jsonValue["SubscriptionType"] =
666 subValue->subscriptionType;
667 asyncResp->res.jsonValue["HttpHeaders"] = nlohmann::json::array();
668 asyncResp->res.jsonValue["EventFormatType"] = subValue->eventFormatType;
669 asyncResp->res.jsonValue["RegistryPrefixes"] =
670 subValue->registryPrefixes;
671 asyncResp->res.jsonValue["ResourceTypes"] = subValue->resourceTypes;
zhanghch058d1b46d2021-04-01 11:18:24 +0800672
Ed Tanous002d39b2022-05-31 08:59:27 -0700673 asyncResp->res.jsonValue["MessageIds"] = subValue->registryMsgIds;
674 asyncResp->res.jsonValue["DeliveryRetryPolicy"] = subValue->retryPolicy;
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530675
Ed Tanous002d39b2022-05-31 08:59:27 -0700676 nlohmann::json::array_t mrdJsonArray;
677 for (const auto& mdrUri : subValue->metricReportDefinitions)
678 {
679 nlohmann::json::object_t mdr;
680 mdr["@odata.id"] = mdrUri;
681 mrdJsonArray.emplace_back(std::move(mdr));
682 }
683 asyncResp->res.jsonValue["MetricReportDefinitions"] = mrdJsonArray;
Patrick Williams5a39f772023-10-20 11:20:21 -0500684 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500685 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700686 // The below privilege is wrong, it should be ConfigureManager OR
687 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500688 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700689 //.privileges(redfish::privileges::patchEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700690 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700691 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700692 [&app](const crow::Request& req,
693 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
694 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000695 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700696 {
697 return;
698 }
699 std::shared_ptr<Subscription> subValue =
700 EventServiceManager::getInstance().getSubscription(param);
701 if (subValue == nullptr)
702 {
703 asyncResp->res.result(boost::beast::http::status::not_found);
704 return;
705 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530706
Ed Tanous002d39b2022-05-31 08:59:27 -0700707 std::optional<std::string> context;
708 std::optional<std::string> retryPolicy;
Ed Tanous78d4ec42024-03-06 17:36:33 -0800709 std::optional<std::vector<nlohmann::json::object_t>> headers;
Sunitha Harishe56f2542020-07-22 02:38:59 -0500710
Ed Tanous002d39b2022-05-31 08:59:27 -0700711 if (!json_util::readJsonPatch(req, asyncResp->res, "Context", context,
712 "DeliveryRetryPolicy", retryPolicy,
713 "HttpHeaders", headers))
714 {
715 return;
716 }
AppaRao Puli144b6312020-08-03 22:23:12 +0530717
Ed Tanous002d39b2022-05-31 08:59:27 -0700718 if (context)
719 {
720 subValue->customText = *context;
721 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530722
Ed Tanous002d39b2022-05-31 08:59:27 -0700723 if (headers)
724 {
725 boost::beast::http::fields fields;
Ed Tanous78d4ec42024-03-06 17:36:33 -0800726 for (const nlohmann::json::object_t& headerChunk : *headers)
Ed Tanous002d39b2022-05-31 08:59:27 -0700727 {
Ed Tanous78d4ec42024-03-06 17:36:33 -0800728 for (const auto& it : headerChunk)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700729 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700730 const std::string* value =
Ed Tanous78d4ec42024-03-06 17:36:33 -0800731 it.second.get_ptr<const std::string*>();
Ed Tanous002d39b2022-05-31 08:59:27 -0700732 if (value == nullptr)
Ed Tanous601c71a2021-09-08 16:40:12 -0700733 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700734 messages::propertyValueFormatError(
Ed Tanous78d4ec42024-03-06 17:36:33 -0800735 asyncResp->res, it.second,
736 "HttpHeaders/" + it.first);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700737 return;
738 }
Ed Tanous78d4ec42024-03-06 17:36:33 -0800739 fields.set(it.first, *value);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700740 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700741 }
Ed Tanous78d4ec42024-03-06 17:36:33 -0800742 subValue->httpHeaders = std::move(fields);
Ed Tanous002d39b2022-05-31 08:59:27 -0700743 }
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530744
Ed Tanous002d39b2022-05-31 08:59:27 -0700745 if (retryPolicy)
746 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700747 if (std::ranges::find(supportedRetryPolicies, *retryPolicy) ==
748 supportedRetryPolicies.end())
Ed Tanous002d39b2022-05-31 08:59:27 -0700749 {
750 messages::propertyValueNotInList(asyncResp->res, *retryPolicy,
751 "DeliveryRetryPolicy");
752 return;
753 }
754 subValue->retryPolicy = *retryPolicy;
Ed Tanous002d39b2022-05-31 08:59:27 -0700755 }
756
757 EventServiceManager::getInstance().updateSubscriptionData();
Patrick Williams5a39f772023-10-20 11:20:21 -0500758 });
Ravi Teja9d41aec2021-07-23 01:57:01 -0500759 BMCWEB_ROUTE(app, "/redfish/v1/EventService/Subscriptions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700760 // The below privilege is wrong, it should be ConfigureManager OR
761 // ConfigureSelf
Abhishek Patel7eeafa72021-07-28 10:59:16 -0500762 // https://github.com/openbmc/bmcweb/issues/220
Ed Tanoused398212021-06-09 17:05:54 -0700763 //.privileges(redfish::privileges::deleteEventDestination)
Ed Tanous432a8902021-06-14 15:28:56 -0700764 .privileges({{"ConfigureManager"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700765 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700766 [&app](const crow::Request& req,
767 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
768 const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +0000769 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -0700770 {
771 return;
772 }
Chicago Duan3d307082020-11-26 14:12:12 +0800773
774 if (param.starts_with("snmp"))
775 {
776 deleteSnmpTrapClient(asyncResp, param);
777 EventServiceManager::getInstance().deleteSubscription(param);
778 return;
779 }
780
Ed Tanous002d39b2022-05-31 08:59:27 -0700781 if (!EventServiceManager::getInstance().isSubscriptionExist(param))
782 {
783 asyncResp->res.result(boost::beast::http::status::not_found);
784 return;
785 }
786 EventServiceManager::getInstance().deleteSubscription(param);
Patrick Williams5a39f772023-10-20 11:20:21 -0500787 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700788}
AppaRao Pulie5aaf042020-03-20 01:05:52 +0530789
790} // namespace redfish