1 """RFC2889 Traffic Controller implementation.
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
12 class TrafficControllerRFC2889(ITrafficController, IResults):
13 """Traffic controller for RFC2889 traffic
15 Used to setup and control a traffic generator for an RFC2889 deployment
19 def __init__(self, traffic_gen_class):
20 """Initialise the trafficgen and store.
22 :param traffic_gen_class: The traffic generator class to be used.
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))
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')
38 self._packet_sizes = [int(x.strip())
39 for x in packet_sizes_cli.split(',')]
41 self._packet_sizes = settings.getValue('TRAFFICGEN_PKT_SIZES')
44 """Call initialisation function.
46 self._traffic_gen_class.connect()
48 def __exit__(self, type_, value, traceback):
49 """Stop traffic, clean up.
51 if self._traffic_started:
55 def _append_results(result_dict, packet_size):
56 """Adds common values to traffic generator results.
58 :param result_dict: Dictionary containing results from trafficgen
59 :param packet_size: Packet size value.
61 :returns: dictionary of results with additional entries.
64 ret_value = result_dict
66 ret_value[ResultsConstants.TYPE] = 'rfc2889'
67 ret_value[ResultsConstants.PACKET_SIZE] = str(packet_size)
71 def send_traffic(self, traffic):
72 """See ITrafficController for description
74 self._logger.debug('send_traffic with ' +
75 str(self._traffic_gen_class))
77 for packet_size in self._packet_sizes:
78 # Merge framesize with the default traffic definition
80 traffic['l2'] = dict(traffic['l2'],
81 **{'framesize': packet_size})
83 traffic['l2'] = {'framesize': packet_size}
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)
92 result = self._traffic_gen_class.send_rfc2889_forwarding(
93 traffic, tests=self._trials, duration=self._duration)
95 result = TrafficControllerRFC2889._append_results(result,
97 self._results.append(result)
99 def send_traffic_async(self, traffic, function):
100 """See ITrafficController for description
102 self._logger.debug('send_traffic_async with ' +
103 str(self._traffic_gen_class))
105 for packet_size in self._packet_sizes:
106 traffic['l2'] = {'framesize': packet_size}
107 self._traffic_gen_class.start_rfc2889_forwarding(
110 duration=self._duration)
111 self._traffic_started = True
112 if len(function['args']) > 0:
113 function['function'](function['args'])
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,
120 self._results.append(result)
122 def stop_traffic(self):
123 """Kills traffic being sent from the traffic generator.
125 self._logger.debug("stop_traffic()")
127 def print_results(self):
128 """IResult interface implementation.
131 for item in self._results:
132 logging.info("Record: " + str(counter))
134 for(key, value) in list(item.items()):
135 logging.info(" Key: " + str(key) +
136 ", Value: " + str(value))
138 def get_results(self):
139 """IResult interface implementation.
143 def validate_send_traffic(self, result, traffic):
144 """Verify that send traffic has succeeded