blob: b523c5dccd213eef9d1a1d0c004df4101f730bad [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
Ratan Guptad84e3272018-09-06 16:52:52 +053032std::string ConfManager::client(std::string address, uint16_t port)
Ratan Gupta1dc91782018-04-19 16:47:12 +053033{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053034 // TODO: Check whether the given manager is already there or not.
35
36 lastClientId++;
Ratan Gupta213517b2018-04-28 13:41:09 +053037 try
38 {
39 // just to check whether given address is valid or not.
40 resolveAddress(address);
41 }
42 catch (InternalFailure& e)
43 {
44 log<level::ERR>("Not a valid address"),
45 entry("ADDRESS=%s", address.c_str());
46 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"),
47 Argument::ARGUMENT_VALUE(address.c_str()));
48 }
Ratan Guptad84e3272018-09-06 16:52:52 +053049
Ratan Guptaa7ff3852018-11-16 14:05:57 +053050 // create the D-Bus object
Ratan Gupta213517b2018-04-28 13:41:09 +053051 std::experimental::filesystem::path objPath;
52 objPath /= objectPath;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053053 objPath /= std::to_string(lastClientId);
54
Ratan Gupta212f53e2018-04-30 17:28:05 +053055 auto client = std::make_unique<phosphor::network::snmp::Client>(
56 bus, objPath.string().c_str(), *this, address, port);
Ratan Gupta212f53e2018-04-30 17:28:05 +053057
Ratan Guptaa7ff3852018-11-16 14:05:57 +053058 // save the D-Bus object
59 serialize(lastClientId, *client, dbusPersistentLocation);
60
61 this->clients.emplace(lastClientId, std::move(client));
Ratan Guptad84e3272018-09-06 16:52:52 +053062 return objPath.string();
Ratan Gupta1dc91782018-04-19 16:47:12 +053063}
64
Ratan Guptaa7ff3852018-11-16 14:05:57 +053065void ConfManager::deleteSNMPClient(Id id)
Ratan Gupta1dc91782018-04-19 16:47:12 +053066{
Ratan Guptaa7ff3852018-11-16 14:05:57 +053067 auto it = clients.find(id);
Ratan Gupta1dc91782018-04-19 16:47:12 +053068 if (it == clients.end())
69 {
70 log<level::ERR>("Unable to delete the snmp client.",
Ratan Guptaa7ff3852018-11-16 14:05:57 +053071 entry("ID=%d", id));
Ratan Gupta1dc91782018-04-19 16:47:12 +053072 return;
73 }
Ratan Gupta212f53e2018-04-30 17:28:05 +053074
75 std::error_code ec;
76 // remove the persistent file
77 fs::path fileName = dbusPersistentLocation;
Ratan Guptaa7ff3852018-11-16 14:05:57 +053078 fileName /= std::to_string(id);
Ratan Gupta212f53e2018-04-30 17:28:05 +053079
80 if (fs::exists(fileName))
81 {
82 if (!fs::remove(fileName, ec))
83 {
84 log<level::ERR>("Unable to delete the file",
85 entry("FILE=%s", fileName.c_str()),
86 entry("ERROR=%d", ec.value()));
87 }
88 }
89 else
90 {
91 log<level::ERR>("File doesn't exist",
92 entry("FILE=%s", fileName.c_str()));
93 }
94 // remove the D-Bus Object.
Ratan Gupta1dc91782018-04-19 16:47:12 +053095 this->clients.erase(it);
96}
97
Ratan Gupta212f53e2018-04-30 17:28:05 +053098void ConfManager::restoreClients()
99{
100 if (!fs::exists(dbusPersistentLocation) ||
101 fs::is_empty(dbusPersistentLocation))
102 {
103 return;
104 }
105
106 for (auto& confFile :
107 fs::recursive_directory_iterator(dbusPersistentLocation))
108 {
109 if (!fs::is_regular_file(confFile))
110 {
111 continue;
112 }
113
114 auto managerID = confFile.path().filename().string();
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530115 Id idNum = std::stol(managerID);
Ratan Gupta212f53e2018-04-30 17:28:05 +0530116
117 fs::path objPath = objectPath;
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530118 objPath /= managerID;
Ratan Gupta212f53e2018-04-30 17:28:05 +0530119 auto manager =
120 std::make_unique<Client>(bus, objPath.string().c_str(), *this);
121 if (deserialize(confFile.path(), *manager))
122 {
123 manager->emit_object_added();
Ratan Guptaa7ff3852018-11-16 14:05:57 +0530124 this->clients.emplace(idNum, std::move(manager));
125 if (idNum > lastClientId)
126 {
127 lastClientId = idNum;
128 }
Ratan Gupta212f53e2018-04-30 17:28:05 +0530129 }
130 }
131}
132
Ratan Gupta1dc91782018-04-19 16:47:12 +0530133} // namespace snmp
134} // namespace network
135} // namespace phosphor