1*** Settings ***
2Documentation  Utilities for SNMP testing.
3
4Resource  ../../lib/rest_client.robot
5Resource  ../../lib/utils.robot
6
7*** Keywords ***
8
9Get SNMP URI List
10    [Documentation]  Get all SNMP URIs and return them as list.
11
12    # Sample output:
13    #   "data": [
14    #     "/xyz/openbmc_project/network/snmp/manager/e9767624",
15    #     "/xyz/openbmc_project/network/snmp/manager/31f4ce8b"
16    #   ],
17
18    @{snmp_uri_list}=  Read Properties  ${SNMP_MANAGER_URI}
19
20    [Return]  @{snmp_uri_list}
21
22Configure SNMP Manager On BMC
23    [Documentation]  Configure SNMP manager on BMC.
24    [Arguments]  ${snmp_ip}  ${port}  ${expected_result}
25
26    # Description of argument(s):
27    # snmp_ip          SNMP manager IP.
28    # port             Network port where SNMP manager is listening.
29    # expected_result  Expected status of SNMP configuration.
30
31    @{snmp_parm_list}=  Create List  ${snmp_ip}  ${port}
32    ...  xyz.openbmc_project.Network.Client.IPProtocol.IPv4
33
34    ${data}=  Create Dictionary  data=@{snmp_parm_list}
35
36    ${resp}=  OpenBMC Post Request
37    ...  ${SNMP_MANAGER_URI}/action/Client  data=${data}
38
39    Run Keyword If  '${expected_result}' == 'error'
40    ...      Should Be Equal As Strings
41    ...      ${resp.status_code}  ${HTTP_BAD_REQUEST}
42    ...      msg=Allowing the configuration of an invalid SNMP.
43    ...  ELSE
44    ...      Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
45    ...      msg=Not allowing the configuration of a valid SNMP.
46
47Get List Of SNMP Manager And Port Configured On BMC
48    [Documentation]  Get list of SNMP managers and return the list.
49
50    @{snmp_uri_list}=  Get SNMP URI List
51    @{ip_and_port_list}=  Create List
52
53    # Sample output of snmp_uri_list enumeration.
54    # {
55    #  "data": {
56    #    "/xyz/openbmc_project/network/snmp/manager/92ae7a66": {
57    #      "Address": "10.6.6.6",
58    #      "AddressFamily": "xyz.openbmc_project.Network.Client.IPProtocol.IPv4",
59    #      "Port": 186
60    #    },
61
62    : FOR  ${snmp_uri}  IN   @{snmp_uri_list}
63    \  ${ip}=  Read Attribute  ${snmp_uri}  Address
64    \  ${port}=  Read Attribute  ${snmp_uri}  Port
65    \  Append To List  ${ip_and_port_list}  ${ip}  ${port}
66
67    [Return]  @{ip_and_port_list}
68
69Verify SNMP Manager
70    [Documentation]  Verify SNMP manager configured on BMC.
71    [Arguments]  ${snmp_ip}  ${port}
72
73    # Description of argument(s):
74    # snmp_ip  SNMP manager IP.
75    # port     Network port where SNMP manager is listening.
76
77    @{ip_and_port}=  Create List  ${snmp_ip}  ${port}
78
79    @{ip_and_port_list}=  Get List Of SNMP Manager And Port Configured On BMC
80
81    List Should Contain Sub List  ${ip_and_port_list}  ${ip_and_port}
82    ...  msg=Valid SNMP manager is not found on BMC.
83
84Delete SNMP Manager And Object
85    [Documentation]  Delete SNMP manager.
86    [Arguments]  ${snmp_ip}  ${port}
87
88    # Description of argument(s):
89    # snmp_ip  SNMP manager IP.
90    # port     Network port where SNMP manager is listening.
91
92    @{snmp_uri_list}=  Get SNMP URI List
93
94    # Find SNMP object having this IP and port.
95
96    : FOR  ${snmp_uri}  IN  @{snmp_uri_list}
97    \  ${ip}=  Read Attribute  ${snmp_uri}  Address
98    \  ${port1}=  Read Attribute  ${snmp_uri}  Port
99    \  Run Keyword If  "${snmp_ip}" == "${ip}" and "${port}" == "${port1}"
100    ...  Exit For Loop
101
102    # If the given IP and port is not configured, return.
103    # Otherwise, delete the IP and object.
104
105    Run Keyword And Return If  '${snmp_ip}' != '${ip}'
106    ...  Pass Execution  SNMP manager to be deleted is not configured.
107
108    OpenBMC Delete Request  ${snmp_uri}
109
110    # Verify whether deleted SNMP is removed from BMC system.
111    @{ip_and_port}=  Create List  ${snmp_ip}  ${port}
112    @{ip_and_port_list}=  Get List Of SNMP Manager And Port Configured On BMC
113
114    ${status}=  Run Keyword And Return Status  List Should Contain Sub List
115    ...  ${ip_and_port_list}  ${ip_and_port}
116
117    Should Be Equal  ${status}  ${False}  msg=SNMP manager is not deleted.
118