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    ${data}=  Create Dictionary  data=@{snmp_parm_list}
33
34    ${resp}=  OpenBMC Post Request
35    ...  ${SNMP_MANAGER_URI}action/Client  data=${data}
36
37    Run Keyword If  '${expected_result}' == 'error'
38    ...      Should Be Equal As Strings
39    ...      ${resp.status_code}  ${HTTP_BAD_REQUEST}
40    ...      msg=Allowing the configuration of an invalid SNMP.
41    ...  ELSE
42    ...      Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
43    ...      msg=Not allowing the configuration of a valid SNMP.
44
45
46Get List Of SNMP Manager And Port Configured On BMC
47    [Documentation]  Get list of SNMP managers and return the list.
48
49    @{snmp_uri_list}=  Get SNMP URI List
50    @{ip_and_port_list}=  Create List
51
52    # Sample output of snmp_uri_list enumeration.
53    # {
54    #  "data": {
55    #    "/xyz/openbmc_project/network/snmp/manager/92ae7a66": {
56    #      "Address": "10.6.6.6",
57    #      "AddressFamily": "xyz.openbmc_project.Network.Client.IPProtocol.IPv4",
58    #      "Port": 186
59    #    },
60
61    : FOR  ${snmp_uri}  IN   @{snmp_uri_list}
62    \  ${ip}=  Read Attribute  ${snmp_uri}  Address
63    \  ${port}=  Read Attribute  ${snmp_uri}  Port
64    \  Append To List  ${ip_and_port_list}  ${ip}  ${port}
65
66    [Return]  @{ip_and_port_list}
67
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
84
85Get SNMP Manager Object
86    [Documentation]  Find the SNMP object for the given ip and port and return it.
87    # If no object can be located, return ${EMPTY}.
88    [Arguments]  ${ip}  ${port}
89
90    # Description of argument(s):
91    # ip             SNMP manager IP.
92    # port           Network port where SNMP manager is listening.
93
94    ${snmp_objs}=  Read Properties  ${SNMP_MANAGER_URI}enumerate
95    : FOR  ${snmp_obj}  IN   @{snmp_objs}
96    \  ${obj}=  Set Variable  ${snmp_objs['${snmp_obj}']}
97    \   Run Keyword If
98    ...  '${obj['Address']}' == '${ip}' and '${obj['Port']}' == '${port}'
99    ...    Return From Keyword  ${snmp_obj}
100
101    Return From Keyword  ${EMPTY}
102
103
104Delete SNMP Manager And Object
105    [Documentation]  Delete SNMP manager.
106    [Arguments]  ${snmp_ip}  ${port}
107
108    # Description of argument(s):
109    # snmp_ip  SNMP manager IP.
110    # port     Network port where SNMP manager is listening.
111
112    ${snmp_obj}=  Get SNMP Manager Object  ${snmp_ip}  ${port}
113
114    # If the given IP and port is not configured, return.
115    # Otherwise, delete the IP and object.
116
117    Run Keyword And Return If  '${snmp_obj}' == '${EMPTY}'
118    ...  Pass Execution  SNMP manager to be deleted is not configured.
119
120    OpenBMC Delete Request  ${snmp_obj}
121
122    # Verify whether deleted SNMP is removed from BMC system.
123    ${status}=  Run Keyword And Return Status  Verify SNMP Manager
124    ...  ${snmp_ip}  ${port}
125    Should Be Equal  ${status}  ${False}  msg=SNMP manager is not deleted.
126
127
128Start SNMP Manager
129    [Documentation]  Start SNMP listener on the remote SNMP manager.
130
131    Open Connection And Log In  ${SNMP_MGR1_USERNAME}  ${SNMP_MGR1_PASSWORD}
132    ...  alias=snmp_server  host=${SNMP_MGR1_IP}
133
134    # The execution of the SNMP_TRAPD_CMD is necessary to cause SNMP to begin
135    # listening to SNMP messages.
136    SSHLibrary.write  ${SNMP_TRAPD_CMD} &
137