1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2df9ad391SGeorge Keishing
3df9ad391SGeorge Keishingr"""
4df9ad391SGeorge KeishingState Manager module:
5df9ad391SGeorge Keishing
6df9ad391SGeorge Keishing   - Defines Valid states of the system
7df9ad391SGeorge Keishing
8df9ad391SGeorge Keishing"""
9678b65c0SGeorge Keishingimport os
10678b65c0SGeorge Keishingimport re
11678b65c0SGeorge Keishingimport sys
12678b65c0SGeorge Keishing
13df9ad391SGeorge Keishingfrom robot.libraries.BuiltIn import BuiltIn
14df9ad391SGeorge Keishing
15678b65c0SGeorge Keishingrobot_pgm_dir_path = os.path.dirname(__file__) + os.sep
16*20f38712SPatrick Williamsrepo_data_dir_path = re.sub("/lib", "/data", robot_pgm_dir_path)
17678b65c0SGeorge Keishingsys.path.append(repo_data_dir_path)
18678b65c0SGeorge Keishing
1909679890SGeorge Keishingimport gen_robot_keyword as keyword  # NOQA
2009679890SGeorge Keishingimport variables as var  # NOQA
21678b65c0SGeorge Keishing
22df9ad391SGeorge KeishingBuiltIn().import_resource("state_manager.robot")
23678b65c0SGeorge KeishingBuiltIn().import_resource("rest_client.robot")
24df9ad391SGeorge Keishing
25*20f38712SPatrick Williamsplatform_arch_type = os.environ.get(
26*20f38712SPatrick Williams    "PLATFORM_ARCH_TYPE", ""
27*20f38712SPatrick Williams) or BuiltIn().get_variable_value("${PLATFORM_ARCH_TYPE}", default="power")
28d78dc937SKonstantin Aladyshev
29df9ad391SGeorge Keishing# We will build eventually the mapping for warm, cold reset as well.
30df9ad391SGeorge KeishingVALID_STATES = {
31*20f38712SPatrick Williams    "reboot": {
324d6f3d66SGeorge Keishing        # (Power Policy, BMC state, Chassis State, Host State)
33*20f38712SPatrick Williams        ("LEAVE_OFF", "Ready", "Off", "Off"),
34*20f38712SPatrick Williams        ("ALWAYS_POWER_ON", "Ready", "On", "Running"),
35*20f38712SPatrick Williams        ("ALWAYS_POWER_ON", "Ready", "On", "Off"),
36*20f38712SPatrick Williams        ("RESTORE_LAST_STATE", "Ready", "On", "Running"),
37*20f38712SPatrick Williams        ("RESTORE_LAST_STATE", "Ready", "On", "Off"),
38*20f38712SPatrick Williams        ("ALWAYS_POWER_OFF", "Ready", "On", "Running"),
39*20f38712SPatrick Williams        ("ALWAYS_POWER_OFF", "Ready", "Off", "Off"),
40df9ad391SGeorge Keishing    },
41df9ad391SGeorge Keishing}
42df9ad391SGeorge Keishing
43678b65c0SGeorge KeishingVALID_BOOT_STATES = {
44*20f38712SPatrick Williams    "Off": {  # Valid states when Host is Off.
45678b65c0SGeorge Keishing        # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
46678b65c0SGeorge Keishing        (
47678b65c0SGeorge Keishing            "xyz.openbmc_project.State.BMC.BMCState.Ready",
48678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Chassis.PowerState.Off",
49678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Host.HostState.Off",
50678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified",
51*20f38712SPatrick Williams            "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
52678b65c0SGeorge Keishing        ),
53678b65c0SGeorge Keishing    },
54*20f38712SPatrick Williams    "Reboot": {  # Valid states when BMC reset to standby.
55678b65c0SGeorge Keishing        # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
56678b65c0SGeorge Keishing        (
57678b65c0SGeorge Keishing            "xyz.openbmc_project.State.BMC.BMCState.Ready",
58678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Chassis.PowerState.Off",
59678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Host.HostState.Off",
60678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Boot.Progress.ProgressStages.Unspecified",
61*20f38712SPatrick Williams            "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
62678b65c0SGeorge Keishing        ),
63678b65c0SGeorge Keishing    },
64*20f38712SPatrick Williams    "Running": {  # Valid states when Host is powering on.
65678b65c0SGeorge Keishing        # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
66678b65c0SGeorge Keishing        (
67678b65c0SGeorge Keishing            "xyz.openbmc_project.State.BMC.BMCState.Ready",
68678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Chassis.PowerState.On",
69678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Host.HostState.Running",
70678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MotherboardInit",
71*20f38712SPatrick Williams            "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.Inactive",
72678b65c0SGeorge Keishing        ),
73678b65c0SGeorge Keishing    },
74*20f38712SPatrick Williams    "Booted": {  # Valid state when Host is booted.
75678b65c0SGeorge Keishing        # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
76678b65c0SGeorge Keishing        (
77678b65c0SGeorge Keishing            "xyz.openbmc_project.State.BMC.BMCState.Ready",
78678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Chassis.PowerState.On",
79678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Host.HostState.Running",
80678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
81*20f38712SPatrick Williams            "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete",
82678b65c0SGeorge Keishing        ),
83678b65c0SGeorge Keishing    },
84*20f38712SPatrick Williams    "ResetReload": {  # Valid state BMC reset reload when host is booted.
85678b65c0SGeorge Keishing        # (BMC , Chassis , Host , BootProgress, OperatingSystemState)
86678b65c0SGeorge Keishing        (
87678b65c0SGeorge Keishing            "xyz.openbmc_project.State.BMC.BMCState.Ready",
88678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Chassis.PowerState.On",
89678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Host.HostState.Running",
90678b65c0SGeorge Keishing            "xyz.openbmc_project.State.Boot.Progress.ProgressStages.OSStart",
91*20f38712SPatrick Williams            "xyz.openbmc_project.State.OperatingSystem.Status.OSStatus.BootComplete",
92678b65c0SGeorge Keishing        ),
93678b65c0SGeorge Keishing    },
94678b65c0SGeorge Keishing}
9590b555aaSGeorge KeishingREDFISH_VALID_BOOT_STATES = {
96*20f38712SPatrick Williams    "Off": {  # Valid states when Host is Off.
9790b555aaSGeorge Keishing        # (BMC , Chassis , Host , BootProgress)
9890b555aaSGeorge Keishing        (
9990b555aaSGeorge Keishing            "Enabled",
10090b555aaSGeorge Keishing            "Off",
10190b555aaSGeorge Keishing            "Disabled",
10290b555aaSGeorge Keishing            "None",
10390b555aaSGeorge Keishing        ),
10490b555aaSGeorge Keishing    },
105*20f38712SPatrick Williams    "Reboot": {  # Valid states when BMC reset to standby.
10690b555aaSGeorge Keishing        # (BMC , Chassis , Host , BootProgress)
10790b555aaSGeorge Keishing        (
10890b555aaSGeorge Keishing            "Enabled",
10990b555aaSGeorge Keishing            "Off",
11090b555aaSGeorge Keishing            "Disabled",
11190b555aaSGeorge Keishing            "None",
11290b555aaSGeorge Keishing        ),
11390b555aaSGeorge Keishing    },
114*20f38712SPatrick Williams    "Running": {  # Valid states when Host is powering on.
11590b555aaSGeorge Keishing        # (BMC , Chassis , Host , BootProgress)
11690b555aaSGeorge Keishing        (
11790b555aaSGeorge Keishing            "Enabled",
11890b555aaSGeorge Keishing            "On",
11990b555aaSGeorge Keishing            "Enabled",
12090b555aaSGeorge Keishing            "OSRunning",
12190b555aaSGeorge Keishing        ),
12290b555aaSGeorge Keishing    },
123*20f38712SPatrick Williams    "Booted": {  # Valid state when Host is booted.
12490b555aaSGeorge Keishing        # (BMC , Chassis , Host , BootProgress)
12590b555aaSGeorge Keishing        (
12690b555aaSGeorge Keishing            "Enabled",
12790b555aaSGeorge Keishing            "On",
12890b555aaSGeorge Keishing            "Enabled",
12990b555aaSGeorge Keishing            "OSRunning",
13090b555aaSGeorge Keishing        ),
13190b555aaSGeorge Keishing    },
132*20f38712SPatrick Williams    "ResetReload": {  # Valid state BMC reset reload when host is booted.
13390b555aaSGeorge Keishing        # (BMC , Chassis , Host , BootProgress)
13490b555aaSGeorge Keishing        (
13590b555aaSGeorge Keishing            "Enabled",
13690b555aaSGeorge Keishing            "On",
13790b555aaSGeorge Keishing            "Enabled",
13890b555aaSGeorge Keishing            "OSRunning",
13990b555aaSGeorge Keishing        ),
14090b555aaSGeorge Keishing    },
14190b555aaSGeorge Keishing}
142df9ad391SGeorge Keishing
143d78dc937SKonstantin Aladyshevif platform_arch_type == "x86":
144d78dc937SKonstantin Aladyshev    VALID_BOOT_STATES_X86 = {}
145d78dc937SKonstantin Aladyshev    for state_name, state_set in VALID_BOOT_STATES.items():
146d78dc937SKonstantin Aladyshev        VALID_BOOT_STATES_X86[state_name] = set()
147d78dc937SKonstantin Aladyshev        for state_tuple in state_set:
148d78dc937SKonstantin Aladyshev            state_tuple_new = tuple(
149d78dc937SKonstantin Aladyshev                x
150d78dc937SKonstantin Aladyshev                for x in state_tuple
151d78dc937SKonstantin Aladyshev                if not (
152d78dc937SKonstantin Aladyshev                    x.startswith("xyz.openbmc_project.State.Boot.Progress")
153*20f38712SPatrick Williams                    or x.startswith(
154*20f38712SPatrick Williams                        "xyz.openbmc_project.State.OperatingSystem"
155*20f38712SPatrick Williams                    )
156d78dc937SKonstantin Aladyshev                )
157d78dc937SKonstantin Aladyshev            )
158d78dc937SKonstantin Aladyshev            VALID_BOOT_STATES_X86[state_name].add(state_tuple_new)
159d78dc937SKonstantin Aladyshev    VALID_BOOT_STATES = VALID_BOOT_STATES_X86
160d78dc937SKonstantin Aladyshev
161678b65c0SGeorge Keishing
162*20f38712SPatrick Williamsclass state_map:
163678b65c0SGeorge Keishing    def get_boot_state(self):
164df9ad391SGeorge Keishing        r"""
165678b65c0SGeorge Keishing        Return the system state as a tuple of bmc, chassis, host state,
166678b65c0SGeorge Keishing        BootProgress and OperatingSystemState.
167df9ad391SGeorge Keishing        """
168678b65c0SGeorge Keishing
169*20f38712SPatrick Williams        status, state = keyword.run_key(
170*20f38712SPatrick Williams            "Read Properties  " + var.SYSTEM_STATE_URI + "enumerate"
171*20f38712SPatrick Williams        )
172*20f38712SPatrick Williams        bmc_state = state[var.SYSTEM_STATE_URI + "bmc0"]["CurrentBMCState"]
173*20f38712SPatrick Williams        chassis_state = state[var.SYSTEM_STATE_URI + "chassis0"][
174*20f38712SPatrick Williams            "CurrentPowerState"
175*20f38712SPatrick Williams        ]
176*20f38712SPatrick Williams        host_state = state[var.SYSTEM_STATE_URI + "host0"]["CurrentHostState"]
177d78dc937SKonstantin Aladyshev        if platform_arch_type == "x86":
178*20f38712SPatrick Williams            return (str(bmc_state), str(chassis_state), str(host_state))
179d78dc937SKonstantin Aladyshev        else:
180*20f38712SPatrick Williams            boot_state = state[var.SYSTEM_STATE_URI + "host0"]["BootProgress"]
181*20f38712SPatrick Williams            os_state = state[var.SYSTEM_STATE_URI + "host0"][
182*20f38712SPatrick Williams                "OperatingSystemState"
183*20f38712SPatrick Williams            ]
184678b65c0SGeorge Keishing
185*20f38712SPatrick Williams            return (
186*20f38712SPatrick Williams                str(bmc_state),
1874d6f3d66SGeorge Keishing                str(chassis_state),
188678b65c0SGeorge Keishing                str(host_state),
189678b65c0SGeorge Keishing                str(boot_state),
190*20f38712SPatrick Williams                str(os_state),
191*20f38712SPatrick Williams            )
192df9ad391SGeorge Keishing
193df9ad391SGeorge Keishing    def valid_boot_state(self, boot_type, state_set):
194df9ad391SGeorge Keishing        r"""
195df9ad391SGeorge Keishing        Validate a given set of states is valid.
196df9ad391SGeorge Keishing
197678b65c0SGeorge Keishing        Description of argument(s):
198004ad3c9SJoy Onyerikwu        boot_type                   Boot type (e.g. off/running/host booted
199004ad3c9SJoy Onyerikwu                                    etc.)
200004ad3c9SJoy Onyerikwu        state_set                   State set (e.g.bmc,chassis,host,
201004ad3c9SJoy Onyerikwu                                    BootProgress,OperatingSystemState)
202df9ad391SGeorge Keishing        """
203678b65c0SGeorge Keishing
204678b65c0SGeorge Keishing        if state_set in set(VALID_BOOT_STATES[boot_type]):
205df9ad391SGeorge Keishing            return True
206df9ad391SGeorge Keishing        else:
207df9ad391SGeorge Keishing            return False
20890b555aaSGeorge Keishing
20990b555aaSGeorge Keishing    def redfish_valid_boot_state(self, boot_type, state_dict):
21090b555aaSGeorge Keishing        r"""
21190b555aaSGeorge Keishing        Validate a given set of states is valid.
21290b555aaSGeorge Keishing
21390b555aaSGeorge Keishing        Description of argument(s):
21490b555aaSGeorge Keishing        boot_type                   Boot type (e.g. off/running/host booted
21590b555aaSGeorge Keishing                                    etc.)
21690b555aaSGeorge Keishing        state_dict                  State dictionary.
21790b555aaSGeorge Keishing        """
21890b555aaSGeorge Keishing
219*20f38712SPatrick Williams        if set(state_dict.values()) in set(
220*20f38712SPatrick Williams            REDFISH_VALID_BOOT_STATES[boot_type]
221*20f38712SPatrick Williams        ):
22290b555aaSGeorge Keishing            return True
22390b555aaSGeorge Keishing        else:
22490b555aaSGeorge Keishing            return False
225