Add the check for the existense of the client
Don't add the client if it is already configured.
Change-Id: Ib0c64e2eb73b272a290599e1b83630a8ab5709ec
Signed-off-by: Ratan Gupta <ratagupt@linux.vnet.ibm.com>
diff --git a/snmp_conf_manager.cpp b/snmp_conf_manager.cpp
index b523c5d..52f4678 100644
--- a/snmp_conf_manager.cpp
+++ b/snmp_conf_manager.cpp
@@ -31,7 +31,8 @@
std::string ConfManager::client(std::string address, uint16_t port)
{
- // TODO: Check whether the given manager is already there or not.
+ // will throw exception if it is already configured.
+ checkClientConfigured(address, port);
lastClientId++;
try
@@ -62,6 +63,31 @@
return objPath.string();
}
+void ConfManager::checkClientConfigured(const std::string& address,
+ uint16_t port)
+{
+ if (address.empty())
+ {
+ log<level::ERR>("Invalid address");
+ elog<InvalidArgument>(Argument::ARGUMENT_NAME("ADDRESS"),
+ Argument::ARGUMENT_VALUE(address.c_str()));
+ }
+
+ for (const auto& val : clients)
+ {
+ if (val.second.get()->address() == address &&
+ val.second.get()->port() == port)
+ {
+ log<level::ERR>("Client already exist");
+ // TODO Add the error(Object already exist) in the D-Bus interface
+ // then make the change here,meanwhile send the Internal Failure.
+ elog<InvalidArgument>(
+ Argument::ARGUMENT_NAME("ADDRESS"),
+ Argument::ARGUMENT_VALUE("Client already exist."));
+ }
+ }
+}
+
void ConfManager::deleteSNMPClient(Id id)
{
auto it = clients.find(id);
diff --git a/snmp_conf_manager.hpp b/snmp_conf_manager.hpp
index f477b31..de9e486 100644
--- a/snmp_conf_manager.hpp
+++ b/snmp_conf_manager.hpp
@@ -63,6 +63,15 @@
*/
void restoreClients();
+ /** @brief Check if client is already configured or not.
+ *
+ * @param[in] address - SNMP manager address.
+ * @param[in] port - SNMP manager port.
+ *
+ * @return throw exception if client is already configured.
+ */
+ void checkClientConfigured(const std::string& address, uint16_t port);
+
/** @brief location of the persisted D-Bus object.*/
fs::path dbusPersistentLocation;
diff --git a/test/test_snmp_conf_manager.cpp b/test/test_snmp_conf_manager.cpp
index f6111f7..6cb2a53 100644
--- a/test/test_snmp_conf_manager.cpp
+++ b/test/test_snmp_conf_manager.cpp
@@ -13,6 +13,8 @@
{
auto managerObjPath = "/xyz/openbmc_test/snmp/manager";
+using InvalidArgument =
+ sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
class TestSNMPConfManager : public testing::Test
{
@@ -103,6 +105,13 @@
EXPECT_EQ(true, isClientExist("192.168.1.2"));
}
+// Add duplicate SNMP client
+TEST_F(TestSNMPConfManager, AddDuplicateSNMPClient)
+{
+ createSNMPClient("192.168.1.1", 24);
+ EXPECT_THROW(createSNMPClient("192.168.1.1", 24), InvalidArgument);
+}
+
// Delete SNMP client
TEST_F(TestSNMPConfManager, DeleteSNMPClient)
{