Adding BMC nodes, Power on/off using XCAT

This change includes:
    Adding BMC nodes to XCAT
    Verify added BMC nodes
    Powering on BMC nodes via XCAT
    Powering off BMC nodes via XCAT
    Checking power status of BMC nodes

This resolves openbmc/openbmc-test-automation#620

Change-Id: Iaf73ab6142a09723b59317fd735004ca1bd0ff83
Signed-off-by: Prashanth Katti <prkatti1@in.ibm.com>
diff --git a/lib/xcat/resource.txt b/lib/xcat/resource.txt
index 66a184f..7636cf2 100644
--- a/lib/xcat/resource.txt
+++ b/lib/xcat/resource.txt
@@ -6,7 +6,7 @@
 ${XCAT_USERNAME}  ${EMPTY}
 ${XCAT_PASSWORD}  ${EMPTY}
 ${XCAT_PORT}      31578
-${XCAT_DIR_PATH}      /opt/xcat/bin/
+${XCAT_DIR_PATH}  /opt/xcat/bin/
 
 # Default BMC nodes config file.
 
diff --git a/lib/xcat/xcat_utils.robot b/lib/xcat/xcat_utils.robot
index 505ef80..9af70e4 100644
--- a/lib/xcat/xcat_utils.robot
+++ b/lib/xcat/xcat_utils.robot
@@ -1,9 +1,11 @@
 *** Settings ***
 
 Resource    ../xcat/resource.txt
+Resource    ../../lib/resource.txt
 
 Library     SSHLibrary
 Library     OperatingSystem
+Library     String
 
 *** Keywords  ***
 
@@ -32,3 +34,62 @@
 
     ${bmc_list} =  OperatingSystem.Get File  ${node_cfg_file_path}
     [Return]  ${bmc_list}
+
+Add Nodes To XCAT
+    [Documentation]  Add nodes to XCAT configuration.
+    [Arguments]  ${node}  ${username}=${OPENBMC_USERNAME}
+    ...          ${password}=${OPENBMC_PASSWORD}
+
+    # Description of the argument(s):
+    # node  Name of the node to be added.
+
+    ${cmd_buf}=  Catenate  ${XCAT_DIR_PATH}/mkdef ${node} bmc=${node}
+    ...  bmcusername=${username} bmcpassword=${password} mgt=openbmc groups=all
+    Execute Command  ${cmd_buf}
+
+Validate Added Node
+    [Documentation]  Validate added node.
+    [Arguments]  ${node}
+
+    # Description of the argument(s):
+    # node  Name of the node.
+
+    ${stdout}  ${stderr}=  Execute Command  ${XCAT_DIR_PATH}/nodels
+    ...  return_stderr=True
+    Should Be Empty  ${stderr}
+    Should Contain  ${std_out}  ${node}  msg=Node is not added.
+
+Power On Via XCAT
+    [Documentation]  Power on via XCAT.
+    [Arguments]  ${node}
+
+    # Description of the argument(s):
+    # node  Name of the node.
+
+    ${stdout}  ${stderr}=  Execute Command  ${XCAT_DIR_PATH}/rpower ${node} on
+    ...  return_stderr=True
+    Should Be Empty  ${stderr}
+
+Power Off Via XCAT
+    [Documentation]  Power off via XCAT.
+    [Arguments]  ${node}
+
+    # Description of the argument(s):
+    # node  Name of the node.
+
+    ${stdout}  ${stderr}=  Execute Command  ${XCAT_DIR_PATH}/rpower ${node} off
+    ...  return_stderr=True
+    Should Be Empty  ${stderr}
+
+Get Power Status
+    [Documentation]  Get power status via XCAT.
+    [Arguments]  ${node}
+
+    # Description of the argument(s):
+    # node  Name of the node.
+
+    ${stdout}  ${stderr}=  Execute Command
+    ...  ${XCAT_DIR_PATH}/rpower ${node} state  return_stderr=True
+    Should Be Empty  ${stderr}
+
+    [Return]  ${stdout}
diff --git a/xcat/xcat_testing.robot b/xcat/xcat_testing.robot
index dfee1f8..7a6fb1f 100644
--- a/xcat/xcat_testing.robot
+++ b/xcat/xcat_testing.robot
@@ -5,20 +5,43 @@
 Resource        ../lib/xcat/xcat_utils.robot
 
 Library         OperatingSystem
+Library         String
 
 Suite Setup  Validate XCAT Setup
 
+*** Variables ***
+
+${poweron_flag}   ON
+${poweroff_flag}  OFF
+
 *** Test Cases ***
 
 Add BMC Nodes To XCAT
     [Documentation]  Connect and add BMC nodes.
     [Tags]  Add_BMC_Nodes_To_XCAT
 
-    # It reads out file having list of BMC nodes and adds
-    # those nodes into XCAT.
+    # Add BMC nodes one by one and check whether it is successfully added.
+    : FOR  ${bmc}  IN  @{BMC_LIST}
+    \  Add Nodes To XCAT  ${bmc}
+    \  Validate Added Node  ${bmc}
 
-    # TBD- Adding BMC nodes to XCAT
-    # https://github.com/openbmc/openbmc-test-automation/issues/620
+Power On Via XCAT And Validate
+    [Documentation]  Power on via XCAT and validate.
+    [Tags]  Power_On_Via_XCAT_And_Validate
+
+    # Power on each BMC node and validate the power status.
+    : FOR  ${bmc}  IN  @{BMC_LIST}
+    \  Power On Via XCAT  ${bmc}
+    \  Validate Power Status Via XCAT  ${bmc}  ${poweron_flag}
+
+Power Off Via XCAT And Validate
+    [Documentation]  Power off via XCAT and validate.
+    [Tags]  Power_Off_Via_XCAT_And_Validate
+
+    # Power off each BMC node and validate the power status.
+    : FOR  ${bmc}  IN  @{BMC_LIST}
+    \  Power Off Via XCAT  ${bmc}
+    \  Validate Power Status Via XCAT  ${bmc}  ${poweroff_flag}
 
 *** Keywords ***
 
@@ -32,3 +55,19 @@
     Should Not Be Empty  ${cmd_output}  msg=XCAT not installed.
 
     Log  \n XCAT Version is: \n${cmd_output}
+
+    # Get all the BMC nodes from the config file.
+    ${nodes}=  Get List Of BMC Nodes
+    # Make a list of BMC nodes.
+    @{BMC_LIST}=  Split To Lines  ${nodes}
+    Log To Console  BMC nodes to be added:\n ${BMC_LIST}
+    Set Suite Variable  @{BMC_LIST}
+
+Validate Power Status Via XCAT
+    [Documentation]  Validate power status.
+    [Arguments]  ${node}  ${flag}=ON
+
+    ${status}=  Get Power Status  ${node}
+    Run Keyword If  '${flag}' == 'ON'
+    ...  Should Contain  ${status}  on  msg=Host is off.
+    ...  ELSE  Should Contain  ${status}  off  msg=Host is on.