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