xref: /openbmc/bmcweb/redfish-core/include/snmp_trap_event_clients.hpp (revision a08c3de2e9a788addf7c71fc2db1912e39a85d3b)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "async_resp.hpp"
6 #include "dbus_utility.hpp"
7 #include "error_messages.hpp"
8 #include "event_service_manager.hpp"
9 #include "generated/enums/event_destination.hpp"
10 #include "http_response.hpp"
11 #include "logging.hpp"
12 #include "utils/dbus_utils.hpp"
13 
14 #include <asm-generic/errno.h>
15 #include <systemd/sd-bus.h>
16 
17 #include <boost/system/error_code.hpp>
18 #include <boost/url/format.hpp>
19 #include <boost/url/url.hpp>
20 #include <sdbusplus/message.hpp>
21 #include <sdbusplus/message/native_types.hpp>
22 #include <sdbusplus/unpack_properties.hpp>
23 
24 #include <cstdint>
25 #include <memory>
26 #include <string>
27 #include <string_view>
28 #include <utility>
29 
30 namespace redfish
31 {
32 
33 inline void afterGetSnmpTrapClientdata(
34     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35     const boost::system::error_code& ec,
36     const dbus::utility::DBusPropertiesMap& propertiesList)
37 {
38     if (ec)
39     {
40         BMCWEB_LOG_ERROR("D-Bus response error on GetSubTree {}", ec);
41         messages::internalError(asyncResp->res);
42         return;
43     }
44 
45     std::string address;
46     uint16_t port = 0;
47 
48     bool success = sdbusplus::unpackPropertiesNoThrow(
49         dbus_utils::UnpackErrorPrinter(), propertiesList, "Address", address,
50         "Port", port);
51 
52     if (!success)
53     {
54         messages::internalError(asyncResp->res);
55         return;
56     }
57 
58     asyncResp->res.jsonValue["Destination"] =
59         boost::urls::format("snmp://{}:{}", address, port);
60 }
61 
62 inline void getSnmpTrapClientdata(
63     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id,
64     const std::string& objectPath)
65 {
66     asyncResp->res.jsonValue["@odata.type"] =
67         "#EventDestination.v1_8_0.EventDestination";
68     asyncResp->res.jsonValue["Protocol"] =
69         event_destination::EventDestinationProtocol::SNMPv2c;
70     asyncResp->res.jsonValue["@odata.id"] =
71         boost::urls::format("/redfish/v1/EventService/Subscriptions/{}", id);
72 
73     asyncResp->res.jsonValue["Id"] = id;
74     asyncResp->res.jsonValue["Name"] = "Event Destination";
75 
76     asyncResp->res.jsonValue["SubscriptionType"] =
77         event_destination::SubscriptionType::SNMPTrap;
78     asyncResp->res.jsonValue["EventFormatType"] =
79         event_destination::EventFormatType::Event;
80 
81     dbus::utility::getAllProperties(
82         "xyz.openbmc_project.Network.SNMP", objectPath,
83         "xyz.openbmc_project.Network.Client",
84         [asyncResp](const boost::system::error_code& ec,
85                     const dbus::utility::DBusPropertiesMap& properties) {
86             afterGetSnmpTrapClientdata(asyncResp, ec, properties);
87         });
88 }
89 
90 inline void getSnmpTrapClient(
91     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
92 {
93     dbus::utility::async_method_call(
94         asyncResp,
95         [asyncResp, id](const boost::system::error_code& ec,
96                         dbus::utility::ManagedObjectType& resp) {
97             if (ec)
98             {
99                 BMCWEB_LOG_ERROR("D-Bus response error on GetManagedObjects {}",
100                                  ec);
101                 messages::internalError(asyncResp->res);
102                 return;
103             }
104 
105             for (const auto& objpath : resp)
106             {
107                 sdbusplus::message::object_path path(objpath.first);
108                 const std::string snmpId = path.filename();
109                 if (snmpId.empty())
110                 {
111                     BMCWEB_LOG_ERROR("The SNMP client ID is wrong");
112                     messages::internalError(asyncResp->res);
113                     return;
114                 }
115                 const std::string subscriptionId = "snmp" + snmpId;
116                 if (id != subscriptionId)
117                 {
118                     continue;
119                 }
120 
121                 getSnmpTrapClientdata(asyncResp, id, objpath.first);
122                 return;
123             }
124 
125             messages::resourceNotFound(asyncResp->res, "Subscriptions", id);
126             EventServiceManager::getInstance().deleteSubscription(id);
127         },
128         "xyz.openbmc_project.Network.SNMP",
129         "/xyz/openbmc_project/network/snmp/manager",
130         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
131 }
132 
133 inline void afterSnmpClientCreate(
134     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
135     const boost::system::error_code& ec, const sdbusplus::message_t& msg,
136     const std::string& host, const std::string& dbusSNMPid)
137 {
138     if (ec)
139     {
140         const sd_bus_error* dbusError = msg.get_error();
141         if (dbusError != nullptr)
142         {
143             if (std::string_view(
144                     "xyz.openbmc_project.Common.Error.InvalidArgument") ==
145                 dbusError->name)
146             {
147                 messages::propertyValueIncorrect(asyncResp->res, "Destination",
148                                                  host);
149                 return;
150             }
151             if (ec.value() != EBADR)
152             {
153                 // SNMP not installed
154                 messages::propertyValueOutOfRange(asyncResp->res, "SNMPv2c",
155                                                   "Protocol");
156                 return;
157             }
158         }
159         messages::internalError(asyncResp->res);
160         return;
161     }
162     sdbusplus::message::object_path path(dbusSNMPid);
163     const std::string snmpId = path.filename();
164     if (snmpId.empty())
165     {
166         messages::internalError(asyncResp->res);
167         return;
168     }
169 
170     std::string subscriptionId = "snmp" + snmpId;
171 
172     boost::urls::url uri = boost::urls::format(
173         "/redfish/v1/EventService/Subscriptions/{}", subscriptionId);
174     asyncResp->res.addHeader("Location", uri.buffer());
175     messages::created(asyncResp->res);
176 }
177 
178 inline void addSnmpTrapClient(
179     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
180     const std::string& host, uint16_t snmpTrapPort)
181 {
182     dbus::utility::async_method_call(
183         asyncResp,
184         [asyncResp,
185          host](const boost::system::error_code& ec,
186                const sdbusplus::message_t& msg, const std::string& dbusSNMPid) {
187             afterSnmpClientCreate(asyncResp, ec, msg, host, dbusSNMPid);
188         },
189         "xyz.openbmc_project.Network.SNMP",
190         "/xyz/openbmc_project/network/snmp/manager",
191         "xyz.openbmc_project.Network.Client.Create", "Client", host,
192         snmpTrapPort);
193 }
194 
195 inline void getSnmpSubscriptionList(
196     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
197     const std::string& snmpId, nlohmann::json& memberArray)
198 {
199     const std::string subscriptionId = "snmp" + snmpId;
200 
201     nlohmann::json::object_t member;
202     member["@odata.id"] = boost::urls::format(
203         "/redfish/v1/EventService/Subscriptions/{}", subscriptionId);
204     memberArray.push_back(std::move(member));
205 
206     asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
207 }
208 
209 inline void deleteSnmpTrapClient(
210     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
211     const std::string& param)
212 {
213     std::string_view snmpTrapId = param;
214 
215     // Erase "snmp" in the request to find the corresponding
216     // dbus snmp client id. For example, the snmpid in the
217     // request is "snmp1", which will be "1" after being erased.
218     snmpTrapId.remove_prefix(4);
219 
220     sdbusplus::message::object_path snmpPath =
221         sdbusplus::message::object_path(
222             "/xyz/openbmc_project/network/snmp/manager") /
223         std::string(snmpTrapId);
224 
225     dbus::utility::async_method_call(
226         asyncResp,
227         [asyncResp, param](const boost::system::error_code& ec) {
228             if (ec)
229             {
230                 // The snmp trap id is incorrect
231                 if (ec.value() == EBADR)
232                 {
233                     messages::resourceNotFound(asyncResp->res, "Subscription",
234                                                param);
235                     return;
236                 }
237                 messages::internalError(asyncResp->res);
238                 return;
239             }
240             messages::success(asyncResp->res);
241         },
242         "xyz.openbmc_project.Network.SNMP", static_cast<std::string>(snmpPath),
243         "xyz.openbmc_project.Object.Delete", "Delete");
244 }
245 
246 } // namespace redfish
247