Merge "Add send socket commands function"
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_rfc2544_ixia.py
index 94ab698..875ae93 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import time
-import os
 import logging
-import sys
 
-from yardstick.common import exceptions
 from yardstick.common import utils
+from yardstick.network_services.libs.ixia_libs.ixnet import ixnet_api
 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
 from yardstick.network_services.vnf_generic.vnf.sample_vnf import Rfc2544ResourceHelper
 
+
 LOG = logging.getLogger(__name__)
 
 WAIT_AFTER_CFG_LOAD = 10
 WAIT_FOR_TRAFFIC = 30
-IXIA_LIB = os.path.dirname(os.path.realpath(__file__))
-IXNET_LIB = os.path.join(IXIA_LIB, "../../libs/ixia_libs/IxNet")
-sys.path.append(IXNET_LIB)
-
-try:
-    from IxNet import IxNextgen
-except ImportError:
-    IxNextgen = exceptions.ErrorClass
 
 
 class IxiaRfc2544Helper(Rfc2544ResourceHelper):
@@ -51,7 +41,7 @@ class IxiaResourceHelper(ClientResourceHelper):
         super(IxiaResourceHelper, self).__init__(setup_helper)
         self.scenario_helper = setup_helper.scenario_helper
 
-        self.client = IxNextgen()
+        self.client = ixnet_api.IxNextgen()
 
         if rfc_helper_type is None:
             rfc_helper_type = IxiaRfc2544Helper
@@ -62,20 +52,16 @@ class IxiaResourceHelper(ClientResourceHelper):
         self._connect()
 
     def _connect(self, client=None):
-        self.client._connect(self.vnfd_helper)
+        self.client.connect(self.vnfd_helper)
 
     def get_stats(self, *args, **kwargs):
-        return self.client.ix_get_statistics()
+        return self.client.get_statistics()
 
     def stop_collect(self):
         self._terminated.value = 1
-        if self.client:
-            self.client.ix_stop_traffic()
 
-    def generate_samples(self, ports, key=None, default=None):
+    def generate_samples(self, ports, key=None):
         stats = self.get_stats()
-        last_result = stats[1]
-        latency = stats[0]
 
         samples = {}
         # this is not DPDK port num, but this is whatever number we gave
@@ -86,19 +72,21 @@ class IxiaResourceHelper(ClientResourceHelper):
                 intf = self.vnfd_helper.find_interface_by_port(port_num)
                 port_name = intf["name"]
                 samples[port_name] = {
-                    "rx_throughput_kps": float(last_result["Rx_Rate_Kbps"][port_num]),
-                    "tx_throughput_kps": float(last_result["Tx_Rate_Kbps"][port_num]),
-                    "rx_throughput_mbps": float(last_result["Rx_Rate_Mbps"][port_num]),
-                    "tx_throughput_mbps": float(last_result["Tx_Rate_Mbps"][port_num]),
-                    "in_packets": int(last_result["Valid_Frames_Rx"][port_num]),
-                    "out_packets": int(last_result["Frames_Tx"][port_num]),
-                    "RxThroughput": int(last_result["Valid_Frames_Rx"][port_num]) / 30,
-                    "TxThroughput": int(last_result["Frames_Tx"][port_num]) / 30,
+                    "rx_throughput_kps": float(stats["Rx_Rate_Kbps"][port_num]),
+                    "tx_throughput_kps": float(stats["Tx_Rate_Kbps"][port_num]),
+                    "rx_throughput_mbps": float(stats["Rx_Rate_Mbps"][port_num]),
+                    "tx_throughput_mbps": float(stats["Tx_Rate_Mbps"][port_num]),
+                    "in_packets": int(stats["Valid_Frames_Rx"][port_num]),
+                    "out_packets": int(stats["Frames_Tx"][port_num]),
+                    # NOTE(ralonsoh): we need to make the traffic injection
+                    # time variable.
+                    "RxThroughput": int(stats["Valid_Frames_Rx"][port_num]) / 30,
+                    "TxThroughput": int(stats["Frames_Tx"][port_num]) / 30,
                 }
                 if key:
-                    avg_latency = latency["Store-Forward_Avg_latency_ns"][port_num]
-                    min_latency = latency["Store-Forward_Min_latency_ns"][port_num]
-                    max_latency = latency["Store-Forward_Max_latency_ns"][port_num]
+                    avg_latency = stats["Store-Forward_Avg_latency_ns"][port_num]
+                    min_latency = stats["Store-Forward_Min_latency_ns"][port_num]
+                    max_latency = stats["Store-Forward_Max_latency_ns"][port_num]
                     samples[port_name][key] = \
                         {"Store-Forward_Avg_latency_ns": avg_latency,
                          "Store-Forward_Min_latency_ns": min_latency,
@@ -108,7 +96,13 @@ class IxiaResourceHelper(ClientResourceHelper):
 
         return samples
 
-    def run_traffic(self, traffic_profile):
+    def _initialize_client(self):
+        """Initialize the IXIA IxNetwork client and configure the server"""
+        self.client.clear_config()
+        self.client.assign_ports()
+        self.client.create_traffic_model()
+
+    def run_traffic(self, traffic_profile, *args):
         if self._terminated.value:
             return
 
@@ -117,16 +111,7 @@ class IxiaResourceHelper(ClientResourceHelper):
         default = "00:00:00:00:00:00"
 
         self._build_ports()
-
-        # we don't know client_file_name until runtime as instantiate
-        client_file_name = \
-            utils.find_relative_file(
-                self.scenario_helper.scenario_cfg['ixia_profile'],
-                self.scenario_helper.scenario_cfg["task_path"])
-        self.client.ix_load_config(client_file_name)
-        time.sleep(WAIT_AFTER_CFG_LOAD)
-
-        self.client.ix_assign_ports()
+        self._initialize_client()
 
         mac = {}
         for port_name in self.vnfd_helper.port_pairs.all_ports:
@@ -138,43 +123,28 @@ class IxiaResourceHelper(ClientResourceHelper):
             mac["src_mac_{}".format(port_num)] = virt_intf.get("local_mac", default)
             mac["dst_mac_{}".format(port_num)] = virt_intf.get("dst_mac", default)
 
-        samples = {}
-        # Generate ixia traffic config...
         try:
             while not self._terminated.value:
-                traffic_profile.execute_traffic(self, self.client, mac)
+                first_run = traffic_profile.execute_traffic(
+                    self, self.client, mac)
                 self.client_started.value = 1
-                time.sleep(WAIT_FOR_TRAFFIC)
-                self.client.ix_stop_traffic()
+                # pylint: disable=unnecessary-lambda
+                utils.wait_until_true(lambda: self.client.is_traffic_stopped())
                 samples = self.generate_samples(traffic_profile.ports)
+
+                # NOTE(ralonsoh): the traffic injection duration is fixed to 30
+                # seconds. This parameter is configurable and must be retrieved
+                # from the traffic_profile.full_profile information.
+                # Every flow must have the same duration.
+                completed, samples = traffic_profile.get_drop_percentage(
+                    samples, min_tol, max_tol, first_run=first_run)
                 self._queue.put(samples)
-                status, samples = traffic_profile.get_drop_percentage(samples, min_tol,
-                                                                      max_tol, self.client, mac)
 
-                current = samples['CurrentDropPercentage']
-                if min_tol <= current <= max_tol or status == 'Completed':
+                if completed:
                     self._terminated.value = 1
 
-            self.client.ix_stop_traffic()
-            self._queue.put(samples)
-
-            if not self.rfc_helper.is_done():
-                self._terminated.value = 1
-                return
-
-            traffic_profile.execute_traffic(self, self.client, mac)
-            for _ in range(5):
-                time.sleep(self.LATENCY_TIME_SLEEP)
-                self.client.ix_stop_traffic()
-                samples = self.generate_samples(traffic_profile.ports, 'latency', {})
-                self._queue.put(samples)
-                traffic_profile.start_ixia_latency(self, self.client, mac)
-                if self._terminated.value:
-                    break
-
-            self.client.ix_stop_traffic()
         except Exception:  # pylint: disable=broad-except
-            LOG.exception("Run Traffic terminated")
+            LOG.exception('Run Traffic terminated')
 
         self._terminated.value = 1