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