External interface code changes

Changes:
     1. rest_client.robot:
       - rename openbmc session to redfish.
       - Add new keyword to perform POST request.
    2. test_lock_management.robot:
       - Import redfish utils resource.
       - Add suite setup and teardown.
       - Replace session login with "Redfish Login" keyword.
       - Split read and write locks for Reboot template.
       - Cosmetic chnages to the codes alignment and dictionary.
       - Add test setup "No Operation."
       - Fix Reboot session for get lock list.
       - Remove "Run Keyword" directive for keywords calls.
       - Add Delete redfish session in strategic location.
       - Re-structure keyword for keyword "Create New Session".
       - Set test variable constant.
       - Replace logout with "Delete All Redfish Sessions"

Tested: Ran couple of times to confirm the changes

    Test Lock Management feature of Management... | PASS |
    10 critical tests, 10 passed, 0 failed
    10 tests total, 10 passed, 0 failed

Change-Id: I530c8a6753e7d1c105d7cb3b2fd586b2c3bb68fa
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/lib/rest_client.robot b/lib/rest_client.robot
index 2b70d54..d7fde91 100644
--- a/lib/rest_client.robot
+++ b/lib/rest_client.robot
@@ -386,13 +386,13 @@
     #          set kwargs as follows:
     #          ${kwargs}=  Create Dictionary  allow_redirect=${True}.
 
-    Create Session  openbmc  ${AUTH_URI}  timeout=${timeout}
+    Create Session  redfish  ${AUTH_URI}  timeout=${timeout}
     ${headers}=  Create Dictionary  Content-Type=application/json
     ${data}=  Set Variable If  '${kwargs}' == '${EMPTY}'
     ...    {"UserName":"${rest_username}", "Password":"${rest_password}"}
     ...    {"UserName":"${rest_username}", "Password":"${rest_password}", ${kwargs}}
 
-    ${resp}=  Post Request  openbmc  /redfish/v1/SessionService/Sessions
+    ${resp}=  Post Request  redfish  /redfish/v1/SessionService/Sessions
     ...  data=${data}  headers=${headers}
     Should Be Equal As Strings  ${resp.status_code}  ${HTTP_CREATED}
 
@@ -401,3 +401,27 @@
     Set Global Variable  ${XAUTH_TOKEN}  ${resp.headers["X-Auth-Token"]}
 
     [Return]  ${content}
+
+
+Redfish Post Request
+    [Documentation]  Do REST POST request and return the result.
+    [Arguments]  ${uri}  ${timeout}=10  ${quiet}=${QUIET}  &{kwargs}
+
+    # Description of argument(s):
+    # uri      The URI to establish connection with
+    #          (e.g. '/xyz/openbmc_project/software/').
+    # timeout  Timeout in seconds to establish connection with URI.
+    # quiet    If enabled, turns off logging to console.
+    # kwargs   Any additional arguments to be passed directly to the
+    #          Post Request call. For example, the caller might
+    #          set kwargs as follows:
+    #          ${kwargs}=  Create Dictionary  allow_redirect=${True}.
+
+    ${base_uri}=  Catenate  SEPARATOR=  ${DBUS_PREFIX}  ${uri}
+    ${headers}=  Create Dictionary  Content-Type=application/json  X-Auth-Token=${XAUTH_TOKEN}
+    Set To Dictionary   ${kwargs}  headers  ${headers}
+    Run Keyword If  '${quiet}' == '${0}'  Log Request  method=Post  base_uri=${base_uri}  args=&{kwargs}
+    ${resp}=  Post Request  redfish  ${base_uri}  &{kwargs}  timeout=${timeout}
+    Run Keyword If  '${quiet}' == '${0}'  Log Response  ${resp}
+
+    [Return]  ${resp}
diff --git a/openpower/ext_interfaces/test_lock_management.robot b/openpower/ext_interfaces/test_lock_management.robot
index cbef02f..7e391c3 100644
--- a/openpower/ext_interfaces/test_lock_management.robot
+++ b/openpower/ext_interfaces/test_lock_management.robot
@@ -3,11 +3,13 @@
 Documentation    Test Lock Management feature of Management Console on BMC.
 
 Resource         ../../lib/resource.robot
-Resource         ../../lib/bmc_redfish_resource.robot
 Resource         ../../lib/openbmc_ffdc.robot
+Resource         ../../lib/bmc_redfish_utils.robot
 
+Suite Setup      Delete All Redfish Sessions
 Test Setup       Test Setup Execution
 Test Teardown    Test Teardown Execution
+Suite Teardown   Delete All Redfish Sessions
 
 *** Variables ***
 
@@ -192,11 +194,10 @@
     [Documentation]  Verify GetLockList returns an empty record for an invalid session id.
     [Tags]  Verify_GetLockList_Returns_An_Empty_Record_For_An_Invalid_Session_Id
 
-    ${session_location}=  Redfish.Get Session Location
-    ${session_id}=  Evaluate  os.path.basename($session_location)  modules=os
+    ${resp}=  Redfish Login  kwargs= "Oem":{"OpenBMC" : {"ClientID":"${hmc_id}"}}
 
-    ${records}=  Run Keyword  Get Locks List  ${session_id}
-    ${records}=  Run Keyword  Get Locks List  ZZzZZz9zzZ
+    ${records}=  Get Locks List  ${resp['Id']}
+    ${records}=  Get Locks List  ZZzZZz9zzZ
     ${length}=  Get Length  ${records}
     Should Be Equal  ${length}  ${0}
 
@@ -214,13 +215,21 @@
     Write  ${TWO_SEG_FLAG_2}  ${234}  hmc-id  ${HTTP_CONFLICT}  ['NA']  ${False}
 
 
-Verify Persistency Of Locks After BMC Reboot
+Verify Persistency Of Read Locks After BMC Reboot
     [Documentation]  Verify persistency of locks after BMC reboot.
-    [Tags]  Verify_Persistency_Of_Locks_After_BMC_Reboot
+    [Tags]  Verify_Persistency_Of_Read_Locks_After_BMC_Reboot
     [Template]  Locks Persistency Check After BMC Reboot
 
     # lock_type  seg_flags          resource_id
     Read         ${TWO_SEG_FLAG_2}  ${234}
+
+
+Verify Persistency Of Write Locks After BMC Reboot
+    [Documentation]  Verify persistency of locks after BMC reboot.
+    [Tags]  Verify_Persistency_Of_Write_Locks_After_BMC_Reboot
+    [Template]  Locks Persistency Check After BMC Reboot
+
+    # lock_type  seg_flags          resource_id
     Write        ${TWO_SEG_FLAG_2}  ${234}
 
 
@@ -228,16 +237,12 @@
     [Documentation]  Verify valid lock transactions release.
     [Tags]  Verify_Valid_Lock_Transactions_Release
 
-    ${transaction_id1}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_2}  ${234}
+    ${transaction_id1}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
     ${locks_before}=  Get Locks List  ${SESSION_ID}
 
-    ${transaction_id2}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_3}  ${234}
-    ${transaction_id3}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_2}  ${234}
-    ${transaction_id4}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_3}  ${234}
+    ${transaction_id2}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_3}  ${234}
+    ${transaction_id3}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
+    ${transaction_id4}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_3}  ${234}
 
     ${transaction_ids}=  Create List  ${transaction_id2}  ${transaction_id3}  ${transaction_id4}
     Release Locks  ${transaction_ids}
@@ -249,8 +254,7 @@
     [Documentation]  Verify invalid lock transactions release.
     [Tags]  Verify_Invalid_Lock_Transactions_Release
 
-    ${transaction_id1}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_2}  ${234}
+    ${transaction_id1}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
     ${locks_before}=  Get Locks List  ${SESSION_ID}
 
     ${transaction_id2}=  Evaluate  ${transaction_id1} + 1
@@ -268,18 +272,15 @@
     [Tags]  Verify_Locks_Release_By_Session
 
     ${locks_before}=  Get Locks List  ${SESSION_ID}
-    ${transaction_id1}=  Acquire Lock On A Given Resource
-    ...  Write  ${TWO_SEG_FLAG_2}  ${234}
+    ${transaction_id1}=  Acquire Lock On A Given Resource  Write  ${TWO_SEG_FLAG_2}  ${234}
 
     # Release Lock by Session without mentioning transaction_ids.
     Release Locks  release_type=Session
     ${locks_after}=  Get Locks List  ${SESSION_ID}
     Should Be Equal  ${locks_before}  ${locks_after}
 
-    ${transaction_id1}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_2}  ${234}
-    ${transaction_id2}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_3}  ${234}
+    ${transaction_id1}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
+    ${transaction_id2}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_3}  ${234}
     ${transaction_ids}=  Create List  ${transaction_id1}  ${transaction_id2}
 
     # Release Lock by Session by mentioning transaction_ids also in the request.
@@ -289,17 +290,18 @@
 Verify Locks Created By One Session Cannot Be Deleted By Another Session
     [Documentation]  Verify locks created by one session cannot be deleted by another session.
     [Tags]  Verify_Locks_Created_By_One_Session_Cannot_Be_Deleted_By_Another_Session
+    [Setup]  No Operation
 
-    ${transaction_id1}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_2}  ${234}
-    ${locks_tran1}=  Get Locks List  ${SESSION_ID}
+    ${resp}=  Redfish Login  kwargs= "Oem":{"OpenBMC" : {"ClientID":"hmc-id"}}
+    Set Test Variable  ${SESSION_ID}  ${resp['Id']}
+    ${transaction_id1}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_2}  ${234}
+    ${locks_tran1}=  Get Locks List  ${resp['Id']}
 
-    Redfish.Login
-    ${session_id}  ${session_key}=  Return Session Id And Session Key
+    ${resp}=  Redfish Login  kwargs= "Oem":{"OpenBMC" : {"ClientID":"hmc-id"}}
+    Set Test Variable  ${SESSION_ID}  ${resp['Id']}
 
-    ${transaction_id2}=  Acquire Lock On A Given Resource
-    ...  Read  ${TWO_SEG_FLAG_3}  ${234}
-    ${locks_before}=  Get Locks List  ${SESSION_ID}
+    ${transaction_id2}=  Acquire Lock On A Given Resource  Read  ${TWO_SEG_FLAG_3}  ${234}
+    ${locks_before}=  Get Locks List  ${resp['Id']}
 
     ${transaction_ids}=  Create List  ${transaction_id1}  ${transaction_id2}
     Release Locks  ${transaction_ids}  exp_status_code=${HTTP_UNAUTHORIZED}  conflict_record=${locks_tran1}
@@ -323,16 +325,16 @@
     # seg_flags    Segmentation Flags to identify lock elements under system level in the hierarchy.
     # resource_id  Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
 
-    ${transaction_id}=  Run Keyword  Acquire Lock On A Given Resource
-    ...  ${lock_type}  ${seg_flags}  ${resource_id}
+    ${transaction_id}=  Acquire Lock On A Given Resource  ${lock_type}  ${seg_flags}  ${resource_id}
 
-    ${locks_prev}=  Run Keyword  Get Locks List  ${SESSION_ID}
+    ${locks_prev}=  Get Locks List  ${SESSION_ID}
 
-    Initialize OpenBMC
-    OBMC Reboot (off)
+    Redfish OBMC Reboot (off)
+    Redfish Login
 
-    ${locks_curr}=  Run Keyword  Get Locks List  ${SESSION_ID}
+    ${locks_curr}=  Get Locks List  ${SESSION_ID}
     Should Be Equal  ${locks_prev}  ${locks_curr}
+
     ${transaction_ids}=  Create List  ${transaction_id}
     Release Locks  ${transaction_ids}
 
@@ -346,11 +348,14 @@
     # seg_flags    Segmentation Flags to identify lock elements under system level in the hierarchy.
     # resource_id  Decimal +ve integer value of maximum 8 hex bytes.  Ex: 134, 2048 etc.
 
-    ${SEG_FLAGS_LOCK}=  Create Dictionary  LockType=${lock_type}  SegmentFlags=@{seg_flags}
+    ${SEG_FLAGS_LOCK}=  Create Dictionary
+    ...  LockType=${lock_type}
+    ...  SegmentFlags=@{seg_flags}
     ...  ResourceID=${resource_id}
+
     ${SEG_FLAGS_ENTRIES}=  Create List  ${SEG_FLAGS_LOCK}
     ${LOCK_REQUEST}=  Create Dictionary  Request=${SEG_FLAGS_ENTRIES}
-    Log To Console  ${LOCK_REQUEST}
+    Log To Console  \n ${LOCK_REQUEST}
 
     [Return]  ${LOCK_REQUEST}
 
@@ -370,17 +375,17 @@
     # err_msgs         List of expected error messages.
 
     ${data}=  Return Data Dictionary For Single Request  ${lock_type}  ${seg_flags}  ${resource_id}
-    ${resp}=  Redfish.Post  /ibm/v1/HMC/LockService/Actions/LockService.AcquireLock
-    ...  body=${data}  valid_status_codes=[${exp_status_code}]
+    ${resp}=  Redfish Post Request  /ibm/v1/HMC/LockService/Actions/LockService.AcquireLock  data=${data}
 
     ${transaction_id}=  Run Keyword If  ${exp_status_code} != ${HTTP_OK}
-    ...  Set Variable  ${0}
-    ...  ELSE  Load Lock Record And Build Transaction To Session Map  ${resp.text}
+    ...      Set Variable  ${0}
+    ...  ELSE
+    ...     Load Lock Record And Build Transaction To Session Map  ${resp.text}
 
     Run Keyword If  ${exp_status_code} == ${HTTP_CONFLICT} and ${err_msgs} == ['NA']
-    ...  Load Response And Verify Conflict  ${resp.text}  ${SESSION_ID}
+    ...      Load Response And Verify Conflict  ${resp.text}  ${SESSION_ID}
     ...  ELSE  Run Keyword If  ${exp_status_code} != ${HTTP_OK} and ${err_msgs} != ${EMPTY_LIST}
-    ...  Load Response And Verify Error  ${resp.text}  err_msgs=${err_msgs}
+    ...     Load Response And Verify Error  ${resp.text}  err_msgs=${err_msgs}
 
     Append Transaction Id And Session Id To Locks Dictionary  ${transaction_id}
 
@@ -418,7 +423,7 @@
       Append To List  ${extended_errors}  ${error[0]["Message"]}
     END
 
-    Log To Console  EXTENDED = ${extended_errors}
+    Log To Console  \n ${extended_errors}
 
     FOR  ${exp_error}  IN  @{err_msgs}
         Run Keyword  Expect List Of Errors In An Extended Errors  ${exp_error}  ${extended_errors}
@@ -459,9 +464,8 @@
 
     ${sessions}=  Evaluate  json.dumps(${sessions})  json
     ${data}=  Set Variable  {"SessionIDs": ${sessions}}
-    ${resp}=  Redfish.Post  /ibm/v1/HMC/LockService/Actions/LockService.GetLockList
-    ...  body=${data}  valid_status_codes=[${exp_status_code}]
-
+    ${resp}=  Redfish Post Request  /ibm/v1/HMC/LockService/Actions/LockService.GetLockList
+    ...  data=${data}
     ${locks}=  Evaluate  json.loads('''${resp.text}''')  json
 
     [Return]  ${locks["Records"]}
@@ -482,9 +486,8 @@
     # When release_type=Session then TransactionIDs list will be ignored.
     ${data}=  Set Variable  {"Type": "${release_type}", "TransactionIDs": ${transaction_ids}}
     ${data}=  Evaluate  json.dumps(${data})  json
-    ${resp}=  Redfish.Post  /ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock
-    ...  body=${data}  valid_status_codes=[${exp_status_code}]
-    Should Be True  ${resp.status}  ${exp_status_code}
+    ${resp}=  Redfish Post Request  /ibm/v1/HMC/LockService/Actions/LockService.ReleaseLock  data=${data}
+    Should Be True  ${resp.status_code}  ${exp_status_code}
     Return From Keyword If  ${conflict_record} == ${EMPTY_LIST}
 
     ${conflict}=  Evaluate  json.loads('''${resp.text}''')  json
@@ -522,7 +525,7 @@
     # lock_records  A dictionary containing key value pairs of a lock record.
 
     ${session}=  Get From Dictionary  ${LOCKS}  ${lock_records["TransactionID"]}
-    ${locks}=  Run Keyword  Get Locks List  ${session}
+    ${locks}=  Get Locks List  ${session}
 
     ${lock_record_found}=  Set Variable  ${False}
 
@@ -552,7 +555,7 @@
     #                            "SessionID": "B6geYEdo6T", "TransactionID": 104 } }
     # sessions       Comma separated list of sessions
 
-    ${curr_locks}=  Run Keyword  Get Locks List  ${sessions}
+    ${curr_locks}=  Get Locks List  ${sessions}
     ${conflict_resp}=  Replace String  ${conflict_resp}  \"  \\"
     ${conflict_response}=  Evaluate  json.loads('''${conflict_resp}''')  json
 
@@ -576,13 +579,19 @@
     # err_msgs         List of expected error messages.
     # new_sess_req     Create a new session before acquiring a lock if True.
 
+    # Delete the session.
+    Run Keyword If  ${new_sess_req} == ${True}  Delete All Redfish Sessions
+
     # Get REST session to BMC.
     Run Keyword If  ${new_sess_req} == ${True}  Create New Session
 
-    ${inputs}=  Create Dictionary  LockType=${lock_type}  ResourceID=${resource_id}
-    ...  SegmentFlags=${seg_flags}  HMCID=${hmc_id}
+    ${inputs}=  Create Dictionary
+    ...  LockType=${lock_type}
+    ...  ResourceID=${resource_id}
+    ...  SegmentFlags=${seg_flags}
+    ...  HMCID=${hmc_id}
 
-    ${transaction_id}=  Run Keyword  Acquire Lock On A Given Resource  ${inputs["LockType"]}
+    ${transaction_id}=  Acquire Lock On A Given Resource  ${inputs["LockType"]}
     ...  ${inputs["SegmentFlags"]}  ${inputs["ResourceID"]}  ${exp_status_code}  err_msgs=${err_msgs}
 
     # Each lock request from a new session is saved so that for next lock request using same session
@@ -601,28 +610,19 @@
     Release Locks  ${transaction_ids}
     Verify Lock Record  ${False}  &{inputs}
 
-    # Delete the session.
-    Redfish.Logout
-
 
 Create New Session
     [Documentation]  Create new session.
 
-    # Delete current session.
-    Redfish.Logout
-
-    # Get a redfish session to BMC.
-    Redfish.Login
-    ${session_id}  ${session_key}=  Return Session Id And Session Key
-    Set Test Variable  ${SESSION_ID}  ${session_id}
-    Set Test Variable  ${SESSION_KEY}  ${session_key}
+    ${resp}=  Redfish Login  kwargs= "Oem":{"OpenBMC" : {"ClientID":"${hmc_id}"}}
+    Set Test Variable  ${SESSION_ID}  ${resp['Id']}
 
 
 Test Teardown Execution
     [Documentation]  Test teardown execution.
 
     FFDC On Test Case Fail
-    Redfish.Logout
+    Delete All Redfish Sessions
 
 
 Return Session Id And Session Key
@@ -638,10 +638,12 @@
 Test Setup Execution
     [Documentation]  Test setup execution.
 
+    # This is a test constant value.
+    Set Test Variable  ${hmc_id}  hmc-id
     Create New Session
 
-    Set Test Variable Dictionary Of Previous Lock Request  ${EMPTY}  ${EMPTY_LIST}  ${EMPTY}  ${EMPTY}
-    ...  ${EMPTY}  ${EMPTY}
+    Set Test Variable Dictionary Of Previous Lock Request
+    ...  ${EMPTY}  ${EMPTY_LIST}  ${EMPTY}  ${EMPTY}  ${EMPTY}  ${EMPTY}
 
 
 Set Test Variable Dictionary Of Previous Lock Request
@@ -658,7 +660,12 @@
     # session_id           Session id of the transaction.
     # transaction_id       Transaction_id of the lock request.
 
-    ${prev_inputs}=  Create Dictionary  LockType=${lock_type}  ResourceID=${resource_id}
-    ...  SegmentFlags=${seg_flags}  HMCID=${hmc_id}  SessionID=${session_id}  TransactionID=${transaction_id}
+    ${prev_inputs}=  Create Dictionary
+    ...  LockType=${lock_type}
+    ...  ResourceID=${resource_id}
+    ...  SegmentFlags=${seg_flags}
+    ...  HMCID=${hmc_id}
+    ...  SessionID=${session_id}
+    ...  TransactionID=${transaction_id}
 
     Set Test Variable  ${PREV_INPUTS}  ${prev_inputs}