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