[VOL-3007] Moving the scale pipeline in robot

Change-Id: I729685325f6b05e9f9f17f14e96d597c885ebe68
diff --git a/libraries/k8s.robot b/libraries/k8s.robot
index d1ef61c..6dbf6b1 100644
--- a/libraries/k8s.robot
+++ b/libraries/k8s.robot
@@ -138,7 +138,7 @@
     [Arguments]    ${datetime}
     [Documentation]    This keyword checks for the error occurence in the voltha pods
     &{errorPodDict}    Create Dictionary
-    &{containerDict}    Get Container Dictionary
+    &{containerDict}    Get Container Dictionary    voltha
     FOR    ${podName}    IN    @{PODLIST1}
         ${containerName}    Get From Dictionary    ${containerDict}    ${podName}
         ${rc}    ${logOutput}    Run And Return Rc And Output
@@ -218,16 +218,17 @@
     [Return]    ${errorDict}
 
 Get Container Dictionary
+    [Arguments]    ${namespace}
     [Documentation]    Creates a mapping for pod name and container name and returns the same
     &{containerDict}    Create Dictionary
     ${containerName}    Set Variable    ${EMPTY}
-    ${podName}    Run    kubectl get deployment -n voltha | awk 'NR>1 {print $1}'
+    ${podName}    Run    kubectl get deployment -n ${namespace} | awk 'NR>1 {print $1}'
     @{podNameList}=    Split To Lines    ${podName}
     Append To List    ${podNameList}    voltha-etcd-cluster    voltha-kafka    voltha-ro-core    voltha-zookeeper
     Log    ${podNameList}
     #Creatiing dictionary to correspond pod name and container name
     FOR    ${pod}    IN    @{podNameList}
-        ${containerName}    Run    kubectl get pod -n voltha | grep ${pod} | awk '{print $1}'
+        ${containerName}    Run    kubectl get pod -n ${namespace} | grep ${pod} | awk '{print $1}'
         &{containerDict}    Set To Dictionary    ${containerDict}    ${pod}    ${containerName}
     END
     Log    ${containerDict}
diff --git a/libraries/onos.robot b/libraries/onos.robot
index 3d61332..4d10af5 100644
--- a/libraries/onos.robot
+++ b/libraries/onos.robot
@@ -234,19 +234,17 @@
     ${aaa_users}=    Execute ONOS CLI Command    ${ip}    ${port}    aaa-users | grep AUTHORIZED | grep ${onu_port}
     Should Not Be Empty    ${aaa_users}    ONU port ${onu_port} not found in aaa-users
 
-Verify Number of AAA-Users
+Assert Number of AAA-Users
     [Arguments]    ${ip}    ${port}    ${expected_onus}
     [Documentation]    Matches for number of aaa-users authorized based on number of onus
-    ##TODO: filter by onu serial number instead of count
     ${aaa_users}=    Execute ONOS CLI Command    ${ip}    ${port}    aaa-users | grep AUTHORIZED | wc -l
-    Should Contain    ${aaa_users}    ${expected_onus}
+    Should Be Equal As Integers    ${aaa_users}    ${expected_onus}
 
 Validate DHCP Allocations
     [Arguments]    ${ip}    ${port}    ${expected_onus}
     [Documentation]    Matches for number of dhcpacks based on number of onus
-    ##TODO: filter by onu serial number instead of count
     ${allocations}=    Execute ONOS CLI Command    ${ip}    ${port}    dhcpl2relay-allocations | grep DHCPACK | wc -l
-    Should Contain    ${allocations}    ${expected_onus}
+    Should Be Equal As Integers    ${allocations}    ${expected_onus}
 
 Validate Subscriber DHCP Allocation
     [Arguments]    ${ip}    ${port}    ${onu_port}
@@ -278,3 +276,88 @@
         Run Keyword If    '${dpid}' != ''
         ...    Should Be Equal As Integers    ${rc}    0
     END
+
+Assert Ports in ONOS
+    [Arguments]    ${ip}    ${port}     ${count}     ${filter}
+    [Documentation]    Check that a certain number of ports are enabled in ONOS
+    ${ports}=    Execute ONOS CLI Command    ${ip}    ${port}
+        ...    ports -e | grep ${filter} | wc -l
+    Should Be Equal As Integers    ${ports}    ${count}
+
+Wait for Ports in ONOS
+    [Arguments]    ${ip}    ${port}     ${count}     ${filter}
+    [Documentation]    Waits untill a certain number of ports are enabled in ONOS
+    Wait Until Keyword Succeeds     10m     5s      Assert Ports in ONOS   ${ip}    ${port}     ${count}     ${filter}
+
+Wait for AAA Authentication
+    [Arguments]    ${ip}    ${port}     ${count}
+    [Documentation]    Waits untill a certain number of subscribers are authenticated in ONOS
+    Wait Until Keyword Succeeds     10m     5s      Assert Number of AAA-Users      ${ip}    ${port}     ${count}
+
+Wait for DHCP Ack
+    [Arguments]    ${ip}    ${port}     ${count}
+    [Documentation]    Waits untill a certain number of subscribers have received a DHCP_ACK
+    Wait Until Keyword Succeeds     10m     5s      Validate DHCP Allocations      ${ip}    ${port}     ${count}
+
+Provision subscriber
+    [Documentation]  Calls volt-add-subscriber-access in ONOS
+    [Arguments]    ${onos_ip}    ${onos_port}   ${of_id}    ${onu_port}
+    Execute ONOS CLI Command    ${ONOS_SSH_IP}    ${ONOS_SSH_PORT}
+            ...    volt-add-subscriber-access ${of_id} ${onu_port}
+
+List Enabled UNI Ports
+    [Documentation]  List all the UNI Ports, the only way we have is to filter out the one called NNI
+    ...     Creates a list of dictionaries
+    [Arguments]     ${onos_ip}    ${onos_port}   ${of_id}
+    [Return]  [{'port': '16', 'of_id': 'of:00000a0a0a0a0a00'}, {'port': '32', 'of_id': 'of:00000a0a0a0a0a00'}]
+    ${result}=      Create List
+    ${out}=    Execute ONOS CLI Command    ${onos_ip}    ${onos_port}
+    ...    ports -e ${of_id} | grep -v SWITCH | grep -v nni
+    @{unis}=	Split To Lines	${out}
+    FOR    ${uni}    IN    @{unis}
+        ${matches} =	Get Regexp Matches	${uni}  .*port=([0-9]+),.*  1
+        &{portDict}    Create Dictionary    of_id=${of_id}    port=${matches[0]}
+        Append To List  ${result}    ${portDict}
+    END
+    Log     ${result}
+    Return From Keyword     ${result}
+
+Provision all subscribers on device
+    [Documentation]  Calls volt-add-subscriber-access in ONOS for all the enabled UNI ports on a partivular device
+    [Arguments]     ${onos_ip}    ${onos_port}   ${of_id}
+    ${unis}=    List Enabled UNI Ports  ${onos_ip}  ${onos_port}   ${of_id}
+    FOR     ${uni}  IN      @{unis}
+        Provision subscriber   ${onos_ip}  ${onos_port}   ${uni['of_id']}   ${uni['port']}
+    END
+
+List OLTs
+    [Documentation]  Returns a list of OLTs known to ONOS
+    [Arguments]  ${onos_ip}    ${onos_port}
+    [Return]  ['of:00000a0a0a0a0a00', 'of:00000a0a0a0a0a01']
+    ${result}=      Create List
+    ${out}=    Execute ONOS CLI Command    ${onos_ip}    ${onos_port}
+    ...     volt-olts
+    @{olts}=    Split To Lines	${out}
+    FOR    ${olt}    IN    @{olts}
+        Log     ${olt}
+        ${matches} =	Get Regexp Matches	${olt}  ^OLT (.+)$  1
+        # there may be some logs mixed with the output so only append if we have a match
+        ${matches_length}=      Get Length  ${matches}
+        Run Keyword If  ${matches_length}==1
+        ...     Append To List  ${result}    ${matches[0]}
+    END
+    Return From Keyword     ${result}
+
+Count ADDED flows
+    [Documentation]  Count the flows in ADDED state in ONOS
+    [Arguments]  ${onos_ip}     ${onos_port}    ${targetFlows}
+    ${flows}=    Execute ONOS CLI Command    ${onos_ip}    ${onos_port}
+    ...     flows -s | grep ADDED | wc -l
+    Should Be Equal As Integers    ${targetFlows}    ${flows}
+
+Wait for all flows to in ADDED state
+    [Documentation]  Waits until the flows have been provisioned
+    [Arguments]  ${onos_ip}    ${onos_port}     ${workflow}    ${uni_count}    ${olt_count}    ${provisioned}
+    ${targetFlows}=     Calculate flows by workflow     ${workflow}    ${uni_count}    ${olt_count}     ${provisioned}
+    Wait Until Keyword Succeeds     10m     5s      Count ADDED flows
+    ...     ${onos_ip}    ${onos_port}  ${targetFlows}
diff --git a/libraries/utils.robot b/libraries/utils.robot
index b0e5548..258b465 100644
--- a/libraries/utils.robot
+++ b/libraries/utils.robot
@@ -483,3 +483,50 @@
     ${rc}    ${output}=    Run and Return Rc and Output     date --date="${dateStr}" "+%s"
     Should Be Equal As Numbers    ${rc}    0
     [return]    ${output}
+
+Calculate flows by workflow
+    [Documentation]  Calculate how many flows should be created based on the workflow, the number of UNIs
+    ...     and whether the subscribers have been provisioned
+    [Arguments]  ${workflow}    ${uni_count}    ${olt_count}    ${provisioned}
+    ${expectedFlows}=   Run Keyword If  $workflow=="att"  Calculate Att flows
+    ...     ${uni_count}    ${olt_count}    ${provisioned}
+    ...     ELSE IF     $workflow=="dt"     Calculate Dt Flows
+    ...     ${uni_count}    ${olt_count}    ${provisioned}
+    ...     ELSE IF     $workflow=="tt"     Calculate Tt Flows
+    ...     ${uni_count}    ${olt_count}    ${provisioned}
+    ...     ELSE    Fail    Workflow ${workflow} should be one of 'att', 'dt', 'tt'
+    Return From Keyword     ${expectedFlows}
+
+Calculate Att flows
+    [Documentation]  Calculate the flow for the ATT workflow
+    ...     NOTE we may need to add support for IGMP enabled/disabled
+    [Arguments]  ${uni_count}    ${olt_count}   ${provisioned}
+    # (1 EAPOL * ONUs) * (1 LLDP + 1 DHCP * OLTS) before provisioning
+    # (1 EAPOL, 1 DHCP, 1 IGMP, 4 DP * ONUs) * (1 LLDP + 1 DHCP * OLTS) before provisioning
+    ${flow_count}=  Run Keyword If  $provisioned=='false'
+    ...     Evaluate    (${uni_count} * 1) + (${olt_count} * 2)
+    ...     ELSE
+    ...     Evaluate    (${uni_count} * 7) + (${olt_count} * 2)
+    Return From Keyword     ${flow_count}
+
+Calculate Dt flows
+    [Documentation]  Calculate the flow for the DT workflow
+    [Arguments]  ${uni_count}    ${olt_count}   ${provisioned}
+    # (1 LLDP * OLTS) before provisioning
+    # (4 DP * ONUs) * (1 LLDP * OLTS) before provisioning
+    ${flow_count}=  Run Keyword If  $provisioned=='false'
+        ...     Evaluate    (${olt_count} * 1)
+        ...     ELSE
+        ...     Evaluate    (${uni_count} * 4) + (${olt_count} * 1)
+    Return From Keyword     ${flow_count}
+
+Calculate Tt flows
+    [Documentation]  Calculate the flow for the TT workflow
+    [Arguments]  ${uni_count}    ${olt_count}   ${provisioned}
+    # (1 LLDP + 1 DHCP * OLTS) before provisioning
+    # (1 DHCP, 1 IGMP, 4 DP * ONUs) * (1 LLDP + 1 DHCP * OLTS) before provisioning
+    ${flow_count}=  Run Keyword If  $provisioned=='false'
+        ...     Evaluate    (${olt_count} * 2)
+        ...     ELSE
+        ...     Evaluate    (${uni_count} * 6) + (${olt_count} * 1)
+    Return From Keyword     ${flow_count}
\ No newline at end of file
diff --git a/libraries/voltctl.robot b/libraries/voltctl.robot
index 0051e28..d315ff6 100644
--- a/libraries/voltctl.robot
+++ b/libraries/voltctl.robot
@@ -22,6 +22,7 @@
 Library           Collections
 Library           RequestsLibrary
 Library           OperatingSystem
+Resource          ./utils.robot
 
 *** Keywords ***
 Test Empty Device List
@@ -34,11 +35,11 @@
     Should Be Equal As Integers    ${length}    0
 
 Create Device
-    [Arguments]    ${ip}    ${port}
+    [Arguments]    ${ip}    ${port}     ${type}=openolt
     [Documentation]    Creates a device in VOLTHA
     #create/preprovision device
     ${rc}    ${device_id}=    Run and Return Rc and Output
-    ...    ${VOLTCTL_CONFIG}; voltctl device create -t openolt -H ${ip}:${port}
+    ...    ${VOLTCTL_CONFIG}; voltctl device create -t ${type} -H ${ip}:${port}
     Should Be Equal As Integers    ${rc}    0
     [Return]    ${device_id}
 
@@ -140,6 +141,13 @@
     Log    ${devices}
     Should Be Equal As Integers    ${rc1}    0
 
+Get Logical Device List from Voltha
+    [Documentation]    Gets Logical Device List Output from Voltha (in json format)
+    ${rc1}    ${devices}=    Run and Return Rc and Output    ${VOLTCTL_CONFIG}; voltctl logicaldevice list -o json
+    Log    ${devices}
+    Should Be Equal As Integers    ${rc1}    0
+    Return From Keyword     ${devices}
+
 Validate Device
     [Documentation]
     ...    Parses the output of "voltctl device list" and inspects a device ${id}, specified as either
@@ -447,3 +455,39 @@
     Should Be Equal As Integers    ${rc}    0
     Run Keyword and Ignore Error    Wait Until Keyword Succeeds    60s   1s    Validate Device
     ...    ENABLED    DISCOVERED    UNREACHABLE   ${onu_id}    onu=True
+
+Assert ONUs in Voltha
+    [Arguments]    ${count}
+    [Documentation]    Check that a certain number of devices reached the ACTIVE/ENABLE state
+    ${rc1}    ${devices}=    Run and Return Rc and Output
+    ...     ${VOLTCTL_CONFIG}; voltctl device list | grep -v OLT | grep ACTIVE | wc -l
+    Should Be Equal As Integers    ${rc1}    0
+    Should Be Equal As Integers    ${devices}    ${count}
+
+Wait for ONUs in VOLTHA
+    [Arguments]    ${count}
+    [Documentation]    Waits until a certain number of devices reached the ACTIVE/ENABLE state
+    Wait Until Keyword Succeeds     10m     5s      Assert ONUs In Voltha   ${count}
+
+Count Logical Devices flows
+    [Documentation]  Count the flows across logical devices in VOLTHA
+    [Arguments]  ${targetFlows}
+    ${output}=     Get Logical Device List From Voltha
+    ${logical_devices}=    To Json    ${output}
+    ${total_flows}=     Set Variable    0
+    FOR     ${device}   IN  @{logical_devices}
+        ${rc}    ${flows}=    Run and Return Rc and Output
+        ...    ${VOLTCTL_CONFIG}; voltctl logicaldevice flows ${device['id']} | grep -v ID | wc -l
+        Should Be Equal As Integers    ${rc}    0
+        ${total_flows}=     Evaluate    ${total_flows} + ${flows}
+    END
+    Should Be Equal As Integers    ${targetFlows}    ${total_flows}
+
+Wait for Logical Devices flows
+    [Documentation]  Waits until the flows have been provisioned in the logical device
+    [Arguments]  ${workflow}    ${uni_count}    ${olt_count}    ${provisioned}
+    ${targetFlows}=     Calculate flows by workflow     ${workflow}    ${uni_count}    ${olt_count}     ${provisioned}
+    Log     ${targetFlows}
+    # TODO extend Validate Logical Device Flows to check the correct number of flows
+    Wait Until Keyword Succeeds     10m     5s  Count Logical Devices flows     ${targetFlows}
+