1*** Settings ***
2Resource          ../lib/utils.robot
3Variables         ../data/variables.py
4
5*** Variables ***
6
7${BMC_READY_STATE}           Ready
8${BMC_NOT_READY_STATE}       NotReady
9${QUIET}  ${0}
10
11# "0" indicates that the new "xyz" interface should be used.
12${OBMC_STATES_VERSION}    ${0}
13
14*** Keywords ***
15
16Initiate Host Boot
17    [Documentation]  Initiate host power on.
18    ${args}=  Create Dictionary   data=${HOST_POWERON_TRANS}
19    Write Attribute
20    ...  ${HOST_STATE_URI}  RequestedHostTransition   data=${args}
21
22    Wait Until Keyword Succeeds
23    ...  10 min  10 sec  Is Host Running
24
25
26Initiate Host PowerOff
27    [Documentation]  Initiate host power off.
28    ${args}=  Create Dictionary   data=${HOST_POWEROFF_TRANS}
29    Write Attribute
30    ...  ${HOST_STATE_URI}  RequestedHostTransition   data=${args}
31
32    Wait Until Keyword Succeeds
33    ...  3 min  10 sec  Is Host Off
34
35
36Is Host Running
37    [Documentation]  Check if Chassis and Host state is ON.
38    ${power_state}=  Get Chassis Power State
39    Should Be Equal  On  ${power_state}
40    ${host_state}=  Get Host State
41    Should Be Equal  Running  ${host_state}
42
43
44Is Host Off
45    [Documentation]  Check if Chassis and Host state is OFF.
46    ${power_state}=  Get Chassis Power State
47    Should Be Equal  Off  ${power_state}
48    ${host_state}=  Get Host State
49    Should Be Equal  Off  ${host_state}
50
51Is Host Quiesced
52    [Documentation]  Check if host state is quiesced.
53    ${host_state}=  Get Host State
54    Should Be Equal  Quiesced  ${host_state}
55    ${status}=  Run Keyword And Return Status  Should Be Equal
56    ...  ${host_state}  Quiesced
57    [Return]  ${status}
58
59
60Get Host State
61    [Documentation]  Return the state of the host as a string.
62    [Arguments]  ${quiet}=${QUIET}
63    # quiet - Suppress REST output logging to console.
64    ${state}=
65    ...  Read Attribute  ${HOST_STATE_URI}  CurrentHostState
66    ...  quiet=${quiet}
67    [Return]  ${state.rsplit('.', 1)[1]}
68
69
70Get Chassis Power State
71    [Documentation]  Return the power state of the Chassis
72    ...              as a string.
73    [Arguments]  ${quiet}=${QUIET}
74    # quiet - Suppress REST output logging to console.
75    ${state}=
76    ...  Read Attribute  ${CHASSIS_STATE_URI}  CurrentPowerState
77    ...  quiet=${quiet}
78    [Return]  ${state.rsplit('.', 1)[1]}
79
80
81Get BMC State
82    [Documentation]  Return the state of the BMC.
83    [Arguments]  ${quiet}=${QUIET}
84    # quiet - Suppress REST output logging to console.
85    ${state}=
86    ...  Read Attribute  ${BMC_STATE_URI}  CurrentBMCState  quiet=${quiet}
87    [Return]  ${state.rsplit('.', 1)[1]}
88
89
90Put BMC State
91    [Documentation]  Put BMC in given state.
92    [Arguments]  ${expected_state}
93    # expected_state - expected BMC state
94
95    ${bmc_state}=  Get BMC State
96    Run Keyword If  '${bmc_state}' == '${expected_state}'
97    ...  Log  BMC is already in ${expected_state} state
98    ...  ELSE  Run Keywords  Initiate BMC Reboot  AND
99    ...  Wait for BMC state  ${expected_state}
100
101
102Initiate BMC Reboot
103    [Documentation]  Initiate BMC reboot.
104    ${args}=  Create Dictionary   data=${BMC_REBOOT_TRANS}
105    Write Attribute
106    ...  ${BMC_STATE_URI}  RequestedBMCTransition   data=${args}
107
108    ${session_active}=   Check If BMC Reboot Is Initiated
109    Run Keyword If   '${session_active}' == '${True}'
110    ...    Fail   msg=BMC Reboot didn't occur
111
112    Check If BMC is Up
113
114Check If BMC Reboot Is Initiated
115    [Documentation]  Checks whether BMC Reboot is initiated by checking
116    ...              BMC connection loss.
117    # Reboot adds 3 seconds delay before forcing reboot
118    # To minimize race conditions, we wait for 7 seconds
119    Sleep  7s
120    ${alive}=   Run Keyword and Return Status
121    ...    Open Connection And Log In
122    Return From Keyword If   '${alive}' == '${False}'    ${False}
123    [Return]    ${True}
124
125Is BMC Ready
126    [Documentation]  Check if BMC state is Ready.
127    ${bmc_state}=  Get BMC State
128    Should Be Equal  ${BMC_READY_STATE}  ${bmc_state}
129
130Is BMC Not Ready
131    [Documentation]  Check if BMC state is Not Ready.
132    ${bmc_state}=  Get BMC State
133    Should Be Equal  ${BMC_NOT_READY_STATE}  ${bmc_state}
134
135Wait for BMC state
136    [Documentation]  Wait until given BMC state is reached.
137    [Arguments]  ${state}
138    # state - BMC state to wait for
139    Run Keyword If  '${state}' == '${BMC_READY_STATE}'
140    ...    Wait Until Keyword Succeeds
141    ...    10 min  10 sec  Is BMC Ready
142    ...  ELSE IF  '${state}' == '${BMC_NOT_READY_STATE}'
143    ...    Wait Until Keyword Succeeds
144    ...    10 min  10 sec  Is BMC Not Ready
145    ...  ELSE  Fail  msg=Invalid BMC state
146
147
148Set State Interface Version
149    [Documentation]  Set version to indicate which interface to use.
150    ${resp}=  Openbmc Get Request  ${CONTROL_URI}chassis0
151    ${status}=  Run Keyword And Return Status
152    ...  Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
153    Run Keyword If  '${status}' == '${True}'
154    ...  Set Global Variable  ${OBMC_STATES_VERSION}  ${1}
155    ...  ELSE
156    ...  Set Global Variable  ${OBMC_STATES_VERSION}  ${0}
157
158
159Power Off Request
160    [Documentation]  Select appropriate poweroff keyword.
161    Run Keyword If  '${OBMC_STATES_VERSION}' == '${1}'
162    ...  Initiate Power Off
163    ...  ELSE
164    ...  Initiate Host PowerOff
165
166
167Wait For BMC Ready
168    [Documentation]  Check BMC state and wait for BMC Ready.
169    @{states}=  Create List  BMC_READY  HOST_POWERED_OFF
170    Run Keyword If  '${OBMC_STATES_VERSION}' == '${1}'
171    ...  Wait Until Keyword Succeeds  10 min  10 sec
172    ...  Verify BMC State  ${states}
173    ...  ELSE
174    ...  Wait Until Keyword Succeeds  10 min  10 sec
175    ...  Is BMC Ready
176
177
178