pkt_gen: Spirent Testcenter RFC 2889 Support
[vswitchperf.git] / core / traffic_controller_rfc2889.py
1 """RFC2889 Traffic Controller implementation.
2 """
3 import logging
4
5 from core.traffic_controller import ITrafficController
6 from core.results.results_constants import ResultsConstants
7 from core.results.results import IResults
8 from conf import settings
9 from conf import get_test_param
10
11
12 class TrafficControllerRFC2889(ITrafficController, IResults):
13     """Traffic controller for RFC2889 traffic
14
15     Used to setup and control a traffic generator for an RFC2889 deployment
16     traffic scenario.
17     """
18
19     def __init__(self, traffic_gen_class):
20         """Initialise the trafficgen and store.
21
22         :param traffic_gen_class: The traffic generator class to be used.
23         """
24         self._logger = logging.getLogger(__name__)
25         self._logger.debug("__init__")
26         self._traffic_gen_class = traffic_gen_class()
27         self._traffic_started = False
28         self._traffic_started_call_count = 0
29         self._trials = int(get_test_param('rfc2889_trials', 1))
30         self._duration = int(get_test_param('duration', 30))
31         self._results = []
32
33         # If set, comma separated packet_sizes value from --test_params
34         # on cli takes precedence over value in settings file.
35         self._packet_sizes = None
36         packet_sizes_cli = get_test_param('pkt_sizes')
37         if packet_sizes_cli:
38             self._packet_sizes = [int(x.strip())
39                                   for x in packet_sizes_cli.split(',')]
40         else:
41             self._packet_sizes = settings.getValue('TRAFFICGEN_PKT_SIZES')
42
43     def __enter__(self):
44         """Call initialisation function.
45         """
46         self._traffic_gen_class.connect()
47
48     def __exit__(self, type_, value, traceback):
49         """Stop traffic, clean up.
50         """
51         if self._traffic_started:
52             self.stop_traffic()
53
54     @staticmethod
55     def _append_results(result_dict, packet_size):
56         """Adds common values to traffic generator results.
57
58         :param result_dict: Dictionary containing results from trafficgen
59         :param packet_size: Packet size value.
60
61         :returns: dictionary of results with additional entries.
62         """
63
64         ret_value = result_dict
65
66         ret_value[ResultsConstants.TYPE] = 'rfc2889'
67         ret_value[ResultsConstants.PACKET_SIZE] = str(packet_size)
68
69         return ret_value
70
71     def send_traffic(self, traffic):
72         """See ITrafficController for description
73         """
74         self._logger.debug('send_traffic with ' +
75                            str(self._traffic_gen_class))
76
77         for packet_size in self._packet_sizes:
78             # Merge framesize with the default traffic definition
79             if 'l2' in traffic:
80                 traffic['l2'] = dict(traffic['l2'],
81                                      **{'framesize': packet_size})
82             else:
83                 traffic['l2'] = {'framesize': packet_size}
84
85             if traffic['traffic_type'] == 'caching':
86                 result = self._traffic_gen_class.send_rfc2889_caching(
87                     traffic, trials=self._trials, duration=self._duration)
88             elif traffic['traffic_type'] == 'congestion':
89                 result = self._traffic_gen_class.send_rfc2889_congestion(
90                     traffic, duration=self._duration)
91             else:
92                 result = self._traffic_gen_class.send_rfc2889_forwarding(
93                     traffic, tests=self._trials, duration=self._duration)
94
95             result = TrafficControllerRFC2889._append_results(result,
96                                                               packet_size)
97             self._results.append(result)
98
99     def send_traffic_async(self, traffic, function):
100         """See ITrafficController for description
101         """
102         self._logger.debug('send_traffic_async with ' +
103                            str(self._traffic_gen_class))
104
105         for packet_size in self._packet_sizes:
106             traffic['l2'] = {'framesize': packet_size}
107             self._traffic_gen_class.start_rfc2889_forwarding(
108                 traffic,
109                 trials=self._trials,
110                 duration=self._duration)
111             self._traffic_started = True
112             if len(function['args']) > 0:
113                 function['function'](function['args'])
114             else:
115                 function['function']()
116             result = self._traffic_gen_class.wait_rfc2889_forwarding(
117                         traffic, trials=self._trials, duration=self._duration)
118             result = TrafficControllerRFC2889._append_results(result,
119                                                               packet_size)
120             self._results.append(result)
121
122     def stop_traffic(self):
123         """Kills traffic being sent from the traffic generator.
124         """
125         self._logger.debug("stop_traffic()")
126
127     def print_results(self):
128         """IResult interface implementation.
129         """
130         counter = 0
131         for item in self._results:
132             logging.info("Record: " + str(counter))
133             counter += 1
134             for(key, value) in list(item.items()):
135                 logging.info("         Key: " + str(key) +
136                              ", Value: " + str(value))
137
138     def get_results(self):
139         """IResult interface implementation.
140         """
141         return self._results
142
143     def validate_send_traffic(self, result, traffic):
144         """Verify that send traffic has succeeded
145         """
146         return True