Removing reduandant code by adding a new keyword

Delete SNMP Manager And Object keyword has redundant code
to check if snmp_ip and port is configured or not

Resolves  openbmc/openbmc-test-automation#1407

Change-Id: I28475ba6a6d1b4083ce73d22396807594ce8a303
Signed-off-by: Vijay Kantanavar  <vikantan@in.ibm.com>
diff --git a/lib/snmp/snmp_utils.robot b/lib/snmp/snmp_utils.robot
index 21d1d28..27594b5 100644
--- a/lib/snmp/snmp_utils.robot
+++ b/lib/snmp/snmp_utils.robot
@@ -29,7 +29,6 @@
     # expected_result  Expected status of SNMP configuration.
 
     @{snmp_parm_list}=  Create List  ${snmp_ip}  ${port}
-
     ${data}=  Create Dictionary  data=@{snmp_parm_list}
 
     ${resp}=  OpenBMC Post Request
@@ -43,6 +42,7 @@
     ...      Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
     ...      msg=Not allowing the configuration of a valid SNMP.
 
+
 Get List Of SNMP Manager And Port Configured On BMC
     [Documentation]  Get list of SNMP managers and return the list.
 
@@ -65,6 +65,7 @@
 
     [Return]  @{ip_and_port_list}
 
+
 Verify SNMP Manager
     [Documentation]  Verify SNMP manager configured on BMC.
     [Arguments]  ${snmp_ip}  ${port}
@@ -80,6 +81,26 @@
     List Should Contain Sub List  ${ip_and_port_list}  ${ip_and_port}
     ...  msg=Valid SNMP manager is not found on BMC.
 
+
+Get SNMP Manager Object
+    [Documentation]  Find the SNMP object for the given ip and port and return it.
+    # If no object can be located, return ${EMPTY}.
+    [Arguments]  ${ip}  ${port}
+
+    # Description of argument(s):
+    # ip             SNMP manager IP.
+    # port           Network port where SNMP manager is listening.
+
+    ${snmp_objs}=  Read Properties  ${SNMP_MANAGER_URI}enumerate
+    : FOR  ${snmp_obj}  IN   @{snmp_objs}
+    \  ${obj}=  Set Variable  ${snmp_objs['${snmp_obj}']}
+    \   Run Keyword If
+    ...  '${obj['Address']}' == '${ip}' and '${obj['Port']}' == '${port}'
+    ...    Return From Keyword  ${snmp_obj}
+
+    Return From Keyword  ${EMPTY}
+
+
 Delete SNMP Manager And Object
     [Documentation]  Delete SNMP manager.
     [Arguments]  ${snmp_ip}  ${port}
@@ -88,29 +109,17 @@
     # snmp_ip  SNMP manager IP.
     # port     Network port where SNMP manager is listening.
 
-    @{snmp_uri_list}=  Get SNMP URI List
-
-    # Find SNMP object having this IP and port.
-
-    : FOR  ${snmp_uri}  IN  @{snmp_uri_list}
-    \  ${ip}=  Read Attribute  ${snmp_uri}  Address
-    \  ${port1}=  Read Attribute  ${snmp_uri}  Port
-    \  Run Keyword If  "${snmp_ip}" == "${ip}" and "${port}" == "${port1}"
-    ...  Exit For Loop
+    ${snmp_obj}=  Get SNMP Manager Object  ${snmp_ip}  ${port}
 
     # If the given IP and port is not configured, return.
     # Otherwise, delete the IP and object.
 
-    Run Keyword And Return If  '${snmp_ip}' != '${ip}'
+    Run Keyword And Return If  '${snmp_obj}' == '${EMPTY}'
     ...  Pass Execution  SNMP manager to be deleted is not configured.
 
-    OpenBMC Delete Request  ${snmp_uri}
+    OpenBMC Delete Request  ${snmp_obj}
 
     # Verify whether deleted SNMP is removed from BMC system.
-    @{ip_and_port}=  Create List  ${snmp_ip}  ${port}
-    @{ip_and_port_list}=  Get List Of SNMP Manager And Port Configured On BMC
-
-    ${status}=  Run Keyword And Return Status  List Should Contain Sub List
-    ...  ${ip_and_port_list}  ${ip_and_port}
-
+    ${status}=  Run Keyword And Return Status  Verify SNMP Manager
+    ...  ${snmp_ip}  ${port}
     Should Be Equal  ${status}  ${False}  msg=SNMP manager is not deleted.