[VOL-4170] Updated ONU Software Upgrade test

Change-Id: Ifeb62432b56e7bd2639b1b58590b5b93d5f1ccce
diff --git a/Makefile b/Makefile
index 306b3e7..b90fdfe 100755
--- a/Makefile
+++ b/Makefile
@@ -439,7 +439,7 @@
 voltha-comp-upgrade-test: software-upgrade-test
 
 # Requirement: Pass ONU image details in following parameters
-# onu_image_name, onu_image_url, onu_image_version, onu_image_crc, onu_image_local_dir
+# image_version, image_url, image_vendor, image_activate_on_success, image_commit_on_success, image_crc
 onu-upgrade-test: ROBOT_MISC_ARGS +=  -e notready -i functional
 onu-upgrade-test: ROBOT_FILE := ONU_Upgrade.robot
 onu-upgrade-test: ROBOT_CONFIG_FILE := $(ROBOT_SW_UPGRADE_FILE)
diff --git a/libraries/voltctl.robot b/libraries/voltctl.robot
index 9e4f2c7..9ed7ff7 100755
--- a/libraries/voltctl.robot
+++ b/libraries/voltctl.robot
@@ -804,25 +804,73 @@
     Log     ${targetFlows}
     Wait Until Keyword Succeeds     10m     5s  Count OpenOLT Device Flows     ${targetFlows}
 
+Validate ONU Device Image
+    [Documentation]    Validates the ONU device image
+    [Arguments]    ${data}    ${image_version}    ${dev_id}    ${download_state}    ${expected_reason}    ${image_status}
+    ${jsondata}=    To Json    ${data}
+    ${length}=    Get Length    ${jsondata}
+    Should Be Equal As Integers    ${length}    1    No record found for ${dev_id} to validate device image
+    ${value}=    Get From List    ${jsondata}    0
+    Log    ${value}
+    ${deviceId}=    Get From Dictionary    ${value}    deviceId
+    Should Be Equal    '${deviceId}'    '${dev_id}'    No match found for ${dev_id} to validate device image
+    ...    values=False
+    ${imageState}=    Get From Dictionary    ${value}    imageState
+    ${version}=    Get From Dictionary    ${imageState}    version
+    ${downloadState}=    Get From Dictionary    ${imageState}    downloadState
+    ${reason}=    Get From Dictionary    ${imageState}    reason
+    ${imageStatus}=    Get From Dictionary    ${imageState}    imageState
+    Should Be Equal    '${version}'    '${image_version}'    Device ${dev_id}: '${version}' != '${image_version}'
+    ...    values=False
+    Should Be Equal    '${downloadState}'    '${download_state}'    Device ${dev_id}: '${downloadState}' != '${download_state}'
+    ...    values=False
+    Should Be Equal    '${reason}'    '${expected_reason}'    Device ${dev_id}: '${reason}' != '${expected_reason}'
+    ...    values=False
+    Should Be Equal    '${imageStatus}'    '${image_status}'    Device ${dev_id}: '${imageStatus}' != '${image_status}'
+    ...    values=False
+
 Download ONU Device Image
     [Documentation]    Downloads the given ONU software image
-    [Arguments]    ${id}    ${image}    ${url}    ${ver}    ${crc}    ${local_dir}
-    ${rc}=    Run and Return Rc
-    ...    voltctl -c ${VOLTCTL_CONFIG} device image download ${id} ${image} ${url} ${ver} ${crc} ${local_dir}
+    [Arguments]    ${ver}    ${url}    ${vendor}    ${active}    ${commit}    ${crc}    ${id}
+    ${rc}    ${output}=    Run and Return Rc and Output
+    ...    voltctl -c ${VOLTCTL_CONFIG} device onuimage download ${ver} ${url} ${vendor} ${active} ${commit} ${crc} ${id} -o json
+    Log    ${output}
     Should Be Equal As Integers    ${rc}    0
+    Validate ONU Device Image    ${output}    ${ver}    ${id}    DOWNLOAD_STARTED    NO_ERROR    IMAGE_INACTIVE
 
 Activate ONU Device Image
-    [Documentation]    Activatess the given ONU software image
-    [Arguments]    ${id}    ${image}    ${ver}    ${crc}    ${local_dir}
-    ${rc}=    Run and Return Rc
-    ...    voltctl -c ${VOLTCTL_CONFIG} device image activate ${id} ${image} ${ver} ${crc} ${local_dir}
-    Should Be Equal As Integers    ${rc}    0
-
-Verify ONU Device Image
-    [Documentation]    Verfies the ONU device image state
-    [Arguments]    ${dev_id}    ${download_state}    ${image_state}    ${expected_reason}
+    [Documentation]    Activates the given ONU software image
+    [Arguments]    ${ver}    ${commit}    ${id}
     ${rc}    ${output}=    Run and Return Rc and Output
-    ...    voltctl -c ${VOLTCTL_CONFIG} device image list ${dev_id} -o json
+    ...    voltctl -c ${VOLTCTL_CONFIG} device onuimage activate ${ver} ${commit} ${id} -o json
+    Log    ${output}
+    Should Be Equal As Integers    ${rc}    0
+    Validate ONU Device Image    ${output}    ${ver}    ${id}    DOWNLOAD_SUCCEEDED    NO_ERROR    IMAGE_ACTIVATING
+
+Commit ONU Device Image
+    [Documentation]    Commits the given ONU software image
+    [Arguments]    ${ver}    ${id}
+    ${rc}    ${output}=    Run and Return Rc and Output
+    ...    voltctl -c ${VOLTCTL_CONFIG} device onuimage commit ${ver} ${id} -o json
+    Log    ${output}
+    Should Be Equal As Integers    ${rc}    0
+    Validate ONU Device Image    ${output}    ${ver}    ${id}    DOWNLOAD_SUCCEEDED    NO_ERROR    IMAGE_COMMITTING
+
+Verify ONU Device Image Status
+    [Documentation]    Verfies the ONU device image state
+    [Arguments]    ${image_version}    ${dev_id}    ${download_state}    ${expected_reason}    ${image_state}
+    ${rc}    ${output}=    Run and Return Rc and Output
+    ...    voltctl -c ${VOLTCTL_CONFIG} device onuimage status ${image_version} ${dev_id} -o json
+    Log    ${output}
+    Should Be Equal As Integers    ${rc}    0
+    Validate ONU Device Image    ${output}    ${image_version}    ${dev_id}    ${download_state}    ${expected_reason}
+    ...    ${image_state}
+
+Verify ONU Device Image List
+    [Documentation]    Verfies the ONU device image list
+    [Arguments]    ${dev_id}    ${image_version}    ${committed}    ${activated}    ${valid}
+    ${rc}    ${output}=    Run and Return Rc and Output
+    ...    voltctl -c ${VOLTCTL_CONFIG} device onuimage list ${dev_id} -o json
     Should Be Equal As Integers    ${rc}    0
     ${jsondata}=    To Json    ${output}
     Log    ${jsondata}
@@ -830,18 +878,20 @@
     ${matched}=    Set Variable    False
     FOR    ${INDEX}    IN RANGE    0    ${length}
         ${value}=    Get From List    ${jsondata}    ${INDEX}
-        ${id}=    Get From Dictionary    ${value}    id
-        ${downloadstate}=    Get From Dictionary    ${value}    downloadState
-        ${imagestate}=    Get From Dictionary    ${value}    imageState
-        ${reason}=    Get From Dictionary    ${value}    reason
-        ${matched}=    Set Variable If    '${id}' == '${dev_id}'    True    False
+        ${version}=    Get From Dictionary    ${value}    version
+        ${isCommited}=    Get From Dictionary    ${value}    isCommited
+        ${isActive}=    Get From Dictionary    ${value}    isActive
+        ${isValid}=    Get From Dictionary    ${value}    isValid
+        ${matched}=    Set Variable If    '${version}' == '${image_version}'    True    False
         Exit For Loop If    ${matched}
     END
-    Should Be True    ${matched}    No ONU Device Image for Id: ${dev_id}
-    Should Be Equal    '${downloadstate}'    '${download_state}'    ONU Device ${id} Image downloadState does not match
-    Should Be Equal    '${imagestate}'    '${image_state}'    ONU Device ${id} Image imageState does not match
-    Should Be Equal    '${reason}'    '${expected_reason}'    ONU Device ${id} Image reason does not match
-
+    Should Be True    ${matched}     No ONU Image found with Version ${image_version}
+    Should Be Equal    '${isCommited}'    '${committed}'    Device ${dev_id}: '${isCommited}' != '${committed}'
+    ...    values=False
+    Should Be Equal    '${isActive}'    '${activated}'    Device ${dev_id}: '${isActive}' != '${activated}'
+    ...    values=False
+    Should Be Equal    '${isValid}'    '${valid}'    Device ${dev_id}: '${isValid}' != '${valid}'
+    ...    values=False
 
 # pm-data relevant keywords
 Read Default Interval From Pmconfig
diff --git a/tests/software-upgrades/ONU_Upgrade.robot b/tests/software-upgrades/ONU_Upgrade.robot
index a342d1c..40b4e1d 100755
--- a/tests/software-upgrades/ONU_Upgrade.robot
+++ b/tests/software-upgrades/ONU_Upgrade.robot
@@ -58,16 +58,18 @@
 ${suppressaddsubscriber}    True
 
 # ONU Image to test for Upgrade needs to be passed in the following format:
-${onu_image_name}    ${EMPTY}
-# Example value: twsh.img
-${onu_image_url}    ${EMPTY}
+${image_version}    ${EMPTY}
+# Example value: BBSM_IMG_00002
+${image_url}    ${EMPTY}
 # Example value: http://bbsim0:50074/images/software-image.img
-${onu_image_version}    ${EMPTY}
-# Example value: v1.0.0
-${onu_image_crc}    ${EMPTY}
+${image_vendor}    ${EMPTY}
+# Example value: BBSM
+${image_activate_on_success}    ${EMPTY}
+# Example value: false
+${image_commit_on_success}    ${EMPTY}
+# Example value: false
+${image_crc}    ${EMPTY}
 # Example value: 0
-${onu_image_local_dir}    ${EMPTY}
-# Example value: /tmp
 
 *** Test Cases ***
 Test ONU Upgrade
@@ -109,22 +111,37 @@
         ${dst}=    Set Variable    ${hosts.dst[${I}]}
         Continue For Loop If    "${olt_serial_number}"!="${src['olt']}"
         ${onu_device_id}=    Get Device ID From SN    ${src['onu']}
-        Download ONU Device Image    ${onu_device_id}    ${onu_image_name}    ${onu_image_url}    ${onu_image_version}
-        ...    ${onu_image_crc}    ${onu_image_local_dir}
-        # This additional pause to let image download finish on the OLT adapter
-        # as the DOWNLOADED_SUCCEEDED below only indicates that the command is accepted.
-        Run Keyword If    ${has_dataplane}    Sleep    12s
-        ...    ELSE    Sleep    2s
-        Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image    ${onu_device_id}
-        ...    DOWNLOAD_SUCCEEDED    IMAGE_UNKNOWN    NO_ERROR
-        Activate ONU Device Image    ${onu_device_id}    ${onu_image_name}    ${onu_image_version}
-        ...    ${onu_image_crc}    ${onu_image_local_dir}
-        # This additional pause to let image download and finish activate on the ONU (BBSim)
-        # as the IMAGE_ACTIVE below is only an indication that the image is accepted for the download to the ONU (BBSim).
-        Run Keyword If    ${has_dataplane}    Sleep    600s
-        ...    ELSE    Sleep    180s
-        Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image    ${onu_device_id}
-        ...    DOWNLOAD_SUCCEEDED    IMAGE_ACTIVE    NO_ERROR
+        # Download Image
+        Download ONU Device Image    ${image_version}    ${image_url}    ${image_vendor}
+        ...    ${image_activate_on_success}    ${image_commit_on_success}
+        ...    ${image_crc}    ${onu_device_id}
+        ${imageState}=    Run Keyword If    '${image_activate_on_success}'=='true'    Set Variable    IMAGE_ACTIVE
+        ...    ELSE IF    '${image_activate_on_success}'=='true' and '${image_commit_on_success}'=='true'
+        ...    Set Variable    IMAGE_COMMITTED
+        ...    ELSE    Set Variable    IMAGE_INACTIVE
+        ${activated}=    Set Variable If    '${image_activate_on_success}'=='true'    True    False
+        ${committed}=    Set Variable If    '${image_activate_on_success}'=='true' and '${image_commit_on_success}'=='true'
+        ...    True    False
+        Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image Status    ${image_version}
+        ...    ${onu_device_id}    DOWNLOAD_SUCCEEDED    NO_ERROR    ${imageState}
+        Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image List    ${onu_device_id}
+        ...    ${image_version}    ${committed}    ${activated}    True
+        # Activate Image
+        ${imageState}=    Set Variable If    '${image_commit_on_success}'=='true'    IMAGE_COMMITTED    IMAGE_ACTIVE
+        ${committed}=    Set Variable If    '${image_commit_on_success}'=='true'    True    False
+        Run Keyword If    '${image_activate_on_success}'=='false'    Run Keywords
+        ...    Activate ONU Device Image    ${image_version}    ${image_commit_on_success}    ${onu_device_id}
+        ...    AND    Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image Status    ${image_version}
+        ...    ${onu_device_id}    DOWNLOAD_SUCCEEDED    NO_ERROR    ${imageState}
+        ...    AND    Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image List    ${onu_device_id}
+        ...    ${image_version}    ${committed}    True    True
+        # Commit Image
+        Run Keyword If    '${image_commit_on_success}'=='false'    Run Keywords
+        ...    Commit ONU Device Image    ${image_version}    ${onu_device_id}
+        ...    AND    Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image Status    ${image_version}
+        ...    ${onu_device_id}    DOWNLOAD_SUCCEEDED    NO_ERROR    IMAGE_COMMITTED
+        ...    AND    Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image List    ${onu_device_id}
+        ...    ${image_version}    True    True    True
         Wait Until Keyword Succeeds    ${timeout}    2s    Verify ONU Device Image On BBSim    ${NAMESPACE}    ${bbsim_pod}
         ...    ${src['onu']}    software_image_committed
         Wait Until Keyword Succeeds    ${timeout}    5s    Perform Sanity Test     ${suppressaddsubscriber}