1#!/usr/bin/env python
2
3r"""
4Companion file to utils.robot.
5"""
6
7import gen_print as gp
8import gen_robot_keyword as grk
9from robot.libraries.BuiltIn import BuiltIn
10
11
12###############################################################################
13def set_power_policy_method():
14
15    r"""
16    Set the global bmc_power_policy_method to either 'Old' or 'New'.
17
18    The power policy data has moved from an 'org' location to an 'xyz'
19    location.  This keyword will determine whether the new method of getting
20    the power policy is valid and will set the global bmc_power_policy_method
21    variable accordingly.  If power_policy_setup is already set (by a prior
22    call to this function), this keyword will simply return.
23
24    If bmc_power_policy_method is "Old", this function will adjust the global
25    policy variables from data/variables.py: RESTORE_LAST_STATE,
26    ALWAYS_POWER_ON, ALWAYS_POWER_OFF.
27    """
28
29    # Retrieve global variables.
30    power_policy_setup = \
31        int(BuiltIn().get_variable_value("${power_policy_setup}",
32                                         default=0))
33    bmc_power_policy_method = \
34        BuiltIn().get_variable_value("${bmc_power_policy_method}",
35                                     default=0)
36    gp.dpvar(power_policy_setup)
37
38    # If this function has already been run once, we need not continue.
39    if power_policy_setup:
40        return
41
42    gp.dpvar(bmc_power_policy_method, 1)
43
44    # The user has not set bmc_power_policy_method via a -v parm so we will
45    # determine what it should be.
46    if bmc_power_policy_method == "":
47        status, ret_values = grk.run_key_u("New Get Power Policy", ignore=1)
48        if status == 'PASS':
49            bmc_power_policy_method = 'New'
50        else:
51            bmc_power_policy_method = 'Old'
52
53    gp.qpvar(bmc_power_policy_method)
54    # For old style, we will rewrite these global variable settings to old
55    # values.
56    if bmc_power_policy_method == "Old":
57        BuiltIn().set_global_variable("${RESTORE_LAST_STATE}",
58                                      "RESTORE_LAST_STATE")
59        BuiltIn().set_global_variable("${ALWAYS_POWER_ON}",
60                                      "ALWAYS_POWER_ON")
61        BuiltIn().set_global_variable("${ALWAYS_POWER_OFF}",
62                                      "ALWAYS_POWER_OFF")
63
64    # Set global variables to control subsequent calls to this function.
65    BuiltIn().set_global_variable("${bmc_power_policy_method}",
66                                  bmc_power_policy_method)
67    BuiltIn().set_global_variable("${power_policy_setup}", 1)
68
69
70###############################################################################
71
72
73###############################################################################
74def translate_power_policy_value(policy):
75
76    r"""
77    Translate the policy value and return the result.
78
79    Using old style functions, callers might call like this with a hard-
80    code value for policy:
81
82    Set BMC Power Policy  RESTORE_LAST_STATE
83
84    This function will get the value of the corresponding global variable (if
85    it exists) and return it.
86
87    This will allow the old style call to still work on systems using the new
88    method of storing the policy value.
89    """
90
91    valid_power_policy_vars = \
92        BuiltIn().get_variable_value("${valid_power_policy_vars}")
93
94    if policy not in valid_power_policy_vars:
95        return policy
96
97    status, ret_values = grk.run_key_u("Get Variable Value  ${" + policy + "}",
98                                       quiet=1)
99    return ret_values
100
101###############################################################################
102