Merge "SA: Fix private key file name generation issue"
[yardstick.git] / yardstick / network_services / traffic_profile / prox_profile.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ Fixed traffic profile definitions """
15
16 from __future__ import absolute_import
17
18 import logging
19
20 from yardstick.network_services.traffic_profile.base import TrafficProfile
21 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxProfileHelper
22
23 LOG = logging.getLogger(__name__)
24
25
26 class ProxProfile(TrafficProfile):
27     """
28     This profile adds a single stream at the beginning of the traffic session
29     """
30
31     @staticmethod
32     def sort_vpci(traffic_gen):
33         """Return the list of external interfaces ordered by vpci and name
34
35         :param traffic_gen: (ProxTrafficGen) traffic generator
36         :return: list of ordered interfaces
37         """
38         def key_func(interface):
39             return interface['virtual-interface']['vpci'], interface['name']
40
41         return sorted(traffic_gen.vnfd_helper['vdu'][0]['external-interface'],
42                       key=key_func)
43
44     @staticmethod
45     def fill_samples(samples, traffic_gen):
46         vpci_if_name_ascending = ProxProfile.sort_vpci(traffic_gen)
47         for vpci_idx, intf in enumerate(vpci_if_name_ascending):
48             name = intf[1]
49             # TODO: VNFDs KPIs values needs to be mapped to TRex structure
50             xe_port = traffic_gen.resource_helper.sut.port_stats([vpci_idx])
51             samples[name] = {
52                 "in_packets": xe_port[6],
53                 "out_packets": xe_port[7],
54             }
55
56     def __init__(self, tp_config):
57         super(ProxProfile, self).__init__(tp_config)
58         self.queue = None
59         self.done = False
60         self.results = []
61
62         # TODO: get init values from tp_config
63         self.prox_config = tp_config["traffic_profile"]
64         self.pkt_sizes = [int(x) for x in self.prox_config.get("packet_sizes", [])]
65         self.pkt_size_iterator = iter(self.pkt_sizes)
66         self.duration = int(self.prox_config.get("duration", 5))
67         self.precision = float(self.prox_config.get('test_precision', 1.0))
68         self.tolerated_loss = float(self.prox_config.get('tolerated_loss', 0.0))
69
70         # TODO: is this ever a function of packet size?
71         self.lower_bound = float(self.prox_config.get('lower_bound', 10.0))
72         self.upper_bound = float(self.prox_config.get('upper_bound', 100.0))
73         self.step_value = float(self.prox_config.get('step_value', 10.0))
74         self._profile_helper = None
75
76     def make_profile_helper(self, traffic_gen):
77         if self._profile_helper is None:
78             self._profile_helper = ProxProfileHelper.make_profile_helper(traffic_gen)
79         return self._profile_helper
80
81     def init(self, queue):
82         self.pkt_size_iterator = iter(self.pkt_sizes)
83         self.queue = queue
84         self.queue.cancel_join_thread()
85
86     def bounds_iterator(self, logger=None):
87         if logger:
88             logger.debug("Interval [%s, %s), step: %d", self.lower_bound,
89                          self.upper_bound, self.step_value)
90
91         test_value = self.lower_bound
92         while test_value <= self.upper_bound:
93             if logger:
94                 logger.info("Testing with value %s", test_value)
95             yield test_value
96             test_value += self.step_value
97
98     @property
99     def min_pkt_size(self):
100         """Return the minimum required packet size for the test.
101
102         Defaults to 64. Individual test must override this method if they have
103         other requirements.
104
105         Returns:
106             int. The minimum required packet size for the test.
107         """
108         return 64
109
110     def run_test_with_pkt_size(self, traffic_generator, pkt_size, duration):
111         raise NotImplementedError
112
113     def execute_traffic(self, traffic_generator):
114         self.make_profile_helper(traffic_generator)
115
116         try:
117             pkt_size = next(self.pkt_size_iterator)
118         except StopIteration:
119             self.done = True
120             return
121
122         # Adjust packet size upwards if it's less than the minimum
123         # required packet size for the test.
124         if pkt_size < self.min_pkt_size:
125             pkt_size += self.min_pkt_size - 64
126
127         duration = self.duration
128         self.run_test_with_pkt_size(traffic_generator, pkt_size, duration)