Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "snmp_conf_manager.hpp" |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 3 | #include "snmp_serialize.hpp" |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 4 | #include "snmp_util.hpp" |
| 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | |
| 7 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | |
| 10 | #include <experimental/filesystem> |
| 11 | |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 12 | #include <arpa/inet.h> |
| 13 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | namespace snmp |
| 19 | { |
| 20 | |
| 21 | using namespace phosphor::logging; |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 22 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 23 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 24 | |
| 25 | ConfManager::ConfManager(sdbusplus::bus::bus& bus, const char* objPath) : |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 26 | details::CreateIface(bus, objPath, true), |
| 27 | dbusPersistentLocation(SNMP_CONF_PERSIST_PATH), bus(bus), |
| 28 | objectPath(objPath) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 32 | std::string ConfManager::client(std::string address, uint16_t port) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 33 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 34 | // TODO: Check whether the given manager is already there or not. |
| 35 | |
| 36 | lastClientId++; |
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 | } |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 49 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 50 | // create the D-Bus object |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 51 | std::experimental::filesystem::path objPath; |
| 52 | objPath /= objectPath; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 53 | objPath /= std::to_string(lastClientId); |
| 54 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 55 | auto client = std::make_unique<phosphor::network::snmp::Client>( |
| 56 | bus, objPath.string().c_str(), *this, address, port); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 57 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 58 | // save the D-Bus object |
| 59 | serialize(lastClientId, *client, dbusPersistentLocation); |
| 60 | |
| 61 | this->clients.emplace(lastClientId, std::move(client)); |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 62 | return objPath.string(); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 63 | } |
| 64 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 65 | void ConfManager::deleteSNMPClient(Id id) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 66 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 67 | auto it = clients.find(id); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 68 | if (it == clients.end()) |
| 69 | { |
| 70 | log<level::ERR>("Unable to delete the snmp client.", |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 71 | entry("ID=%d", id)); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 72 | return; |
| 73 | } |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 74 | |
| 75 | std::error_code ec; |
| 76 | // remove the persistent file |
| 77 | fs::path fileName = dbusPersistentLocation; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 78 | fileName /= std::to_string(id); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 79 | |
| 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 Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 95 | this->clients.erase(it); |
| 96 | } |
| 97 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 98 | void 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 Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 115 | Id idNum = std::stol(managerID); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 116 | |
| 117 | fs::path objPath = objectPath; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 118 | objPath /= managerID; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 119 | 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 Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame^] | 124 | this->clients.emplace(idNum, std::move(manager)); |
| 125 | if (idNum > lastClientId) |
| 126 | { |
| 127 | lastClientId = idNum; |
| 128 | } |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 133 | } // namespace snmp |
| 134 | } // namespace network |
| 135 | } // namespace phosphor |