NSB update
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_rfc2544_trex.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 """ Trex traffic generation definitions which implements rfc2544 """
15
16 from __future__ import absolute_import
17 from __future__ import print_function
18 import time
19 import logging
20 from collections import Mapping
21 from itertools import chain
22
23 from yardstick.network_services.helpers.samplevnf_helper import MultiPortConfig
24 from yardstick.network_services.vnf_generic.vnf.tg_trex import TrexTrafficGen
25 from yardstick.network_services.vnf_generic.vnf.sample_vnf import Rfc2544ResourceHelper
26 from yardstick.network_services.vnf_generic.vnf.tg_trex import TrexResourceHelper
27
28 LOGGING = logging.getLogger(__name__)
29
30
31 class TrexRfc2544ResourceHelper(Rfc2544ResourceHelper):
32
33     def is_done(self):
34         return self.latency and self.iteration.value > 10
35
36
37 class TrexRfcResourceHelper(TrexResourceHelper):
38
39     LATENCY_TIME_SLEEP = 120
40     RUN_DURATION = 30
41     WAIT_TIME = 3
42
43     def __init__(self, setup_helper, rfc_helper_type=None):
44         super(TrexRfcResourceHelper, self).__init__(setup_helper)
45
46         if rfc_helper_type is None:
47             rfc_helper_type = TrexRfc2544ResourceHelper
48
49         self.rfc2544_helper = rfc_helper_type(self.scenario_helper)
50         # self.tg_port_pairs = []
51
52     def _build_ports(self):
53         self.tg_port_pairs, self.networks = MultiPortConfig.get_port_pairs(
54             self.vnfd_helper.interfaces)
55         self.priv_ports = [int(x[0][-1]) for x in self.tg_port_pairs]
56         self.pub_ports = [int(x[1][-1]) for x in self.tg_port_pairs]
57         self.my_ports = list(set(chain(self.priv_ports, self.pub_ports)))
58
59     def _run_traffic_once(self, traffic_profile):
60         traffic_profile.execute(self)
61         self.client_started.value = 1
62         time.sleep(self.RUN_DURATION)
63         self.client.stop(self.my_ports)
64         time.sleep(self.WAIT_TIME)
65         samples = traffic_profile.get_drop_percentage(self)
66         self._queue.put(samples)
67
68         if not self.rfc2544_helper.is_done():
69             return
70
71         self.client.stop(self.my_ports)
72         self.client.reset(ports=self.my_ports)
73         self.client.remove_all_streams(self.my_ports)
74         traffic_profile.execute_latency(samples=samples)
75         multiplier = traffic_profile.calculate_pps(samples)[1]
76         for _ in range(5):
77             time.sleep(self.LATENCY_TIME_SLEEP)
78             self.client.stop(self.my_ports)
79             time.sleep(self.WAIT_TIME)
80             last_res = self.client.get_stats(self.my_ports)
81             if not isinstance(last_res, Mapping):
82                 self._terminated.value = 1
83                 continue
84             self.generate_samples('latency', {})
85             self._queue.put(samples)
86             self.client.start(mult=str(multiplier),
87                               ports=self.my_ports,
88                               duration=120, force=True)
89
90     def start_client(self, mult, duration, force=True):
91         self.client.start(ports=self.my_ports, mult=mult, duration=duration, force=force)
92
93     def clear_client_stats(self):
94         self.client.clear_stats(ports=self.my_ports)
95
96     def collect_kpi(self):
97         self.rfc2544_helper.iteration.value += 1
98         super(TrexRfcResourceHelper, self).collect_kpi()
99
100
101 class TrexTrafficGenRFC(TrexTrafficGen):
102     """
103     This class handles mapping traffic profile and generating
104     traffic for rfc2544 testcase.
105     """
106
107     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
108         if resource_helper_type is None:
109             resource_helper_type = TrexRfcResourceHelper
110
111         super(TrexTrafficGenRFC, self).__init__(name, vnfd, setup_env_helper_type,
112                                                 resource_helper_type)