Merge "Add "duration" parameter to test case definition"
[yardstick.git] / yardstick / network_services / traffic_profile / base.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
15 from yardstick.common import exceptions
16 from yardstick.common import utils
17
18
19 class TrafficProfileConfig(object):
20     """Class to contain the TrafficProfile class information
21
22     This object will parse and validate the traffic profile information.
23     """
24     def __init__(self, tp_config):
25         self.schema = tp_config.get('schema', 'nsb:traffic_profile:0.1')
26         self.name = tp_config.get('name')
27         self.description = tp_config.get('description')
28         tprofile = tp_config['traffic_profile']
29         self.traffic_type = tprofile.get('traffic_type')
30         self.frame_rate = tprofile.get('frame_rate')
31         self.test_precision = tprofile.get('test_precision')
32         self.packet_sizes = tprofile.get('packet_sizes')
33         self.duration = tprofile.get('duration')
34         self.lower_bound = tprofile.get('lower_bound')
35         self.upper_bound = tprofile.get('upper_bound')
36         self.step_interval = tprofile.get('step_interval')
37
38
39 class TrafficProfile(object):
40     """
41     This class defines the behavior
42
43     """
44     UPLINK = "uplink"
45     DOWNLINK = "downlink"
46     DEFAULT_DURATION = 30
47
48     @staticmethod
49     def get(tp_config):
50         """Get the traffic profile instance for the given traffic type
51
52         :param tp_config: loaded YAML file
53         :return:
54         """
55         profile_class = tp_config["traffic_profile"]["traffic_type"]
56         try:
57             return next(c for c in utils.itersubclasses(TrafficProfile)
58                         if c.__name__ == profile_class)(tp_config)
59         except StopIteration:
60             raise exceptions.TrafficProfileNotImplemented(
61                 profile_class=profile_class)
62
63     def __init__(self, tp_config):
64         # e.g. RFC2544 start_ip, stop_ip, drop_rate,
65         # IMIX = {"10K": 0.1, "100M": 0.5}
66         self.params = tp_config
67         self.config = TrafficProfileConfig(tp_config)
68
69     def execute_traffic(self, traffic_generator, **kawrgs):
70         """ This methods defines the behavior of the traffic generator.
71         It will be called in a loop until the traffic generator exits.
72
73         :param traffic_generator: TrafficGen instance
74         :return: None
75         """
76         raise NotImplementedError()
77
78
79 class DummyProfile(TrafficProfile):
80     """
81     This is an empty TrafficProfile implementation - if it is used,
82     the traffic will be completely handled by the Traffic Generator
83     implementation with no regard for the Traffic Profile.
84     """
85     def execute(self, traffic_generator):
86         pass