Power policy: Determine old or new location.

The Power Policy value is moving from its old location:

/org/openbmc/settings/host0/attr/power_policy

To a new location:

/xyz/openbmc_project/control/host0/power_restore_policy/attr/PowerRestorePolicy

This change allows power policy keywords to figure out whether the new
location is legitimate.  If so, it will read/write the value using the
new location.

Change-Id: I0cba136219ad22e47ff2c58a6d0b6dacec4e7451
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/extended/code_update/update_bmc.robot b/extended/code_update/update_bmc.robot
index a9a0968..bf965b3 100644
--- a/extended/code_update/update_bmc.robot
+++ b/extended/code_update/update_bmc.robot
@@ -144,10 +144,7 @@
     Check Boot Count And Time
     Prune Journal Log
     Power Off Request
-    ${status}=  Run Keyword And Return Status  Set BMC Power Policy
-    Run Keyword If  '${status}' == '${False}'
-    ...  Set BMC Power Policy
-    ...  ${HOST_SETTING}  power_policy  RESTORE_LAST_STATE
+    Set BMC Power Policy  RESTORE_LAST_STATE
 
     Prepare For Update
     Check If BMC is Up  20 min  10 sec
diff --git a/lib/obmc_boot_test.py b/lib/obmc_boot_test.py
index 76f804d..07e9f77 100755
--- a/lib/obmc_boot_test.py
+++ b/lib/obmc_boot_test.py
@@ -350,9 +350,7 @@
 
     grp.rqprint_pgm_header()
 
-    # TODO: Need to fix to switch between old and new.
-    # Disabling for a brief moment.
-    #grk.run_key("Set BMC Power Policy  ${RESTORE_LAST_STATE}")
+    grk.run_key("Set BMC Power Policy  RESTORE_LAST_STATE")
 
     initial_plug_in_setup()
 
diff --git a/lib/utils.py b/lib/utils.py
new file mode 100644
index 0000000..6a3b809
--- /dev/null
+++ b/lib/utils.py
@@ -0,0 +1,101 @@
+#!/usr/bin/env python
+
+r"""
+Companion file to utils.robot.
+"""
+
+import gen_print as gp
+import gen_robot_keyword as grk
+from robot.libraries.BuiltIn import BuiltIn
+
+
+###############################################################################
+def set_power_policy_method():
+
+    r"""
+    Set the global bmc_power_policy_method to either 'Old' or 'New'.
+
+    The power policy data has moved from an 'org' location to an 'xyz'
+    location.  This keyword will determine whether the new method of getting
+    the power policy is valid and will set the global bmc_power_policy_method
+    variable accordingly.  If power_policy_setup is already set (by a prior
+    call to this function), this keyword will simply return.
+
+    If bmc_power_policy_method is "Old", this function will adjust the global
+    policy variables from data/variables.py: RESTORE_LAST_STATE,
+    ALWAYS_POWER_ON, ALWAYS_POWER_OFF.
+    """
+
+    # Retrieve global variables.
+    power_policy_setup = \
+        int(BuiltIn().get_variable_value("${power_policy_setup}",
+                                         default=0))
+    bmc_power_policy_method = \
+        BuiltIn().get_variable_value("${bmc_power_policy_method}",
+                                     default=0)
+    gp.dpvar(power_policy_setup)
+
+    # If this function has already been run once, we need not continue.
+    if power_policy_setup:
+        return
+
+    gp.dpvar(bmc_power_policy_method, 1)
+
+    # The user has not set bmc_power_policy_method via a -v parm so we will
+    # determine what it should be.
+    if bmc_power_policy_method == "":
+        status, ret_values = grk.run_key_u("New Get Power Policy", ignore=1)
+        if status == 'PASS':
+            bmc_power_policy_method = 'New'
+        else:
+            bmc_power_policy_method = 'Old'
+
+    gp.qpvar(bmc_power_policy_method)
+    # For old style, we will rewrite these global variable settings to old
+    # values.
+    if bmc_power_policy_method == "Old":
+        BuiltIn().set_global_variable("${RESTORE_LAST_STATE}",
+                                      "RESTORE_LAST_STATE")
+        BuiltIn().set_global_variable("${ALWAYS_POWER_ON}",
+                                      "ALWAYS_POWER_ON")
+        BuiltIn().set_global_variable("${ALWAYS_POWER_OFF}",
+                                      "ALWAYS_POWER_OFF")
+
+    # Set global variables to control subsequent calls to this function.
+    BuiltIn().set_global_variable("${bmc_power_policy_method}",
+                                  bmc_power_policy_method)
+    BuiltIn().set_global_variable("${power_policy_setup}", 1)
+
+
+###############################################################################
+
+
+###############################################################################
+def translate_power_policy_value(policy):
+
+    r"""
+    Translate the policy value and return the result.
+
+    Using old style functions, callers might call like this with a hard-
+    code value for policy:
+
+    Set BMC Power Policy  RESTORE_LAST_STATE
+
+    This function will get the value of the corresponding global variable (if
+    it exists) and return it.
+
+    This will allow the old style call to still work on systems using the new
+    method of storing the policy value.
+    """
+
+    valid_power_policy_vars = \
+        BuiltIn().get_variable_value("${valid_power_policy_vars}")
+
+    if policy not in valid_power_policy_vars:
+        return policy
+
+    status, ret_values = grk.run_key_u("Get Variable Value  ${" + policy + "}",
+                                       quiet=1)
+    return ret_values
+
+###############################################################################
diff --git a/lib/utils.robot b/lib/utils.robot
index bf33b84..df00d2d 100755
--- a/lib/utils.robot
+++ b/lib/utils.robot
@@ -10,6 +10,7 @@
 Library                 gen_cmd.py
 Library                 gen_robot_keyword.py
 Library                 bmc_ssh_utils.py
+Library                 utils.py
 
 *** Variables ***
 ${pflash_cmd}           /usr/sbin/pflash -r /dev/stdout -P VERSION
@@ -40,8 +41,15 @@
 # Initialize default debug value to 0.
 ${DEBUG}         ${0}
 
+# These variables are used to straddle between new and old methods of setting
+# values.
 ${boot_prog_method}     ${EMPTY}
 
+${power_policy_setup}             ${0}
+${bmc_power_policy_method}        ${EMPTY}
+@{valid_power_policy_vars}        RESTORE_LAST_STATE  ALWAYS_POWER_ON
+...                               ALWAYS_POWER_OFF
+
 *** Keywords ***
 
 Verify PNOR Update
@@ -792,28 +800,66 @@
 
 Set BMC Power Policy
     [Documentation]   Set the given BMC power policy.
-    [arguments]  ${power_restore_uri}=${POWER_RESTORE_URI}
-    ...          ${policy_attribute}=PowerRestorePolicy
-    ...          ${policy}=${RESTORE_LAST_STATE}
+    [Arguments]   ${policy}
+
+    # Note that this function will translate the old style "RESTORE_LAST_STATE"
+    # policy to the new style "xyz.openbmc_project.Control.Power.RestorePolicy.
+    # Policy.Restore" for you.
 
     # Description of argument(s):
-    # power_restore_uri    Power restore policy url.
-    #                      By default "/xyz/openbmc_project/control/host0/power_restore_policy/".
-    # policy               Power restore policy.
-    #                      By default ""xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore".
+    # policy    Power restore policy (e.g "RESTORE_LAST_STATE",
+    #           ${RESTORE_LAST_STATE}).
+
+    # Set the bmc_power_policy_method to either 'Old' or 'New'.
+    Set Power Policy Method
+    # This translation helps bridge between old and new method for calling.
+    ${policy}=  Translate Power Policy Value  ${policy}
+    # Run the appropriate keyword.
+    Run Key  ${bmc_power_policy_method} Set Power Policy \ ${policy}
+    ${currentPolicy}=  Get System Power Policy
+    Should Be Equal    ${currentPolicy}   ${policy}
+
+New Set Power Policy
+    [Documentation]   Set the given BMC power policy (new method).
+    [Arguments]   ${policy}
+
+    # Description of argument(s):
+    # policy    Power restore policy (e.g. ${RESTORE_LAST_STATE}).
 
     ${valueDict}=  Create Dictionary  data=${policy}
     Write Attribute
-    ...  ${power_restore_uri}  ${policy_attribute}  data=${valueDict}
-    ${currentPolicy}=
-    ...  Read Attribute  ${power_restore_uri}  ${policy_attribute}
-    Should Be Equal    ${currentPolicy}   ${policy}
+    ...  ${POWER_RESTORE_URI}  PowerRestorePolicy  data=${valueDict}
+
+Old Set Power Policy
+    [Documentation]   Set the given BMC power policy (old method).
+    [Arguments]   ${policy}
+
+    # Description of argument(s):
+    # policy    Power restore policy (e.g. "RESTORE_LAST_STATE").
+
+    ${valueDict}=     create dictionary  data=${policy}
+    Write Attribute    ${HOST_SETTING}    power_policy   data=${valueDict}
 
 Get System Power Policy
     [Documentation]  Get the BMC power policy.
+
+    # Set the bmc_power_policy_method to either 'Old' or 'New'.
+    Set Power Policy Method
+    ${cmd_buf}=  Create List  ${bmc_power_policy_method} Get Power Policy
+    # Run the appropriate keyword.
+    ${currentPolicy}=  Run Keyword  @{cmd_buf}
+    [Return]  ${currentPolicy}
+
+New Get Power Policy
+    [Documentation]  Get the BMC power policy (new method).
     ${currentPolicy}=  Read Attribute  ${POWER_RESTORE_URI}  PowerRestorePolicy
     [Return]  ${currentPolicy}
 
+Old Get Power Policy
+    [Documentation]  Get the BMC power policy (old method).
+    ${currentPolicy}=  Read Attribute  ${HOST_SETTING}  power_policy
+    [Return]  ${currentPolicy}
+
 Get Auto Reboot
     [Documentation]  Returns auto reboot setting.
     ${setting}=  Read Attribute  ${CONTROL_HOST_URI}/auto_reboot  AutoReboot