| George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 2 |  | 
|  | 3 | r""" | 
|  | 4 | State Manager module: | 
|  | 5 |  | 
|  | 6 | - Defines Valid states of the system | 
|  | 7 |  | 
|  | 8 | """ | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 9 | import os | 
|  | 10 | import re | 
|  | 11 | import sys | 
|  | 12 |  | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 13 | from robot.libraries.BuiltIn import BuiltIn | 
|  | 14 |  | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 15 | robot_pgm_dir_path = os.path.dirname(__file__) + os.sep | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 16 | repo_data_dir_path = re.sub("/lib", "/data", robot_pgm_dir_path) | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 17 | sys.path.append(repo_data_dir_path) | 
|  | 18 |  | 
| George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 19 | import gen_robot_keyword as keyword  # NOQA | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 20 | import variables as var  # NOQA | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 21 |  | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 22 | BuiltIn().import_resource("state_manager.robot") | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 23 | BuiltIn().import_resource("rest_client.robot") | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 24 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 25 | platform_arch_type = os.environ.get( | 
|  | 26 | "PLATFORM_ARCH_TYPE", "" | 
|  | 27 | ) or BuiltIn().get_variable_value("${PLATFORM_ARCH_TYPE}", default="power") | 
| Konstantin Aladyshev | d78dc93 | 2021-03-24 18:17:17 +0300 | [diff] [blame] | 28 |  | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 29 | # We will build eventually the mapping for warm, cold reset as well. | 
|  | 30 | VALID_STATES = { | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 31 | "reboot": { | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 32 | # (Power Policy, BMC state, Chassis State, Host State) | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 33 | ("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 Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 40 | }, | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | VALID_BOOT_STATES = { | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 44 | "Off": {  # Valid states when Host is Off. | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 45 | # (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 Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 51 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive", | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 52 | ), | 
|  | 53 | }, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 54 | "Reboot": {  # Valid states when BMC reset to standby. | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 55 | # (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 Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 61 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive", | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 62 | ), | 
|  | 63 | }, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 64 | "Running": {  # Valid states when Host is powering on. | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 65 | # (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 Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 71 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive", | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 72 | ), | 
|  | 73 | }, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 74 | "Booted": {  # Valid state when Host is booted. | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 75 | # (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 Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 81 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete", | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 82 | ), | 
|  | 83 | }, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 84 | "ResetReload": {  # Valid state BMC reset reload when host is booted. | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 85 | # (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 Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 91 | "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete", | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 92 | ), | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 93 | }, | 
|  | 94 | } | 
| George Keishing | 90b555a | 2021-05-20 11:54:16 -0500 | [diff] [blame] | 95 | REDFISH_VALID_BOOT_STATES = { | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 96 | "Off": {  # Valid states when Host is Off. | 
| George Keishing | 90b555a | 2021-05-20 11:54:16 -0500 | [diff] [blame] | 97 | # (BMC , Chassis , Host , BootProgress) | 
|  | 98 | ( | 
|  | 99 | "Enabled", | 
|  | 100 | "Off", | 
|  | 101 | "Disabled", | 
|  | 102 | "None", | 
|  | 103 | ), | 
|  | 104 | }, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 105 | "Reboot": {  # Valid states when BMC reset to standby. | 
| George Keishing | 90b555a | 2021-05-20 11:54:16 -0500 | [diff] [blame] | 106 | # (BMC , Chassis , Host , BootProgress) | 
|  | 107 | ( | 
|  | 108 | "Enabled", | 
|  | 109 | "Off", | 
|  | 110 | "Disabled", | 
|  | 111 | "None", | 
|  | 112 | ), | 
|  | 113 | }, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 114 | "Running": {  # Valid states when Host is powering on. | 
| George Keishing | 90b555a | 2021-05-20 11:54:16 -0500 | [diff] [blame] | 115 | # (BMC , Chassis , Host , BootProgress) | 
|  | 116 | ( | 
|  | 117 | "Enabled", | 
|  | 118 | "On", | 
|  | 119 | "Enabled", | 
|  | 120 | "OSRunning", | 
|  | 121 | ), | 
|  | 122 | }, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 123 | "Booted": {  # Valid state when Host is booted. | 
| George Keishing | 90b555a | 2021-05-20 11:54:16 -0500 | [diff] [blame] | 124 | # (BMC , Chassis , Host , BootProgress) | 
|  | 125 | ( | 
|  | 126 | "Enabled", | 
|  | 127 | "On", | 
|  | 128 | "Enabled", | 
|  | 129 | "OSRunning", | 
|  | 130 | ), | 
|  | 131 | }, | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 132 | "ResetReload": {  # Valid state BMC reset reload when host is booted. | 
| George Keishing | 90b555a | 2021-05-20 11:54:16 -0500 | [diff] [blame] | 133 | # (BMC , Chassis , Host , BootProgress) | 
|  | 134 | ( | 
|  | 135 | "Enabled", | 
|  | 136 | "On", | 
|  | 137 | "Enabled", | 
|  | 138 | "OSRunning", | 
|  | 139 | ), | 
|  | 140 | }, | 
|  | 141 | } | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 142 |  | 
| Konstantin Aladyshev | d78dc93 | 2021-03-24 18:17:17 +0300 | [diff] [blame] | 143 | if 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 Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 153 | or x.startswith( | 
|  | 154 | "xyz.openbmc_project.State.OperatingSystem" | 
|  | 155 | ) | 
| Konstantin Aladyshev | d78dc93 | 2021-03-24 18:17:17 +0300 | [diff] [blame] | 156 | ) | 
|  | 157 | ) | 
|  | 158 | VALID_BOOT_STATES_X86[state_name].add(state_tuple_new) | 
|  | 159 | VALID_BOOT_STATES = VALID_BOOT_STATES_X86 | 
|  | 160 |  | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 161 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 162 | class state_map: | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 163 | def get_boot_state(self): | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 164 | r""" | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 165 | Return the system state as a tuple of bmc, chassis, host state, | 
|  | 166 | BootProgress and OperatingSystemState. | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 167 | """ | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 168 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 169 | 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 Aladyshev | d78dc93 | 2021-03-24 18:17:17 +0300 | [diff] [blame] | 177 | if platform_arch_type == "x86": | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 178 | return (str(bmc_state), str(chassis_state), str(host_state)) | 
| Konstantin Aladyshev | d78dc93 | 2021-03-24 18:17:17 +0300 | [diff] [blame] | 179 | else: | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 180 | boot_state = state[var.SYSTEM_STATE_URI + "host0"]["BootProgress"] | 
|  | 181 | os_state = state[var.SYSTEM_STATE_URI + "host0"][ | 
|  | 182 | "OperatingSystemState" | 
|  | 183 | ] | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 184 |  | 
| Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 185 | return ( | 
|  | 186 | str(bmc_state), | 
|  | 187 | str(chassis_state), | 
|  | 188 | str(host_state), | 
|  | 189 | str(boot_state), | 
|  | 190 | str(os_state), | 
|  | 191 | ) | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 192 |  | 
|  | 193 | def valid_boot_state(self, boot_type, state_set): | 
|  | 194 | r""" | 
|  | 195 | Validate a given set of states is valid. | 
|  | 196 |  | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 197 | Description of argument(s): | 
| Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 198 | 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 Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 202 | """ | 
| George Keishing | 678b65c | 2017-08-31 16:16:39 -0500 | [diff] [blame] | 203 |  | 
|  | 204 | if state_set in set(VALID_BOOT_STATES[boot_type]): | 
| George Keishing | df9ad39 | 2017-01-25 13:01:15 -0600 | [diff] [blame] | 205 | return True | 
|  | 206 | else: | 
|  | 207 | return False | 
| George Keishing | 90b555a | 2021-05-20 11:54:16 -0500 | [diff] [blame] | 208 |  | 
|  | 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 Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 219 | if set(state_dict.values()) in set( | 
|  | 220 | REDFISH_VALID_BOOT_STATES[boot_type] | 
|  | 221 | ): | 
| George Keishing | 90b555a | 2021-05-20 11:54:16 -0500 | [diff] [blame] | 222 | return True | 
|  | 223 | else: | 
|  | 224 | return False |