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 | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 34 | // will throw exception if it is already configured. |
| 35 | checkClientConfigured(address, port); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 36 | |
| 37 | lastClientId++; |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 38 | try |
| 39 | { |
| 40 | // just to check whether given address is valid or not. |
| 41 | resolveAddress(address); |
| 42 | } |
| 43 | catch (InternalFailure& e) |
| 44 | { |
| 45 | log<level::ERR>("Not a valid address"), |
| 46 | entry("ADDRESS=%s", address.c_str()); |
| 47 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"), |
| 48 | Argument::ARGUMENT_VALUE(address.c_str())); |
| 49 | } |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 50 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 51 | // create the D-Bus object |
Ratan Gupta | 213517b | 2018-04-28 13:41:09 +0530 | [diff] [blame] | 52 | std::experimental::filesystem::path objPath; |
| 53 | objPath /= objectPath; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 54 | objPath /= std::to_string(lastClientId); |
| 55 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 56 | auto client = std::make_unique<phosphor::network::snmp::Client>( |
| 57 | bus, objPath.string().c_str(), *this, address, port); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 58 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 59 | // save the D-Bus object |
| 60 | serialize(lastClientId, *client, dbusPersistentLocation); |
| 61 | |
| 62 | this->clients.emplace(lastClientId, std::move(client)); |
Ratan Gupta | d84e327 | 2018-09-06 16:52:52 +0530 | [diff] [blame] | 63 | return objPath.string(); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 64 | } |
| 65 | |
Ratan Gupta | 9c4fed6 | 2018-11-16 17:47:54 +0530 | [diff] [blame] | 66 | void ConfManager::checkClientConfigured(const std::string& address, |
| 67 | uint16_t port) |
| 68 | { |
| 69 | if (address.empty()) |
| 70 | { |
| 71 | log<level::ERR>("Invalid address"); |
| 72 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("ADDRESS"), |
| 73 | Argument::ARGUMENT_VALUE(address.c_str())); |
| 74 | } |
| 75 | |
| 76 | for (const auto& val : clients) |
| 77 | { |
| 78 | if (val.second.get()->address() == address && |
| 79 | val.second.get()->port() == port) |
| 80 | { |
| 81 | log<level::ERR>("Client already exist"); |
| 82 | // TODO Add the error(Object already exist) in the D-Bus interface |
| 83 | // then make the change here,meanwhile send the Internal Failure. |
| 84 | elog<InvalidArgument>( |
| 85 | Argument::ARGUMENT_NAME("ADDRESS"), |
| 86 | Argument::ARGUMENT_VALUE("Client already exist.")); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 91 | void ConfManager::deleteSNMPClient(Id id) |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 92 | { |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 93 | auto it = clients.find(id); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 94 | if (it == clients.end()) |
| 95 | { |
| 96 | log<level::ERR>("Unable to delete the snmp client.", |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 97 | entry("ID=%d", id)); |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 98 | return; |
| 99 | } |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 100 | |
| 101 | std::error_code ec; |
| 102 | // remove the persistent file |
| 103 | fs::path fileName = dbusPersistentLocation; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 104 | fileName /= std::to_string(id); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 105 | |
| 106 | if (fs::exists(fileName)) |
| 107 | { |
| 108 | if (!fs::remove(fileName, ec)) |
| 109 | { |
| 110 | log<level::ERR>("Unable to delete the file", |
| 111 | entry("FILE=%s", fileName.c_str()), |
| 112 | entry("ERROR=%d", ec.value())); |
| 113 | } |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | log<level::ERR>("File doesn't exist", |
| 118 | entry("FILE=%s", fileName.c_str())); |
| 119 | } |
| 120 | // remove the D-Bus Object. |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 121 | this->clients.erase(it); |
| 122 | } |
| 123 | |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 124 | void ConfManager::restoreClients() |
| 125 | { |
| 126 | if (!fs::exists(dbusPersistentLocation) || |
| 127 | fs::is_empty(dbusPersistentLocation)) |
| 128 | { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | for (auto& confFile : |
| 133 | fs::recursive_directory_iterator(dbusPersistentLocation)) |
| 134 | { |
| 135 | if (!fs::is_regular_file(confFile)) |
| 136 | { |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | auto managerID = confFile.path().filename().string(); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 141 | Id idNum = std::stol(managerID); |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 142 | |
| 143 | fs::path objPath = objectPath; |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 144 | objPath /= managerID; |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 145 | auto manager = |
| 146 | std::make_unique<Client>(bus, objPath.string().c_str(), *this); |
| 147 | if (deserialize(confFile.path(), *manager)) |
| 148 | { |
| 149 | manager->emit_object_added(); |
Ratan Gupta | a7ff385 | 2018-11-16 14:05:57 +0530 | [diff] [blame] | 150 | this->clients.emplace(idNum, std::move(manager)); |
| 151 | if (idNum > lastClientId) |
| 152 | { |
| 153 | lastClientId = idNum; |
| 154 | } |
Ratan Gupta | 212f53e | 2018-04-30 17:28:05 +0530 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Ratan Gupta | 1dc9178 | 2018-04-19 16:47:12 +0530 | [diff] [blame] | 159 | } // namespace snmp |
| 160 | } // namespace network |
| 161 | } // namespace phosphor |