[SDFAB-988] QER rate limiting tests

Change-Id: I4c542a5c9a122c0595b36e5e96d2b093682cfc7c
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/Policing.params b/TestON/tests/USECASE/SegmentRouting/Policing/Policing.params
new file mode 100644
index 0000000..532e13e
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/Policing.params
@@ -0,0 +1,186 @@
+<PARAMS>
+    <testcases>1,2</testcases>
+
+    <GRAPH>
+        <nodeCluster>pairedleaves</nodeCluster>
+        <builds>20</builds>
+        <jobName>QOS</jobName>
+        <branch>master</branch>
+    </GRAPH>
+
+    <persistent_setup>True</persistent_setup>
+
+    <kubernetes>
+        <appName>onos-classic</appName>
+        <namespace>tost</namespace>
+    </kubernetes>
+    <use_stern>True</use_stern>
+
+    <UP4>
+        <s1u_address>10.32.11.126</s1u_address>
+        <slice_id>1</slice_id>
+        <enodebs>
+            <enodeb_1>
+                <host>TRexClient</host>
+                <enb_address>10.32.11.124</enb_address>
+                <interface>pairbond</interface> <!-- useless for this test, we use TRex to generate traffic -->
+                <ues>ue1,ue2</ues>
+            </enodeb_1>
+        </enodebs>
+        <ues>
+            <ue1>
+                <ue_address>10.240.0.1</ue_address>
+                <teid>100</teid>
+                <up_id>10</up_id>
+                <down_id>20</down_id>
+                <tc>3</tc>
+                <five_g>False</five_g>
+                <max_bps>200000000</max_bps>
+            </ue1>
+            <ue2>
+                <ue_address>10.240.0.2</ue_address>
+                <teid>200</teid>
+                <up_id>30</up_id>
+                <down_id>40</down_id>
+                <tc>3</tc>
+                <five_g>False</five_g>
+                <max_bps>200000000</max_bps>
+            </ue2>
+        </ues>
+        <app_filters>
+            <allowPort>
+                <app_id>1</app_id>
+                <ip_prefix>10.32.11.125/32</ip_prefix>
+                <ip_proto>17</ip_proto>
+                <port_range>80..80</port_range>
+                <priority>20</priority>
+                <action>allow</action>
+                <max_bps>100000000</max_bps>
+            </allowPort>
+            <allowAll>
+                <app_id>0</app_id>
+                <action>allow</action>
+            </allowAll>
+        </app_filters>
+    </UP4>
+
+    <TREX>
+        <port_stats>0,2,3</port_stats> <!-- TRex port 0 = PDN, TRex port 2 = eNodeB, TRex port 3 = second eNB -->
+        <flows>
+            <UE1>
+                <name>UE1: Conformant Session</name>
+                <l1_bps>200000000</l1_bps>
+                <trex_port>2</trex_port>
+                <packet>
+                    <pktlen>1400</pktlen>
+                    <ip_src>10.240.0.1</ip_src>
+                    <ip_dst>10.32.11.125</ip_dst>
+                    <udp_dport>81</udp_dport>
+                    <eth_src>40:A6:B7:22:AB:20</eth_src>
+                    <eth_dst>00:00:0A:4C:1C:46</eth_dst>
+                    <gtp_teid>100</gtp_teid>
+                    <s1u_addr>10.32.11.126</s1u_addr>
+                    <enb_addr>10.32.11.124</enb_addr>
+                </packet>
+                <latency_stats>true</latency_stats>
+                <flow_id>20</flow_id>
+                <expected_max_dropped>0</expected_max_dropped>
+                <expected_rx_bps>200000000</expected_rx_bps>
+            </UE1>
+            <UE2>
+                <name>UE2: Non-Conformant Session</name>
+                <l1_bps>300000000</l1_bps>
+                <trex_port>2</trex_port>
+                <packet>
+                    <pktlen>1400</pktlen>
+                    <ip_src>10.240.0.2</ip_src>
+                    <ip_dst>10.32.11.125</ip_dst>
+                    <udp_dport>81</udp_dport>
+                    <eth_src>40:A6:B7:22:AB:20</eth_src>
+                    <eth_dst>00:00:0A:4C:1C:46</eth_dst>
+                    <gtp_teid>200</gtp_teid>
+                    <s1u_addr>10.32.11.126</s1u_addr>
+                    <enb_addr>10.32.11.124</enb_addr>
+                </packet>
+                <latency_stats>true</latency_stats>
+                <flow_id>21</flow_id>
+                <expected_rx_bps>200000000</expected_rx_bps>
+            </UE2>
+            <APP1>
+                <name>APP1: Conformant Application</name>
+                <l1_bps>100000000</l1_bps>
+                <trex_port>2</trex_port>
+                <packet>
+                    <pktlen>1400</pktlen>
+                    <ip_src>10.240.0.1</ip_src>
+                    <ip_dst>10.32.11.125</ip_dst>
+                    <udp_dport>80</udp_dport>
+                    <eth_src>40:A6:B7:22:AB:20</eth_src>
+                    <eth_dst>00:00:0A:4C:1C:46</eth_dst>
+                    <gtp_teid>100</gtp_teid>
+                    <s1u_addr>10.32.11.126</s1u_addr>
+                    <enb_addr>10.32.11.124</enb_addr>
+                </packet>
+                <latency_stats>true</latency_stats>
+                <flow_id>30</flow_id>
+                <expected_max_dropped>0</expected_max_dropped>
+                <expected_rx_bps>100000000</expected_rx_bps>
+            </APP1>
+            <APP2>
+                <name>APP2: Non-Conformant Application</name>
+                <l1_bps>200000000</l1_bps>
+                <trex_port>2</trex_port>
+                <packet>
+                    <pktlen>1400</pktlen>
+                    <ip_src>10.240.0.2</ip_src>
+                    <ip_dst>10.32.11.125</ip_dst>
+                    <udp_dport>80</udp_dport>
+                    <eth_src>40:A6:B7:22:AB:20</eth_src>
+                    <eth_dst>00:00:0A:4C:1C:46</eth_dst>
+                    <gtp_teid>200</gtp_teid>
+                    <s1u_addr>10.32.11.126</s1u_addr>
+                    <enb_addr>10.32.11.124</enb_addr>
+                </packet>
+                <latency_stats>true</latency_stats>
+                <flow_id>31</flow_id>
+                <expected_rx_bps>100000000</expected_rx_bps>
+            </APP2>
+        </flows>
+    </TREX>
+
+    <TOPO>
+        <switchNum>2</switchNum>
+        <linkNum>2</linkNum>
+    </TOPO>
+
+    <ONOS_Logging>
+        <org.onosproject.segmentrouting>DEBUG</org.onosproject.segmentrouting>
+        <org.omecproject.up4>TRACE</org.omecproject.up4>
+    </ONOS_Logging>
+    <ONOS_Logging_Reset>
+        <org.onosproject.segmentrouting>DEBUG</org.onosproject.segmentrouting>
+        <org.omecproject.up4>INFO</org.omecproject.up4>
+    </ONOS_Logging_Reset>
+
+    <ENV>
+        <cellName>productionCell</cellName>
+        <cellApps>drivers,fpm,lldpprovider,hostprovider,netcfghostprovider,drivers.bmv2,org.opencord.fabric-tofino,pipelines.fabric,org.stratumproject.fabric-tna,drivers.barefoot,segmentrouting,up4</cellApps>
+    </ENV>
+
+    <DEPENDENCY>
+        <useCommonConf>False</useCommonConf>
+        <useCommonTopo>True</useCommonTopo>
+        <useBmv2>True</useBmv2>
+        <bmv2SwitchType>stratum</bmv2SwitchType>
+        <switchPrefix></switchPrefix>
+        <stratumRoot>~/stratum</stratumRoot>
+        <topology>trellis_fabric.py</topology>
+        <lib></lib>
+    </DEPENDENCY>
+
+    <SCALE>
+        <size>3</size>
+        <max>3</max>
+    </SCALE>
+
+</PARAMS>
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/Policing.py b/TestON/tests/USECASE/SegmentRouting/Policing/Policing.py
new file mode 100644
index 0000000..730d20e
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/Policing.py
@@ -0,0 +1,34 @@
+class Policing:
+
+    def __init__(self):
+        self.default = ''
+
+    def CASE1(self, main):
+        main.case("Session level QER")
+        try:
+            from tests.USECASE.SegmentRouting.Policing.dependencies.PolicingTest import \
+                PolicingTest
+        except ImportError as e:
+            main.log.error("Import not found. Exiting the test")
+            main.log.error(e)
+            main.cleanAndExit()
+        test = PolicingTest()
+        test.runTest(
+            main,
+            test_idx=1
+        )
+
+    def CASE2(self, main):
+        main.case("Application level QER")
+        try:
+            from tests.USECASE.SegmentRouting.Policing.dependencies.PolicingTest import \
+                PolicingTest
+        except ImportError as e:
+            main.log.error("Import not found. Exiting the test")
+            main.log.error(e)
+            main.cleanAndExit()
+        test = PolicingTest()
+        test.runTest(
+            main,
+            test_idx=2
+        )
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/Policing.topo b/TestON/tests/USECASE/SegmentRouting/Policing/Policing.topo
new file mode 100644
index 0000000..7aee367
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/Policing.topo
@@ -0,0 +1,56 @@
+<TOPOLOGY>
+    <COMPONENT>
+        <ONOScell>
+            <host>localhost</host>  # ONOS "bench" machine
+            <user>jenkins</user>
+            <password></password>
+            <type>OnosClusterDriver</type>
+            <connect_order>50</connect_order>
+            <jump_host></jump_host>
+            <home>~/onos</home>   # defines where onos home is on the build machine. Defaults to "~/onos/" if empty.
+            <COMPONENTS>
+                <kubeConfig>~/.kube/dev-pairedleaves-tucson</kubeConfig>  # If set, will attempt to use this file for setting up port-forwarding
+                <useDocker>True</useDocker>  # Whether to use docker for ONOS nodes
+                <docker_prompt>\$</docker_prompt>
+                <cluster_name></cluster_name>  # Used as a prefix for cluster components. Defaults to 'ONOS'
+                <diff_clihost>True</diff_clihost> # if it has different host other than localhost for CLI. True or empty. OC# will be used if True.
+                <karaf_username>karaf</karaf_username>
+                <karaf_password>karaf</karaf_password>
+                <web_user>karaf</web_user>
+                <web_pass>karaf</web_pass>
+                <karafPrompt_username>karaf</karafPrompt_username>
+                <rest_port></rest_port>
+                <prompt></prompt>  # TODO: we technically need a few of these, one per component
+                <onos_home>~/onos/</onos_home>  # defines where onos home is on the target cell machine. Defaults to entry in "home" if empty.
+                <nodes> 3 </nodes>  # number of nodes in the cluster
+                <up4_port>51001</up4_port> # Port where the UP4 P4Runtime server is listening
+            </COMPONENTS>
+        </ONOScell>
+
+        <!-- No need for any HostDriver components, traffic is being generated by TRex-->
+        <TRexClient>
+            <host>localhost</host>
+            <type>TrexClientDriver</type>
+            <connect_order>5</connect_order>
+            <COMPONENTS>
+                <trex_address>10.76.28.72</trex_address> <!-- Compute2 -->
+                <trex_config>trex_config.yaml</trex_config> <!-- relative path starting from ./dependencies-->
+                <force_restart>True</force_restart>
+                <software_mode>True</software_mode>
+                <trex_library_python_path>/home/jenkins/trex_python</trex_library_python_path>
+            </COMPONENTS>
+        </TRexClient>
+
+    <!--  This component is not needed, but required to use the Testcaselib  -->
+        <NetworkBench>
+            <host>10.76.28.66</host>
+            <user>jenkins</user>
+            <password></password>
+            <type>NetworkDriver</type>
+            <connect_order>1</connect_order>
+            <COMPONENTS>
+            </COMPONENTS>
+        </NetworkBench>
+
+    </COMPONENT>
+</TOPOLOGY>
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/__init__.py b/TestON/tests/USECASE/SegmentRouting/Policing/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/__init__.py
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/PolicingTest.py b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/PolicingTest.py
new file mode 100644
index 0000000..0ef3465
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/PolicingTest.py
@@ -0,0 +1,59 @@
+from tests.USECASE.SegmentRouting.dependencies.Testcaselib import \
+    Testcaselib as run
+from tests.USECASE.SegmentRouting.dependencies.trex import Trex
+from tests.USECASE.SegmentRouting.dependencies.up4 import UP4
+import json
+
+
+class PolicingTest:
+
+    def runTest(self, main, test_idx):
+        run.initTest(main)
+        main.log.info(main.Cluster.numCtrls)
+        main.Cluster.setRunningNode(3)
+        run.installOnos(main, skipPackage=True, cliSleep=5)
+
+        main.step("Start P4rt client and setup TRex")
+        # Use the first available ONOS instance CLI
+        onos_cli = main.Cluster.active(0).CLI
+        up4 = UP4()
+        trex = Trex()
+        # Get the P4RT client connected to UP4 in the first available ONOS instance
+        up4.setup(main.Cluster.active(0).p4rtUp4, no_host=True)
+        trex.setup(main.TRexClient)
+
+        try:
+            main.step("Program UPF entities via UP4")
+            up4.attachUes()
+            up4.verifyUp4Flow(onos_cli)
+
+            # Load traffic config for the current test case
+            main.step("Load test JSON config")
+            cfgFile = main.configPath + "/tests/" + "CASE_%d.json" % test_idx
+            with open(cfgFile) as cfg:
+                testCfg = json.load(cfg)
+
+            for flow in testCfg["flows"]:
+                trex.createFlow(flow)
+            results = trex.sendAndReceiveTraffic(testCfg["duration"])
+
+            main.step("Log port and flow stats")
+            trex.logPortStats()
+            for flow in testCfg["flows"]:
+                trex.logFlowStats(flow)
+
+            for flow in testCfg["flows"]:
+                if trex.isFlowStats(flow):
+                    main.step("{}: Assert RX Packets".format(flow))
+                    trex.assertRxPackets(flow)
+                    # Assert received traffic is similar to expected for that flow
+                    trex.assertRxRate(flow, results["duration"][-1])
+        finally:
+            main.step("Remove UPF entities via UP4")
+            up4.detachUes()
+            up4.verifyNoUesFlow(onos_cli)
+
+            main.step("Teardown")
+            trex.teardown()
+            up4.teardown()
+            run.cleanup(main)
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/__init__.py b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/__init__.py
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/tests/CASE_1.json b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/tests/CASE_1.json
new file mode 100644
index 0000000..c1bf2f9
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/tests/CASE_1.json
@@ -0,0 +1,4 @@
+{
+  "flows": ["UE1", "UE2"],
+  "duration": 10
+}
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/tests/CASE_2.json b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/tests/CASE_2.json
new file mode 100644
index 0000000..483a7a5
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/tests/CASE_2.json
@@ -0,0 +1,4 @@
+{
+  "flows": ["APP1", "APP2"],
+  "duration": 10
+}
diff --git a/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/trex_config.yaml b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/trex_config.yaml
new file mode 100644
index 0000000..5309605
--- /dev/null
+++ b/TestON/tests/USECASE/SegmentRouting/Policing/dependencies/trex_config.yaml
@@ -0,0 +1,19 @@
+# TRex Port ID=0 --> PCI BUS: d8:00.0, NUMA: 1 (CPU2), Linux Intf: enp216s0f0 connected to leaf1/0 (PDN)
+# TRex Port ID=1 --> PCI BUS: d8:00.1, NUMA: 1 (CPU2), Linux Intf: enp216s0f1 not connected, but required by TRex to have an even number of interfaces
+# TRex Port ID=2 --> PCI BUS: 5e:00.0, NUMA: 0 (CPU1), Linux Intf: enp94s0f0 connected to leaf2/0 (eNodeB)
+# TRex Port ID=3 --> PCI BUS: 5e:00.1, NUMA: 0 (CPU1), Linux Intf: enp94s0f1 connected to leaf2/4
+
+- version: 2
+  port_limit: 4
+  interfaces: [ 'd8:00.0', 'd8:00.1', '5e:00.0', '5e:00.1']
+  port_bandwidth_gb: 40
+  c: 20
+  port_info:
+    - src_mac: 40:A6:B7:22:AB:40
+      dest_mac: 00:00:0A:4C:1C:46
+    - src_mac: 40:A6:B7:22:AB:41
+      dest_mac: 00:00:0A:4C:1C:46
+    - src_mac: 40:A6:B7:22:AB:20
+      dest_mac: 00:00:0A:4C:1C:46
+    - src_mac: 40:A6:B7:22:AB:21
+      dest_mac: 00:00:0A:4C:1C:46