Automation code to check task progress state

changes:
    - Keywords "Get All Task",
      "Get Task Objects",
      "Check Task Attribute",
      "Check Task Progress State"

Change-Id: Ia88d06eaa0b348997f6d40fd1b7dc9d9f6d62b0f
Signed-off-by: Sushil Singh <susilsi7@in.ibm.com>
diff --git a/lib/code_update_utils.robot b/lib/code_update_utils.robot
index bd186d6..6d4f254 100644
--- a/lib/code_update_utils.robot
+++ b/lib/code_update_utils.robot
@@ -550,6 +550,111 @@
     Valid Value  state  valid_values=[${match_state}]
 
 
+Get All Task
+    [Documentation]  Get all the active tasks.
+    [Arguments]  ${target_uri}  ${match_status}  ${match_state}
+
+    # Description of argument(s):
+    # target_uri      Target URI.
+    # match_status    The expected status. The value is (e.g. OK). If the actual state matches
+    #                 any of the states named in this argument, this keyword passes.
+    # match_state     The expected state. This may be one or more comma-separated values
+    #                 (e.g. Running, Completed). If the actual state matches
+    #                 any of the states named in this argument, this keyword passes.
+
+    ${task_list}=  Redfish.Get Members List  /redfish/v1/TaskService/Tasks
+    ${num_records}=  Get Length  ${task_list}
+    Return From Keyword If  ${num_records} == ${0}  ${EMPTY
+
+    ${task_dict}=  Get Task Objects  ${task_list}
+
+    ${task_inventory}=  Check Task Attribute  ${target_uri}  ${match_status}  ${match_state}  ${task_dict}
+
+    ${num_records}=  Get Length  ${task_inventory}
+    Return From Keyword If  ${num_records} == ${0}  ${EMPTY}
+
+    [Return]  ${task_inventory}
+
+
+Get Task Objects
+    [Documentation]  Check all the task to filter update service target URI and
+    ...  return the task attributes as dictionary.
+    [Arguments]  ${task_list}
+
+    # Description of argument(s):
+    # task_list  List of all active task.
+
+    &{task_inv_dict}=  Create Dictionary
+
+    FOR  ${task_id}  IN  @{task_list}
+      &{tmp_dict}=  Create Dictionary
+      ${task_payload}=  Redfish.Get Attribute  ${task_id}  Payload
+      Set To Dictionary  ${tmp_dict}  TargetUri  ${task_payload['TargetUri']}
+
+      ${task_state}=  Redfish.Get Attribute  ${task_id}  TaskState
+      Set To Dictionary  ${tmp_dict}  TaskState  ${task_state}
+
+      ${task_status}=  Redfish.Get Attribute  ${task_id}  TaskStatus
+      Set To Dictionary  ${tmp_dict}  TaskStatus  ${task_status}
+      Set To Dictionary  ${task_inv_dict}  ${task_id.split("/")[-1]}  ${tmp_dict}
+    END
+
+    [Return]  ${task_inv_dict}
+
+
+Check Task Attribute
+    [Documentation]  Get the active task progress state.
+    [Arguments]  ${target_uri}  ${match_status}  ${match_state}  ${task_dict}
+
+    # Description of argument(s):
+    # target_uri      Active task which has update service target URI.
+    # match_status    The expected status. The value is (e.g. OK). If the actual state matches
+    #                 any of the states named in this argument, this keyword passes.
+    # match_state     The expected state. This may be one or more comma-separated values
+    #                 (e.g. Running, Completed). If the actual state matches
+    #                 any of the states named in this argument, this keyword passes.
+    # task_dict       Task inventory.
+
+    Return From Keyword If  ${target_uri} == '' or ${target_uri} == None  ${task_dict}
+    Return From Keyword If  ${match_state} == '' or ${match_state} == None  ${task_dict}
+
+    FOR  ${task_ins}  IN  @{task_dict.items()}
+      &{tmp_dict}=  Create Dictionary
+      Set To Dictionary  ${tmp_dict}  ${task_ins[0]}  ${task_ins[1]}
+      Return From Keyword If
+      ...  ${target_uri} == '${task_ins[1]['TargetUri']}' and ${match_state} == '${task_ins[1]['TaskState']}'
+      ...  ${tmp_dict}
+    END
+
+    [Return]  ${EMPTY}
+
+
+Check Task Progress State
+    [Documentation]  Check that the task update progress state matches the specified state.
+    [Arguments]  ${task_inv_dict}  ${match_status}  ${match_state}
+
+    # Description of argument(s):
+    # task_inv_dict    Task inventory dictionary.
+    # match_status     The expected state. The value is (e.g. OK). If the actual state matches
+    #                  any of the states named in this argument, this keyword passes.
+    # match_state      The expected state. This may be one or more comma-separated values
+    #                  (e.g. Running, Completed). If the actual state matches
+    #                  any of the states named in this argument, this keyword passes.
+
+    FOR  ${task_ins}  IN  @{task_inv_dict.items()}
+      ${task_state}=  Redfish.Get Attribute  /redfish/v1/TaskService/Tasks/${task_ins}[0]  TaskState
+      ${task_status}=  Redfish.Get Attribute  /redfish/v1/TaskService/Tasks/${task_ins}[0]  TaskStatus
+      ${task_payload}=  Redfish.Get Attribute  /redfish/v1/TaskService/Tasks/${task_ins}[0]  Payload
+
+      Rprint Vars  task_state
+      Rprint Vars  task_status
+      Rprint Vars  task_payload['TargetUri']
+
+      Valid Value  task_state  valid_values=[${match_state}]
+      Valid Value  task_status  valid_values=[${match_status}]
+    END
+
+
 Get Image Id
     [Documentation]  Get image id.
     [Arguments]  ${match_state}