1*** Settings ***
2Library           Collections
3Library           String
4Library           RequestsLibrary.RequestsKeywords
5Library           OperatingSystem
6Resource          resource.txt
7Library           disable_warning_urllib.py
8Library           utils.py
9Library           gen_misc.py
10Resource          rest_response_code.robot
11
12*** Variables ***
13# Assign default value to QUIET for programs which may not define it.
14${QUIET}  ${0}
15
16*** Keywords ***
17OpenBMC Get Request
18    [Documentation]  Do REST GET request and return the result.
19    # Example result data:
20    # Response code:200, Content:{
21    #   "data": [
22    #     "/xyz/openbmc_project/state/host0",
23    #     "/xyz/openbmc_project/state/chassis0",
24    #     "/xyz/openbmc_project/state/bmc0"
25    #   ],
26    #   "message": "200 OK",
27    #   "status": "ok"
28    # }
29    [Arguments]    ${uri}    ${timeout}=30  ${quiet}=${QUIET}  &{kwargs}
30    # Description of argument(s):
31    # uri      The URI to establish connection with
32    #          (e.g. '/xyz/openbmc_project/software/').
33    # timeout  Timeout in seconds to establish connection with URI.
34    # quiet    If enabled, turns off logging to console.
35    # kwargs   Any additional arguments to be passed directly to the
36    #          Get Request call. For example, the caller might
37    #          set kwargs as follows:
38    #          ${kwargs}=  Create Dictionary  allow_redirect=${True}.
39
40    Initialize OpenBMC    ${timeout}  quiet=${quiet}
41    ${base_uri}=    Catenate    SEPARATOR=    ${DBUS_PREFIX}    ${uri}
42    Run Keyword If  '${quiet}' == '${0}'  Log Request  method=Get
43    ...  base_uri=${base_uri}  args=&{kwargs}
44    ${ret}=  Get Request  openbmc  ${base_uri}  &{kwargs}  timeout=${timeout}
45    Run Keyword If  '${quiet}' == '${0}'  Log Response  ${ret}
46    Delete All Sessions
47    [Return]    ${ret}
48
49OpenBMC Post Request
50    [Documentation]  Do REST POST request and return the result.
51    # Example result data:
52    # <Response [200]>
53    [Arguments]    ${uri}    ${timeout}=10  ${quiet}=${QUIET}  &{kwargs}
54    # Description of argument(s):
55    # uri      The URI to establish connection with
56    #          (e.g. '/xyz/openbmc_project/software/').
57    # timeout  Timeout in seconds to establish connection with URI.
58    # quiet    If enabled, turns off logging to console.
59    # kwargs   Any additional arguments to be passed directly to the
60    #          Post Request call. For example, the caller might
61    #          set kwargs as follows:
62    #          ${kwargs}=  Create Dictionary  allow_redirect=${True}.
63
64    Initialize OpenBMC    ${timeout}  quiet=${quiet}
65    ${base_uri}=    Catenate    SEPARATOR=    ${DBUS_PREFIX}    ${uri}
66    ${headers}=     Create Dictionary   Content-Type=application/json
67    set to dictionary   ${kwargs}       headers     ${headers}
68    Run Keyword If  '${quiet}' == '${0}'  Log Request  method=Post
69    ...  base_uri=${base_uri}  args=&{kwargs}
70    ${ret}=  Post Request  openbmc  ${base_uri}  &{kwargs}  timeout=${timeout}
71    Run Keyword If  '${quiet}' == '${0}'  Log Response  ${ret}
72    Delete All Sessions
73    [Return]    ${ret}
74
75OpenBMC Put Request
76    [Documentation]  Do REST PUT request on the resource identified by the URI.
77    [Arguments]    ${uri}    ${timeout}=10    &{kwargs}
78    # Description of argument(s):
79    # uri      The URI to establish connection with
80    #          (e.g. '/xyz/openbmc_project/software/').
81    # timeout  Timeout in seconds to establish connection with URI.
82    # kwargs   Arguments passed to the REST call.
83    # kwargs   Any additional arguments to be passed directly to the
84    #          Put Request call. For example, the caller might
85    #          set kwargs as follows:
86    #          ${kwargs}=  Create Dictionary  allow_redirect=${True}.
87
88    Initialize OpenBMC    ${timeout}
89    ${base_uri}=    Catenate    SEPARATOR=    ${DBUS_PREFIX}    ${uri}
90    ${headers}=     Create Dictionary   Content-Type=application/json
91    set to dictionary   ${kwargs}       headers     ${headers}
92    Log Request    method=Put    base_uri=${base_uri}    args=&{kwargs}
93    ${ret}=  Put Request  openbmc  ${base_uri}  &{kwargs}  timeout=${timeout}
94    Log Response    ${ret}
95    Delete All Sessions
96    [Return]    ${ret}
97
98OpenBMC Delete Request
99    [Documentation]  Do REST request to delete the resource identified by the
100    ...  URI.
101    [Arguments]    ${uri}    ${timeout}=10    &{kwargs}
102    # Description of argument(s):
103    # uri      The URI to establish connection with
104    #          (e.g. '/xyz/openbmc_project/software/').
105    # timeout  Timeout in seconds to establish connection with URI.
106    # kwargs   Any additional arguments to be passed directly to the
107    #          Delete Request call. For example, the caller might
108    #          set kwargs as follows:
109    #          ${kwargs}=  Create Dictionary  allow_redirect=${True}.
110
111    Initialize OpenBMC    ${timeout}
112    ${base_uri}=    Catenate    SEPARATOR=    ${DBUS_PREFIX}    ${uri}
113    Log Request    method=Delete    base_uri=${base_uri}    args=&{kwargs}
114    ${ret}=  Delete Request  openbmc  ${base_uri}  &{kwargs}  timeout=${timeout}
115    Log Response    ${ret}
116    Delete All Sessions
117    [Return]    ${ret}
118
119Initialize OpenBMC
120    [Documentation]  Do a REST login connection within specified time.
121    [Arguments]  ${timeout}=20  ${quiet}=${1}
122    ...  ${REST_USERNAME}=${REST_USERNAME}
123    ...  ${REST_PASSWORD}=${REST_PASSWORD}
124
125    # Description of argument(s):
126    # timeout  REST login attempt time out.
127    # quiet    Suppress console log if set.
128
129    # TODO : Task to revert this changes openbmc/openbmc-test-automation#532
130    # This will retry at 20 second interval.
131    Wait Until Keyword Succeeds  40 sec  20 sec
132    ...  Post Login Request  ${timeout}  ${quiet}
133    ...  ${REST_USERNAME}  ${REST_PASSWORD}
134
135Post Login Request
136    [Documentation]  Do REST login request.
137    [Arguments]  ${timeout}=20  ${quiet}=${1}
138    ...  ${REST_USERNAME}=${REST_USERNAME}
139    ...  ${REST_PASSWORD}=${REST_PASSWORD}
140
141    # Description of argument(s):
142    # timeout  REST login attempt time out.
143    # quiet    Suppress console log if set.
144
145    Create Session  openbmc  ${AUTH_URI}  timeout=${timeout}  max_retries=3
146    ${headers}=  Create Dictionary  Content-Type=application/json
147    @{credentials}=  Create List  ${REST_USERNAME}  ${REST_PASSWORD}
148    ${data}=  create dictionary   data=@{credentials}
149    ${status}  ${resp}=  Run Keyword And Ignore Error  Post Request  openbmc
150    ...  /login  data=${data}  headers=${headers}
151
152    Should Be Equal  ${status}  PASS  msg=${resp}
153    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
154
155Log Out OpenBMC
156    [Documentation]  Log out of the openbmc REST session.
157
158    ${headers}=  Create Dictionary  Content-Type=application/json
159    ${data}=  Create dictionary  data=@{EMPTY}
160
161    # If there is no active sesion it will throw the following exception
162    # "Non-existing index or alias 'openbmc'"
163    ${resp}=  Post Request  openbmc
164    ...  /logout  data=${data}  headers=${headers}
165
166    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
167    ...  msg=${resp}
168
169Log Request
170    [Documentation]  Log the specific REST URI, method name on the console.
171    [Arguments]    &{kwargs}
172    ${msg}=  Catenate  SEPARATOR=  URI:  ${AUTH_URI}  ${kwargs["base_uri"]}
173    ...  , method:  ${kwargs["method"]}  , args:  ${kwargs["args"]}
174    Logging    ${msg}    console=True
175
176Log Response
177    [Documentation]  Log the response code on the console.
178    [Arguments]    ${resp}
179    ${msg}=  Catenate  SEPARATOR=  Response code:  ${resp.status_code}
180    ...  , Content:  ${resp.content}
181    Logging    ${msg}    console=True
182
183Logging
184    [Documentation]  Log the specified message on the console.
185    [Arguments]    ${msg}    ${console}=default False
186    Log    ${msg}    console=True
187
188Read Attribute
189    [Documentation]  Retrieve attribute value from URI and return result.
190    # Example result data for the attribute 'FieldModeEnabled' in
191    # "/xyz/openbmc_project/software/attr/" :
192    # 0
193    [Arguments]    ${uri}    ${attr}    ${timeout}=10  ${quiet}=${QUIET}
194    ...  ${expected_value}=${EMPTY}
195    # Description of argument(s):
196    # uri               URI of the object that the attribute lives on
197    #                   (e.g. '/xyz/openbmc_project/software/').
198    # attr              Name of the attribute (e.g. 'FieldModeEnabled').
199    # timeout           Timeout for the REST call.
200    # quiet             If enabled, turns off logging to console.
201    # expected_value    If this argument is not empty, the retrieved value
202    #                   must match this value.
203
204    # Make sure uri ends with slash.
205    ${uri}=  Add Trailing Slash  ${uri}
206
207    ${resp}=  OpenBMC Get Request  ${uri}attr/${attr}  timeout=${timeout}
208    ...  quiet=${quiet}
209    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
210    ${content}=     To Json    ${resp.content}
211    Run Keyword If  '${expected_value}' != '${EMPTY}'
212    ...  Should Be Equal As Strings  ${expected_value}  ${content["data"]}
213    [Return]    ${content["data"]}
214
215
216Write Attribute
217    [Documentation]  Write a D-Bus attribute with REST.
218    [Arguments]  ${uri}  ${attr}  ${timeout}=10  ${verify}=${FALSE}
219    ...  ${expected_value}=${EMPTY}  &{kwargs}
220
221    # Description of argument(s):
222    # uri               URI of the object that the attribute lives on
223    #                   (e.g. '/xyz/openbmc_project/software/').
224    # attr              Name of the attribute (e.g. 'FieldModeEnabled').
225    # timeout           Timeout for the REST call.
226    # verify            If set to ${TRUE}, the attribute will be read back to
227    #                   ensure that its value is set to ${verify_attr}.
228    # expected_value    Only used if verify is set to ${TRUE}. The value that
229    #                   ${attr} should be set to. This defaults to
230    #                   ${kwargs['data']. There are cases where the caller
231    #                   expects some other value in which case this value can
232    #                   be explicitly specified.
233    # kwargs            Arguments passed to the REST call. This should always
234    #                   contain the value to set the property to at the 'data'
235    #                   key (e.g. data={"data": 1}).
236
237    ${base_uri}=  Catenate  SEPARATOR=  ${DBUS_PREFIX}  ${uri}
238    ${resp}=  Openbmc Put Request  ${base_uri}/attr/${attr}
239    ...  timeout=${timeout}  &{kwargs}
240    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
241
242    # Verify the attribute was set correctly if the caller requested it.
243    Return From Keyword If  ${verify} == ${FALSE}
244
245    ${expected_value}=  Set Variable If  '${expected_value}' == '${EMPTY}'
246    ...  ${kwargs['data']['data']}  ${expected_value}
247    ${value}=  Read Attribute  ${uri}  ${attr}
248    Should Be Equal  ${value}  ${expected_value}
249
250Read Properties
251    [Documentation]  Read data part of the URI object and return result.
252    # Example result data:
253    # [u'/xyz/openbmc_project/software/cf7bf9d5',
254    #  u'/xyz/openbmc_project/software/5ecb8b2c',
255    #  u'/xyz/openbmc_project/software/active',
256    #  u'/xyz/openbmc_project/software/functional']
257    [Arguments]  ${uri}  ${timeout}=10  ${quiet}=${QUIET}
258    # Description of argument(s):
259    # uri               URI of the object
260    #                   (e.g. '/xyz/openbmc_project/software/').
261    # timeout           Timeout for the REST call.
262    # quiet             If enabled, turns off logging to console.
263
264    ${resp}=  OpenBMC Get Request  ${uri}  timeout=${timeout}  quiet=${quiet}
265    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
266    ${content}=  To Json Ordered  ${resp.content}
267
268    [Return]  ${content["data"]}
269
270Call Method
271    [Documentation]  Invoke the specific REST service method.
272    [Arguments]  ${uri}  ${method}  ${timeout}=10  ${quiet}=${QUIET}  &{kwargs}
273    # Description of arguments:
274    # uri      The URI to establish connection with
275    #          (e.g. '/xyz/openbmc_project/software/').
276    # timeout  Timeout in seconds to establish connection with URI.
277    # quiet    If enabled, turns off logging to console.
278    # kwargs   Arguments passed to the REST call.
279
280    ${base_uri}=    Catenate    SEPARATOR=    ${DBUS_PREFIX}    ${uri}
281    ${resp}=  OpenBmc Post Request  ${base_uri}/action/${method}
282    ...  timeout=${timeout}  quiet=${quiet}  &{kwargs}
283    [Return]     ${resp}
284
285Upload Image To BMC
286    [Documentation]  Upload image to BMC device using REST POST operation.
287    [Arguments]  ${uri}  ${timeout}=10  ${quiet}=${1}  &{kwargs}
288
289    # Description of argument(s):
290    # uri             URI for uploading image via REST e.g. "/upload/image".
291    # timeout         Time allocated for the REST command to return status
292    #                 (specified in Robot Framework Time Format e.g. "3 mins").
293    # quiet           If enabled, turns off logging to console.
294    # kwargs          A dictionary keys/values to be passed directly to
295    #                 Post Request.
296
297    Initialize OpenBMC  ${timeout}  quiet=${quiet}
298    ${base_uri}=  Catenate  SEPARATOR=  ${DBUS_PREFIX}  ${uri}
299    ${headers}=  Create Dictionary  Content-Type=application/octet-stream
300    ...  Accept=application/octet-stream
301    Set To Dictionary  ${kwargs}  headers  ${headers}
302    Run Keyword If  '${quiet}' == '${0}'  Log Request  method=Post
303    ...  base_uri=${base_uri}  args=&{kwargs}
304    ${ret}=  Post Request  openbmc  ${base_uri}  &{kwargs}  timeout=${timeout}
305    Run Keyword If  '${quiet}' == '${0}'  Log Response  ${ret}
306    Should Be Equal As Strings  ${ret.status_code}  ${HTTP_OK}
307    Delete All Sessions
308