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