Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame^] | 1 | #include "config.h" |
| 2 | #include "snmp_conf_manager.hpp" |
| 3 | #include <phosphor-logging/log.hpp> |
| 4 | |
| 5 | #include <experimental/filesystem> |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace network |
| 10 | { |
| 11 | namespace snmp |
| 12 | { |
| 13 | |
| 14 | using namespace phosphor::logging; |
| 15 | |
| 16 | ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) : |
| 17 | details::CreateIface(bus, objPath, true), bus(bus), objectPath(objPath) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | void 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 | |
| 36 | std::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 | |
| 47 | void 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 |