[VOL-3612] OpenOnu-Go-Adapter: Extend Flow Test for OpenOnu-Go

Change-Id: I1263602daca9fa5c3c14355450ce0db2237ce390
diff --git a/tests/openonu-go-adapter/Voltha_ONUStateTests.robot b/tests/openonu-go-adapter/Voltha_ONUStateTests.robot
index c772a00..ee16b80 100755
--- a/tests/openonu-go-adapter/Voltha_ONUStateTests.robot
+++ b/tests/openonu-go-adapter/Voltha_ONUStateTests.robot
@@ -296,7 +296,9 @@
     Wait for Ports in ONOS    ${onos_ssh_connection}    ${num_all_onus}    BBSM
 
 Do Onu Flow Check
-    [Documentation]    This keyword iterate all OLTs and performs Do Onu Flow Check Per OLT
+    [Documentation]    This keyword iterate all OLTs and performs Do Onu Flow Checks Per OLT
+    # Check and store vlan rules
+    ${firstvlanrules}=    Run Keyword And Continue On Failure    Validate Vlan Rules In Etcd
     FOR    ${J}    IN RANGE    0    ${num_olts}
         ${olt_serial_number}=    Set Variable    ${list_olts}[${J}][sn]
         ${onu_count}=    Set Variable    ${list_olts}[${J}][onucount]
@@ -307,11 +309,36 @@
         ...    Get NNI Port in ONOS    ${of_id}
         Set Global Variable    ${nni_port}
         # Verify Default Meter in ONOS (valid only for ATT)
-        Do Onu Flow Check Per OLT    ${of_id}    ${nni_port}    ${olt_serial_number}   ${onu_count}
+        Do Onu Subscriber Add And Flow Check Per OLT    ${of_id}    ${nni_port}    ${olt_serial_number}   ${onu_count}
     END
+    #log flows for verification
+    ${flowsresult}=    Execute ONOS CLI Command    ${ONOS_SSH_IP}    ${ONOS_SSH_PORT}    flows -s
+    log     ${flowsresult}
+    #check  for previous state is kept (normally omci-flows-pushed)
+    Sleep    10s
+    Run Keyword And Continue On Failure    Do Current State Test All Onus    ${state2test}
+    ${secondvlanrules}=    Run Keyword And Continue On Failure    Validate Vlan Rules In Etcd    nbofcookieslice=3
+    ...    prevvlanrules=${firstvlanrules}
+    FOR    ${J}    IN RANGE    0    ${num_olts}
+        ${olt_serial_number}=    Set Variable    ${list_olts}[${J}][sn]
+        ${onu_count}=    Set Variable    ${list_olts}[${J}][onucount]
+        ${of_id}=    Wait Until Keyword Succeeds    ${timeout}    15s    Validate OLT Device in ONOS
+        ...    ${olt_serial_number}
+        Set Global Variable    ${of_id}
+        ${nni_port}=    Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    2s
+        ...    Get NNI Port in ONOS    ${of_id}
+        Set Global Variable    ${nni_port}
+        # Verify Default Meter in ONOS (valid only for ATT)
+        Do Onu Subscriber Remove Per OLT    ${of_id}    ${nni_port}    ${olt_serial_number}   ${onu_count}
+    END
+    #check  for previous state is kept (normally omci-flows-pushed)
+    Sleep    10s
+    Run Keyword And Continue On Failure    Do Current State Test All Onus    ${state2test}
+    Run Keyword And Continue On Failure    Validate Vlan Rules In Etcd    prevvlanrules=${firstvlanrules}
+    ...                                    setvidequal=True
 
-Do Onu Flow Check Per OLT
-    [Documentation]    Check per OLT that all ONU flows show up in ONOS and Voltha
+Do Onu Subscriber Add And Flow Check Per OLT
+    [Documentation]    Add Subscriber per OLT and checks all ONU flows show up in ONOS and Voltha
     [Arguments]    ${of_id}    ${nni_port}    ${olt_serial_number}    ${num_onus}
     FOR    ${I}    IN RANGE    0    ${num_all_onus}
         ${src}=    Set Variable    ${hosts.src[${I}]}
@@ -328,8 +355,21 @@
         ...    Verify Subscriber Access Flows Added For ONU    ${ONOS_SSH_IP}    ${ONOS_SSH_PORT}    ${of_id}
         ...    ${onu_port}    ${nni_port}    ${src['c_tag']}    ${src['s_tag']}
     END
-    #check  for previous state is kept (normally omci-flows-pushed)
-    Do Current State Test All Onus    ${state2test}
+
+Do Onu Subscriber Remove Per OLT
+    [Documentation]    Removes per OLT subscribers in ONOS and Voltha
+    [Arguments]    ${of_id}    ${nni_port}    ${olt_serial_number}    ${num_onus}
+    FOR    ${I}    IN RANGE    0    ${num_all_onus}
+        ${src}=    Set Variable    ${hosts.src[${I}]}
+        ${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']}
+        ${onu_port}=    Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    2s
+        ...    Get ONU Port in ONOS    ${src['onu']}    ${of_id}
+        Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    2
+        ...    Execute ONOS CLI Command    ${ONOS_SSH_IP}    ${ONOS_SSH_PORT}
+        ...    volt-remove-subscriber-access ${of_id} ${onu_port}
+    END
 
 Set Tech Profile
     [Documentation]    This keyword set the passed TechProfile for the test
diff --git a/tests/openonu-go-adapter/Voltha_ONUUtilities.robot b/tests/openonu-go-adapter/Voltha_ONUUtilities.robot
index c92fd38..d1e1bfa 100755
--- a/tests/openonu-go-adapter/Voltha_ONUUtilities.robot
+++ b/tests/openonu-go-adapter/Voltha_ONUUtilities.robot
@@ -54,7 +54,7 @@
     Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    50ms
     ...    Validate ONU Devices With Duration
     ...    ${admin_state}    ${oper_status}    ${connect_status}
-    ...    ${onu_state}    ${list_onus}    ${timeStart}    alternate_reason=${alternativeonustate)
+    ...    ${onu_state}    ${list_onus}    ${timeStart}    alternate_reason=${alternativeonustate}
 
 Do Current Reason Test All Onus
     [Documentation]    This keyword checks the passed state of all onus.
@@ -106,6 +106,109 @@
     ${result}=    Exec Pod    ${namespace}    ${podname}    ${commandget}
     Should Be Empty    ${result}    Could not delete MIB Template Data stored in etcd!
 
+Validate Vlan Rules In Etcd
+    [Documentation]    This keyword validates Vlan rules of openonu-go-adapter Data stored in etcd.
+    ...                It checks the given number of cookie_slice, match_vid (=4096) and set_vid.
+    ...                Furthermore it returns a list of all set_vid.
+    ...                In case of a passed dictionary containing set_vids these will be checked for to
+    ...                current set-vid depending on setvidequal (True=equal, False=not equal).
+    [Arguments]    ${nbofcookieslice}=1    ${reqmatchvid}=4096    ${prevvlanrules}=${NONE}    ${setvidequal}=False
+    ${etcddata}=    Get ONU Go Adapter ETCD Data
+    ${etcddata}=    Remove Lines Containing String    ${etcddata}    service/voltha/openonu    \n
+    #prepare result for json convert
+    ${result}=    Prepare ONU Go Adapter ETCD Data For Json    ${etcddata}
+    ${jsondata}=    To Json    ${result}
+    ${length}=    Get Length    ${jsondata}
+    log    ${jsondata}
+    ${vlan_rules}=    Create Dictionary
+    FOR    ${INDEX}    IN RANGE    0    ${length}
+        ${value}=    Get From List    ${jsondata}    ${INDEX}
+        ${tp_path}=    Get From Dictionary    ${value['uni_config'][0]}    tp_path
+        ${pononuuniid}=    Read Pon Onu Uni String    ${tp_path}
+        ${cookieslice}=    Get From Dictionary    ${value['uni_config'][0]['flow_params'][0]}    cookie_slice
+        #@{cookieslicelist}=    Split String    ${cookieslice}    ,
+        ${foundcookieslices}=    Get Length    ${cookieslice}
+        Should Be Equal As Integers    ${foundcookieslices}    ${nbofcookieslice}
+        ${matchvid}=    Get From Dictionary    ${value['uni_config'][0]['flow_params'][0]['vlan_rule_params']}
+        ...    match_vid
+        Should Be Equal As Integers    ${matchvid}    ${reqmatchvid}
+        ${setvid}=    Get From Dictionary    ${value['uni_config'][0]['flow_params'][0]['vlan_rule_params']}
+        ...    set_vid
+        ${evalresult}=    Evaluate    2 <= ${setvid} <= 4095
+        Should Be True    ${evalresult}    msg=set_vid out of range (${setvid})!
+        Set To Dictionary    ${vlan_rules}    ${pononuuniid}    ${setvid}
+        ${oldsetvidvalid}     Set Variable If    ${prevvlanrules} is ${NONE}    False    True
+        ${prevsetvid}=    Set Variable If    ${oldsetvidvalid}    ${prevvlanrules['${pononuuniid}']}
+        Run Keyword If    ${oldsetvidvalid} and ${setvidequal}
+        ...               Should Be Equal As Integers    ${prevsetvid}    ${setvid}
+        ...    ELSE IF    ${oldsetvidvalid} and not ${setvidequal}
+        ...               Should Not Be Equal As Integers    ${prevsetvid}    ${setvid}
+    END
+    log Many   ${vlan_rules}
+    [Return]    ${vlan_rules}
+
+Get ONU Go Adapter ETCD Data
+    [Documentation]    This keyword delivers openonu-go-adapter Data stored in etcd
+    ${namespace}=    Set Variable    default
+    ${podname}=    Set Variable    etcd
+    ${commandget}    Catenate
+    ...    /bin/sh -c 'ETCDCTL_API=3 etcdctl get --prefix --prefix service/voltha/openonu'
+    ${result}=    Exec Pod    ${namespace}    ${podname}    ${commandget}
+    log    ${result}
+    [Return]    ${result}
+
+Prepare ONU Go Adapter ETCD Data For Json
+    [Documentation]    This keyword prepares openonu-go-adapter Data stored in etcd for converting
+    ...                to json
+    [Arguments]    ${etcddata}
+    #prepare result for json convert
+    ${prepresult}=    Replace String    ${etcddata}   \n    ,
+    ${prepresult}=    Strip String    ${prepresult}    mode=right    characters=,
+    ${prepresult}=    Set Variable    [${prepresult}]
+    log    ${prepresult}
+    [Return]    ${prepresult}
+
+Remove Lines Containing String
+    [Documentation]    This keyword deletes all lines from given string containing passed remove string
+    [Arguments]    ${string}    ${toremove}    ${appendtoremoveline}
+    ${lines}=    Get Lines Containing String    ${string}    ${toremove}
+    ${length}=    Get Line Count    ${lines}
+    ${firstline}=    Set Variable    False
+    FOR    ${INDEX}    IN RANGE    0    ${length}
+        ${String2remove}    Get Line    ${lines}    ${INDEX}
+        ${String2remove}    Set Variable    ${String2remove}${appendtoremoveline}
+        ${string}=    Remove String    ${string}    ${String2remove}
+    END
+    log    ${string}
+    [Return]    ${string}
+
+Read Pon Onu Uni String
+    [Documentation]    This keyword builds a thre digit string using Pon, Onu and Uni value of given tp-path taken
+    ...                taken from etcd data of onu go adapter
+    [Arguments]    ${tp_path}
+    #@{tppathlist}=    Split String    ${tp_path}    /
+    #${length}=    Get Length    ${tppathlist}
+    #FOR    ${I}    IN RANGE    0    ${length}
+    #    ${value}=    Get From List    ${tppathlist}    ${I}
+    #END
+    ${tppathlines}=   Replace String    ${tp_path}    /    \n
+    ${pon}=    Get Value Of Tp Path Element    ${tppathlines}    pon
+    ${onu}=    Get Value Of Tp Path Element    ${tppathlines}    onu
+    ${uni}=    Get Value Of Tp Path Element    ${tppathlines}    uni
+    ${valuesid}=    Set Variable   ${pon}/${onu}/${uni}
+    log    ${valuesid}
+    [Return]    ${valuesid}
+
+Get Value Of Tp Path Element
+    [Documentation]    This keyword delivers numeric value of given tp path element
+    [Arguments]    ${tp_path_lines}    ${element}
+    ${value}=    Get Lines Containing String    ${tp_path_lines}    ${element}-\{
+    ${value}=    Remove String    ${value}    ${element}-\{
+    ${value}=    Remove String    ${value}    \}
+    log    ${value}
+    [Return]    ${value}
+
+
 Map State
     [Documentation]    This keyword converts the passed numeric value or name of a onu state to its state values.
     [Arguments]    ${state}