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