blob: d2d5d8f898f974247de8e47574d7ef137cee3e7b [file] [log] [blame]
Ratan Gupta1dc91782018-04-19 16:47:12 +05301#include "config.h"
2#include "snmp_conf_manager.hpp"
Ratan Gupta212f53e2018-04-30 17:28:05 +05303#include "snmp_serialize.hpp"
Ratan Gupta213517b2018-04-28 13:41:09 +05304#include "snmp_util.hpp"
5#include "xyz/openbmc_project/Common/error.hpp"
6
7#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta1dc91782018-04-19 16:47:12 +05308#include <phosphor-logging/log.hpp>
9
10#include <experimental/filesystem>
11
Ratan Gupta213517b2018-04-28 13:41:09 +053012#include <arpa/inet.h>
13
Ratan Gupta1dc91782018-04-19 16:47:12 +053014namespace phosphor
15{
16namespace network
17{
18namespace snmp
19{
20
21using namespace phosphor::logging;
Ratan Gupta213517b2018-04-28 13:41:09 +053022using namespace sdbusplus::xyz::openbmc_project::Common::Error;
23using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta1dc91782018-04-19 16:47:12 +053024
25ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) :
Ratan Gupta212f53e2018-04-30 17:28:05 +053026 details::CreateIface(bus, objPath, true),
27 dbusPersistentLocation(SNMP_CONF_PERSIST_PATH), bus(bus),
28 objectPath(objPath)
Ratan Gupta1dc91782018-04-19 16:47:12 +053029{
30}
31
32void ConfManager::client(std::string address, uint16_t port)
33{
34 auto clientEntry = this->clients.find(address);
Ratan Gupta213517b2018-04-28 13:41:09 +053035 if (clientEntry != this->clients.end())
Ratan Gupta1dc91782018-04-19 16:47:12 +053036 {
Ratan Gupta213517b2018-04-28 13:41:09 +053037 // address is already there
38 return;
Ratan Gupta1dc91782018-04-19 16:47:12 +053039 }
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 }
45 catch (InternalFailure& e)
46 {
47 log<level::ERR>("Not a valid address"),
48 entry("ADDRESS=%s", address.c_str());
49 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"),
50 Argument::ARGUMENT_VALUE(address.c_str()));
51 }
52 // create the D-Bus object
53 std::experimental::filesystem::path objPath;
54 objPath /= objectPath;
55 objPath /= generateId(address, port);
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);
59 // save the D-Bus object
60 serialize(*client, dbusPersistentLocation);
61
62 this->clients.emplace(address, std::move(client));
Ratan Gupta1dc91782018-04-19 16:47:12 +053063}
64
65std::string ConfManager::generateId(const std::string& address, uint16_t port)
66{
67 std::stringstream hexId;
68 std::string hashString = address;
69 hashString += std::to_string(port);
70
71 // Only want 8 hex digits.
72 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
73 return hexId.str();
74}
75
76void ConfManager::deleteSNMPClient(const std::string& address)
77{
78 auto it = clients.find(address);
79 if (it == clients.end())
80 {
81 log<level::ERR>("Unable to delete the snmp client.",
82 entry("ADDRESS=%s", address.c_str()));
83 return;
84 }
Ratan Gupta212f53e2018-04-30 17:28:05 +053085
86 std::error_code ec;
87 // remove the persistent file
88 fs::path fileName = dbusPersistentLocation;
89 fileName /=
Gunnar Millsf3fac222018-08-14 11:59:10 -050090 it->second->address() + SEPARATOR + std::to_string(it->second->port());
Ratan Gupta212f53e2018-04-30 17:28:05 +053091
92 if (fs::exists(fileName))
93 {
94 if (!fs::remove(fileName, ec))
95 {
96 log<level::ERR>("Unable to delete the file",
97 entry("FILE=%s", fileName.c_str()),
98 entry("ERROR=%d", ec.value()));
99 }
100 }
101 else
102 {
103 log<level::ERR>("File doesn't exist",
104 entry("FILE=%s", fileName.c_str()));
105 }
106 // remove the D-Bus Object.
Ratan Gupta1dc91782018-04-19 16:47:12 +0530107 this->clients.erase(it);
108}
109
Ratan Gupta212f53e2018-04-30 17:28:05 +0530110void ConfManager::restoreClients()
111{
112 if (!fs::exists(dbusPersistentLocation) ||
113 fs::is_empty(dbusPersistentLocation))
114 {
115 return;
116 }
117
118 for (auto& confFile :
119 fs::recursive_directory_iterator(dbusPersistentLocation))
120 {
121 if (!fs::is_regular_file(confFile))
122 {
123 continue;
124 }
125
126 auto managerID = confFile.path().filename().string();
Gunnar Millsf3fac222018-08-14 11:59:10 -0500127 auto pos = managerID.find(SEPARATOR);
Ratan Gupta212f53e2018-04-30 17:28:05 +0530128 auto ipaddress = managerID.substr(0, pos);
129 auto port_str = managerID.substr(pos + 1);
130 uint16_t port = stoi(port_str, nullptr);
131
132 fs::path objPath = objectPath;
133 objPath /= generateId(ipaddress, port);
134 auto manager =
135 std::make_unique<Client>(bus, objPath.string().c_str(), *this);
136 if (deserialize(confFile.path(), *manager))
137 {
138 manager->emit_object_added();
139 this->clients.emplace(ipaddress, std::move(manager));
140 }
141 }
142}
143
Ratan Gupta1dc91782018-04-19 16:47:12 +0530144} // namespace snmp
145} // namespace network
146} // namespace phosphor