blob: cdd1014522c429feb42f886cf0358ea7e9040757 [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 Keishing678b65c2017-08-31 16:16:39 -050010import os
11import re
12import sys
13
Patrick Williams57318182022-12-08 06:18:26 -060014import gen_robot_keyword as keyword
15import variables as var
George Keishingdf9ad392017-01-25 13:01:15 -060016from robot.libraries.BuiltIn import BuiltIn
17
George Keishing678b65c2017-08-31 16:16:39 -050018robot_pgm_dir_path = os.path.dirname(__file__) + os.sep
Patrick Williams57318182022-12-08 06:18:26 -060019repo_data_dir_path = re.sub("/lib", "/data", robot_pgm_dir_path)
George Keishing678b65c2017-08-31 16:16:39 -050020sys.path.append(repo_data_dir_path)
21
George Keishing678b65c2017-08-31 16:16:39 -050022
George Keishingdf9ad392017-01-25 13:01:15 -060023BuiltIn().import_resource("state_manager.robot")
George Keishing678b65c2017-08-31 16:16:39 -050024BuiltIn().import_resource("rest_client.robot")
George Keishingdf9ad392017-01-25 13:01:15 -060025
Patrick Williams57318182022-12-08 06:18:26 -060026platform_arch_type = os.environ.get(
27 "PLATFORM_ARCH_TYPE", ""
28) or 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 = {
Patrick Williams57318182022-12-08 06:18:26 -060032 "reboot": {
George Keishing678b65c2017-08-31 16:16:39 -050033 # (Power Policy, BMC state, Chassis State, Host State)
Patrick Williams57318182022-12-08 06:18:26 -060034 ("LEAVE_OFF", "Ready", "Off", "Off"),
35 ("ALWAYS_POWER_ON", "Ready", "On", "Running"),
36 ("ALWAYS_POWER_ON", "Ready", "On", "Off"),
37 ("RESTORE_LAST_STATE", "Ready", "On", "Running"),
38 ("RESTORE_LAST_STATE", "Ready", "On", "Off"),
39 ("ALWAYS_POWER_OFF", "Ready", "On", "Running"),
40 ("ALWAYS_POWER_OFF", "Ready", "Off", "Off"),
George Keishing678b65c2017-08-31 16:16:39 -050041 },
42}
43
44VALID_BOOT_STATES = {
Patrick Williams57318182022-12-08 06:18:26 -060045 "Off": { # Valid states when Host is Off.
George Keishing678b65c2017-08-31 16:16:39 -050046 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
47 (
48 "xyz.openbmc_project.State.BMC.BMCState.Ready",
49 "xyz.openbmc_project.State.Chassis.PowerState.Off",
50 "xyz.openbmc_project.State.Host.HostState.Off",
51 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified",
Patrick Williams57318182022-12-08 06:18:26 -060052 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
George Keishing678b65c2017-08-31 16:16:39 -050053 ),
54 },
Patrick Williams57318182022-12-08 06:18:26 -060055 "Reboot": { # Valid states when BMC reset to standby.
George Keishing678b65c2017-08-31 16:16:39 -050056 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
57 (
58 "xyz.openbmc_project.State.BMC.BMCState.Ready",
59 "xyz.openbmc_project.State.Chassis.PowerState.Off",
60 "xyz.openbmc_project.State.Host.HostState.Off",
61 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified",
Patrick Williams57318182022-12-08 06:18:26 -060062 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
George Keishing678b65c2017-08-31 16:16:39 -050063 ),
64 },
Patrick Williams57318182022-12-08 06:18:26 -060065 "Running": { # Valid states when Host is powering on.
George Keishing678b65c2017-08-31 16:16:39 -050066 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
67 (
68 "xyz.openbmc_project.State.BMC.BMCState.Ready",
69 "xyz.openbmc_project.State.Chassis.PowerState.On",
70 "xyz.openbmc_project.State.Host.HostState.Running",
71 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MotherboardInit",
Patrick Williams57318182022-12-08 06:18:26 -060072 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
George Keishing678b65c2017-08-31 16:16:39 -050073 ),
74 },
Patrick Williams57318182022-12-08 06:18:26 -060075 "Booted": { # Valid state when Host is booted.
George Keishing678b65c2017-08-31 16:16:39 -050076 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
77 (
78 "xyz.openbmc_project.State.BMC.BMCState.Ready",
79 "xyz.openbmc_project.State.Chassis.PowerState.On",
80 "xyz.openbmc_project.State.Host.HostState.Running",
81 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
Patrick Williams57318182022-12-08 06:18:26 -060082 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete",
George Keishing678b65c2017-08-31 16:16:39 -050083 ),
84 },
Patrick Williams57318182022-12-08 06:18:26 -060085 "ResetReload": { # Valid state BMC reset reload when host is booted.
George Keishing678b65c2017-08-31 16:16:39 -050086 # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
87 (
88 "xyz.openbmc_project.State.BMC.BMCState.Ready",
89 "xyz.openbmc_project.State.Chassis.PowerState.On",
90 "xyz.openbmc_project.State.Host.HostState.Running",
91 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
Patrick Williams57318182022-12-08 06:18:26 -060092 "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete",
George Keishing678b65c2017-08-31 16:16:39 -050093 ),
George Keishingdf9ad392017-01-25 13:01:15 -060094 },
95}
George Keishing90b555a2021-05-20 11:54:16 -050096REDFISH_VALID_BOOT_STATES = {
Patrick Williams57318182022-12-08 06:18:26 -060097 "Off": { # Valid states when Host is Off.
George Keishing90b555a2021-05-20 11:54:16 -050098 # (BMC , Chassis , Host , BootProgress)
99 (
100 "Enabled",
101 "Off",
102 "Disabled",
103 "None",
104 ),
105 },
Patrick Williams57318182022-12-08 06:18:26 -0600106 "Reboot": { # Valid states when BMC reset to standby.
George Keishing90b555a2021-05-20 11:54:16 -0500107 # (BMC , Chassis , Host , BootProgress)
108 (
109 "Enabled",
110 "Off",
111 "Disabled",
112 "None",
113 ),
114 },
Patrick Williams57318182022-12-08 06:18:26 -0600115 "Running": { # Valid states when Host is powering on.
George Keishing90b555a2021-05-20 11:54:16 -0500116 # (BMC , Chassis , Host , BootProgress)
117 (
118 "Enabled",
119 "On",
120 "Enabled",
121 "OSRunning",
122 ),
123 },
Patrick Williams57318182022-12-08 06:18:26 -0600124 "Booted": { # Valid state when Host is booted.
George Keishing90b555a2021-05-20 11:54:16 -0500125 # (BMC , Chassis , Host , BootProgress)
126 (
127 "Enabled",
128 "On",
129 "Enabled",
130 "OSRunning",
131 ),
132 },
Patrick Williams57318182022-12-08 06:18:26 -0600133 "ResetReload": { # Valid state BMC reset reload when host is booted.
George Keishing90b555a2021-05-20 11:54:16 -0500134 # (BMC , Chassis , Host , BootProgress)
135 (
136 "Enabled",
137 "On",
138 "Enabled",
139 "OSRunning",
140 ),
141 },
142}
George Keishingdf9ad392017-01-25 13:01:15 -0600143
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300144if platform_arch_type == "x86":
145 VALID_BOOT_STATES_X86 = {}
146 for state_name, state_set in VALID_BOOT_STATES.items():
147 VALID_BOOT_STATES_X86[state_name] = set()
148 for state_tuple in state_set:
149 state_tuple_new = tuple(
150 x
151 for x in state_tuple
152 if not (
153 x.startswith("xyz.openbmc_project.State.Boot.Progress")
Patrick Williams57318182022-12-08 06:18:26 -0600154 or x.startswith(
155 "xyz.openbmc_project.State.OperatingSystem"
156 )
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300157 )
158 )
159 VALID_BOOT_STATES_X86[state_name].add(state_tuple_new)
160 VALID_BOOT_STATES = VALID_BOOT_STATES_X86
161
George Keishingdf9ad392017-01-25 13:01:15 -0600162
Patrick Williams57318182022-12-08 06:18:26 -0600163class state_map:
George Keishing678b65c2017-08-31 16:16:39 -0500164 def get_boot_state(self):
George Keishingdf9ad392017-01-25 13:01:15 -0600165 r"""
George Keishing678b65c2017-08-31 16:16:39 -0500166 Return the system state as a tuple of bmc, chassis, host state,
167 BootProgress and OperatingSystemState.
George Keishingdf9ad392017-01-25 13:01:15 -0600168 """
George Keishing678b65c2017-08-31 16:16:39 -0500169
Patrick Williams57318182022-12-08 06:18:26 -0600170 status, state = keyword.run_key(
171 "Read Properties " + var.SYSTEM_STATE_URI + "enumerate"
172 )
173 bmc_state = state[var.SYSTEM_STATE_URI + "bmc0"]["CurrentBMCState"]
174 chassis_state = state[var.SYSTEM_STATE_URI + "chassis0"][
175 "CurrentPowerState"
176 ]
177 host_state = state[var.SYSTEM_STATE_URI + "host0"]["CurrentHostState"]
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300178 if platform_arch_type == "x86":
Patrick Williams57318182022-12-08 06:18:26 -0600179 return (str(bmc_state), str(chassis_state), str(host_state))
Konstantin Aladyshevd78dc932021-03-24 18:17:17 +0300180 else:
Patrick Williams57318182022-12-08 06:18:26 -0600181 boot_state = state[var.SYSTEM_STATE_URI + "host0"]["BootProgress"]
182 os_state = state[var.SYSTEM_STATE_URI + "host0"][
183 "OperatingSystemState"
184 ]
George Keishing678b65c2017-08-31 16:16:39 -0500185
Patrick Williams57318182022-12-08 06:18:26 -0600186 return (
187 str(bmc_state),
188 str(chassis_state),
189 str(host_state),
190 str(boot_state),
191 str(os_state),
192 )
George Keishingdf9ad392017-01-25 13:01:15 -0600193
194 def valid_boot_state(self, boot_type, state_set):
195 r"""
196 Validate a given set of states is valid.
197
George Keishing678b65c2017-08-31 16:16:39 -0500198 Description of argument(s):
Joy Onyerikwu004ad3c2018-06-11 16:29:56 -0500199 boot_type Boot type (e.g. off/running/host booted
200 etc.)
201 state_set State set (e.g.bmc,chassis,host,
202 BootProgress,OperatingSystemState)
George Keishingdf9ad392017-01-25 13:01:15 -0600203 """
George Keishing678b65c2017-08-31 16:16:39 -0500204
205 if state_set in set(VALID_BOOT_STATES[boot_type]):
George Keishingdf9ad392017-01-25 13:01:15 -0600206 return True
207 else:
208 return False
George Keishing90b555a2021-05-20 11:54:16 -0500209
210 def redfish_valid_boot_state(self, boot_type, state_dict):
211 r"""
212 Validate a given set of states is valid.
213
214 Description of argument(s):
215 boot_type Boot type (e.g. off/running/host booted
216 etc.)
217 state_dict State dictionary.
218 """
219
Patrick Williams57318182022-12-08 06:18:26 -0600220 if set(state_dict.values()) in set(
221 REDFISH_VALID_BOOT_STATES[boot_type]
222 ):
George Keishing90b555a2021-05-20 11:54:16 -0500223 return True
224 else:
225 return False