Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "snmp_conf_manager.hpp" |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame^] | 3 | #include "snmp_util.hpp" |
| 4 | #include "xyz/openbmc_project/Common/error.hpp" |
| 5 | |
| 6 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
| 8 | |
| 9 | #include <experimental/filesystem> |
| 10 | |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame^] | 11 | #include <arpa/inet.h> |
| 12 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 13 | namespace phosphor |
| 14 | { |
| 15 | namespace network |
| 16 | { |
| 17 | namespace snmp |
| 18 | { |
| 19 | |
| 20 | using namespace phosphor::logging; |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame^] | 21 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 22 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 23 | |
| 24 | ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) : |
| 25 | details::CreateIface(bus, objPath, true), bus(bus), objectPath(objPath) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | void ConfManager::client(std::string address, uint16_t port) |
| 30 | { |
| 31 | auto clientEntry = this->clients.find(address); |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame^] | 32 | if (clientEntry != this->clients.end()) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 33 | { |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame^] | 34 | // address is already there |
| 35 | return; |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 36 | } |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame^] | 37 | 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 Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | std::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 | |
| 70 | void 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 |