Adding support for BMC Power On/BMC Power Off

This change includes the following new keywords:
  BMC Power On
  Check Power On States
  BMC Power Off
  Check Power Off States
  Get BMC State
  Get Power State

It also includes modifications to the following keywods:
  Is Power On
  Is Power Off
  Wait For Host To Ping
  Check OS
  Open Connection and Log In

A variable definition has been added to resource.txt and a
file to keep track of DVT's resource files has been created.

Change-Id: Idc81be7e2dfb21c35bc51214f8650a10d8481120
Signed-off-by: Leah McNutt <lmcnutt@us.ibm.com>
diff --git a/lib/boot/boot_resource_master.robot b/lib/boot/boot_resource_master.robot
new file mode 100644
index 0000000..1c8e85a
--- /dev/null
+++ b/lib/boot/boot_resource_master.robot
@@ -0,0 +1,5 @@
+*** Settings ***
+# This file pulls in all of the other resource files in lib/boot/
+
+Resource    powerons.robot
+Resource    poweroffs.robot
diff --git a/lib/boot/poweroffs.robot b/lib/boot/poweroffs.robot
new file mode 100644
index 0000000..f9a6940
--- /dev/null
+++ b/lib/boot/poweroffs.robot
@@ -0,0 +1,32 @@
+*** Settings ***
+Documentation  Contains all of the keywords that do various power offs.
+
+Resource    ../resource.txt
+Resource    ../utils.robot
+Resource    ../connection_client.robot
+
+*** Keywords ***
+BMC Power Off
+    [Documentation]  Powers off the system and makes sure that all states are
+    ...  powered off.
+
+    Open Connection and Log In
+    Initiate Power Off
+    Check Power Off States
+    Close Connection
+
+Check Power Off States
+    [Documentation]  Checks that the BMC state, power state, and boot progress
+    ...  are correctly powered off.
+
+    ${power_state}=  Get Power State
+    Should Be Equal  ${power_state}  ${0}
+    Log to Console  Power State: ${power_state}
+
+    ${boot_progress}=  Get Boot Progress
+    Should Be Equal  ${boot_progress}  Off
+    Log to Console  Boot Progress: ${boot_progress}
+
+    ${bmc_state}=  Get BMC State
+    Should Contain  ${bmc_state}  HOST_POWERED_OFF
+    Log to Console  BMC State: ${bmc_state}
diff --git a/lib/boot/powerons.robot b/lib/boot/powerons.robot
new file mode 100644
index 0000000..b3e402f
--- /dev/null
+++ b/lib/boot/powerons.robot
@@ -0,0 +1,36 @@
+*** Settings ***
+Documentation  Contains all of the keywords that do various power ons.
+
+Resource    ../resource.txt
+Resource    ../utils.robot
+Resource    ../connection_client.robot
+
+*** Keywords ***
+BMC Power On
+    [Documentation]  Powers on the system, checks that the OS is functional, and
+    ...  makes sure that all states are powered on.
+
+    &{bmc_connection_args}=  Create Dictionary  alias=bmc_connection
+
+    Open Connection and Log In  &{bmc_connection_args}
+    Initiate Power On
+    Wait For OS
+    Switch Connection  bmc_connection
+    Check Power On States
+    Close Connection
+
+Check Power On States
+    [Documentation]  Checks that the BMC state, power state, and boot progress
+    ...  are correctly powered on.
+
+    ${bmc_state}=  Get BMC State
+    Should Contain  ${bmc_state}  HOST_BOOTED
+    Log to Console  BMC State: ${bmc_state}
+
+    ${boot_progress}=  Get Boot Progress
+    Should Be Equal  ${boot_progress}  FW Progress, Starting OS
+    Log to Console  Boot Progress: ${boot_progress}
+
+    ${power_state}=  Get Power State
+    Should Be Equal  ${power_state}  ${1}
+    Log to Console  Power State: ${power_state}
diff --git a/lib/connection_client.robot b/lib/connection_client.robot
index 2cc8413..b67996c 100755
--- a/lib/connection_client.robot
+++ b/lib/connection_client.robot
@@ -4,18 +4,54 @@
 
 Library           SSHLibrary
 Library           OperatingSystem
+Library           Collections
 
 *** Variables ***
 
 *** Keywords ***
 Open Connection And Log In
-    Run Keyword If   '${SSH_PORT}' != '${EMPTY}' and '${HTTPS_PORT}' != '${EMPTY}'
+    [Documentation]  Opens a connection with the given arguments, and logs in.
+    ...  Defaults to logging into the BMC.
+    [Arguments]  ${username}=${OPENBMC_USERNAME}
+    ...          ${password}=${OPENBMC_PASSWORD}  &{connection_args}
+
+    # username          The username to log into the connection with.
+    # password          The password to log into the connection with.
+    # connection_args   A dictionary of acceptable inputs to the Open Connection
+    #                   keyword. This includes, but is not limited to, the
+    #                   following:
+    #                   host, alias, port, timeout, newline, prompt, term_type,
+    #                   width, height, path_separator, endcoding
+    #                   (For more information, please visit the SSHLibrary doc)
+
+    #                   Of the above arguments to Open Connection, this keyword
+    #                   will provide the following default values:
+    #                   host             ${OPENBMC_HOST}
+
+    # If no host was provided, add ${OPENBMC_HOST} to the dictionary
+    ${has_host}=  Run Keyword and Return Status
+    ...           Dictionary Should Contain Key  ${connection_args}  host
+    Run Keyword If  ${has_host} == ${FALSE}
+    ...             Set To Dictionary  ${connection_args}  host=${OPENBMC_HOST}
+
+    Run Keyword If
+    ...   '${SSH_PORT}' != '${EMPTY}' and '${HTTPS_PORT}' != '${EMPTY}'
     ...   User input SSH and HTTPs Ports
 
-    Run Keyword If  '${SSH_PORT}' == '${EMPTY}'    Open connection     ${OPENBMC_HOST}
-    ...    ELSE  Run Keyword   Open connection     ${OPENBMC_HOST}    port=${SSH_PORT}
+    # Check to see if a port to connect to was provided.
+    ${has_port}=  Run Keyword and Return Status
+    ...           Dictionary Should Contain Key  ${connection_args}  port
 
-    Login   ${OPENBMC_USERNAME}    ${OPENBMC_PASSWORD}
+    # If the ${SSH_PORT} is set and no port was provided, add the defined port
+    # to the dictionary and open the connection. Otherwise, open the connection
+    # with the either the provided port or the default port.
+    Run Keyword If  '${SSH_PORT}' != '${EMPTY}' and ${has_port} == ${FALSE}
+    ...            Run Keywords
+    ...            Set To Dictionary  ${connection_args}  port=${SSH_PORT}  AND
+    ...            Open connection  &{connection_args}
+    ...   ELSE  Run Keyword  Open connection  &{connection_args}
+
+    Login  ${username}  ${password}
 
 User input SSH and HTTPs Ports
     [Documentation]   Update the global SSH and HTTPs port variable for QEMU
diff --git a/lib/utils.robot b/lib/utils.robot
index 7cb179a..f7bae9c 100644
--- a/lib/utils.robot
+++ b/lib/utils.robot
@@ -8,10 +8,15 @@
 ${SYSTEM_SHUTDOWN_TIME}       ${5}
 
 *** Keywords ***
-
 Wait For Host To Ping
-    [Arguments]     ${host}
-    Wait Until Keyword Succeeds     ${OPENBMC_REBOOT_TIMEOUT}min    5 sec   Ping Host   ${host}
+    [Arguments]  ${host}  ${timeout}=${OPENBMC_REBOOT_TIMEOUT}min
+    ...          ${interval}=5 sec
+
+    # host      The DNS name or IP of the host to ping.
+    # timeout   The amount of time after which attempts to ping cease.
+    # interval  The amount of time in between attempts to ping.
+
+    Wait Until Keyword Succeeds  ${timeout}  ${interval}  Ping Host  ${host}
 
 Ping Host
     [Arguments]     ${host}
@@ -24,21 +29,25 @@
     [return]  ${state}
 
 Is Power On
-    ${state} =    Get Boot Progress
-    Should be equal   ${state}     FW Progress, Starting OS
+    ${state}=  Get Power State
+    Should be equal  ${state}  ${1}
 
 Is Power Off
-    ${state} =    Get Boot Progress
-    Should be equal   ${state}     Off
+    ${state}=  Get Power State
+    Should be equal  ${state}  ${0}
 
-Power On Host
+Initiate Power On
+    [Documentation]  Initiates the power on and waits until the Is Power On
+    ...  keyword returns that the power state has switched to on.
     @{arglist}=   Create List
     ${args}=     Create Dictionary    data=@{arglist}
     ${resp}=   Call Method    /org/openbmc/control/chassis0/    powerOn    data=${args}
     should be equal as strings      ${resp.status_code}     ${HTTP_OK}
     Wait Until Keyword Succeeds	  3 min    	10 sec    Is Power On
 
-Power Off Host
+Initiate Power Off
+    [Documentation]  Initiates the power off and waits until the Is Power Off
+    ...  keyword returns that the power state has switched to off.
     @{arglist}=   Create List
     ${args}=     Create Dictionary    data=@{arglist}
     ${resp}=   Call Method    /org/openbmc/control/chassis0/    powerOff   data=${args}
@@ -54,7 +63,8 @@
     Wait For Host To Ping   ${OPENBMC_HOST}
 
 Check OS
-    [Documentation]  Checks that the host OS is up by running an SSH command.
+    [Documentation]  Attempts to ping the host OS and then checks that the host
+    ...              OS is up by running an SSH command.
 
     [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
     ...          ${os_password}=${OS_PASSWORD}
@@ -64,6 +74,9 @@
     # os_username       The username to be used to sign on to the OS host.
     # os_password       The password to be used to sign on to the OS host.
 
+    # Attempt to ping the OS. Store the return code to check later.
+    ${ping_rc}=  Run Keyword and Return Status  Ping Host  ${os_host}
+
     Open connection  ${os_host}
     Login  ${os_username}  ${os_password}
 
@@ -76,6 +89,9 @@
     # We will likewise fail if there is any stderr data.
     Should Be Empty  ${stderr}
 
+    # We will likewise fail if the OS did not ping, as we could SSH but not ping
+    Should Be Equal As Strings  ${ping_rc}  ${TRUE}
+
 Wait for OS
     [Documentation]  Waits for the host OS to come up via calls to "Check OS".
     [Arguments]  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
@@ -93,3 +109,23 @@
 
     Wait Until Keyword Succeeds  ${timeout} sec  ${interval}  Check OS
     ...                          ${os_host}  ${os_username}  ${os_password}
+
+Get BMC State
+    [Documentation]  Returns the state of the BMC as a string. (i.e: BMC_READY)
+    @{arglist}=  Create List
+    ${args}=  Create Dictionary  data=@{arglist}
+    ${resp}=  Call Method  /org/openbmc/managers/System/  getSystemState
+    ...        data=${args}
+    Should be equal as strings  ${resp.status_code}  ${HTTP_OK}
+    ${content}=  to json  ${resp.content}
+    [return]  ${content["data"]}
+
+Get Power State
+    [Documentation]  Returns the power state as an integer. Either 0 or 1.
+    @{arglist}=  Create List
+    ${args}=  Create Dictionary  data=@{arglist}
+    ${resp}=  Call Method  /org/openbmc/control/chassis0/  getPowerState
+    ...        data=${args}
+    Should be equal as strings  ${resp.status_code}  ${HTTP_OK}
+    ${content}=  to json  ${resp.content}
+    [return]  ${content["data"]}
diff --git a/tests/test_bootpolicy.robot b/tests/test_bootpolicy.robot
index 621179d..173f5e6 100644
--- a/tests/test_bootpolicy.robot
+++ b/tests/test_bootpolicy.robot
@@ -59,13 +59,13 @@
     [Documentation]   This testcase is to verify that boot order does not change
     ...               after first boot when boot policy set to permanent
 
-    Power Off Host
+    Initiate Power Off
 
     Set Boot Policy   PERMANENT
 
     Set Boot Device   CDROM
 
-    Power On Host
+    Initiate Power On
 
     ${boot} =   Read Attribute  /org/openbmc/settings/host0    boot_policy
     Should Be Equal    ${boot}    PERMANENT
@@ -77,7 +77,7 @@
     [Documentation]   This testcase is to verify that boot policy and order does not change
     ...               after warm reset on a system with onetime boot policy.
 
-    Power On Host
+    Initiate Power On
 
     Set Boot Policy   ONETIME
 
@@ -95,7 +95,7 @@
     [Documentation]   This testcase is to verify that boot policy and order does not change  
     ...               after warm reset on a system with permanent boot policy.
 
-    Power On Host
+    Initiate Power On
 
     Set Boot Policy   PERMANENT
 
diff --git a/tests/test_inventory.robot b/tests/test_inventory.robot
index d2feda9..4d908be 100644
--- a/tests/test_inventory.robot
+++ b/tests/test_inventory.robot
@@ -62,7 +62,7 @@
 *** Keywords ***
 
 Setup The Suite
-	Power On Host
+	Initiate Power On
 
 	@{ret} = 	Get Inventory List 	${OPENBMC_MODEL}
 	Set Suite Variable 	@{sys_inv} 	@{ret}
diff --git a/tests/test_occ_powercap.robot b/tests/test_occ_powercap.robot
index 1c600dd..ee72a4f 100644
--- a/tests/test_occ_powercap.robot
+++ b/tests/test_occ_powercap.robot
@@ -5,7 +5,7 @@
 Resource                ../lib/resource.txt
 Resource                ../lib/utils.robot
 
-Suite Setup            Power On Host
+Suite Setup            Initiate Power On
 
 *** Test Cases ***