From f054c181810e84d8cbe32561c193af11ca6e6b29 Mon Sep 17 00:00:00 2001
From: Deepak S <deepak.s@linux.intel.com>
Date: Thu, 31 Aug 2017 20:35:32 -0700
Subject: Adding ixia latency support for dynamic cgnapt

Change-Id: I346f6064c39cb5662c2b17ca0f520addbe5eae4c
Signed-off-by: Deepak S <deepak.s@linux.intel.com>
---
 ...ith_latency_ipv4_1flow_dynamic_cgnapt_ixia.yaml |  52 ++++++++++++++
 .../cgnat_ipv4_profile_1flows_3node_latency.ixncfg | Bin 0 -> 70874 bytes
 .../traffic_profile/test_ixia_rfc2544.py           |   1 +
 .../vnf_generic/vnf/test_tg_rfc2544_ixia.py        |   4 +-
 .../traffic_profile/ixia_rfc2544.py                |  20 ++++--
 .../vnf_generic/vnf/tg_rfc2544_ixia.py             |  78 ++++++++++++++++-----
 6 files changed, 129 insertions(+), 26 deletions(-)
 create mode 100644 samples/vnf_samples/nsut/cgnapt/tc_baremetal_rfc2544_with_latency_ipv4_1flow_dynamic_cgnapt_ixia.yaml
 create mode 100644 samples/vnf_samples/traffic_profiles/cgnapt/cgnat_ipv4_profile_1flows_3node_latency.ixncfg

diff --git a/samples/vnf_samples/nsut/cgnapt/tc_baremetal_rfc2544_with_latency_ipv4_1flow_dynamic_cgnapt_ixia.yaml b/samples/vnf_samples/nsut/cgnapt/tc_baremetal_rfc2544_with_latency_ipv4_1flow_dynamic_cgnapt_ixia.yaml
new file mode 100644
index 000000000..55292a371
--- /dev/null
+++ b/samples/vnf_samples/nsut/cgnapt/tc_baremetal_rfc2544_with_latency_ipv4_1flow_dynamic_cgnapt_ixia.yaml
@@ -0,0 +1,52 @@
+# Copyright (c) 2016 Intel Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the License);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an AS IS BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+---
+schema: yardstick:task:0.1
+scenarios:
+- type: NSPerf
+  traffic_profile: "../../traffic_profiles/ixia_ipv4_latency.yaml"
+  topology: cgnapt-vnf-topology-ixia-3node.yaml
+  nodes:
+    tg__1: trafficgen_1.yardstick
+    vnf__1: vnf.yardstick
+    tg__2: trafficgen_2.yardstick
+  options:
+    framesize:
+      private: {64B: 100}
+      public: {64B: 100}
+    flow:
+      src_ip: [{'tg__1': 'xe0'}]
+      dst_ip: [{'tg__2': 'xe0'}]
+      count: 1
+    traffic_type: 4
+    rfc2544:
+      allowed_drop_rate: 0.0001 - 0.0001
+      correlated_traffic: true
+      latency: true
+      fec_port_mode: '10G'
+    vnf__1:
+      vnf_config: {lb_config: 'SW', lb_count: 1, worker_config: '1C/1T', worker_threads: 1}
+      napt: 'dynamic'
+      nfvi_enable: True
+  runner:
+    type: Iteration
+    iterations: 28
+    interval: 35
+  ixia_profile: ../../traffic_profiles/cgnapt/cgnat_ipv4_profile_1flows_3node_latency.ixncfg
+context:
+  type: Node
+  name: yardstick
+  nfvi_type: baremetal
+  file: /etc/yardstick/nodes/pod_ixia_3node.yaml
diff --git a/samples/vnf_samples/traffic_profiles/cgnapt/cgnat_ipv4_profile_1flows_3node_latency.ixncfg b/samples/vnf_samples/traffic_profiles/cgnapt/cgnat_ipv4_profile_1flows_3node_latency.ixncfg
new file mode 100644
index 000000000..fdf47dad8
Binary files /dev/null and b/samples/vnf_samples/traffic_profiles/cgnapt/cgnat_ipv4_profile_1flows_3node_latency.ixncfg differ
diff --git a/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py b/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py
index b2cb9dfea..a2a940931 100644
--- a/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py
+++ b/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py
@@ -435,6 +435,7 @@ class TestIXIARFC2544Profile(unittest.TestCase):
                                                           traffic, ixia_obj)
         self.assertIsNone(result)
 
+
     def test_execute(self):
         traffic_generator = mock.Mock(autospec=TrexProfile)
         traffic_generator.my_ports = [0, 1]
diff --git a/tests/unit/network_services/vnf_generic/vnf/test_tg_rfc2544_ixia.py b/tests/unit/network_services/vnf_generic/vnf/test_tg_rfc2544_ixia.py
index 661e885ca..3c5ccefce 100644
--- a/tests/unit/network_services/vnf_generic/vnf/test_tg_rfc2544_ixia.py
+++ b/tests/unit/network_services/vnf_generic/vnf/test_tg_rfc2544_ixia.py
@@ -325,13 +325,15 @@ class TestIXIATrafficGen(unittest.TestCase):
         sut.resource_helper.client = mock.MagicMock()
         sut.resource_helper.client_started = mock.MagicMock()
         sut.resource_helper.client_started.value = 1
+        sut.resource_helper.rfc_helper.iteration.value = 11
 
         sut.scenario_helper.scenario_cfg = {
             'options': {
                 'packetsize': 64,
                 'traffic_type': 4,
                 'rfc2544': {
-                    'allowed_drop_rate': '0.8 - 1'
+                    'allowed_drop_rate': '0.8 - 1',
+                    'latency': True
                 },
                 'vnf__1': {
                     'rules': 'acl_1rule.yaml',
diff --git a/yardstick/network_services/traffic_profile/ixia_rfc2544.py b/yardstick/network_services/traffic_profile/ixia_rfc2544.py
index ba532b820..4745b24b5 100644
--- a/yardstick/network_services/traffic_profile/ixia_rfc2544.py
+++ b/yardstick/network_services/traffic_profile/ixia_rfc2544.py
@@ -46,6 +46,7 @@ class IXIARFC2544Profile(TrexProfile):
                     mac["src_mac_{}".format(traffic['id'])]
                 traffic['outer_l2']['dstmac'] = \
                     mac["dst_mac_{}".format(traffic['id'])]
+
                 # outer_l3
                 if "outer_l3v6" in list(value.keys()):
                     traffic['outer_l3'] = value['outer_l3v6']
@@ -79,16 +80,19 @@ class IXIARFC2544Profile(TrexProfile):
         self.tmp_drop = 0
         self.tmp_throughput = 0
 
+    def update_traffic_profile(self):
+        self.profile = 'private_1'
+        for key, value in self.params.items():
+            if "private" in key or "public" in key:
+                self.profile_data = self.params[key]
+                self.get_streams(self.profile_data)
+                self.full_profile.update({key: self.profile_data})
+
     def execute(self, traffic_generator, ixia_obj, mac={}, xfile=None):
         if self.first_run:
             self.full_profile = {}
             self.pg_id = 0
-            self.profile = 'private_1'
-            for key, value in self.params.items():
-                if "private" in key or "public" in key:
-                    self.profile_data = self.params[key]
-                    self.get_streams(self.profile_data)
-                    self.full_profile.update({key: self.profile_data})
+            self.update_traffic_profile()
             traffic = \
                 self._get_ixia_traffic_profile(self.full_profile, mac, xfile)
             self.max_rate = self.rate
@@ -103,7 +107,9 @@ class IXIARFC2544Profile(TrexProfile):
 
     def start_ixia_latency(self, traffic_generator, ixia_obj,
                            mac={}, xfile=None):
-        traffic = self._get_ixia_traffic_profile(self.full_profile, mac)
+        self.update_traffic_profile()
+        traffic = \
+            self._get_ixia_traffic_profile(self.full_profile, mac, xfile)
         self._ixia_traffic_generate(traffic_generator, traffic,
                                     ixia_obj, xfile)
 
diff --git a/yardstick/network_services/vnf_generic/vnf/tg_rfc2544_ixia.py b/yardstick/network_services/vnf_generic/vnf/tg_rfc2544_ixia.py
index c758baa4c..a52416dd9 100644
--- a/yardstick/network_services/vnf_generic/vnf/tg_rfc2544_ixia.py
+++ b/yardstick/network_services/vnf_generic/vnf/tg_rfc2544_ixia.py
@@ -40,11 +40,14 @@ except ImportError:
 
 class IxiaRfc2544Helper(Rfc2544ResourceHelper):
 
-    pass
+    def is_done(self):
+        return self.latency and self.iteration.value > 10
 
 
 class IxiaResourceHelper(ClientResourceHelper):
 
+    LATENCY_TIME_SLEEP = 120
+
     def __init__(self, setup_helper, rfc_helper_type=None):
         super(IxiaResourceHelper, self).__init__(setup_helper)
         self.scenario_helper = setup_helper.scenario_helper
@@ -69,7 +72,7 @@ class IxiaResourceHelper(ClientResourceHelper):
         self.my_ports = list(set(self.priv_ports).union(set(self.pub_ports)))
 
     def get_stats(self, *args, **kwargs):
-        return self.client.ix_get_statistics()[1]
+        return self.client.ix_get_statistics()
 
     def stop_collect(self):
         self._terminated.value = 0
@@ -77,27 +80,44 @@ class IxiaResourceHelper(ClientResourceHelper):
             self.client.ix_stop_traffic()
 
     def generate_samples(self, key=None, default=None):
-        last_result = self.get_stats()
+        stats = self.get_stats()
+        last_result = stats[1]
+        latency = stats[0]
 
         samples = {}
         for vpci_idx, interface in enumerate(self.vnfd_helper.interfaces):
-            name = "xe{0}".format(vpci_idx)
-            samples[name] = {
-                "rx_throughput_kps": float(last_result["Rx_Rate_Kbps"][vpci_idx]),
-                "tx_throughput_kps": float(last_result["Tx_Rate_Kbps"][vpci_idx]),
-                "rx_throughput_mbps": float(last_result["Rx_Rate_Mbps"][vpci_idx]),
-                "tx_throughput_mbps": float(last_result["Tx_Rate_Mbps"][vpci_idx]),
-                "in_packets": int(last_result["Valid_Frames_Rx"][vpci_idx]),
-                "out_packets": int(last_result["Frames_Tx"][vpci_idx]),
-                "RxThroughput": int(last_result["Valid_Frames_Rx"][vpci_idx]) / 30,
-                "TxThroughput": int(last_result["Frames_Tx"][vpci_idx]) / 30,
-            }
+            try:
+                name = "xe{0}".format(vpci_idx)
+                samples[name] = {
+                    "rx_throughput_kps": float(last_result["Rx_Rate_Kbps"][vpci_idx]),
+                    "tx_throughput_kps": float(last_result["Tx_Rate_Kbps"][vpci_idx]),
+                    "rx_throughput_mbps": float(last_result["Rx_Rate_Mbps"][vpci_idx]),
+                    "tx_throughput_mbps": float(last_result["Tx_Rate_Mbps"][vpci_idx]),
+                    "in_packets": int(last_result["Valid_Frames_Rx"][vpci_idx]),
+                    "out_packets": int(last_result["Frames_Tx"][vpci_idx]),
+                    "RxThroughput": int(last_result["Valid_Frames_Rx"][vpci_idx]) / 30,
+                    "TxThroughput": int(last_result["Frames_Tx"][vpci_idx]) / 30,
+                }
+                if key:
+                    avg_latency = latency["Store-Forward_Avg_latency_ns"][vpci_idx]
+                    min_latency = latency["Store-Forward_Min_latency_ns"][vpci_idx]
+                    max_latency = latency["Store-Forward_Max_latency_ns"][vpci_idx]
+                    samples[name][key] = \
+                        {"Store-Forward_Avg_latency_ns": avg_latency,
+                         "Store-Forward_Min_latency_ns": min_latency,
+                         "Store-Forward_Max_latency_ns": max_latency}
+            except IndexError:
+                pass
 
         return samples
 
     def run_traffic(self, traffic_profile):
+        if self._terminated.value:
+            return
+
         min_tol = self.rfc_helper.tolerance_low
         max_tol = self.rfc_helper.tolerance_high
+        default = "00:00:00:00:00:00"
 
         self._build_ports()
         self._connect()
@@ -115,8 +135,8 @@ class IxiaResourceHelper(ClientResourceHelper):
         for index, interface in enumerate(self.vnfd_helper.interfaces, 1):
             virt_intf = interface["virtual-interface"]
             mac.update({
-                "src_mac_{}".format(index): virt_intf["local_mac"],
-                "dst_mac_{}".format(index): virt_intf["dst_mac"],
+                "src_mac_{}".format(index): virt_intf.get("local_mac", default),
+                "dst_mac_{}".format(index): virt_intf.get("dst_mac", default),
             })
 
         samples = {}
@@ -136,12 +156,34 @@ class IxiaResourceHelper(ClientResourceHelper):
                                                                   ixia_file)
 
             current = samples['CurrentDropPercentage']
-            if min_tol <= current <= max_tol or status == 'Completed':
-                self._terminated.value = 1
+            if min_tol <= current <= max_tol or status == 'Completed' or \
+                self.rfc_helper.is_done():
+                break
 
         self.client.ix_stop_traffic()
         self._queue.put(samples)
 
+        if not self.rfc_helper.is_done():
+            self._terminated.value = 1
+            return
+
+        traffic_profile.execute(self, self.client, mac, ixia_file)
+        for _ in range(5):
+            time.sleep(self.LATENCY_TIME_SLEEP)
+            self.client.ix_stop_traffic()
+            samples = self.generate_samples('latency', {})
+            self._queue.put(samples)
+            traffic_profile.start_ixia_latency(self, self.client, mac, ixia_file)
+            if self._terminated.value:
+                break
+
+        self.client.ix_stop_traffic()
+        self._terminated.value = 1
+
+    def collect_kpi(self):
+        self.rfc_helper.iteration.value += 1
+        return super(IxiaResourceHelper, self).collect_kpi()
+
 
 class IxiaTrafficGen(SampleVNFTrafficGen):
 
-- 
cgit