Merge "test_kubernetes: mock file operations in test_ssh_key"
[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][2:]) for x in self.tg_port_pairs]
56         self.pub_ports = [int(x[1][2:]) 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         if self._terminated.value:
61             return
62
63         traffic_profile.execute(self)
64         self.client_started.value = 1
65         time.sleep(self.RUN_DURATION)
66         self.client.stop(self.my_ports)
67         time.sleep(self.WAIT_TIME)
68         samples = traffic_profile.get_drop_percentage(self)
69         self._queue.put(samples)
70
71         if not self.rfc2544_helper.is_done():
72             return
73
74         self.client.stop(self.my_ports)
75         self.client.reset(ports=self.my_ports)
76         self.client.remove_all_streams(self.my_ports)
77         traffic_profile.execute_latency(samples=samples)
78         multiplier = traffic_profile.calculate_pps(samples)[1]
79         for _ in range(5):
80             time.sleep(self.LATENCY_TIME_SLEEP)
81             self.client.stop(self.my_ports)
82             time.sleep(self.WAIT_TIME)
83             last_res = self.client.get_stats(self.my_ports)
84             if not isinstance(last_res, Mapping):
85                 self._terminated.value = 1
86                 continue
87             self.generate_samples('latency', {})
88             self._queue.put(samples)
89             self.client.start(mult=str(multiplier),
90                               ports=self.my_ports,
91                               duration=120, force=True)
92
93     def start_client(self, mult, duration, force=True):
94         self.client.start(ports=self.my_ports, mult=mult, duration=duration, force=force)
95
96     def clear_client_stats(self):
97         self.client.clear_stats(ports=self.my_ports)
98
99     def collect_kpi(self):
100         self.rfc2544_helper.iteration.value += 1
101         return super(TrexRfcResourceHelper, self).collect_kpi()
102
103
104 class TrexTrafficGenRFC(TrexTrafficGen):
105     """
106     This class handles mapping traffic profile and generating
107     traffic for rfc2544 testcase.
108     """
109
110     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
111         if resource_helper_type is None:
112             resource_helper_type = TrexRfcResourceHelper
113
114         super(TrexTrafficGenRFC, self).__init__(name, vnfd, setup_env_helper_type,
115                                                 resource_helper_type)