blob: e2271b789707f5b9d0da1a8836c52f2706aebff5 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
George Keishingdf9ad392017-01-25 13:01:15 -06002
3r"""
4State Manager module:
5
6 - Defines Valid states of the system
7
8"""
George Keishing678b65c2017-08-31 16:16:39 -05009import os
10import re
11import sys
12
George Keishingdf9ad392017-01-25 13:01:15 -060013from robot.libraries.BuiltIn import BuiltIn
14
George Keishing678b65c2017-08-31 16:16:39 -050015robot_pgm_dir_path = os.path.dirname(__file__) + os.sep
Patrick Williams20f38712022-12-08 06:18:26 -060016repo_data_dir_path = re.sub("/lib", "/data", robot_pgm_dir_path)
George Keishing678b65c2017-08-31 16:16:39 -050017sys.path.append(repo_data_dir_path)
18
George Keishing09679892022-12-08 08:21:52 -060019import gen_robot_keyword as keyword # NOQA
Patrick Williams20f38712022-12-08 06:18:26 -060020import variables as var # NOQA
George Keishing678b65c2017-08-31 16:16:39 -050021
George Keishingdf9ad392017-01-25 13:01:15 -060022BuiltIn().import_resource("state_manager.robot")
George Keishing678b65c2017-08-31 16:16:39 -050023BuiltIn().import_resource("rest_client.robot")
George Keishingdf9ad392017-01-25 13:01:15 -060024
Patrick Williams20f38712022-12-08 06:18:26 -060025platform_arch_type = os.environ.get(
26 "PLATFORM_ARCH_TYPE", ""
27) or BuiltIn().get_variable_value("${PLATFORM_ARCH_TYPE}", default="power")
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +030028
George Keishingdf9ad392017-01-25 13:01:15 -060029# We will build eventually the mapping for warm, cold reset as well.
30VALID_STATES = {
Patrick Williams20f38712022-12-08 06:18:26 -060031 "reboot": {
George Keishing678b65c2017-08-31 16:16:39 -050032 # (Power Policy, BMC state, Chassis State, Host State)
Patrick Williams20f38712022-12-08 06:18:26 -060033 ("LEAVE_OFF", "Ready", "Off", "Off"),
34 ("ALWAYS_POWER_ON", "Ready", "On", "Running"),
35 ("ALWAYS_POWER_ON", "Ready", "On", "Off"),
36 ("RESTORE_LAST_STATE", "Ready", "On", "Running"),
37 ("RESTORE_LAST_STATE", "Ready", "On", "Off"),
38 ("ALWAYS_POWER_OFF", "Ready", "On", "Running"),
39 ("ALWAYS_POWER_OFF", "Ready", "Off", "Off"),
George Keishing678b65c2017-08-31 16:16:39 -050040 },
41}
42
43VALID_BOOT_STATES = {
Patrick Williams20f38712022-12-08 06:18:26 -060044 "Off": { # Valid states when Host is Off.
George Keishing678b65c2017-08-31 16:16:39 -050045 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
46 (
47 "xyz.openbmc_project.State.BMC.BMCState.Ready",
48 "xyz.openbmc_project.State.Chassis.PowerState.Off",
49 "xyz.openbmc_project.State.Host.HostState.Off",
50 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified",
Patrick Williams20f38712022-12-08 06:18:26 -060051 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
George Keishing678b65c2017-08-31 16:16:39 -050052 ),
53 },
Patrick Williams20f38712022-12-08 06:18:26 -060054 "Reboot": { # Valid states when BMC reset to standby.
George Keishing678b65c2017-08-31 16:16:39 -050055 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
56 (
57 "xyz.openbmc_project.State.BMC.BMCState.Ready",
58 "xyz.openbmc_project.State.Chassis.PowerState.Off",
59 "xyz.openbmc_project.State.Host.HostState.Off",
60 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified",
Patrick Williams20f38712022-12-08 06:18:26 -060061 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
George Keishing678b65c2017-08-31 16:16:39 -050062 ),
63 },
Patrick Williams20f38712022-12-08 06:18:26 -060064 "Running": { # Valid states when Host is powering on.
George Keishing678b65c2017-08-31 16:16:39 -050065 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
66 (
67 "xyz.openbmc_project.State.BMC.BMCState.Ready",
68 "xyz.openbmc_project.State.Chassis.PowerState.On",
69 "xyz.openbmc_project.State.Host.HostState.Running",
70 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MotherboardInit",
Patrick Williams20f38712022-12-08 06:18:26 -060071 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
George Keishing678b65c2017-08-31 16:16:39 -050072 ),
73 },
Patrick Williams20f38712022-12-08 06:18:26 -060074 "Booted": { # Valid state when Host is booted.
George Keishing678b65c2017-08-31 16:16:39 -050075 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
76 (
77 "xyz.openbmc_project.State.BMC.BMCState.Ready",
78 "xyz.openbmc_project.State.Chassis.PowerState.On",
79 "xyz.openbmc_project.State.Host.HostState.Running",
80 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
Patrick Williams20f38712022-12-08 06:18:26 -060081 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete",
George Keishing678b65c2017-08-31 16:16:39 -050082 ),
83 },
Patrick Williams20f38712022-12-08 06:18:26 -060084 "ResetReload": { # Valid state BMC reset reload when host is booted.
George Keishing678b65c2017-08-31 16:16:39 -050085 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
86 (
87 "xyz.openbmc_project.State.BMC.BMCState.Ready",
88 "xyz.openbmc_project.State.Chassis.PowerState.On",
89 "xyz.openbmc_project.State.Host.HostState.Running",
90 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
Patrick Williams20f38712022-12-08 06:18:26 -060091 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete",
George Keishing678b65c2017-08-31 16:16:39 -050092 ),
George Keishingdf9ad392017-01-25 13:01:15 -060093 },
94}
George Keishing90b555a2021-05-20 11:54:16 -050095REDFISH_VALID_BOOT_STATES = {
Patrick Williams20f38712022-12-08 06:18:26 -060096 "Off": { # Valid states when Host is Off.
George Keishing90b555a2021-05-20 11:54:16 -050097 # (BMC , Chassis , Host , BootProgress)
98 (
99 "Enabled",
100 "Off",
101 "Disabled",
102 "None",
103 ),
104 },
Patrick Williams20f38712022-12-08 06:18:26 -0600105 "Reboot": { # Valid states when BMC reset to standby.
George Keishing90b555a2021-05-20 11:54:16 -0500106 # (BMC , Chassis , Host , BootProgress)
107 (
108 "Enabled",
109 "Off",
110 "Disabled",
111 "None",
112 ),
113 },
Patrick Williams20f38712022-12-08 06:18:26 -0600114 "Running": { # Valid states when Host is powering on.
George Keishing90b555a2021-05-20 11:54:16 -0500115 # (BMC , Chassis , Host , BootProgress)
116 (
117 "Enabled",
118 "On",
119 "Enabled",
120 "OSRunning",
121 ),
122 },
Patrick Williams20f38712022-12-08 06:18:26 -0600123 "Booted": { # Valid state when Host is booted.
George Keishing90b555a2021-05-20 11:54:16 -0500124 # (BMC , Chassis , Host , BootProgress)
125 (
126 "Enabled",
127 "On",
128 "Enabled",
129 "OSRunning",
130 ),
131 },
Patrick Williams20f38712022-12-08 06:18:26 -0600132 "ResetReload": { # Valid state BMC reset reload when host is booted.
George Keishing90b555a2021-05-20 11:54:16 -0500133 # (BMC , Chassis , Host , BootProgress)
134 (
135 "Enabled",
136 "On",
137 "Enabled",
138 "OSRunning",
139 ),
140 },
141}
George Keishingdf9ad392017-01-25 13:01:15 -0600142
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300143if platform_arch_type == "x86":
144 VALID_BOOT_STATES_X86 = {}
145 for state_name, state_set in VALID_BOOT_STATES.items():
146 VALID_BOOT_STATES_X86[state_name] = set()
147 for state_tuple in state_set:
148 state_tuple_new = tuple(
149 x
150 for x in state_tuple
151 if not (
152 x.startswith("xyz.openbmc_project.State.Boot.Progress")
Patrick Williams20f38712022-12-08 06:18:26 -0600153 or x.startswith(
154 "xyz.openbmc_project.State.OperatingSystem"
155 )
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300156 )
157 )
158 VALID_BOOT_STATES_X86[state_name].add(state_tuple_new)
159 VALID_BOOT_STATES = VALID_BOOT_STATES_X86
160
George Keishingdf9ad392017-01-25 13:01:15 -0600161
Patrick Williams20f38712022-12-08 06:18:26 -0600162class state_map:
George Keishing678b65c2017-08-31 16:16:39 -0500163 def get_boot_state(self):
George Keishingdf9ad392017-01-25 13:01:15 -0600164 r"""
George Keishing678b65c2017-08-31 16:16:39 -0500165 Return the system state as a tuple of bmc, chassis, host state,
166 BootProgress and OperatingSystemState.
George Keishingdf9ad392017-01-25 13:01:15 -0600167 """
George Keishing678b65c2017-08-31 16:16:39 -0500168
Patrick Williams20f38712022-12-08 06:18:26 -0600169 status, state = keyword.run_key(
170 "Read Properties " + var.SYSTEM_STATE_URI + "enumerate"
171 )
172 bmc_state = state[var.SYSTEM_STATE_URI + "bmc0"]["CurrentBMCState"]
173 chassis_state = state[var.SYSTEM_STATE_URI + "chassis0"][
174 "CurrentPowerState"
175 ]
176 host_state = state[var.SYSTEM_STATE_URI + "host0"]["CurrentHostState"]
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300177 if platform_arch_type == "x86":
Patrick Williams20f38712022-12-08 06:18:26 -0600178 return (str(bmc_state), str(chassis_state), str(host_state))
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300179 else:
Patrick Williams20f38712022-12-08 06:18:26 -0600180 boot_state = state[var.SYSTEM_STATE_URI + "host0"]["BootProgress"]
181 os_state = state[var.SYSTEM_STATE_URI + "host0"][
182 "OperatingSystemState"
183 ]
George Keishing678b65c2017-08-31 16:16:39 -0500184
Patrick Williams20f38712022-12-08 06:18:26 -0600185 return (
186 str(bmc_state),
187 str(chassis_state),
188 str(host_state),
189 str(boot_state),
190 str(os_state),
191 )
George Keishingdf9ad392017-01-25 13:01:15 -0600192
193 def valid_boot_state(self, boot_type, state_set):
194 r"""
195 Validate a given set of states is valid.
196
George Keishing678b65c2017-08-31 16:16:39 -0500197 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500198 boot_type Boot type (e.g. off/running/host booted
199 etc.)
200 state_set State set (e.g.bmc,chassis,host,
201 BootProgress,OperatingSystemState)
George Keishingdf9ad392017-01-25 13:01:15 -0600202 """
George Keishing678b65c2017-08-31 16:16:39 -0500203
204 if state_set in set(VALID_BOOT_STATES[boot_type]):
George Keishingdf9ad392017-01-25 13:01:15 -0600205 return True
206 else:
207 return False
George Keishing90b555a2021-05-20 11:54:16 -0500208
209 def redfish_valid_boot_state(self, boot_type, state_dict):
210 r"""
211 Validate a given set of states is valid.
212
213 Description of argument(s):
214 boot_type Boot type (e.g. off/running/host booted
215 etc.)
216 state_dict State dictionary.
217 """
218
Patrick Williams20f38712022-12-08 06:18:26 -0600219 if set(state_dict.values()) in set(
220 REDFISH_VALID_BOOT_STATES[boot_type]
221 ):
George Keishing90b555a2021-05-20 11:54:16 -0500222 return True
223 else:
224 return False