blob: f8353de743d731e2efd55862ac7159eec3067f8f [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"""
Sridevi Ramesh47375aa2022-12-08 05:05:31 -06009
George Keishinge635ddc2022-12-08 07:38:02 -060010import gen_robot_keyword as keyword
11import variables as var
12
George Keishing678b65c2017-08-31 16:16:39 -050013import os
14import re
15import sys
16
George Keishingdf9ad392017-01-25 13:01:15 -060017from robot.libraries.BuiltIn import BuiltIn
18
George Keishing678b65c2017-08-31 16:16:39 -050019robot_pgm_dir_path = os.path.dirname(__file__) + os.sep
George Keishinge635ddc2022-12-08 07:38:02 -060020repo_data_dir_path = re.sub('/lib', '/data', robot_pgm_dir_path)
George Keishing678b65c2017-08-31 16:16:39 -050021sys.path.append(repo_data_dir_path)
22
George Keishing678b65c2017-08-31 16:16:39 -050023
George Keishingdf9ad392017-01-25 13:01:15 -060024BuiltIn().import_resource("state_manager.robot")
George Keishing678b65c2017-08-31 16:16:39 -050025BuiltIn().import_resource("rest_client.robot")
George Keishingdf9ad392017-01-25 13:01:15 -060026
George Keishinge635ddc2022-12-08 07:38:02 -060027platform_arch_type = os.environ.get('PLATFORM_ARCH_TYPE', '') or \
28 BuiltIn().get_variable_value("${PLATFORM_ARCH_TYPE}", default="power")
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +030029
George Keishingdf9ad392017-01-25 13:01:15 -060030# We will build eventually the mapping for warm, cold reset as well.
31VALID_STATES = {
George Keishinge635ddc2022-12-08 07:38:02 -060032 'reboot':
33 {
George Keishing678b65c2017-08-31 16:16:39 -050034 # (Power Policy, BMC state, Chassis State, Host State)
George Keishinge635ddc2022-12-08 07:38:02 -060035 ('LEAVE_OFF', 'Ready', 'Off', 'Off'),
36 ('ALWAYS_POWER_ON', 'Ready', 'On', 'Running'),
37 ('ALWAYS_POWER_ON', 'Ready', 'On', 'Off'),
38 ('RESTORE_LAST_STATE', 'Ready', 'On', 'Running'),
39 ('RESTORE_LAST_STATE', 'Ready', 'On', 'Off'),
40 ('ALWAYS_POWER_OFF', 'Ready', 'On', 'Running'),
41 ('ALWAYS_POWER_OFF', 'Ready', 'Off', 'Off'),
George Keishing678b65c2017-08-31 16:16:39 -050042 },
43}
44
45VALID_BOOT_STATES = {
George Keishinge635ddc2022-12-08 07:38:02 -060046 'Off': # Valid states when Host is Off.
47 {
George Keishing678b65c2017-08-31 16:16:39 -050048 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
49 (
50 "xyz.openbmc_project.State.BMC.BMCState.Ready",
51 "xyz.openbmc_project.State.Chassis.PowerState.Off",
52 "xyz.openbmc_project.State.Host.HostState.Off",
53 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified",
George Keishinge635ddc2022-12-08 07:38:02 -060054 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive"
George Keishing678b65c2017-08-31 16:16:39 -050055 ),
56 },
George Keishinge635ddc2022-12-08 07:38:02 -060057 'Reboot': # Valid states when BMC reset to standby.
58 {
George Keishing678b65c2017-08-31 16:16:39 -050059 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
60 (
61 "xyz.openbmc_project.State.BMC.BMCState.Ready",
62 "xyz.openbmc_project.State.Chassis.PowerState.Off",
63 "xyz.openbmc_project.State.Host.HostState.Off",
64 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified",
George Keishinge635ddc2022-12-08 07:38:02 -060065 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive"
George Keishing678b65c2017-08-31 16:16:39 -050066 ),
67 },
George Keishinge635ddc2022-12-08 07:38:02 -060068 'Running': # Valid states when Host is powering on.
69 {
George Keishing678b65c2017-08-31 16:16:39 -050070 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
71 (
72 "xyz.openbmc_project.State.BMC.BMCState.Ready",
73 "xyz.openbmc_project.State.Chassis.PowerState.On",
74 "xyz.openbmc_project.State.Host.HostState.Running",
75 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MotherboardInit",
George Keishinge635ddc2022-12-08 07:38:02 -060076 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive"
George Keishing678b65c2017-08-31 16:16:39 -050077 ),
78 },
George Keishinge635ddc2022-12-08 07:38:02 -060079 'Booted': # Valid state when Host is booted.
80 {
George Keishing678b65c2017-08-31 16:16:39 -050081 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
82 (
83 "xyz.openbmc_project.State.BMC.BMCState.Ready",
84 "xyz.openbmc_project.State.Chassis.PowerState.On",
85 "xyz.openbmc_project.State.Host.HostState.Running",
86 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
George Keishinge635ddc2022-12-08 07:38:02 -060087 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete"
George Keishing678b65c2017-08-31 16:16:39 -050088 ),
89 },
George Keishinge635ddc2022-12-08 07:38:02 -060090 'ResetReload': # Valid state BMC reset reload when host is booted.
91 {
George Keishing678b65c2017-08-31 16:16:39 -050092 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
93 (
94 "xyz.openbmc_project.State.BMC.BMCState.Ready",
95 "xyz.openbmc_project.State.Chassis.PowerState.On",
96 "xyz.openbmc_project.State.Host.HostState.Running",
97 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
George Keishinge635ddc2022-12-08 07:38:02 -060098 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete"
George Keishing678b65c2017-08-31 16:16:39 -050099 ),
George Keishingdf9ad392017-01-25 13:01:15 -0600100 },
101}
George Keishing90b555a2021-05-20 11:54:16 -0500102REDFISH_VALID_BOOT_STATES = {
George Keishinge635ddc2022-12-08 07:38:02 -0600103 'Off': # Valid states when Host is Off.
104 {
George Keishing90b555a2021-05-20 11:54:16 -0500105 # (BMC , Chassis , Host , BootProgress)
106 (
107 "Enabled",
108 "Off",
109 "Disabled",
110 "None",
111 ),
112 },
George Keishinge635ddc2022-12-08 07:38:02 -0600113 'Reboot': # Valid states when BMC reset to standby.
114 {
George Keishing90b555a2021-05-20 11:54:16 -0500115 # (BMC , Chassis , Host , BootProgress)
116 (
117 "Enabled",
118 "Off",
119 "Disabled",
120 "None",
121 ),
122 },
George Keishinge635ddc2022-12-08 07:38:02 -0600123 'Running': # Valid states when Host is powering on.
124 {
George Keishing90b555a2021-05-20 11:54:16 -0500125 # (BMC , Chassis , Host , BootProgress)
126 (
127 "Enabled",
128 "On",
129 "Enabled",
130 "OSRunning",
131 ),
132 },
George Keishinge635ddc2022-12-08 07:38:02 -0600133 'Booted': # Valid state when Host is booted.
134 {
George Keishing90b555a2021-05-20 11:54:16 -0500135 # (BMC , Chassis , Host , BootProgress)
136 (
137 "Enabled",
138 "On",
139 "Enabled",
140 "OSRunning",
141 ),
142 },
George Keishinge635ddc2022-12-08 07:38:02 -0600143 'ResetReload': # Valid state BMC reset reload when host is booted.
144 {
George Keishing90b555a2021-05-20 11:54:16 -0500145 # (BMC , Chassis , Host , BootProgress)
146 (
147 "Enabled",
148 "On",
149 "Enabled",
150 "OSRunning",
151 ),
152 },
153}
George Keishingdf9ad392017-01-25 13:01:15 -0600154
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300155if platform_arch_type == "x86":
156 VALID_BOOT_STATES_X86 = {}
157 for state_name, state_set in VALID_BOOT_STATES.items():
158 VALID_BOOT_STATES_X86[state_name] = set()
159 for state_tuple in state_set:
160 state_tuple_new = tuple(
161 x
162 for x in state_tuple
163 if not (
164 x.startswith("xyz.openbmc_project.State.Boot.Progress")
George Keishinge635ddc2022-12-08 07:38:02 -0600165 or x.startswith("xyz.openbmc_project.State.OperatingSystem")
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300166 )
167 )
168 VALID_BOOT_STATES_X86[state_name].add(state_tuple_new)
169 VALID_BOOT_STATES = VALID_BOOT_STATES_X86
170
George Keishingdf9ad392017-01-25 13:01:15 -0600171
George Keishinge635ddc2022-12-08 07:38:02 -0600172class state_map():
173
George Keishing678b65c2017-08-31 16:16:39 -0500174 def get_boot_state(self):
George Keishingdf9ad392017-01-25 13:01:15 -0600175 r"""
George Keishing678b65c2017-08-31 16:16:39 -0500176 Return the system state as a tuple of bmc, chassis, host state,
177 BootProgress and OperatingSystemState.
George Keishingdf9ad392017-01-25 13:01:15 -0600178 """
George Keishing678b65c2017-08-31 16:16:39 -0500179
George Keishinge635ddc2022-12-08 07:38:02 -0600180 status, state = keyword.run_key("Read Properties "
181 + var.SYSTEM_STATE_URI + "enumerate")
182 bmc_state = state[var.SYSTEM_STATE_URI + 'bmc0']['CurrentBMCState']
183 chassis_state = \
184 state[var.SYSTEM_STATE_URI + 'chassis0']['CurrentPowerState']
185 host_state = state[var.SYSTEM_STATE_URI + 'host0']['CurrentHostState']
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300186 if platform_arch_type == "x86":
George Keishinge635ddc2022-12-08 07:38:02 -0600187 return (str(bmc_state),
188 str(chassis_state),
189 str(host_state))
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300190 else:
George Keishinge635ddc2022-12-08 07:38:02 -0600191 boot_state = state[var.SYSTEM_STATE_URI + 'host0']['BootProgress']
192 os_state = \
193 state[var.SYSTEM_STATE_URI + 'host0']['OperatingSystemState']
George Keishing678b65c2017-08-31 16:16:39 -0500194
George Keishinge635ddc2022-12-08 07:38:02 -0600195 return (str(bmc_state),
196 str(chassis_state),
197 str(host_state),
198 str(boot_state),
199 str(os_state))
George Keishingdf9ad392017-01-25 13:01:15 -0600200
201 def valid_boot_state(self, boot_type, state_set):
202 r"""
203 Validate a given set of states is valid.
204
George Keishing678b65c2017-08-31 16:16:39 -0500205 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500206 boot_type Boot type (e.g. off/running/host booted
207 etc.)
208 state_set State set (e.g.bmc,chassis,host,
209 BootProgress,OperatingSystemState)
George Keishingdf9ad392017-01-25 13:01:15 -0600210 """
George Keishing678b65c2017-08-31 16:16:39 -0500211
212 if state_set in set(VALID_BOOT_STATES[boot_type]):
George Keishingdf9ad392017-01-25 13:01:15 -0600213 return True
214 else:
215 return False
George Keishing90b555a2021-05-20 11:54:16 -0500216
217 def redfish_valid_boot_state(self, boot_type, state_dict):
218 r"""
219 Validate a given set of states is valid.
220
221 Description of argument(s):
222 boot_type Boot type (e.g. off/running/host booted
223 etc.)
224 state_dict State dictionary.
225 """
226
George Keishinge635ddc2022-12-08 07:38:02 -0600227 if set(state_dict.values()) in set(REDFISH_VALID_BOOT_STATES[boot_type]):
George Keishing90b555a2021-05-20 11:54:16 -0500228 return True
229 else:
230 return False