blob: 3c7eaa6656e1903c626ea5fd246367902942de80 [file] [log] [blame]
Ratan Gupta1dc91782018-04-19 16:47:12 +05301#include "config.h"
2#include "snmp_conf_manager.hpp"
Ratan Gupta213517b2018-04-28 13:41:09 +05303#include "snmp_util.hpp"
4#include "xyz/openbmc_project/Common/error.hpp"
5
6#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta1dc91782018-04-19 16:47:12 +05307#include <phosphor-logging/log.hpp>
8
9#include <experimental/filesystem>
10
Ratan Gupta213517b2018-04-28 13:41:09 +053011#include <arpa/inet.h>
12
Ratan Gupta1dc91782018-04-19 16:47:12 +053013namespace phosphor
14{
15namespace network
16{
17namespace snmp
18{
19
20using namespace phosphor::logging;
Ratan Gupta213517b2018-04-28 13:41:09 +053021using namespace sdbusplus::xyz::openbmc_project::Common::Error;
22using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta1dc91782018-04-19 16:47:12 +053023
24ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) :
25 details::CreateIface(bus, objPath, true), bus(bus), objectPath(objPath)
26{
27}
28
29void ConfManager::client(std::string address, uint16_t port)
30{
31 auto clientEntry = this->clients.find(address);
Ratan Gupta213517b2018-04-28 13:41:09 +053032 if (clientEntry != this->clients.end())
Ratan Gupta1dc91782018-04-19 16:47:12 +053033 {
Ratan Gupta213517b2018-04-28 13:41:09 +053034 // address is already there
35 return;
Ratan Gupta1dc91782018-04-19 16:47:12 +053036 }
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 }
49 // create the D-Bus object
50 std::experimental::filesystem::path objPath;
51 objPath /= objectPath;
52 objPath /= generateId(address, port);
53
54 this->clients.emplace(
55 address, std::make_unique<phosphor::network::snmp::Client>(
56 bus, objPath.string().c_str(), *this, address, port));
Ratan Gupta1dc91782018-04-19 16:47:12 +053057}
58
59std::string ConfManager::generateId(const std::string& address, uint16_t port)
60{
61 std::stringstream hexId;
62 std::string hashString = address;
63 hashString += std::to_string(port);
64
65 // Only want 8 hex digits.
66 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
67 return hexId.str();
68}
69
70void ConfManager::deleteSNMPClient(const std::string& address)
71{
72 auto it = clients.find(address);
73 if (it == clients.end())
74 {
75 log<level::ERR>("Unable to delete the snmp client.",
76 entry("ADDRESS=%s", address.c_str()));
77 return;
78 }
79 this->clients.erase(it);
80}
81
82} // namespace snmp
83} // namespace network
84} // namespace phosphor