[VOL-2824] DT Workflow: Verify Subscriber Access Flows Added For ONUs

Change-Id: Id70a9bb8769344f80da6007417f331d0eda4dba6
diff --git a/libraries/onos.robot b/libraries/onos.robot
index 921ca50..dc30c9a 100644
--- a/libraries/onos.robot
+++ b/libraries/onos.robot
@@ -65,6 +65,27 @@
     Should Be True    ${matched}    No match for ${onu_serial_number} found
     [Return]    ${onu_port}
 
+Get NNI Port in ONOS
+    [Arguments]    ${olt_of_id}
+    [Documentation]    Retrieves NNI port for the OLT in ONOS
+    ${resp}=    Get Request    ONOS    onos/v1/devices/${olt_of_id}/ports
+    ${jsondata}=    To Json    ${resp.content}
+    Should Not Be Empty    ${jsondata['ports']}
+    ${length}=    Get Length    ${jsondata['ports']}
+    @{ports}=    Create List
+    ${matched}=    Set Variable    False
+    FOR    ${INDEX}    IN RANGE    0    ${length}
+        ${value}=    Get From List    ${jsondata['ports']}    ${INDEX}
+        ${annotations}=    Get From Dictionary    ${value}    annotations
+        ${nni_port}=    Get From Dictionary    ${value}    port
+        ${nniPortName}=    Catenate    SEPARATOR=    nni-    ${nni_port}
+        ${portName}=    Get From Dictionary    ${annotations}    portName
+        ${matched}=    Set Variable If    '${portName}' == '${nniPortName}'    True    False
+        Exit For Loop If    ${matched}
+    END
+    Should Be True    ${matched}    No match for NNI found for ${olt_of_id}
+    [Return]    ${nni_port}
+
 Get FabricSwitch in ONOS
     [Documentation]    Returns of_id of the Fabric Switch in ONOS
     ${resp}=    Get Request    ONOS    onos/v1/devices
@@ -82,6 +103,39 @@
     Should Be True    ${matched}    No fabric switch found
     [Return]    ${of_id}
 
+Verify Subscriber Access Flows Added for ONU DT
+    [Arguments]    ${ip}    ${port}    ${onu_port}    ${nni_port}    ${s_tag}
+    [Documentation]    Verifies if the Subscriber Access Flows are added in ONOS for the ONU
+    # Verify upstream table=0 flow
+    ${upstream_flow_0_added}=    Execute ONOS CLI Command    ${ip}    ${port}
+    ...    flows -s -f ADDED | grep IN_PORT:${onu_port} | grep VLAN_VID:Any | grep transition=TABLE:1
+    Should Not Be Empty    ${upstream_flow_0_added}
+    # Verify upstream table=1 flow
+    ${flow_vlan_push_cmd}=    Catenate    SEPARATOR=
+    ...    flows -s -f ADDED | grep IN_PORT:${onu_port} | grep VLAN_VID:Any |
+    ...     grep VLAN_PUSH | grep VLAN_ID:${s_tag} | grep OUTPUT:${nni_port}
+    ${upstream_flow_1_added}=    Execute ONOS CLI Command    ${ip}    ${port}
+    ...    ${flow_vlan_push_cmd}
+    Should Not Be Empty    ${upstream_flow_1_added}
+    # Verify downstream table=0 flow
+    ${flow_vlan_pop_cmd}=    Catenate    SEPARATOR=
+    ...    flows -s -f ADDED | grep IN_PORT:${nni_port} | grep VLAN_VID:${s_tag} |
+    ...     grep VLAN_POP | grep transition=TABLE:1
+    ${downstream_flow_0_added}=    Execute ONOS CLI Command    ${ip}    ${port}
+    ...    ${flow_vlan_pop_cmd}
+    Should Not Be Empty    ${downstream_flow_0_added}
+    # Verify downstream table=1 flow
+    ${downstream_flow_1_added}=    Execute ONOS CLI Command    ${ip}    ${port}
+    ...    flows -s -f ADDED | grep IN_PORT:${nni_port} | grep VLAN_VID:Any | grep OUTPUT:${onu_port}
+    Should Not Be Empty    ${downstream_flow_1_added}
+
+Verify Subscriber Access Flows Added Count DT
+    [Arguments]    ${ip}    ${port}    ${expected_flows}
+    [Documentation]    Matches for total number of subscriber access flows added for all onus
+    ${access_flows_added}=    Execute ONOS CLI Command    ${ip}    ${port}
+    ...    flows -s -f ADDED | grep -v deviceId | grep -v ETH_TYPE:lldp | wc -l
+    Should Be Equal As Integers    ${access_flows_added}    ${expected_flows}
+
 Verify Eapol Flows Added
     [Arguments]    ${ip}    ${port}    ${expected_flows}
     [Documentation]    Matches for number of eapol flows based on number of onus
diff --git a/libraries/utils.robot b/libraries/utils.robot
index 2646788..4064690 100644
--- a/libraries/utils.robot
+++ b/libraries/utils.robot
@@ -200,6 +200,9 @@
     ...    and avoids duplication of code.
     ${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}
     FOR    ${I}    IN RANGE    0    ${num_onus}
         ${src}=    Set Variable    ${hosts.src[${I}]}
         ${dst}=    Set Variable    ${hosts.dst[${I}]}
@@ -212,11 +215,15 @@
         Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    2
         ...    Execute ONOS CLI Command    ${ONOS_SSH_IP}    ${ONOS_SSH_PORT}
         ...    volt-add-subscriber-access ${of_id} ${onu_port}
+        # Verify subscriber access flows are added for the ONU port
+        Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    5s
+        ...    Verify Subscriber Access Flows Added For ONU DT    ${ONOS_SSH_IP}    ${ONOS_SSH_PORT}    ${onu_port}
+        ...    ${nni_port}    ${src['s_tag']}
         # Verify ONU state in voltha
         Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    5s    Validate Device
         ...    ENABLED    ACTIVE    REACHABLE
         ...    ${src['onu']}    onu=True    onu_reason=omci-flows-pushed
-        # TODO: Yet to Verify on the Physical POD
+        # TODO: Yet to Verify on the GPON based Physical POD (VOL-2652)
         Run Keyword If    ${has_dataplane}    Run Keyword And Continue On Failure    Validate DHCP and Ping    True
         ...    True    ${src['dp_iface_name']}    ${src['s_tag']}    ${src['c_tag']}    ${dst['dp_iface_ip_qinq']}
         ...    ${src['ip']}    ${src['user']}    ${src['pass']}    ${src['container_type']}    ${src['container_name']}
@@ -225,7 +232,12 @@
         Run Keyword and Ignore Error    Get Device Output from Voltha    ${onu_device_id}
         Run Keyword and Ignore Error    Collect Logs
     END
-    # Verify VOLTHA Flows (TODO: Add verification for ONOS Flows)
+    # Verify ONOS Flows
+    # Number of Access Flows on ONOS equals 4 * the Number of Active ONUs (2 for each downstream and upstream)
+    ${onos_flows_count}=    Evaluate    4 * ${num_onus}
+    Run Keyword And Continue On Failure    Wait Until Keyword Succeeds    ${timeout}    5s
+    ...    Verify Subscriber Access Flows Added Count DT    ${ONOS_SSH_IP}    ${ONOS_SSH_PORT}    ${onos_flows_count}
+    # Verify VOLTHA Flows
     # Number of per OLT Flows equals Twice the Number of Active ONUs (each for downstream and upstream) + 1 for LLDP
     ${olt_flows}=    Evaluate    2 * ${num_onus} + 1
     # Number of per ONU Flows equals 2 (one each for downstream and upstream)