xref: /openbmc/bmcweb/features/redfish/include/snmp_trap_event_clients.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
33d30708fSChicago Duan #pragma once
43d30708fSChicago Duan 
53d30708fSChicago Duan #include "async_resp.hpp"
63d30708fSChicago Duan #include "dbus_singleton.hpp"
73d30708fSChicago Duan #include "dbus_utility.hpp"
83d30708fSChicago Duan #include "error_messages.hpp"
93d30708fSChicago Duan #include "event_service_manager.hpp"
10539d8c6bSEd Tanous #include "generated/enums/event_destination.hpp"
113d30708fSChicago Duan #include "http_request.hpp"
123d30708fSChicago Duan #include "http_response.hpp"
133d30708fSChicago Duan #include "logging.hpp"
143d30708fSChicago Duan #include "utils/dbus_utils.hpp"
153d30708fSChicago Duan 
163d30708fSChicago Duan #include <boost/system/error_code.hpp>
173d30708fSChicago Duan #include <boost/url/format.hpp>
183d30708fSChicago Duan #include <sdbusplus/unpack_properties.hpp>
193d30708fSChicago Duan 
203d30708fSChicago Duan #include <memory>
213d30708fSChicago Duan #include <string>
223d30708fSChicago Duan #include <string_view>
233d30708fSChicago Duan 
243d30708fSChicago Duan namespace redfish
253d30708fSChicago Duan {
263d30708fSChicago Duan 
273d30708fSChicago Duan inline void afterGetSnmpTrapClientdata(
283d30708fSChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
293d30708fSChicago Duan     const boost::system::error_code& ec,
303d30708fSChicago Duan     const dbus::utility::DBusPropertiesMap& propertiesList)
313d30708fSChicago Duan {
323d30708fSChicago Duan     if (ec)
333d30708fSChicago Duan     {
3462598e31SEd Tanous         BMCWEB_LOG_ERROR("D-Bus response error on GetSubTree {}", ec);
353d30708fSChicago Duan         messages::internalError(asyncResp->res);
363d30708fSChicago Duan         return;
373d30708fSChicago Duan     }
383d30708fSChicago Duan 
393d30708fSChicago Duan     std::string address;
403d30708fSChicago Duan     uint16_t port = 0;
413d30708fSChicago Duan 
423d30708fSChicago Duan     bool success = sdbusplus::unpackPropertiesNoThrow(
433d30708fSChicago Duan         dbus_utils::UnpackErrorPrinter(), propertiesList, "Address", address,
443d30708fSChicago Duan         "Port", port);
453d30708fSChicago Duan 
463d30708fSChicago Duan     if (!success)
473d30708fSChicago Duan     {
483d30708fSChicago Duan         messages::internalError(asyncResp->res);
493d30708fSChicago Duan         return;
503d30708fSChicago Duan     }
513d30708fSChicago Duan 
523d30708fSChicago Duan     asyncResp->res.jsonValue["Destination"] =
533d30708fSChicago Duan         boost::urls::format("snmp://{}:{}", address, port);
543d30708fSChicago Duan }
553d30708fSChicago Duan 
563d30708fSChicago Duan inline void
573d30708fSChicago Duan     getSnmpTrapClientdata(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
583d30708fSChicago Duan                           const std::string& id, const std::string& objectPath)
593d30708fSChicago Duan {
603d30708fSChicago Duan     asyncResp->res.jsonValue["@odata.type"] =
613d30708fSChicago Duan         "#EventDestination.v1_8_0.EventDestination";
62539d8c6bSEd Tanous     asyncResp->res.jsonValue["Protocol"] =
63539d8c6bSEd Tanous         event_destination::EventDestinationProtocol::SNMPv2c;
643d30708fSChicago Duan     asyncResp->res.jsonValue["@odata.id"] =
653d30708fSChicago Duan         boost::urls::format("/redfish/v1/EventService/Subscriptions/{}", id);
663d30708fSChicago Duan 
673d30708fSChicago Duan     asyncResp->res.jsonValue["Id"] = id;
683d30708fSChicago Duan     asyncResp->res.jsonValue["Name"] = "Event Destination";
693d30708fSChicago Duan 
70539d8c6bSEd Tanous     asyncResp->res.jsonValue["SubscriptionType"] =
71539d8c6bSEd Tanous         event_destination::SubscriptionType::SNMPTrap;
72539d8c6bSEd Tanous     asyncResp->res.jsonValue["EventFormatType"] =
73539d8c6bSEd Tanous         event_destination::EventFormatType::Event;
743d30708fSChicago Duan 
75deae6a78SEd Tanous     dbus::utility::getAllProperties(
76deae6a78SEd Tanous         "xyz.openbmc_project.Network.SNMP", objectPath,
77deae6a78SEd Tanous         "xyz.openbmc_project.Network.Client",
783d30708fSChicago Duan         [asyncResp](const boost::system::error_code& ec,
793d30708fSChicago Duan                     const dbus::utility::DBusPropertiesMap& properties) {
803d30708fSChicago Duan             afterGetSnmpTrapClientdata(asyncResp, ec, properties);
813d30708fSChicago Duan         });
823d30708fSChicago Duan }
833d30708fSChicago Duan 
84bd79bce8SPatrick Williams inline void getSnmpTrapClient(
85bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
863d30708fSChicago Duan {
873d30708fSChicago Duan     crow::connections::systemBus->async_method_call(
883d30708fSChicago Duan         [asyncResp, id](const boost::system::error_code& ec,
893d30708fSChicago Duan                         dbus::utility::ManagedObjectType& resp) {
903d30708fSChicago Duan             if (ec)
913d30708fSChicago Duan             {
9262598e31SEd Tanous                 BMCWEB_LOG_ERROR("D-Bus response error on GetManagedObjects {}",
9362598e31SEd Tanous                                  ec);
943d30708fSChicago Duan                 messages::internalError(asyncResp->res);
953d30708fSChicago Duan                 return;
963d30708fSChicago Duan             }
973d30708fSChicago Duan 
983d30708fSChicago Duan             for (const auto& objpath : resp)
993d30708fSChicago Duan             {
1003d30708fSChicago Duan                 sdbusplus::message::object_path path(objpath.first);
1013d30708fSChicago Duan                 const std::string snmpId = path.filename();
1023d30708fSChicago Duan                 if (snmpId.empty())
1033d30708fSChicago Duan                 {
10462598e31SEd Tanous                     BMCWEB_LOG_ERROR("The SNMP client ID is wrong");
1053d30708fSChicago Duan                     messages::internalError(asyncResp->res);
1063d30708fSChicago Duan                     return;
1073d30708fSChicago Duan                 }
1083d30708fSChicago Duan                 const std::string subscriptionId = "snmp" + snmpId;
1093d30708fSChicago Duan                 if (id != subscriptionId)
1103d30708fSChicago Duan                 {
1113d30708fSChicago Duan                     continue;
1123d30708fSChicago Duan                 }
1133d30708fSChicago Duan 
1143d30708fSChicago Duan                 getSnmpTrapClientdata(asyncResp, id, objpath.first);
1153d30708fSChicago Duan                 return;
1163d30708fSChicago Duan             }
1173d30708fSChicago Duan 
1183d30708fSChicago Duan             messages::resourceNotFound(asyncResp->res, "Subscriptions", id);
1193d30708fSChicago Duan             EventServiceManager::getInstance().deleteSubscription(id);
1203d30708fSChicago Duan         },
1213d30708fSChicago Duan         "xyz.openbmc_project.Network.SNMP",
1223d30708fSChicago Duan         "/xyz/openbmc_project/network/snmp/manager",
1233d30708fSChicago Duan         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1243d30708fSChicago Duan }
1253d30708fSChicago Duan 
12663509dd5SRavi Teja inline void afterSnmpClientCreate(
12763509dd5SRavi Teja     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12863509dd5SRavi Teja     const boost::system::error_code& ec, const sdbusplus::message_t& msg,
12963509dd5SRavi Teja     const std::string& host, const std::string& dbusSNMPid)
1303d30708fSChicago Duan {
1313d30708fSChicago Duan     if (ec)
1323d30708fSChicago Duan     {
13363509dd5SRavi Teja         const sd_bus_error* dbusError = msg.get_error();
13463509dd5SRavi Teja         if (dbusError != nullptr)
13563509dd5SRavi Teja         {
13663509dd5SRavi Teja             if (std::string_view(
13763509dd5SRavi Teja                     "xyz.openbmc_project.Common.Error.InvalidArgument") ==
13863509dd5SRavi Teja                 dbusError->name)
13963509dd5SRavi Teja             {
14063509dd5SRavi Teja                 messages::propertyValueIncorrect(asyncResp->res, "Destination",
14163509dd5SRavi Teja                                                  host);
14263509dd5SRavi Teja                 return;
14363509dd5SRavi Teja             }
1443d30708fSChicago Duan             if (ec.value() != EBADR)
1453d30708fSChicago Duan             {
1463d30708fSChicago Duan                 // SNMP not installed
1473d30708fSChicago Duan                 messages::propertyValueOutOfRange(asyncResp->res, "SNMPv2c",
1483d30708fSChicago Duan                                                   "Protocol");
1493d30708fSChicago Duan                 return;
1503d30708fSChicago Duan             }
15163509dd5SRavi Teja         }
1523d30708fSChicago Duan         messages::internalError(asyncResp->res);
1533d30708fSChicago Duan         return;
1543d30708fSChicago Duan     }
1553d30708fSChicago Duan     sdbusplus::message::object_path path(dbusSNMPid);
1563d30708fSChicago Duan     const std::string snmpId = path.filename();
1573d30708fSChicago Duan     if (snmpId.empty())
1583d30708fSChicago Duan     {
1593d30708fSChicago Duan         messages::internalError(asyncResp->res);
1603d30708fSChicago Duan         return;
1613d30708fSChicago Duan     }
1623d30708fSChicago Duan 
1633d30708fSChicago Duan     std::string subscriptionId = "snmp" + snmpId;
1643d30708fSChicago Duan 
1653d30708fSChicago Duan     boost::urls::url uri = boost::urls::format(
1663d30708fSChicago Duan         "/redfish/v1/EventService/Subscriptions/{}", subscriptionId);
1673d30708fSChicago Duan     asyncResp->res.addHeader("Location", uri.buffer());
1683d30708fSChicago Duan     messages::created(asyncResp->res);
1693d30708fSChicago Duan }
1703d30708fSChicago Duan 
1713d30708fSChicago Duan inline void
1723d30708fSChicago Duan     addSnmpTrapClient(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1733d30708fSChicago Duan                       const std::string& host, uint16_t snmpTrapPort)
1743d30708fSChicago Duan {
1753d30708fSChicago Duan     crow::connections::systemBus->async_method_call(
176bd79bce8SPatrick Williams         [asyncResp,
177bd79bce8SPatrick Williams          host](const boost::system::error_code& ec,
178bd79bce8SPatrick Williams                const sdbusplus::message_t& msg, const std::string& dbusSNMPid) {
17963509dd5SRavi Teja             afterSnmpClientCreate(asyncResp, ec, msg, host, dbusSNMPid);
1803d30708fSChicago Duan         },
1813d30708fSChicago Duan         "xyz.openbmc_project.Network.SNMP",
1823d30708fSChicago Duan         "/xyz/openbmc_project/network/snmp/manager",
1833d30708fSChicago Duan         "xyz.openbmc_project.Network.Client.Create", "Client", host,
1843d30708fSChicago Duan         snmpTrapPort);
1853d30708fSChicago Duan }
1863d30708fSChicago Duan 
187bd79bce8SPatrick Williams inline void getSnmpSubscriptionList(
188bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
189bd79bce8SPatrick Williams     const std::string& snmpId, nlohmann::json& memberArray)
1903d30708fSChicago Duan {
1913d30708fSChicago Duan     const std::string subscriptionId = "snmp" + snmpId;
1923d30708fSChicago Duan 
1933d30708fSChicago Duan     nlohmann::json::object_t member;
1943d30708fSChicago Duan     member["@odata.id"] = boost::urls::format(
1953d30708fSChicago Duan         "/redfish/v1/EventService/Subscriptions/{}", subscriptionId);
1963d30708fSChicago Duan     memberArray.push_back(std::move(member));
1973d30708fSChicago Duan 
1983d30708fSChicago Duan     asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size();
1993d30708fSChicago Duan }
2003d30708fSChicago Duan 
2013d30708fSChicago Duan inline void
2023d30708fSChicago Duan     deleteSnmpTrapClient(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2033d30708fSChicago Duan                          const std::string& param)
2043d30708fSChicago Duan {
2053d30708fSChicago Duan     std::string_view snmpTrapId = param;
2063d30708fSChicago Duan 
2073d30708fSChicago Duan     // Erase "snmp" in the request to find the corresponding
2083d30708fSChicago Duan     // dbus snmp client id. For example, the snmpid in the
2093d30708fSChicago Duan     // request is "snmp1", which will be "1" after being erased.
2103d30708fSChicago Duan     snmpTrapId.remove_prefix(4);
2113d30708fSChicago Duan 
2123d30708fSChicago Duan     sdbusplus::message::object_path snmpPath =
2133d30708fSChicago Duan         sdbusplus::message::object_path(
2143d30708fSChicago Duan             "/xyz/openbmc_project/network/snmp/manager") /
2153d30708fSChicago Duan         std::string(snmpTrapId);
2163d30708fSChicago Duan 
2173d30708fSChicago Duan     crow::connections::systemBus->async_method_call(
2183d30708fSChicago Duan         [asyncResp, param](const boost::system::error_code& ec) {
2193d30708fSChicago Duan             if (ec)
2203d30708fSChicago Duan             {
2213d30708fSChicago Duan                 // The snmp trap id is incorrect
2223d30708fSChicago Duan                 if (ec.value() == EBADR)
2233d30708fSChicago Duan                 {
2243d30708fSChicago Duan                     messages::resourceNotFound(asyncResp->res, "Subscription",
2253d30708fSChicago Duan                                                param);
2263d30708fSChicago Duan                     return;
2273d30708fSChicago Duan                 }
2283d30708fSChicago Duan                 messages::internalError(asyncResp->res);
2293d30708fSChicago Duan                 return;
2303d30708fSChicago Duan             }
2313d30708fSChicago Duan             messages::success(asyncResp->res);
2323d30708fSChicago Duan         },
2333d30708fSChicago Duan         "xyz.openbmc_project.Network.SNMP", static_cast<std::string>(snmpPath),
2343d30708fSChicago Duan         "xyz.openbmc_project.Object.Delete", "Delete");
2353d30708fSChicago Duan }
2363d30708fSChicago Duan 
2373d30708fSChicago Duan } // namespace redfish
238