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
36Initiate Host Reboot
37    [Documentation]  Initiate host reboot via REST.
38
39    ${args}=  Create Dictionary  data=${HOST_REBOOT_TRANS}
40    Write Attribute
41    ...  ${HOST_STATE_URI}  RequestedHostTransition  data=${args}
42    Is Host Rebooted
43
44
45Is Host Running
46    [Documentation]  Check if host state is "Running".
47    ${host_state}=  Get Host State
48    Should Be Equal  Running  ${host_state}
49
50
51Is Host Off
52    [Documentation]  Check if host state is "Off".
53    ${host_state}=  Get Host State
54    Should Be Equal  Off  ${host_state}
55
56
57Is Host Rebooted
58    [Documentation]  Checks if host rebooted.
59
60    ${host_trans_state}=  Get Host Trans State
61    Should Be Equal  ${host_trans_state}  Reboot
62    Is Host Running
63
64
65Is Chassis On
66    [Documentation]  Check if chassis state is "On".
67    ${power_state}=  Get Chassis Power State
68    Should Be Equal  On  ${power_state}
69
70
71Is Chassis Off
72    [Documentation]  Check if chassis state is "Off".
73    ${power_state}=  Get Chassis Power State
74    Should Be Equal  Off  ${power_state}
75
76Is Host Quiesced
77    [Documentation]  Check if host state is quiesced.
78    ${host_state}=  Get Host State
79    ${status}=  Run Keyword And Return Status  Should Be Equal
80    ...  ${host_state}  Quiesced
81    [Return]  ${status}
82
83
84Recover Quiesced Host
85    [Documentation]  Recover host from quisced state.
86
87    ${resp}=  Run Keyword And Return Status  Is Host Quiesced
88    Run Keyword If  '${resp}' == 'True'
89    ...  Run Keywords  Initiate Host PowerOff  AND
90    ...  Log  HOST is recovered from quiesced state
91
92
93Get Host State
94    [Documentation]  Return the state of the host as a string.
95    [Arguments]  ${quiet}=${QUIET}
96    # quiet - Suppress REST output logging to console.
97    ${state}=
98    ...  Read Attribute  ${HOST_STATE_URI}  CurrentHostState
99    ...  quiet=${quiet}
100    [Return]  ${state.rsplit('.', 1)[1]}
101
102Get Host Trans State
103    [Documentation]  Return the transition state of host as a string.
104    ...              e.g. On, Off, Reboot
105    [Arguments]  ${quiet}=${QUIET}
106    # Description of arguments:
107    # quiet  Suppress REST output logging to console.
108
109    ${state}=
110    ...  Read Attribute  ${HOST_STATE_URI}  RequestedHostTransition
111    ...  quiet=${quiet}
112    [Return]  ${state.rsplit('.', 1)[1]}
113
114Get Chassis Power State
115    [Documentation]  Return the power state of the Chassis
116    ...              as a string.
117    [Arguments]  ${quiet}=${QUIET}
118    # quiet - Suppress REST output logging to console.
119    ${state}=
120    ...  Read Attribute  ${CHASSIS_STATE_URI}  CurrentPowerState
121    ...  quiet=${quiet}
122    [Return]  ${state.rsplit('.', 1)[1]}
123
124
125Get BMC State
126    [Documentation]  Return the state of the BMC.
127    [Arguments]  ${quiet}=${QUIET}
128    # quiet - Suppress REST output logging to console.
129    ${state}=
130    ...  Read Attribute  ${BMC_STATE_URI}  CurrentBMCState  quiet=${quiet}
131    [Return]  ${state.rsplit('.', 1)[1]}
132
133
134Put BMC State
135    [Documentation]  Put BMC in given state.
136    [Arguments]  ${expected_state}
137    # expected_state - expected BMC state
138
139    ${bmc_state}=  Get BMC State
140    Run Keyword If  '${bmc_state}' == '${expected_state}'
141    ...  Log  BMC is already in ${expected_state} state
142    ...  ELSE  Run Keywords  Initiate BMC Reboot  AND
143    ...  Wait for BMC state  ${expected_state}
144
145
146Initiate BMC Reboot
147    [Documentation]  Initiate BMC reboot.
148    ${args}=  Create Dictionary   data=${BMC_REBOOT_TRANS}
149    Write Attribute
150    ...  ${BMC_STATE_URI}  RequestedBMCTransition   data=${args}
151
152    ${session_active}=   Check If BMC Reboot Is Initiated
153    Run Keyword If   '${session_active}' == '${True}'
154    ...    Fail   msg=BMC Reboot didn't occur
155
156    Check If BMC is Up
157
158Check If BMC Reboot Is Initiated
159    [Documentation]  Checks whether BMC Reboot is initiated by checking
160    ...              BMC connection loss.
161    # Reboot adds 3 seconds delay before forcing reboot
162    # To minimize race conditions, we wait for 7 seconds
163    Sleep  7s
164    ${alive}=   Run Keyword and Return Status
165    ...    Open Connection And Log In
166    Return From Keyword If   '${alive}' == '${False}'    ${False}
167    [Return]    ${True}
168
169Is BMC Ready
170    [Documentation]  Check if BMC state is Ready.
171    ${bmc_state}=  Get BMC State
172    Should Be Equal  ${BMC_READY_STATE}  ${bmc_state}
173
174Is BMC Not Ready
175    [Documentation]  Check if BMC state is Not Ready.
176    ${bmc_state}=  Get BMC State
177    Should Be Equal  ${BMC_NOT_READY_STATE}  ${bmc_state}
178
179Wait for BMC state
180    [Documentation]  Wait until given BMC state is reached.
181    [Arguments]  ${state}
182    # state - BMC state to wait for
183    Run Keyword If  '${state}' == '${BMC_READY_STATE}'
184    ...    Wait Until Keyword Succeeds
185    ...    10 min  10 sec  Is BMC Ready
186    ...  ELSE IF  '${state}' == '${BMC_NOT_READY_STATE}'
187    ...    Wait Until Keyword Succeeds
188    ...    10 min  10 sec  Is BMC Not Ready
189    ...  ELSE  Fail  msg=Invalid BMC state
190
191
192Set State Interface Version
193    [Documentation]  Set version to indicate which interface to use.
194    ${resp}=  Openbmc Get Request  ${CHASSIS_STATE_URI}
195    ${status}=  Run Keyword And Return Status
196    ...  Should Be Equal As Strings  ${resp.status_code}  ${HTTP_OK}
197    Run Keyword If  '${status}' == '${True}'
198    ...  Set Global Variable  ${OBMC_STATES_VERSION}  ${1}
199    ...  ELSE
200    ...  Set Global Variable  ${OBMC_STATES_VERSION}  ${0}
201
202
203Power Off Request
204    [Documentation]  Select appropriate poweroff keyword.
205    Run Keyword If  '${OBMC_STATES_VERSION}' == '${0}'
206    ...  Initiate Power Off
207    ...  ELSE
208    ...  Initiate Host PowerOff
209
210
211Wait For BMC Ready
212    [Documentation]  Check BMC state and wait for BMC Ready.
213    @{states}=  Create List  BMC_READY  HOST_POWERED_OFF
214    Run Keyword If  '${OBMC_STATES_VERSION}' == '${0}'
215    ...  Wait Until Keyword Succeeds  10 min  10 sec
216    ...  Verify BMC State  ${states}
217    ...  ELSE
218    ...  Wait Until Keyword Succeeds  10 min  10 sec
219    ...  Is BMC Ready
220
221
222