13d30708fSChicago Duan #pragma once 23d30708fSChicago Duan 33d30708fSChicago Duan #include "async_resp.hpp" 43d30708fSChicago Duan #include "dbus_singleton.hpp" 53d30708fSChicago Duan #include "dbus_utility.hpp" 63d30708fSChicago Duan #include "error_messages.hpp" 73d30708fSChicago Duan #include "event_service_manager.hpp" 8*539d8c6bSEd Tanous #include "generated/enums/event_destination.hpp" 93d30708fSChicago Duan #include "http_request.hpp" 103d30708fSChicago Duan #include "http_response.hpp" 113d30708fSChicago Duan #include "logging.hpp" 123d30708fSChicago Duan #include "utils/dbus_utils.hpp" 133d30708fSChicago Duan 143d30708fSChicago Duan #include <boost/system/error_code.hpp> 153d30708fSChicago Duan #include <boost/url/format.hpp> 163d30708fSChicago Duan #include <sdbusplus/unpack_properties.hpp> 173d30708fSChicago Duan 183d30708fSChicago Duan #include <memory> 193d30708fSChicago Duan #include <string> 203d30708fSChicago Duan #include <string_view> 213d30708fSChicago Duan 223d30708fSChicago Duan namespace redfish 233d30708fSChicago Duan { 243d30708fSChicago Duan 253d30708fSChicago Duan inline void afterGetSnmpTrapClientdata( 263d30708fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 273d30708fSChicago Duan const boost::system::error_code& ec, 283d30708fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList) 293d30708fSChicago Duan { 303d30708fSChicago Duan if (ec) 313d30708fSChicago Duan { 3262598e31SEd Tanous BMCWEB_LOG_ERROR("D-Bus response error on GetSubTree {}", ec); 333d30708fSChicago Duan messages::internalError(asyncResp->res); 343d30708fSChicago Duan return; 353d30708fSChicago Duan } 363d30708fSChicago Duan 373d30708fSChicago Duan std::string address; 383d30708fSChicago Duan uint16_t port = 0; 393d30708fSChicago Duan 403d30708fSChicago Duan bool success = sdbusplus::unpackPropertiesNoThrow( 413d30708fSChicago Duan dbus_utils::UnpackErrorPrinter(), propertiesList, "Address", address, 423d30708fSChicago Duan "Port", port); 433d30708fSChicago Duan 443d30708fSChicago Duan if (!success) 453d30708fSChicago Duan { 463d30708fSChicago Duan messages::internalError(asyncResp->res); 473d30708fSChicago Duan return; 483d30708fSChicago Duan } 493d30708fSChicago Duan 503d30708fSChicago Duan asyncResp->res.jsonValue["Destination"] = 513d30708fSChicago Duan boost::urls::format("snmp://{}:{}", address, port); 523d30708fSChicago Duan } 533d30708fSChicago Duan 543d30708fSChicago Duan inline void 553d30708fSChicago Duan getSnmpTrapClientdata(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 563d30708fSChicago Duan const std::string& id, const std::string& objectPath) 573d30708fSChicago Duan { 583d30708fSChicago Duan asyncResp->res.jsonValue["@odata.type"] = 593d30708fSChicago Duan "#EventDestination.v1_8_0.EventDestination"; 60*539d8c6bSEd Tanous asyncResp->res.jsonValue["Protocol"] = 61*539d8c6bSEd Tanous event_destination::EventDestinationProtocol::SNMPv2c; 623d30708fSChicago Duan asyncResp->res.jsonValue["@odata.id"] = 633d30708fSChicago Duan boost::urls::format("/redfish/v1/EventService/Subscriptions/{}", id); 643d30708fSChicago Duan 653d30708fSChicago Duan asyncResp->res.jsonValue["Id"] = id; 663d30708fSChicago Duan asyncResp->res.jsonValue["Name"] = "Event Destination"; 673d30708fSChicago Duan 68*539d8c6bSEd Tanous asyncResp->res.jsonValue["SubscriptionType"] = 69*539d8c6bSEd Tanous event_destination::SubscriptionType::SNMPTrap; 70*539d8c6bSEd Tanous asyncResp->res.jsonValue["EventFormatType"] = 71*539d8c6bSEd Tanous event_destination::EventFormatType::Event; 723d30708fSChicago Duan 733d30708fSChicago Duan std::shared_ptr<Subscription> subValue = 743d30708fSChicago Duan EventServiceManager::getInstance().getSubscription(id); 753d30708fSChicago Duan if (subValue != nullptr) 763d30708fSChicago Duan { 773d30708fSChicago Duan asyncResp->res.jsonValue["Context"] = subValue->customText; 783d30708fSChicago Duan } 793d30708fSChicago Duan else 803d30708fSChicago Duan { 813d30708fSChicago Duan asyncResp->res.jsonValue["Context"] = ""; 823d30708fSChicago Duan } 833d30708fSChicago Duan 843d30708fSChicago Duan sdbusplus::asio::getAllProperties( 853d30708fSChicago Duan *crow::connections::systemBus, "xyz.openbmc_project.Network.SNMP", 863d30708fSChicago Duan objectPath, "xyz.openbmc_project.Network.Client", 873d30708fSChicago Duan [asyncResp](const boost::system::error_code& ec, 883d30708fSChicago Duan const dbus::utility::DBusPropertiesMap& properties) { 893d30708fSChicago Duan afterGetSnmpTrapClientdata(asyncResp, ec, properties); 903d30708fSChicago Duan }); 913d30708fSChicago Duan } 923d30708fSChicago Duan 933d30708fSChicago Duan inline void 943d30708fSChicago Duan getSnmpTrapClient(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 953d30708fSChicago Duan const std::string& id) 963d30708fSChicago Duan { 973d30708fSChicago Duan crow::connections::systemBus->async_method_call( 983d30708fSChicago Duan [asyncResp, id](const boost::system::error_code& ec, 993d30708fSChicago Duan dbus::utility::ManagedObjectType& resp) { 1003d30708fSChicago Duan if (ec) 1013d30708fSChicago Duan { 10262598e31SEd Tanous BMCWEB_LOG_ERROR("D-Bus response error on GetManagedObjects {}", 10362598e31SEd Tanous ec); 1043d30708fSChicago Duan messages::internalError(asyncResp->res); 1053d30708fSChicago Duan return; 1063d30708fSChicago Duan } 1073d30708fSChicago Duan 1083d30708fSChicago Duan for (const auto& objpath : resp) 1093d30708fSChicago Duan { 1103d30708fSChicago Duan sdbusplus::message::object_path path(objpath.first); 1113d30708fSChicago Duan const std::string snmpId = path.filename(); 1123d30708fSChicago Duan if (snmpId.empty()) 1133d30708fSChicago Duan { 11462598e31SEd Tanous BMCWEB_LOG_ERROR("The SNMP client ID is wrong"); 1153d30708fSChicago Duan messages::internalError(asyncResp->res); 1163d30708fSChicago Duan return; 1173d30708fSChicago Duan } 1183d30708fSChicago Duan const std::string subscriptionId = "snmp" + snmpId; 1193d30708fSChicago Duan if (id != subscriptionId) 1203d30708fSChicago Duan { 1213d30708fSChicago Duan continue; 1223d30708fSChicago Duan } 1233d30708fSChicago Duan 1243d30708fSChicago Duan getSnmpTrapClientdata(asyncResp, id, objpath.first); 1253d30708fSChicago Duan return; 1263d30708fSChicago Duan } 1273d30708fSChicago Duan 1283d30708fSChicago Duan messages::resourceNotFound(asyncResp->res, "Subscriptions", id); 1293d30708fSChicago Duan EventServiceManager::getInstance().deleteSubscription(id); 1303d30708fSChicago Duan }, 1313d30708fSChicago Duan "xyz.openbmc_project.Network.SNMP", 1323d30708fSChicago Duan "/xyz/openbmc_project/network/snmp/manager", 1333d30708fSChicago Duan "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 1343d30708fSChicago Duan } 1353d30708fSChicago Duan 13663509dd5SRavi Teja inline void afterSnmpClientCreate( 13763509dd5SRavi Teja const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 13863509dd5SRavi Teja const boost::system::error_code& ec, const sdbusplus::message_t& msg, 13963509dd5SRavi Teja const std::string& host, const std::string& dbusSNMPid) 1403d30708fSChicago Duan { 1413d30708fSChicago Duan if (ec) 1423d30708fSChicago Duan { 14363509dd5SRavi Teja const sd_bus_error* dbusError = msg.get_error(); 14463509dd5SRavi Teja if (dbusError != nullptr) 14563509dd5SRavi Teja { 14663509dd5SRavi Teja if (std::string_view( 14763509dd5SRavi Teja "xyz.openbmc_project.Common.Error.InvalidArgument") == 14863509dd5SRavi Teja dbusError->name) 14963509dd5SRavi Teja { 15063509dd5SRavi Teja messages::propertyValueIncorrect(asyncResp->res, "Destination", 15163509dd5SRavi Teja host); 15263509dd5SRavi Teja return; 15363509dd5SRavi Teja } 1543d30708fSChicago Duan if (ec.value() != EBADR) 1553d30708fSChicago Duan { 1563d30708fSChicago Duan // SNMP not installed 1573d30708fSChicago Duan messages::propertyValueOutOfRange(asyncResp->res, "SNMPv2c", 1583d30708fSChicago Duan "Protocol"); 1593d30708fSChicago Duan return; 1603d30708fSChicago Duan } 16163509dd5SRavi Teja } 1623d30708fSChicago Duan messages::internalError(asyncResp->res); 1633d30708fSChicago Duan return; 1643d30708fSChicago Duan } 1653d30708fSChicago Duan sdbusplus::message::object_path path(dbusSNMPid); 1663d30708fSChicago Duan const std::string snmpId = path.filename(); 1673d30708fSChicago Duan if (snmpId.empty()) 1683d30708fSChicago Duan { 1693d30708fSChicago Duan messages::internalError(asyncResp->res); 1703d30708fSChicago Duan return; 1713d30708fSChicago Duan } 1723d30708fSChicago Duan 1733d30708fSChicago Duan std::string subscriptionId = "snmp" + snmpId; 1743d30708fSChicago Duan 1753d30708fSChicago Duan boost::urls::url uri = boost::urls::format( 1763d30708fSChicago Duan "/redfish/v1/EventService/Subscriptions/{}", subscriptionId); 1773d30708fSChicago Duan asyncResp->res.addHeader("Location", uri.buffer()); 1783d30708fSChicago Duan messages::created(asyncResp->res); 1793d30708fSChicago Duan } 1803d30708fSChicago Duan 1813d30708fSChicago Duan inline void 1823d30708fSChicago Duan addSnmpTrapClient(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1833d30708fSChicago Duan const std::string& host, uint16_t snmpTrapPort) 1843d30708fSChicago Duan { 1853d30708fSChicago Duan crow::connections::systemBus->async_method_call( 18663509dd5SRavi Teja [asyncResp, host](const boost::system::error_code& ec, 18763509dd5SRavi Teja const sdbusplus::message_t& msg, 1883d30708fSChicago Duan const std::string& dbusSNMPid) { 18963509dd5SRavi Teja afterSnmpClientCreate(asyncResp, ec, msg, host, dbusSNMPid); 1903d30708fSChicago Duan }, 1913d30708fSChicago Duan "xyz.openbmc_project.Network.SNMP", 1923d30708fSChicago Duan "/xyz/openbmc_project/network/snmp/manager", 1933d30708fSChicago Duan "xyz.openbmc_project.Network.Client.Create", "Client", host, 1943d30708fSChicago Duan snmpTrapPort); 1953d30708fSChicago Duan } 1963d30708fSChicago Duan 1973d30708fSChicago Duan inline void 1983d30708fSChicago Duan getSnmpSubscriptionList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1993d30708fSChicago Duan const std::string& snmpId, 2003d30708fSChicago Duan nlohmann::json& memberArray) 2013d30708fSChicago Duan { 2023d30708fSChicago Duan const std::string subscriptionId = "snmp" + snmpId; 2033d30708fSChicago Duan 2043d30708fSChicago Duan nlohmann::json::object_t member; 2053d30708fSChicago Duan member["@odata.id"] = boost::urls::format( 2063d30708fSChicago Duan "/redfish/v1/EventService/Subscriptions/{}", subscriptionId); 2073d30708fSChicago Duan memberArray.push_back(std::move(member)); 2083d30708fSChicago Duan 2093d30708fSChicago Duan asyncResp->res.jsonValue["Members@odata.count"] = memberArray.size(); 2103d30708fSChicago Duan } 2113d30708fSChicago Duan 2123d30708fSChicago Duan inline void 2133d30708fSChicago Duan deleteSnmpTrapClient(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2143d30708fSChicago Duan const std::string& param) 2153d30708fSChicago Duan { 2163d30708fSChicago Duan std::string_view snmpTrapId = param; 2173d30708fSChicago Duan 2183d30708fSChicago Duan // Erase "snmp" in the request to find the corresponding 2193d30708fSChicago Duan // dbus snmp client id. For example, the snmpid in the 2203d30708fSChicago Duan // request is "snmp1", which will be "1" after being erased. 2213d30708fSChicago Duan snmpTrapId.remove_prefix(4); 2223d30708fSChicago Duan 2233d30708fSChicago Duan sdbusplus::message::object_path snmpPath = 2243d30708fSChicago Duan sdbusplus::message::object_path( 2253d30708fSChicago Duan "/xyz/openbmc_project/network/snmp/manager") / 2263d30708fSChicago Duan std::string(snmpTrapId); 2273d30708fSChicago Duan 2283d30708fSChicago Duan crow::connections::systemBus->async_method_call( 2293d30708fSChicago Duan [asyncResp, param](const boost::system::error_code& ec) { 2303d30708fSChicago Duan if (ec) 2313d30708fSChicago Duan { 2323d30708fSChicago Duan // The snmp trap id is incorrect 2333d30708fSChicago Duan if (ec.value() == EBADR) 2343d30708fSChicago Duan { 2353d30708fSChicago Duan messages::resourceNotFound(asyncResp->res, "Subscription", 2363d30708fSChicago Duan param); 2373d30708fSChicago Duan return; 2383d30708fSChicago Duan } 2393d30708fSChicago Duan messages::internalError(asyncResp->res); 2403d30708fSChicago Duan return; 2413d30708fSChicago Duan } 2423d30708fSChicago Duan messages::success(asyncResp->res); 2433d30708fSChicago Duan }, 2443d30708fSChicago Duan "xyz.openbmc_project.Network.SNMP", static_cast<std::string>(snmpPath), 2453d30708fSChicago Duan "xyz.openbmc_project.Object.Delete", "Delete"); 2463d30708fSChicago Duan } 2473d30708fSChicago Duan 2483d30708fSChicago Duan } // namespace redfish 249