1*** Settings ***
2
3Documentation  Utilities for Robot keywords that use REST.
4
5Resource                ../lib/resource.robot
6Resource                ../lib/rest_client.robot
7Resource                ../lib/connection_client.robot
8Resource                ../lib/boot_utils.robot
9Resource                ../lib/common_utils.robot
10Resource                ../lib/bmc_redfish_utils.robot
11Resource                ../lib/ipmi_client.robot
12Library                 String
13Library                 DateTime
14Library                 Process
15Library                 OperatingSystem
16Library                 gen_print.py
17Library                 gen_misc.py
18Library                 gen_robot_print.py
19Library                 gen_cmd.py
20Library                 gen_robot_keyword.py
21Library                 bmc_ssh_utils.py
22Library                 utils.py
23Library                 var_funcs.py
24Library                 SCPLibrary  WITH NAME  scp
25Library                 gen_robot_valid.py
26
27
28*** Variables ***
29
30${SYSTEM_SHUTDOWN_TIME}   ${5}
31
32# Assign default value to QUIET for programs which may not define it.
33${QUIET}  ${0}
34
35${HOST_SETTING}    ${SETTINGS_URI}host0
36
37${boot_prog_method}               ${EMPTY}
38${power_policy_setup}             ${0}
39${bmc_power_policy_method}        ${EMPTY}
40
41
42*** Keywords ***
43
44
45Verify Ping and REST Authentication
46    [Documentation]  Verify ping and rest authentication.
47    ${l_ping}=   Run Keyword And Return Status
48    ...    Ping Host  ${OPENBMC_HOST}
49    Run Keyword If  '${l_ping}' == '${False}'
50    ...    Fail   msg=Ping Failed
51
52    ${l_rest}=   Run Keyword And Return Status
53    ...    Initialize OpenBMC
54    Run Keyword If  '${l_rest}' == '${False}'
55    ...    Fail   msg=REST Authentication Failed
56
57    # Just to make sure the SSH is working for SCP
58    Open Connection And Log In
59    ${system}   ${stderr}=    Execute Command   hostname   return_stderr=True
60    Should Be Empty     ${stderr}
61
62
63Verify Ping SSH And Redfish Authentication
64    [Documentation]  Verify ping, SSH and redfish authentication.
65
66    ${l_ping}=   Run Keyword And Return Status  Ping Host  ${OPENBMC_HOST}
67    Run Keyword If  '${l_ping}' == '${False}'  Fail   msg=Ping Failed
68
69    ${l_rest}=   Run Keyword And Return Status   Redfish.Login
70    Run Keyword If  '${l_rest}' == '${False}'  Fail   msg=REST Authentication Failed
71
72    # Just to make sure the SSH is working.
73    Open Connection And Log In
74    ${system}   ${stderr}=    Execute Command   hostname   return_stderr=True
75    Should Be Empty     ${stderr}
76
77
78Check If BMC is Up
79    [Documentation]  Wait for Host to be online. Checks every X seconds
80    ...              interval for Y minutes and fails if timed out.
81    ...              Default MAX timedout is 10 min, interval 10 seconds.
82    [Arguments]      ${max_timeout}=${OPENBMC_REBOOT_TIMEOUT} min
83    ...              ${interval}=10 sec
84
85    # Description of argument(s):
86    # max_timeout   Maximum time to wait.
87    #               This should be expressed in Robot Framework's time format
88    #               (e.g. "10 minutes").
89    # interval      Interval to wait between status checks.
90    #               This should be expressed in Robot Framework's time format
91    #               (e.g. "5 seconds").
92
93    Wait Until Keyword Succeeds
94    ...   ${max_timeout}  ${interval}   Verify Ping and REST Authentication
95
96
97Flush REST Sessions
98    [Documentation]   Removes all the active session objects
99    Delete All Sessions
100
101
102Trigger Host Watchdog Error
103    [Documentation]  Inject host watchdog timeout error via REST.
104    [Arguments]  ${milliseconds}=1000  ${sleep_time}=5s
105
106    # Description of argument(s):
107    # milliseconds  The time watchdog timer value in milliseconds (e.g. 1000 =
108    #               1 second).
109    # sleep_time    Time delay for host watchdog error to get injected.
110    #               Default is 5 seconds.
111
112    ${data}=  Create Dictionary
113    ...  data=xyz.openbmc_project.State.Watchdog.Action.PowerCycle
114    ${status}  ${result}=  Run Keyword And Ignore Error
115    ...  Read Attribute  ${HOST_WATCHDOG_URI}  ExpireAction
116    Run Keyword If  '${status}' == 'PASS'
117    ...  Write Attribute  ${HOST_WATCHDOG_URI}  ExpireAction  data=${data}
118
119    ${int_milliseconds}=  Convert To Integer  ${milliseconds}
120    ${data}=  Create Dictionary  data=${int_milliseconds}
121    Write Attribute  ${HOST_WATCHDOG_URI}  Interval  data=${data}
122
123    ${data}=  Create Dictionary  data=${True}
124    Write Attribute  ${HOST_WATCHDOG_URI}  Enabled  data=${data}
125
126    Sleep  ${sleep_time}
127
128
129Login To OS Host
130    [Documentation]  Login to OS Host and return the Login response code.
131    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
132    ...          ${os_password}=${OS_PASSWORD}
133
134    # Description of arguments:
135    # ${os_host} IP address of the OS Host.
136    # ${os_username}  OS Host Login user name.
137    # ${os_password}  OS Host Login passwrd.
138
139    Redfish Power On  stack_mode=skip  quiet=1
140
141    SSHLibrary.Open Connection  ${os_host}
142    ${resp}=  SSHLibrary.Login  ${os_username}  ${os_password}
143    [Return]  ${resp}
144
145
146Initiate Auto Reboot
147    [Documentation]  Initiate an auto reboot.
148    [Arguments]  ${milliseconds}=5000
149
150    # Description of argument(s):
151    # milliseconds  The number of milliseconds for the watchdog timer.
152
153    # Set the auto reboot policy.
154    Set Auto Reboot  ${1}
155    # Set the watchdog timer.
156    Trigger Host Watchdog Error  ${milliseconds}
157
158
159Initiate OS Host Reboot
160    [Documentation]  Initiate an OS reboot.
161    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
162    ...          ${os_password}=${OS_PASSWORD}
163
164    # Description of argument(s):
165    # os_host      The host name or IP address of the OS.
166    # os_username  The username to be used to sign in to the OS.
167    # os_password  The password to be used to sign in to the OS.
168
169    ${cmd_buf}=  Run Keyword If  '${os_username}' == 'root'
170    ...      Set Variable  reboot
171    ...  ELSE
172    ...      Set Variable  echo ${os_password} | sudo -S reboot
173
174    ${output}  ${stderr}  ${rc}=  OS Execute Command
175    ...  ${cmd_buf}  fork=${1}
176
177
178Initiate OS Host Power Off
179    [Documentation]  Initiate an OS reboot.
180    [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
181    ...          ${os_password}=${OS_PASSWORD}  ${hard}=${0}
182
183    # Description of argument(s):
184    # os_host      The DNS name or IP of the OS.
185    # os_username  The username to be used to sign in to the OS.
186    # os_password  The password to be used to sign in to the OS.
187    # hard         Indicates whether to do a hard vs. soft power off.
188
189    ${time_string}=  Run Keyword If  ${hard}  Set Variable  ${SPACE}now
190    ...  ELSE  Set Variable  ${EMPTY}
191
192    ${cmd_buf}=  Run Keyword If  '${os_username}' == 'root'
193    ...      Set Variable  shutdown${time_string}
194    ...  ELSE
195    ...      Set Variable  echo ${os_password} | sudo -S shutdown${time_string}
196
197    ${output}  ${stderr}  ${rc}=  OS Execute Command
198    ...  ${cmd_buf}  fork=${1}
199
200
201Set System LED State
202    [Documentation]  Set given system LED via REST.
203    [Arguments]  ${led_name}  ${led_state}
204    # Description of argument(s):
205    # led_name     System LED name (e.g. heartbeat, identify, beep).
206    # led_state    LED state to be set (e.g. On, Off).
207
208    ${args}=  Create Dictionary
209    ...  data=xyz.openbmc_project.Led.Physical.Action.${led_state}
210    Write Attribute  ${LED_PHYSICAL_URI}${led_name}  State  data=${args}
211
212    Verify LED State  ${led_name}  ${led_state}
213
214
215Read Turbo Setting Via REST
216    [Documentation]  Return turbo setting via REST.
217    # Returns 1 if TurboAllowed, 0 if not.
218
219    ${turbo_setting}=  Read Attribute
220    ...  ${CONTROL_HOST_URI}turbo_allowed  TurboAllowed
221    [Return]  ${turbo_setting}
222
223
224Set Turbo Setting Via REST
225    [Documentation]  Set turbo setting via REST.
226    [Arguments]  ${setting}  ${verify}=${False}
227
228    # Description of argument(s):
229    # setting  State to set TurboAllowed, 1=allowed, 0=not allowed.
230    # verify   If True, read the TurboAllowed setting to confirm.
231
232    ${data}=  Create Dictionary  data=${${setting}}
233    Write Attribute  ${CONTROL_HOST_URI}turbo_allowed  TurboAllowed
234    ...  verify=${verify}  data=${data}
235
236
237Set REST Logging Policy
238    [Documentation]  Enable or disable REST logging setting.
239    [Arguments]  ${policy_setting}=${True}
240
241    # Description of argument(s):
242    # policy_setting    The policy setting value which can be either
243    #                   True or False.
244
245    ${log_dict}=  Create Dictionary  data=${policy_setting}
246    Write Attribute  ${BMC_LOGGING_URI}rest_api_logs  Enabled
247    ...  data=${log_dict}  verify=${1}  expected_value=${policy_setting}
248
249
250Old Get Boot Progress
251    [Documentation]  Get the boot progress the old way (via org location).
252    [Arguments]  ${quiet}=${QUIET}
253
254    # Description of argument(s):
255    # quiet   Indicates whether this keyword should run without any output to
256    #         the console.
257
258    ${state}=  Read Attribute  ${OPENBMC_BASE_URI}sensors/host/BootProgress
259    ...  value  quiet=${quiet}
260
261    [Return]  ${state}
262
263
264Set Boot Progress Method
265    [Documentation]  Set the boot_prog_method to either 'Old' or 'New'.
266
267    # The boot progress data has moved from an 'org' location to an 'xyz'
268    # location.  This keyword will determine whether the new method of getting
269    # the boot progress is valid and will set the global boot_prog_method
270    # variable accordingly.  If boot_prog_method is already set (either by a
271    # prior call to this function or via a -v parm), this keyword will simply
272    # return.
273
274    # Note:  There are interim builds that contain boot_progress in both the
275    # old and the new location values.  It is nearly impossible for this
276    # keyword to determine whether the old boot_progress or the new one is
277    # active.  When using such builds where the old boot_progress is active,
278    # the only recourse users will have is that they may specify
279    # -v boot_prog_method:Old to force old behavior on such builds.
280
281    Run Keyword If  '${boot_prog_method}' != '${EMPTY}'  Return From Keyword
282
283    ${new_status}  ${new_value}=  Run Keyword And Ignore Error
284    ...  New Get Boot Progress
285    # If the new style read fails, the method must necessarily be "Old".
286    Run Keyword If  '${new_status}' == 'PASS'
287    ...  Run Keywords
288    ...  Set Global Variable  ${boot_prog_method}  New  AND
289    ...  Rqpvars  boot_prog_method  AND
290    ...  Return From Keyword
291
292    # Default method is "Old".
293    Set Global Variable  ${boot_prog_method}  Old
294    Rqpvars  boot_prog_method
295
296
297Initiate Power On
298    [Documentation]  Initiates the power on and waits until the Is Power On
299    ...  keyword returns that the power state has switched to on.
300    [Arguments]  ${wait}=${1}
301
302    # Description of argument(s):
303    # wait   Indicates whether to wait for a powered on state after issuing
304    #        the power on command.
305
306    @{arglist}=   Create List
307    ${args}=     Create Dictionary    data=@{arglist}
308    ${resp}=  Call Method  ${OPENBMC_BASE_URI}control/chassis0/  powerOn
309    ...  data=${args}
310    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
311
312    # Does caller want to wait for power on status?
313    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
314    Wait Until Keyword Succeeds  3 min  10 sec  Is Power On
315
316
317Initiate Power Off
318    [Documentation]  Initiates the power off and waits until the Is Power Off
319    ...  keyword returns that the power state has switched to off.
320
321    @{arglist}=   Create List
322    ${args}=     Create Dictionary    data=@{arglist}
323    ${resp}=  Call Method  ${OPENBMC_BASE_URI}control/chassis0/  powerOff
324    ...  data=${args}
325    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
326    Wait Until Keyword Succeeds  1 min  10 sec  Is Power Off
327
328
329Get Boot Progress
330    [Documentation]  Get the boot progress and return it.
331    [Arguments]  ${quiet}=${QUIET}
332
333    # Description of argument(s):
334    # quiet   Indicates whether this keyword should run without any output to
335    #         the console.
336
337    Set Boot Progress Method
338    ${state}=  Run Keyword If  '${boot_prog_method}' == 'New'
339    ...      New Get Boot Progress  quiet=${quiet}
340    ...  ELSE
341    ...      Old Get Boot Progress  quiet=${quiet}
342
343    [Return]  ${state}
344
345
346New Get Boot Progress
347    [Documentation]  Get the boot progress the new way (via xyz location).
348    [Arguments]  ${quiet}=${QUIET}
349
350    # Description of argument(s):
351    # quiet   Indicates whether this keyword should run without any output to
352    #         the console.
353
354    ${state}=  Read Attribute  ${HOST_STATE_URI}  BootProgress  quiet=${quiet}
355
356    [Return]  ${state.rsplit('.', 1)[1]}
357
358
359New Get Power Policy
360    [Documentation]  Returns the BMC power policy (new method).
361    ${currentPolicy}=  Read Attribute  ${POWER_RESTORE_URI}  PowerRestorePolicy
362
363    [Return]  ${currentPolicy}
364
365
366Old Get Power Policy
367    [Documentation]  Returns the BMC power policy (old method).
368    ${currentPolicy}=  Read Attribute  ${HOST_SETTING}  power_policy
369
370    [Return]  ${currentPolicy}
371
372
373Redfish Get Power Restore Policy
374    [Documentation]  Returns the BMC power restore policy.
375
376    ${power_restore_policy}=  Redfish.Get Attribute  /redfish/v1/Systems/system  PowerRestorePolicy
377    [Return]  ${power_restore_policy}
378
379Get Auto Reboot
380    [Documentation]  Returns auto reboot setting.
381    ${setting}=  Read Attribute  ${CONTROL_HOST_URI}/auto_reboot  AutoReboot
382
383    [Return]  ${setting}
384
385
386Redfish Get Auto Reboot
387    [Documentation]  Returns auto reboot setting.
388
389    ${resp}=  Redfish.Get Attribute  /redfish/v1/Systems/system  Boot
390    [Return]  ${resp["AutomaticRetryConfig"]}
391
392
393Trigger Warm Reset
394    [Documentation]  Initiate a warm reset.
395
396    log to console    "Triggering warm reset"
397    ${data}=   create dictionary   data=@{EMPTY}
398    ${resp}=  openbmc post request
399    ...  ${OPENBMC_BASE_URI}control/bmc0/action/warmReset  data=${data}
400    Should Be Equal As Strings      ${resp.status_code}     ${HTTP_OK}
401    ${session_active}=   Check If warmReset is Initiated
402    Run Keyword If   '${session_active}' == '${True}'
403    ...    Fail   msg=warm reset didn't occur
404
405    Sleep   ${SYSTEM_SHUTDOWN_TIME}min
406    Check If BMC Is Up
407
408
409Get Power State
410    [Documentation]  Returns the power state as an integer. Either 0 or 1.
411    [Arguments]  ${quiet}=${QUIET}
412
413    # Description of argument(s):
414    # quiet   Indicates whether this keyword should run without any output to
415    #         the console.
416
417    @{arglist}=  Create List
418    ${args}=  Create Dictionary  data=@{arglist}
419
420    ${resp}=  Call Method  ${OPENBMC_BASE_URI}control/chassis0/  getPowerState
421    ...        data=${args}  quiet=${quiet}
422    Should be equal as strings  ${resp.status_code}  ${HTTP_OK}
423    ${content}=  to json  ${resp.content}
424
425    [Return]  ${content["data"]}
426
427
428Clear BMC Gard Record
429    [Documentation]  Clear gard records from the system.
430
431    @{arglist}=  Create List
432    ${args}=  Create Dictionary  data=@{arglist}
433    ${resp}=  Call Method
434    ...  ${OPENPOWER_CONTROL}gard  Reset  data=${args}
435    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
436
437
438Flash PNOR
439    [Documentation]    Calls flash bios update method to flash PNOR image
440    [Arguments]    ${pnor_image}
441
442    # Description of argument(s):
443    # pnor_image  The filename and path of the PNOR image
444    #             (e.g. "/home/image/zaius.pnor").
445
446    @{arglist}=   Create List    ${pnor_image}
447    ${args}=     Create Dictionary    data=@{arglist}
448    ${resp}=  Call Method  /org/openbmc/control/flash/bios/  update
449    ...  data=${args}
450    should be equal as strings      ${resp.status_code}     ${HTTP_OK}
451    Wait Until Keyword Succeeds    2 min   10 sec    Is PNOR Flashing
452
453
454Get Flash BIOS Status
455    [Documentation]  Returns the status of the flash BIOS API as a string. For
456    ...              example 'Flashing', 'Flash Done', etc
457    ${data}=  Read Properties  /org/openbmc/control/flash/bios
458    [Return]    ${data['status']}
459
460
461Is PNOR Flashing
462    [Documentation]  Get BIOS 'Flashing' status. This indicates that PNOR
463    ...              flashing has started.
464    ${status}=    Get Flash BIOS Status
465    Should Contain  ${status}  Flashing
466
467
468Is PNOR Flash Done
469    [Documentation]  Get BIOS 'Flash Done' status.  This indicates that the
470    ...              PNOR flashing has completed.
471    ${status}=    Get Flash BIOS Status
472    should be equal as strings     ${status}     Flash Done
473
474
475Create OS Console File Path
476    [Documentation]  Create OS console file path name and return it.
477    [Arguments]  ${log_file_path}=${EMPTY}
478
479    # Description of arguments:
480    # file_path  The caller's candidate value.  If this value is ${EMPTY}, this
481    #            keyword will compose a file path name.  Otherwise, this
482    #            keyword will use the caller's file_path value.  In either
483    #            case, the value will be returned.
484
485    ${status}=  Run Keyword And Return Status  Variable Should Exist
486    ...  ${TEST_NAME}
487
488    ${default_file_path}=  Set Variable If  ${status} == ${TRUE}
489    ...  ${EXECDIR}${/}tmp${/}${OPENBMC_HOST}_${TEST_NAME.replace(' ', '')}_os_console.txt
490    ...  ${EXECDIR}${/}tmp${/}${OPENBMC_HOST}_os_console.txt
491
492    ${log_file_path}=  Set Variable If  '${log_file_path}' == '${EMPTY}'
493    ...  ${default_file_path}  ${log_file_path}
494
495    [Return]  ${log_file_path}
496
497
498Get Endpoint Paths
499    [Documentation]   Returns all url paths ending with given endpoint
500    ...               Example:
501    ...               Given the following endpoint: cpu
502    ...               This keyword will return: list of all urls ending with
503    ...               cpu -
504    ...               /org/openbmc/inventory/system/chassis/motherboard/cpu0,
505    ...               /org/openbmc/inventory/system/chassis/motherboard/cpu1
506    [Arguments]   ${path}   ${endpoint}
507
508    # Description of arguments:
509    # path       URL path for enumeration.
510    # endpoint   Endpoint string (url path ending).
511
512    # Make sure path ends with slash.
513    ${path}=  Add Trailing Slash  ${path}
514
515    ${resp}=  Read Properties  ${path}enumerate  timeout=30
516    Log Dictionary  ${resp}
517
518    ${list}=  Get Dictionary Keys  ${resp}
519    # For a given string, look for prefix and suffix for matching expression.
520    # Start of string followed by zero or more of any character followed by
521    # any digit or lower case character.
522    ${resp}=  Get Matches  ${list}  regexp=^.*[0-9a-z_].${endpoint}\[_0-9a-z]*$  case_insensitive=${True}
523
524    [Return]  ${resp}
525
526
527Set BMC Power Policy
528    [Documentation]   Set the given BMC power policy.
529    [Arguments]   ${policy}
530
531    # Note that this function will translate the old style "RESTORE_LAST_STATE"
532    # policy to the new style "xyz.openbmc_project.Control.Power.RestorePolicy.
533    # Policy.Restore" for you.
534
535    # Description of argument(s):
536    # policy    Power restore policy (e.g "RESTORE_LAST_STATE",
537    #           ${RESTORE_LAST_STATE}).
538
539    # Set the bmc_power_policy_method to either 'Old' or 'New'.
540    Set Power Policy Method
541    # This translation helps bridge between old and new method for calling.
542    ${policy}=  Translate Power Policy Value  ${policy}
543    # Run the appropriate keyword.
544    Run Key  ${bmc_power_policy_method} Set Power Policy \ ${policy}
545    ${currentPolicy}=  Get System Power Policy
546    Should Be Equal    ${currentPolicy}   ${policy}
547
548
549Delete Error Logs
550    [Documentation]  Delete error logs.
551    [Arguments]  ${quiet}=${0}
552    # Description of argument(s):
553    # quiet    If enabled, turns off logging to console.
554
555    # Check if error logs entries exist, if not return.
556    ${resp}=  OpenBMC Get Request  ${BMC_LOGGING_ENTRY}list  quiet=${1}
557    Return From Keyword If  ${resp.status_code} == ${HTTP_NOT_FOUND}
558
559    # Get the list of error logs entries and delete them all.
560    ${elog_entries}=  Get URL List  ${BMC_LOGGING_ENTRY}
561    FOR  ${entry}  IN  @{elog_entries}
562        Delete Error Log Entry  ${entry}  quiet=${quiet}
563    END
564
565
566Delete All Error Logs
567    [Documentation]  Delete all error log entries using "DeleteAll" interface.
568
569    ${data}=  Create Dictionary  data=@{EMPTY}
570    ${resp}=  Openbmc Post Request  ${BMC_LOGGING_URI}action/DeleteAll
571    ...  data=${data}
572    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
573
574
575Get Elog URL List
576    [Documentation]  Return error log entry list of URLs.
577
578    ${url_list}=  Read Properties  /xyz/openbmc_project/logging/entry/
579    Sort List  ${url_list}
580    [Return]  ${url_list}
581
582
583Get BMC Flash Chip Boot Side
584    [Documentation]  Return the BMC flash chip boot side.
585
586    # Example:
587    # 0  - indicates chip select is current side.
588    # 32 - indicates chip select is alternate side.
589
590    ${boot_side}  ${stderr}  ${rc}=  BMC Execute Command
591    ...  cat /sys/class/watchdog/watchdog1/bootstatus
592
593    [Return]  ${boot_side}
594
595
596Watchdog Object Should Exist
597    [Documentation]  Check that watchdog object exists.
598
599    ${resp}=  OpenBMC Get Request  ${WATCHDOG_URI}host0
600    Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
601    ...  msg=Expected watchdog object does not exist.
602
603
604Get System LED State
605    [Documentation]  Return the state of given system LED.
606    [Arguments]  ${led_name}
607
608    # Description of argument(s):
609    # led_name     System LED name (e.g. heartbeat, identify, beep).
610
611    ${state}=  Read Attribute  ${LED_PHYSICAL_URI}${led_name}  State
612    [Return]  ${state.rsplit('.', 1)[1]}
613
614
615Verify LED State
616    [Documentation]  Checks if LED is in given state.
617    [Arguments]  ${led_name}  ${led_state}
618    # Description of argument(s):
619    # led_name     System LED name (e.g. heartbeat, identify, beep).
620    # led_state    LED state to be verified (e.g. On, Off).
621
622    ${state}=  Get System LED State  ${led_name}
623    Should Be Equal  ${state}  ${led_state}
624
625
626Get LED State XYZ
627    [Documentation]  Returns state of given LED.
628    [Arguments]  ${led_name}
629
630    # Description of argument(s):
631    # led_name  Name of LED.
632
633    ${state}=  Read Attribute  ${LED_GROUPS_URI}${led_name}  Asserted
634    # Returns the state of the LED, either On or Off.
635    [Return]  ${state}
636
637
638Verify Identify LED State
639    [Documentation]  Verify that the identify state of the LED group matches caller's expectations.
640    [Arguments]  ${expected_state}
641
642    # Description of argument(s):
643    # expected_state  The expected LED asserted state (1 = asserted, 0 = not asserted).
644
645    ${led_state}=  Get LED State XYZ  enclosure_identify
646    Should Be Equal  ${led_state}  ${expected_state}  msg=Unexpected LED state.
647
648Verify The Attribute
649    [Documentation]  Verify the given attribute.
650    [Arguments]  ${uri}  ${attribute_name}  ${attribute_value}
651
652    # Description of argument(s):
653    # uri              URI path
654    #                  (e.g. "/xyz/openbmc_project/control/host0/TPMEnable").
655    # attribute_name   Name of attribute to be verified (e.g. "TPMEnable").
656    # attribute_value  The expected value of attribute (e.g. "1", "0", etc.)
657
658    ${output}=  Read Attribute  ${uri}  ${attribute_name}
659    Should Be Equal  ${attribute_value}  ${output}
660    ...  msg=Attribute "${attribute_name} does not have the expected value.
661
662
663New Set Power Policy
664    [Documentation]   Set the given BMC power policy (new method).
665    [Arguments]   ${policy}
666
667    # Description of argument(s):
668    # policy    Power restore policy (e.g. ${ALWAYS_POWER_OFF}).
669
670    ${valueDict}=  Create Dictionary  data=${policy}
671    Write Attribute
672    ...  ${POWER_RESTORE_URI}  PowerRestorePolicy  data=${valueDict}
673
674
675Old Set Power Policy
676    [Documentation]   Set the given BMC power policy (old method).
677    [Arguments]   ${policy}
678
679    # Description of argument(s):
680    # policy    Power restore policy (e.g. "ALWAYS_POWER_OFF").
681
682    ${valueDict}=     create dictionary  data=${policy}
683    Write Attribute    ${HOST_SETTING}    power_policy   data=${valueDict}
684
685
686Redfish Set Power Restore Policy
687    [Documentation]   Set the BMC power restore policy.
688    [Arguments]   ${power_restore_policy}
689
690    # Description of argument(s):
691    # power_restore_policy    Power restore policy (e.g. "AlwaysOff", "AlwaysOn", "LastState").
692
693    Redfish.Patch  /redfish/v1/Systems/system  body={"PowerRestorePolicy": "${power_restore_policy}"}
694    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
695
696
697IPMI Set Power Restore Policy
698    [Documentation]   Set the BMC power restore policy using IPMI.
699    [Arguments]   ${power_restore_policy}=always-off
700
701    # Description of argument(s):
702    # power_restore_policy    Power restore policies
703    #                         always-on   : turn on when power is restored
704    #                         previous    : return to previous state when power is restored
705    #                         always-off  : stay off after power is restored
706
707    ${resp}=  Run IPMI Standard Command  chassis policy ${power_restore_policy}
708    # Example:  Set chassis power restore policy to always-off
709    Should Contain  ${resp}  ${power_restore_policy}
710
711
712Set Auto Reboot Setting
713    [Documentation]  Set the given auto reboot setting (REST or Redfish).
714    [Arguments]  ${value}
715
716    # Description of argument(s):
717    # value    The reboot setting, 1 for enabling and 0 for disabling.
718
719    # This is to cater to boot call points and plugin script which will always
720    # send using value 0 or 1. This dictionary maps to redfish string values.
721    ${rest_redfish_dict}=  Create Dictionary
722    ...                    1=RetryAttempts
723    ...                    0=Disabled
724
725    Run Keyword If  ${REDFISH_SUPPORT_TRANS_STATE} == ${1}
726    ...    Redfish Set Auto Reboot  ${rest_redfish_dict["${value}"]}
727    ...  ELSE
728    ...    Set Auto Reboot  ${value}
729
730Set Auto Reboot
731    [Documentation]  Set the given auto reboot setting.
732    [Arguments]  ${setting}
733
734    # Description of argument(s):
735    # setting    The reboot setting, 1 for enabling and 0 for disabling.
736
737    ${valueDict}=  Convert To Integer  ${setting}
738    ${data}=  Create Dictionary  data=${valueDict}
739    Write Attribute  ${CONTROL_HOST_URI}/auto_reboot  AutoReboot   data=${data}
740    ${current_setting}=  Get Auto Reboot
741    Should Be Equal As Integers  ${current_setting}  ${setting}
742
743
744Redfish Set Auto Reboot
745    [Documentation]  Set the given auto reboot setting.
746    [Arguments]  ${setting}
747
748    # Description of argument(s):
749    # setting    The reboot setting, "RetryAttempts" and "Disabled".
750
751    Redfish.Patch  /redfish/v1/Systems/system  body={"Boot": {"AutomaticRetryConfig": "${setting}"}}
752    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
753
754    ${current_setting}=  Redfish Get Auto Reboot
755    Should Be Equal As Strings  ${current_setting}  ${setting}
756
757
758Set Control Boot Mode
759    [Documentation]  Set given boot mode on the boot object path attribute.
760    [Arguments]  ${boot_path}  ${boot_mode}
761
762    # Description of argument(s):
763    # boot_path  Boot object path.
764    #            Example:
765    #            /xyz/openbmc_project/control/host0/boot
766    #            /xyz/openbmc_project/control/host0/boot/one_time
767    # boot_mode  Boot mode which need to be set.
768    #            Example:
769    #            "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"
770
771    ${valueDict}=  Create Dictionary  data=${boot_mode}
772    Write Attribute  ${boot_path}  BootMode  data=${valueDict}
773
774
775Is Power On
776    [Documentation]  Verify that the BMC chassis state is on.
777    ${state}=  Get Power State
778    Should be equal  ${state}  ${1}
779
780
781Is Power Off
782    [Documentation]  Verify that the BMC chassis state is off.
783    ${state}=  Get Power State
784    Should be equal  ${state}  ${0}
785
786
787CLI Get BMC DateTime
788    [Documentation]  Returns BMC date time from date command.
789
790    ${bmc_time_via_date}  ${stderr}  ${rc}=  BMC Execute Command  date +"%Y-%m-%d %H:%M:%S"  print_err=1
791    [Return]  ${bmc_time_via_date}
792
793
794Update Root Password
795    [Documentation]  Update system "root" user password.
796    [Arguments]  ${openbmc_password}=${OPENBMC_PASSWORD}
797
798    # Description of argument(s):
799    # openbmc_password   The root password for the open BMC system.
800
801    @{password}=  Create List  ${openbmc_password}
802    ${data}=  Create Dictionary  data=@{password}
803
804    ${headers}=  Create Dictionary  Content-Type=application/json  X-Auth-Token=${XAUTH_TOKEN}
805    ${resp}=  Post Request  openbmc  ${BMC_USER_URI}root/action/SetPassword
806    ...  data=${data}  headers=${headers}
807    Valid Value  resp.status_code  [${HTTP_OK}]
808
809
810Get Post Boot Action
811    [Documentation]  Get post boot action.
812
813    # Post code update action dictionary.
814    #
815    # {
816    #    BMC image: {
817    #        OnReset: Redfish OBMC Reboot (off),
818    #        Immediate: Wait For Reboot  start_boot_seconds=${state['epoch_seconds']}
819    #    },
820    #    Host image: {
821    #        OnReset: RF SYS GracefulRestart,
822    #        Immediate: Wait State  os_running_match_state  10 mins
823    #    }
824    # }
825
826    ${code_base_dir_path}=  Get Code Base Dir Path
827    ${post_code_update_actions}=  Evaluate
828    ...  json.load(open('${code_base_dir_path}data/applytime_table.json'))  modules=json
829    Rprint Vars  post_code_update_actions
830
831    [Return]  ${post_code_update_actions}
832
833
834Redfish Set Boot Default
835    [Documentation]  Set and Verify Boot source override
836    [Arguments]      ${override_enabled}  ${override_target}  ${override_mode}=UEFI
837
838    # Description of argument(s):
839    # override_enabled    Boot source override enable type.
840    #                     ('Once', 'Continuous', 'Disabled').
841    # override_target     Boot source override target.
842    #                     ('Pxe', 'Cd', 'Hdd', 'Diags', 'BiosSetup', 'None').
843    # override_mode       Boot source override mode (relevant only for x86 arch).
844    #                     ('Legacy', 'UEFI').
845
846    ${data}=  Create Dictionary  BootSourceOverrideEnabled=${override_enabled}
847    ...  BootSourceOverrideTarget=${override_target}
848
849    Run Keyword If  '${PLATFORM_ARCH_TYPE}' == 'x86'
850    ...  Set To Dictionary  ${data}  BootSourceOverrideMode  ${override_mode}
851
852    ${payload}=  Create Dictionary  Boot=${data}
853
854    Redfish.Patch  /redfish/v1/Systems/system  body=&{payload}
855    ...  valid_status_codes=[${HTTP_OK},${HTTP_NO_CONTENT}]
856
857    ${resp}=  Redfish.Get Attribute  /redfish/v1/Systems/system  Boot
858    Should Be Equal As Strings  ${resp["BootSourceOverrideEnabled"]}  ${override_enabled}
859    Should Be Equal As Strings  ${resp["BootSourceOverrideTarget"]}  ${override_target}
860    Run Keyword If  '${PLATFORM_ARCH_TYPE}' == 'x86'
861    ...  Should Be Equal As Strings  ${resp["BootSourceOverrideMode"]}  ${override_mode}
862
863
864# Redfish state keywords.
865
866Redfish Get BMC State
867    [Documentation]  Return BMC health state.
868
869    # "Enabled" ->  BMC Ready, "Starting" -> BMC NotReady
870
871    # Example:
872    # "Status": {
873    #    "Health": "OK",
874    #    "HealthRollup": "OK",
875    #    "State": "Enabled"
876    # },
877
878    ${status}=  Redfish.Get Attribute  /redfish/v1/Managers/bmc  Status
879    [Return]  ${status["State"]}
880
881
882Redfish Get Host State
883    [Documentation]  Return host power and health state.
884
885    # Refer: http://redfish.dmtf.org/schemas/v1/Resource.json#/definitions/Status
886
887    # Example:
888    # "PowerState": "Off",
889    # "Status": {
890    #    "Health": "OK",
891    #    "HealthRollup": "OK",
892    #    "State": "StandbyOffline"
893    # },
894
895    ${chassis}=  Redfish.Get Properties  /redfish/v1/Chassis/${CHASSIS_ID}
896    [Return]  ${chassis["PowerState"]}  ${chassis["Status"]["State"]}
897
898
899Redfish Get Boot Progress
900    [Documentation]  Return boot progress state.
901
902    # Example: /redfish/v1/Systems/system/
903    # "BootProgress": {
904    #    "LastState": "OSRunning"
905    # },
906
907    ${boot_progress}=  Redfish.Get Properties  /redfish/v1/Systems/system/
908
909    Return From Keyword If  "${PLATFORM_ARCH_TYPE}" == "x86"
910    ...  NA  ${boot_progress["Status"]["State"]}
911
912    [Return]  ${boot_progress["BootProgress"]["LastState"]}  ${boot_progress["Status"]["State"]}
913
914
915Redfish Get States
916    [Documentation]  Return all the BMC and host states in dictionary.
917    [Timeout]  120 Seconds
918
919    # Refer: openbmc/docs/designs/boot-progress.md
920
921    Redfish.Login
922
923    ${bmc_state}=  Redfish Get BMC State
924    ${chassis_state}  ${chassis_status}=  Redfish Get Host State
925    ${boot_progress}  ${host_state}=  Redfish Get Boot Progress
926
927    ${states}=  Create Dictionary
928    ...  bmc=${bmc_state}
929    ...  chassis=${chassis_state}
930    ...  host=${host_state}
931    ...  boot_progress=${boot_progress}
932
933    # Disable loggoing state to prevent huge log.html record when boot
934    # test is run in loops.
935    #Log  ${states}
936
937    [Return]  ${states}
938
939
940Is BMC Standby
941    [Documentation]  Check if BMC is ready and host at standby.
942
943    ${standby_states}=  Create Dictionary
944    ...  bmc=Enabled
945    ...  chassis=Off
946    ...  host=Disabled
947    ...  boot_progress=None
948
949    Wait Until Keyword Succeeds  3 min  10 sec  Redfish Get States
950
951    Wait Until Keyword Succeeds  2 min  10 sec  Match State  ${standby_states}
952
953
954Match State
955    [Documentation]  Check if the expected and current states are matched.
956    [Arguments]  ${match_state}
957
958    # Description of argument(s):
959    # match_state      Expected states in dictionary.
960
961    ${current_state}=  Redfish Get States
962    Dictionaries Should Be Equal  ${match_state}  ${current_state}
963
964
965Redfish Initiate Auto Reboot
966    [Documentation]  Initiate an auto reboot.
967    [Arguments]  ${interval}=2000
968
969    # Description of argument(s):
970    # interval  Value in milliseconds to set Watchdog interval
971
972    # Set auto reboot policy
973    Redfish Set Auto Reboot  RetryAttempts
974
975    Redfish Power Operation  On
976
977    Wait Until Keyword Succeeds  2 min  5 sec  Is Boot Progress Changed
978
979    # Set watchdog timer
980    Set Watchdog Interval Using Busctl  ${interval}
981
982
983Is Boot Progress Changed
984    [Documentation]  Get BootProgress state and expect boot state mismatch.
985    [Arguments]  ${boot_state}=None
986
987    # Description of argument(s):
988    # boot_state   Value of the BootProgress state to match against.
989
990    ${boot_progress}  ${host_state}=  Redfish Get Boot Progress
991
992    Should Not Be Equal  ${boot_progress}   ${boot_state}
993
994
995Set Watchdog Interval Using Busctl
996    [Documentation]  Set Watchdog time interval.
997    [Arguments]  ${milliseconds}=1000
998
999    # Description of argument(s):
1000    # milliseconds     Time interval for watchdog timer
1001
1002    ${cmd}=  Catenate  busctl set-property xyz.openbmc_project.Watchdog
1003    ...                /xyz/openbmc_project/watchdog/host0
1004    ...                xyz.openbmc_project.State.Watchdog Interval t ${milliseconds}
1005    BMC Execute Command  ${cmd}
1006
1007
1008Stop PLDM Service And Wait
1009    [Documentation]  Stop PLDM service and wait for Host to initiate reset.
1010
1011    BMC Execute Command  systemctl stop pldmd.service
1012
1013
1014Get BIOS Attribute
1015    [Documentation]  Get the BIOS attribute for /redfish/v1/Systems/system/Bios.
1016
1017    # Python module:  get_member_list(resource_path)
1018    ${systems}=  Redfish_Utils.Get Member List  /redfish/v1/Systems
1019    ${bios_attr_dict}=  Redfish.Get Attribute  ${systems[0]}/Bios  Attributes
1020
1021    [Return]  ${bios_attr_dict}
1022
1023
1024Set BIOS Attribute
1025    [Documentation]  PATCH the BIOS attribute for /redfish/v1/Systems/system/Bios.
1026    [Arguments]  ${attribute_name}  ${attribute_value}
1027
1028    # Description of argument(s):
1029    # attribute_name     Any valid BIOS attribute.
1030    # attribute_value    Valid allowed attribute values.
1031
1032    # Python module:  get_member_list(resource_path)
1033    ${systems}=  Redfish_Utils.Get Member List  /redfish/v1/Systems
1034    Redfish.Patch  ${systems[0]}/Bios/Settings  body={"Attributes":{"${attribute_name}":"${attribute_value}"}}
1035
1036
1037Is BMC Operational
1038    [Documentation]  Check if BMC is enabled.
1039
1040    ${bmc_status} =  Redfish Get BMC State
1041    Should Be Equal  ${bmc_status}  Enabled
1042