[VOL-4733] OpenOnuAdapter OMCI extended Message support SW upgrade

Patch also contains improvements and small fixes regarding
MIB Template

Change-Id: I80ecd542c4a3a1bc9783725a9774064617a294c6
diff --git a/libraries/bbsim.robot b/libraries/bbsim.robot
index 049385c..9fed918 100755
--- a/libraries/bbsim.robot
+++ b/libraries/bbsim.robot
@@ -124,3 +124,105 @@
     ${value}=    Fetch From Right    ${output}    :
     ${count}=    Fetch From Left     ${value}    }
     [Return]    ${count}
+
+Restart And Check BBSIM
+    [Documentation]    This keyword restarts bbsim and waits for it to come up again
+    ...    Following steps will be executed:
+    ...    - restart bbsim adaptor
+    ...    - check bbsim adaptor is ready again
+    [Arguments]    ${namespace}
+    ${bbsim_apps}   Create List    bbsim
+    ${label_key}    Set Variable   app
+    ${bbsim_label_value}    Set Variable   bbsim
+    Restart Pod By Label    ${namespace}    ${label_key}    ${bbsim_label_value}
+    Sleep    5s
+    Wait For Pods Ready    ${namespace}    ${bbsim_apps}
+
+Get BBSIM Svc and Webserver Port
+    [Documentation]    This keyword gets bbsim instance and bbsim webserver port from image url
+    @{words}=    Split String    ${image_url}    /
+    ${SvcAndPort}    Set Variable     @{words}[2]
+    ${bbsim_svc}    ${webserver_port}=    Split String    ${SvcAndPort}    :    1
+    ${svc_return}    Set Variable If    '${bbsim_svc}'!='${EMPTY}'    ${bbsim_svc}    ${BBSIM_INSTANCE}
+    ${port_return}   Set Variable If    '${webserver_port}'!='${EMPTY}'    ${webserver_port}    ${BBSIM_WEBSERVER_PORT}
+    [Return]    ${svc_return}    ${port_return}
+
+# keywords regarding OMCC message version
+
+Get BBSIM OMCC Version
+    [Documentation]    Retrieves OMCC Version from BBSIM
+    [Arguments]    ${namespace}    ${instance}=0
+    ${rc}    ${exec_pod_name}=    Run and Return Rc and Output
+    ...    kubectl get pods -n ${namespace} | grep bbsim${instance} | awk 'NR==1{print $1}'
+    Log    ${exec_pod_name}
+    Should Not Be Empty    ${exec_pod_name}    Unable to parse pod name
+    ${rc}    ${output}=    Run and Return Rc and Output
+    ...    kubectl -n ${namespace} get pods ${exec_pod_name} -o=jsonpath="{.spec.containers[].command}"
+    Log    ${output}
+    Should Be Equal as Integers    ${rc}    0
+    Should Not Be Empty    ${output}    Unable to read OMCC Version
+    ${output}=    Remove String    ${output}    "    [    ]
+    @{commands}=    Split String    ${output}    ,
+    ${length}=    Get Length    ${commands}
+    ${match}=    Set Variable    False
+    FOR    ${I}    IN RANGE    0    ${length}
+        ${item}=    Get From List    ${commands}    ${I}
+        ${match}=    Set Variable If    "${item}"=="-omccVersion"    True    ${match}
+        ${omcc_version}=    Run Keyword If    ${match}    Get From List    ${commands}    ${I+1}
+        Exit For Loop IF     ${match}
+    END
+    Should Be True    ${match}    Unable to read OMCC Version
+    ${is_extended}=    Is OMCC Extended Version    ${omcc_version}
+    [return]    ${omcc_version}    ${is_extended}
+
+Is OMCC Extended Version
+    [Documentation]    Checks passed value and return False (baseline) or True (extended)
+    ...                baseline: 124-130, 160-163
+    ...                extended: 150, 176-180
+    [Arguments]    ${omcc_version}
+    ${is_extended}=    Set Variable If    '${omcc_version}'=='150'    True
+    ...      '${omcc_version}'>='176' and '${omcc_version}'<='180'    True
+    ...                                                               False
+    [return]    ${is_extended}
+
+# Keywords regarding restart BBSIM by Helm Charts
+
+Restart BBSIM by Helm Charts
+    [Documentation]    Restart BBSIM by helm charts
+    ...                Attention: config-yaml file has to pass by ${extra_helm_flags}!
+    [Arguments]    ${namespace}    ${instance}=0    ${extra_helm_flags}=${EMPTY}
+    Remove BBSIM Helm Charts    ${namespace}    ${instance}
+    Restart BBSIM Helm Charts   ${namespace}    ${instance}    extra_helm_flags=${extra_helm_flags}
+    Restart Port Forward BBSIM  ${namespace}    ${instance}
+
+Remove BBSIM Helm Charts
+    [Documentation]    Remove BBSIM helm charts
+    [Arguments]    ${namespace}    ${instance}=0
+    ${cmd}    Catenate    helm delete -n '${namespace}' 'bbsim${instance}'
+    ${rc}    Run And Return Rc    ${cmd}
+    Should Be Equal as Integers    ${rc}    0
+    ${list}    Create List    bbsim${instance}
+    Wait For Pods Not Exist    ${namespace}    ${list}
+
+Restart BBSIM Helm Charts
+    [Documentation]    Restart BBSIM helm charts
+    ...                Attention: config-yaml file has to pass by ${extra_helm_flags}!
+    [Arguments]    ${namespace}    ${instance}=0    ${extra_helm_flags}=${EMPTY}
+    ${cmd}    Catenate
+    ...    helm upgrade --install -n ${namespace} bbsim${instance} onf/bbsim
+    ...    --set olt_id=1${instance}
+    ...    --set global.image_pullPolicy=Always
+    ...    --set global.image_tag=master
+    ...    --set global.image_org=voltha/
+    ...    --set global.image_registry=
+    ...    --set global.log_level=${helmloglevel} ${extra_helm_flags}
+    ${rc}    Run And Return Rc    ${cmd}
+    Should Be Equal as Integers    ${rc}    0
+    ${list}   Create List    bbsim
+    Wait For Pods Ready    ${namespace}    ${list}
+
+Restart Port Forward BBSIM
+    [Documentation]    Restart Port forward BBSIM
+    [Arguments]    ${namespace}    ${instance}=0
+    ${tag}    Catenate    bbsim${instance}
+    Restart VOLTHA Port Forward    ${tag}
diff --git a/tests/openonu-go-adapter/Voltha_ONUTemplateTests.robot b/tests/openonu-go-adapter/Voltha_ONUTemplateTests.robot
index 8ace201..f021982 100755
--- a/tests/openonu-go-adapter/Voltha_ONUTemplateTests.robot
+++ b/tests/openonu-go-adapter/Voltha_ONUTemplateTests.robot
@@ -172,8 +172,7 @@
     ...                It changes BBSIM configuration to OMCI extended messages.
     ...                ================= !!! Attention!!! ======================
     [Tags]    functionalOnuGo    MibTemplateOmciBaselineVersusExtendedOnuGo
-    [Setup]    Run Keywords    Start Logging    MibTemplateOmciBaselineVersusExtendedOnuGo
-    ...    AND    Setup
+    [Setup]   Start Logging    MibTemplateOmciBaselineVersusExtendedOnuGo
     Perform ONU MIB Template Compare OMCI Baseline and Extended Message
     [Teardown]    Run Keywords    Printout ONU Serial Number and Device Id
     ...    AND    Run Keyword If    ${logging}    Collect Logs
@@ -254,7 +253,13 @@
     ${waittime}=      Set Variable    0ms
     ${state2test}=    Set Variable    initial-mib-downloaded
     Run Keyword If    ${has_dataplane}    Clean Up Linux
-    # Start Onu with OMCI Baseline Message
+    ${omcc_version}    ${is_omcc_extended}=    Get BBSIM OMCC Version    ${NAMESPACE}
+    # Restart BBSIM with OMCI Baseline Message if needed
+    ${extra_helm_flags}    Catenate
+    ...    --set onu=2,pon=2,controlledActivation=only-onu,injectOmciUnknownAttributes=true,injectOmciUnknownMe=true
+    ...    --set omccVersion=163
+    Run Keyword If    ${is_omcc_extended}    Restart BBSIM by Helm Charts    ${NAMESPACE}    extra_helm_flags=${extra_helm_flags}
+    Setup
     ${src}=    Set Variable    ${hosts.src[${firstonu}]}
     Log    \r\nONU ${src['onu']}: startup with MIB upload cycle and storage of template data to etcd.    console=yes
     ${bbsim_pod}=    Get Pod Name By Label    ${NAMESPACE}    release     bbsim0
@@ -270,10 +275,10 @@
     # Restart BBSIM with OMCI Extended Message
     ${extra_helm_flags}    Catenate
     ...    --set onu=2,pon=2,controlledActivation=only-onu,injectOmciUnknownAttributes=true,injectOmciUnknownMe=true
-    ...    --set omccVersion=180
-    Remove BBSIM Helm Charts    ${NAMESPACE}
-    Restart BBSIM               ${NAMESPACE}    extra_helm_flags=${extra_helm_flags}
-    Restart Port Forward BBSIM  ${NAMESPACE}
+    ${extra_helm_flags}=    Run Keyword If    ${is_omcc_extended}
+    ...              Catenate    ${extra_helm_flags} --set omccVersion=${omcc_version}
+    ...     ELSE     Catenate    ${extra_helm_flags} --set omccVersion=180
+    Restart BBSIM by Helm Charts    ${NAMESPACE}    extra_helm_flags=${extra_helm_flags}
     # Start Onu again with OMCI Extended Message
     Setup
     ${src}=    Set Variable    ${hosts.src[${firstonu}]}
@@ -289,7 +294,7 @@
     # Checks:
     # - compare durations of MIB download, OMCI extended message duration should be less than %60 of baseline
     # - both stored MIB tenmplates in ETCD should be equal
-    ${duration_compare}=    Evaluate    ${baselineonustartup}*0,8 > ${extendedonustartup}
+    ${duration_compare}=    Evaluate    ${baselineonustartup}*0.8 > ${extendedonustartup}
     Should Be True    ${duration_compare}   MIB Template download too slow for OMCI extended message!
     # remove "TemplateCreated"  e.g. "TemplateCreated":"2022-06-15 11:23:47.306519",
     ${remove_regexp}    Set Variable    (?ms)"TemplateCreated":"[^"]*",
@@ -302,6 +307,11 @@
     ${MibTemplateDataExtended}=    Remove String Using Regexp    ${MibTemplateDataExtended}    ${remove_regexp}
     # end of handling for VOL-4721
     Should Be Equal As Strings    ${MibTemplateDataBaseline}    ${MibTemplateDataExtended}    MIB Templates not equal!
+    # Restart BBSIM with OMCI Message Version read at begin of test
+    ${extra_helm_flags}=    Catenate
+    ...    --set onu=2,pon=2,controlledActivation=only-onu,injectOmciUnknownAttributes=true,injectOmciUnknownMe=true
+    ...    --set omccVersion=${omcc_version}
+    Run Keyword Unless   ${is_omcc_extended}   Restart BBSIM by Helm Charts   ${NAMESPACE}   extra_helm_flags=${extra_helm_flags}
 
 Get ONU Startup Duration
     [Documentation]    This keyword delivers startup duration of onu
@@ -328,36 +338,3 @@
     Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    1s
     ...    Validate Device    ${admin_state}    ${oper_status}    ${connect_status}
     ...    ${src['onu']}    onu=True    onu_reason=${onu_state}
-
-# Keywords regarding restart BBSIM
-
-Remove BBSIM Helm Charts
-    [Arguments]    ${namespace}    ${instance}=0
-    [Documentation]    Remove BBSIM helm charts
-    ${cmd}    Catenate    helm delete -n '${namespace}' 'bbsim${instance}'
-    ${rc}    Run And Return Rc    ${cmd}
-    Should Be Equal as Integers    ${rc}    0
-    ${list}    Create List    bbsim${instance}
-    Wait For Pods Not Exist    ${namespace}    ${list}
-
-Restart BBSIM
-    [Documentation]    Restart BBSIM helm chart
-    [Arguments]    ${namespace}    ${instance}=0    ${extra_helm_flags}=${EMPTY}
-    ${cmd}    Catenate
-    ...    helm upgrade --install -n ${namespace} bbsim${instance} onf/bbsim
-    ...    --set olt_id=1${instance}
-    ...    --set global.image_pullPolicy=Always
-    ...    --set global.image_tag=master
-    ...    --set global.image_org=voltha/
-    ...    --set global.image_registry=
-    ...    --set global.log_level=${helmloglevel} ${extra_helm_flags}
-    ${rc}    Run And Return Rc    ${cmd}
-    Should Be Equal as Integers    ${rc}    0
-    ${list}   Create List    bbsim
-    Wait For Pods Ready    ${namespace}    ${list}
-
-Restart Port Forward BBSIM
-    [Documentation]    Restart Port forward BBSIM
-    [Arguments]    ${namespace}    ${instance}=0
-    ${tag}    Catenate    bbsim${instance}
-    Restart VOLTHA Port Forward    ${tag}
\ No newline at end of file
diff --git a/tests/software-upgrades/ONU_Upgrade.robot b/tests/software-upgrades/ONU_Upgrade.robot
index 99be3ec..fdf3227 100755
--- a/tests/software-upgrades/ONU_Upgrade.robot
+++ b/tests/software-upgrades/ONU_Upgrade.robot
@@ -30,6 +30,7 @@
 Resource          ../../libraries/voltha.robot
 Resource          ../../libraries/utils.robot
 Resource          ../../libraries/k8s.robot
+Resource          ../../libraries/onu_utilities.robot
 Resource          ../../libraries/bbsim.robot
 Resource          ../../variables/variables.robot
 
@@ -90,6 +91,11 @@
 # port forward handle
 ${portFwdHandle}     None
 
+# Log Level of Helm chart
+# example: -v helmloglevel:WARN
+${helmloglevel}    DEBUG
+
+
 *** Test Cases ***
 Test ONU Upgrade
     [Documentation]    Validates the ONU Upgrade doesn't affect the system functionality
@@ -390,6 +396,26 @@
     Run Keyword If    ${has_dataplane}    Clean Up Linux
     Wait Until Keyword Succeeds    ${timeout}    2s    Perform Sanity Test
 
+Test ONU Upgrade Compare OMCI Message Version
+    [Documentation]    Validates the ONU Upgrade duration depending on OMCI message version
+    ...                - setup one ONU with baseline OMCI message (EXTRA_HELM_FLAGS=" --set omccVersion=163)
+    ...                - perform a SW upgrade
+    ...                - store SW upgrade duration
+    ...                - delete all devices
+    ...                - setup one ONU with extended OMCI message (EXTRA_HELM_FLAGS=" --set omccVersion=180)
+    ...                - perform a SW upgrade
+    ...                - store SW upgrade duration
+    ...                - compare both duration
+    ...                - duration of extended msg ONU should be at least less than 80% of the baseline one
+    ...                  Check [VOL-4733] for more details
+    [Tags]    functional    ONUUpgradeOmciBaselineVersusExtendedOnuGo
+    [Setup]    Start Logging    ONUUpgradeOmciBaselineVersusExtendedOnuGo
+    [Teardown]    Run Keywords    Run Keyword If    ${logging}    Collect Logs
+    ...           AND             Delete All Devices and Verify
+    ...           AND             Restart And Check BBSIM    ${NAMESPACE}
+    ...           AND             Stop Logging    ONUUpgradeOmciBaselineVersusExtendedOnuGo
+    Do ONU Upgrade Compare OMCI Message Version
+
 Test ONU Upgrade Image Download Simultaneously
     [Documentation]    Validates the ONU Upgrade Image Download to all ONUs simultaneously.
     ...    Test case should executed in multiple ONU (OLT) environment!
@@ -694,6 +720,75 @@
         Remove Adapter Image    ${image_version}    ${onu_device_id}
     END
 
+Do ONU Upgrade Compare OMCI Message Version
+    [Documentation]    This keyword performs the ONU Upgrade Compare OMCI Message Version test for the first OLT/ONU
+    [Arguments]    ${activate_on_success}=${image_activate_on_success}    ${commit_on_success}=${image_commit_on_success}
+    [Teardown]     Run Keyword If  '${KEYWORD STATUS}'=='FAIL'    Remove Adapter Image    ${image_version}    ${onu_device_id}
+    ${firstonu}=      Set Variable    0
+    ${outputfile}=    Set variable    ${OUTPUTDIR}/ONU_SW_Upgrade_Time.txt
+    ${omcc_version}    ${is_omcc_extended}=    Get BBSIM OMCC Version    ${NAMESPACE}
+    # Restart BBSIM with OMCI Baseline Message if needed
+    Create File    ${outputfile}    This file contains the SW Upgrade download durations
+    ${extra_helm_flags}    Catenate    --set omccVersion=163
+    Run Keyword If    ${is_omcc_extended}    Restart BBSIM by Helm Charts    ${NAMESPACE}    extra_helm_flags=${extra_helm_flags}
+    # Add OLT device
+    Setup
+    ${src}=    Set Variable    ${hosts.src[${firstonu}]}
+    ${onu_device_id}=    Get Device ID From SN    ${src['onu']}
+    ${admin_state}    ${oper_status}    ${connect_status}    ${onu_state_nb}    ${onu_state}=
+    ...    Map State    omci-flows-pushed
+    Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    1s
+    ...    Validate Device    ${admin_state}    ${oper_status}    ${connect_status}
+    ...    ${src['onu']}    onu=True    onu_reason=${onu_state}
+    ${baselineonu}=    Get ONU SW Upgrade Duration    ${firstonu}   ${activate_on_success}    ${commit_on_success}
+    Log    ONU ${src['onu']}: downloaded SW upgrade in ${baselineonu} sec for OMCI baseline message.    console=yes
+    Append To File    ${outputfile}
+    ...    \r\nONU ${src['onu']} downloaded SW upgrade in ${baselineonu} sec for OMCI baseline message.
+    Delete All Devices and Verify
+    # Restart BBSIM with OMCI Extended Message
+    ${extra_helm_flags}=    Run Keyword If    ${is_omcc_extended}     Catenate    --set omccVersion=${omcc_version}
+    ...     ELSE     Catenate    --set omccVersion=180
+    Restart BBSIM by Helm Charts    ${NAMESPACE}    extra_helm_flags=${extra_helm_flags}
+    # Start Onu again with OMCI Extended Message
+    Setup
+    ${onu_device_id}=    Get Device ID From SN    ${src['onu']}
+    Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    1s
+    ...    Validate Device    ${admin_state}    ${oper_status}    ${connect_status}
+    ...    ${src['onu']}    onu=True    onu_reason=${onu_state}
+    ${extendedonu}=    Get ONU SW Upgrade Duration    ${firstonu}   ${activate_on_success}    ${commit_on_success}
+    Log    ONU ${src['onu']}: downloaded SW upgrade in ${extendedonu} sec for OMCI extended message.    console=yes
+    Append To File    ${outputfile}
+    ...    \r\nONU ${src['onu']} downloaded SW upgrade in ${extendedonu} sec for OMCI extended message.
+    ${duration_compare}=    Evaluate    ${baselineonu}*0.8 > ${extendedonu}
+    Should Be True    ${duration_compare}   SW Upgrade too slow for OMCI extended message!
+    # Restart BBSIM with OMCI Message Version read at begin of test
+    ${extra_helm_flags}=    Catenate    --set omccVersion=${omcc_version}
+    Run Keyword Unless   ${is_omcc_extended}   Restart BBSIM by Helm Charts   ${NAMESPACE}   extra_helm_flags=${extra_helm_flags}
+
+Get ONU SW Upgrade Duration
+    [Documentation]    This keyword delivers SW Upgrade duration of onu
+    [Arguments]    ${onu}   ${activate_on_success}    ${commit_on_success}
+    ${src}=    Set Variable    ${hosts.src[${onu}]}
+    ${onu_device_id}=    Get Device ID From SN    ${src['onu']}
+    ${timeStart}=    Get Current Date
+    Download ONU Device Image    ${image_version}    ${image_url}    ${image_vendor}
+    ...    ${activate_on_success}    ${commit_on_success}
+    ...    ${image_crc}    ${onu_device_id}
+    ${imageState}=    Run Keyword If    '${activate_on_success}'=='true' and '${commit_on_success}'=='false'
+    ...    Set Variable    IMAGE_ACTIVE
+    ...    ELSE IF    '${activate_on_success}'=='true' and '${commit_on_success}'=='true'
+    ...    Set Variable    IMAGE_COMMITTED
+    ...    ELSE    Set Variable    IMAGE_INACTIVE
+    ${activated}=    Set Variable If    '${activate_on_success}'=='true'    True    False
+    ${committed}=    Set Variable If    '${activate_on_success}'=='true' and '${commit_on_success}'=='true'
+    ...    True    False
+    Wait Until Keyword Succeeds    ${timeout}    0s    Verify ONU Device Image Status    ${image_version}
+    ...    ${onu_device_id}    DOWNLOAD_SUCCEEDED    NO_ERROR    ${imageState}
+    ${timeCurrent} =    Get Current Date
+    ${timeTotalMs} =    Subtract Date From Date    ${timeCurrent}    ${timeStart}    result_format=number
+    Remove Adapter Image    ${image_version}    ${onu_device_id}
+    [Return]    ${timeTotalMs}
+
 Do ONU Upgrade Image Download Simultaneously
     [Documentation]    This keyword performs the ONU Upgrade Image Download Simultaneously on all ONUs test
     [Arguments]    ${url}=${image_url}
@@ -733,28 +828,6 @@
     ${Images_Count_Start}=    Evaluate    ${Images_Count_Start}+2
     Should Be Equal as Integers    ${Images_Count_End}    ${Images_Count_Start}   Count of image download not correct!
 
-Restart And Check BBSIM
-    [Documentation]    This keyword restarts bbsim and waits for it to come up again
-    ...    Following steps will be executed:
-    ...    - restart bbsim adaptor
-    ...    - check bbsim adaptor is ready again
-    [Arguments]    ${namespace}
-    ${bbsim_apps}   Create List    bbsim
-    ${label_key}    Set Variable   app
-    ${bbsim_label_value}    Set Variable   bbsim
-    Restart Pod By Label    ${namespace}    ${label_key}    ${bbsim_label_value}
-    Sleep    5s
-    Wait For Pods Ready    ${namespace}    ${bbsim_apps}
-
-Get BBSIM Svc and Webserver Port
-    [Documentation]    This keyword gets bbsim instance and bbsim webserver port from image url
-    @{words}=    Split String    ${image_url}    /
-    ${SvcAndPort}    Set Variable     @{words}[2]
-    ${bbsim_svc}    ${webserver_port}=    Split String    ${SvcAndPort}    :    1
-    ${svc_return}    Set Variable If    '${bbsim_svc}'!='${EMPTY}'    ${bbsim_svc}    ${BBSIM_INSTANCE}
-    ${port_return}   Set Variable If    '${webserver_port}'!='${EMPTY}'    ${webserver_port}    ${BBSIM_WEBSERVER_PORT}
-    [Return]    ${svc_return}    ${port_return}
-
 Setup Suite
     [Documentation]    Set up the test suite
     Start Logging Setup or Teardown    Setup-${SUITE NAME}