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