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