blob: 30d0815029d264f08a06ad7727750fa4a58de4ec [file] [log] [blame]
Ratan Gupta1dc91782018-04-19 16:47:12 +05301#include "config.h"
2#include "snmp_conf_manager.hpp"
3#include <phosphor-logging/log.hpp>
4
5#include <experimental/filesystem>
6
7namespace phosphor
8{
9namespace network
10{
11namespace snmp
12{
13
14using namespace phosphor::logging;
15
16ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) :
17 details::CreateIface(bus, objPath, true), bus(bus), objectPath(objPath)
18{
19}
20
21void ConfManager::client(std::string address, uint16_t port)
22{
23 auto clientEntry = this->clients.find(address);
24 if (clientEntry == this->clients.end())
25 {
26 std::experimental::filesystem::path objPath;
27 objPath /= objectPath;
28 objPath /= generateId(address, port);
29
30 this->clients.emplace(
31 address, std::make_unique<phosphor::network::snmp::Client>(
32 bus, objPath.string().c_str(), *this, address, port));
33 }
34}
35
36std::string ConfManager::generateId(const std::string& address, uint16_t port)
37{
38 std::stringstream hexId;
39 std::string hashString = address;
40 hashString += std::to_string(port);
41
42 // Only want 8 hex digits.
43 hexId << std::hex << ((std::hash<std::string>{}(hashString)) & 0xFFFFFFFF);
44 return hexId.str();
45}
46
47void ConfManager::deleteSNMPClient(const std::string& address)
48{
49 auto it = clients.find(address);
50 if (it == clients.end())
51 {
52 log<level::ERR>("Unable to delete the snmp client.",
53 entry("ADDRESS=%s", address.c_str()));
54 return;
55 }
56 this->clients.erase(it);
57}
58
59} // namespace snmp
60} // namespace network
61} // namespace phosphor