blob: 4ddace11e05a2e91b8d6b11d98bda359cf84fbcb [file] [log] [blame]
Ratan Gupta1dc91782018-04-19 16:47:12 +05301#include "config.h"
Patrick Williams1334b7b2021-02-22 17:15:12 -06002
Ratan Gupta1dc91782018-04-19 16:47:12 +05303#include "snmp_conf_manager.hpp"
Patrick Williams1334b7b2021-02-22 17:15:12 -06004
Ratan Gupta212f53e2018-04-30 17:28:05 +05305#include "snmp_serialize.hpp"
Ratan Gupta213517b2018-04-28 13:41:09 +05306#include "snmp_util.hpp"
7#include "xyz/openbmc_project/Common/error.hpp"
8
Patrick Williams1334b7b2021-02-22 17:15:12 -06009#include <arpa/inet.h>
10
Ratan Gupta213517b2018-04-28 13:41:09 +053011#include <phosphor-logging/elog-errors.hpp>
George Liu4caedfb2022-05-10 16:26:53 +080012#include <phosphor-logging/lg2.hpp>
Ratan Gupta1dc91782018-04-19 16:47:12 +053013
Ratan Gupta34d129a2021-12-04 21:04:51 +053014#include <filesystem>
Ratan Gupta1dc91782018-04-19 16:47:12 +053015
16namespace phosphor
17{
18namespace network
19{
20namespace snmp
21{
22
23using namespace phosphor::logging;
Ratan Gupta213517b2018-04-28 13:41:09 +053024using namespace sdbusplus::xyz::openbmc_project::Common::Error;
25using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta1dc91782018-04-19 16:47:12 +053026
27ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) :
Patrick Williamsb9b4c7d2022-04-05 16:20:37 -050028 details::CreateIface(bus, objPath,
29 details::CreateIface::action::defer_emit),
Ratan Gupta212f53e2018-04-30 17:28:05 +053030 dbusPersistentLocation(SNMP_CONF_PERSIST_PATH), bus(bus),
31 objectPath(objPath)
Patrick Williams1334b7b2021-02-22 17:15:12 -060032{}
Ratan Gupta1dc91782018-04-19 16:47:12 +053033
Ratan Guptad84e3272018-09-06 16:52:52 +053034std::string ConfManager::client(std::string address, uint16_t port)
Ratan Gupta1dc91782018-04-19 16:47:12 +053035{
Ratan Gupta9c4fed62018-11-16 17:47:54 +053036 // will throw exception if it is already configured.
37 checkClientConfigured(address, port);
Ratan Guptaa7ff3852018-11-16 14:05:57 +053038
39 lastClientId++;
Ratan Gupta213517b2018-04-28 13:41:09 +053040 try
41 {
42 // just to check whether given address is valid or not.
43 resolveAddress(address);
44 }
Patrick Williamsc3600d82021-10-06 12:38:25 -050045 catch (const InternalFailure& e)
Ratan Gupta213517b2018-04-28 13:41:09 +053046 {
George Liu4caedfb2022-05-10 16:26:53 +080047 lg2::error("{ADDRESS} is not a valid address", "ADDRESS", address);
Ratan Gupta213517b2018-04-28 13:41:09 +053048 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"),
49 Argument::ARGUMENT_VALUE(address.c_str()));
50 }
Ratan Guptad84e3272018-09-06 16:52:52 +053051
Ratan Guptaa7ff3852018-11-16 14:05:57 +053052 // create the D-Bus object
Ratan Gupta34d129a2021-12-04 21:04:51 +053053 std::filesystem::path objPath;
Ratan Gupta213517b2018-04-28 13:41:09 +053054 objPath /= objectPath;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053055 objPath /= std::to_string(lastClientId);
56
Ratan Gupta212f53e2018-04-30 17:28:05 +053057 auto client = std::make_unique<phosphor::network::snmp::Client>(
58 bus, objPath.string().c_str(), *this, address, port);
Ratan Gupta212f53e2018-04-30 17:28:05 +053059
Ratan Guptaa7ff3852018-11-16 14:05:57 +053060 // save the D-Bus object
61 serialize(lastClientId, *client, dbusPersistentLocation);
62
63 this->clients.emplace(lastClientId, std::move(client));
Ratan Guptad84e3272018-09-06 16:52:52 +053064 return objPath.string();
Ratan Gupta1dc91782018-04-19 16:47:12 +053065}
66
Ratan Gupta9c4fed62018-11-16 17:47:54 +053067void ConfManager::checkClientConfigured(const std::string& address,
68 uint16_t port)
69{
70 if (address.empty())
71 {
George Liu4caedfb2022-05-10 16:26:53 +080072 lg2::error("{ADDRESS} is not a valid address", "ADDRESS", address);
Ratan Gupta9c4fed62018-11-16 17:47:54 +053073 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ADDRESS"),
74 Argument::ARGUMENT_VALUE(address.c_str()));
75 }
76
77 for (const auto& val : clients)
78 {
79 if (val.second.get()->address() == address &&
80 val.second.get()->port() == port)
81 {
George Liu4caedfb2022-05-10 16:26:53 +080082 lg2::error("Client already exist");
Ratan Gupta9c4fed62018-11-16 17:47:54 +053083 // TODO Add the error(Object already exist) in the D-Bus interface
84 // then make the change here,meanwhile send the Internal Failure.
85 elog<InvalidArgument>(
86 Argument::ARGUMENT_NAME("ADDRESS"),
87 Argument::ARGUMENT_VALUE("Client already exist."));
88 }
89 }
90}
91
Ratan Guptaa7ff3852018-11-16 14:05:57 +053092void ConfManager::deleteSNMPClient(Id id)
Ratan Gupta1dc91782018-04-19 16:47:12 +053093{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053094 auto it = clients.find(id);
Ratan Gupta1dc91782018-04-19 16:47:12 +053095 if (it == clients.end())
96 {
George Liu4caedfb2022-05-10 16:26:53 +080097 lg2::error("Unable to delete the snmp client: {ID}", "ID", id);
Ratan Gupta1dc91782018-04-19 16:47:12 +053098 return;
99 }
Ratan Gupta212f53e2018-04-30 17:28:05 +0530100
101 std::error_code ec;
102 // remove the persistent file
103 fs::path fileName = dbusPersistentLocation;
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530104 fileName /= std::to_string(id);
Ratan Gupta212f53e2018-04-30 17:28:05 +0530105
106 if (fs::exists(fileName))
107 {
108 if (!fs::remove(fileName, ec))
109 {
George Liu4caedfb2022-05-10 16:26:53 +0800110 lg2::error("Unable to delete {FILE}: {EC}", "FILE", fileName, "EC",
111 ec.value());
Ratan Gupta212f53e2018-04-30 17:28:05 +0530112 }
113 }
114 else
115 {
George Liu4caedfb2022-05-10 16:26:53 +0800116 lg2::error("{FILE} doesn't exist", "FILE", fileName);
Ratan Gupta212f53e2018-04-30 17:28:05 +0530117 }
118 // remove the D-Bus Object.
Ratan Gupta1dc91782018-04-19 16:47:12 +0530119 this->clients.erase(it);
120}
121
Ratan Gupta212f53e2018-04-30 17:28:05 +0530122void ConfManager::restoreClients()
123{
124 if (!fs::exists(dbusPersistentLocation) ||
125 fs::is_empty(dbusPersistentLocation))
126 {
127 return;
128 }
129
130 for (auto& confFile :
131 fs::recursive_directory_iterator(dbusPersistentLocation))
132 {
133 if (!fs::is_regular_file(confFile))
134 {
135 continue;
136 }
137
138 auto managerID = confFile.path().filename().string();
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530139 Id idNum = std::stol(managerID);
Ratan Gupta212f53e2018-04-30 17:28:05 +0530140
141 fs::path objPath = objectPath;
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530142 objPath /= managerID;
Ratan Gupta212f53e2018-04-30 17:28:05 +0530143 auto manager =
144 std::make_unique<Client>(bus, objPath.string().c_str(), *this);
145 if (deserialize(confFile.path(), *manager))
146 {
147 manager->emit_object_added();
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530148 this->clients.emplace(idNum, std::move(manager));
149 if (idNum > lastClientId)
150 {
151 lastClientId = idNum;
152 }
Ratan Gupta212f53e2018-04-30 17:28:05 +0530153 }
154 }
155}
156
Ratan Gupta1dc91782018-04-19 16:47:12 +0530157} // namespace snmp
158} // namespace network
159} // namespace phosphor