Extend vBNG PPPoE test cases functionality
[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 import re
16
17 from yardstick.common import exceptions
18 from yardstick.common import utils
19
20
21 class TrafficProfileConfig(object):
22     """Class to contain the TrafficProfile class information
23
24     This object will parse and validate the traffic profile information.
25     """
26     DEFAULT_SCHEMA = 'nsb:traffic_profile:0.1'
27     DEFAULT_FRAME_RATE = '100'
28     DEFAULT_DURATION = 30
29     RATE_FPS = 'fps'
30     RATE_PERCENTAGE = '%'
31     RATE_REGEX = re.compile(r'([0-9]*\.[0-9]+|[0-9]+)\s*(fps|%)*(.*)')
32
33     def __init__(self, tp_config):
34         self.schema = tp_config.get('schema', self.DEFAULT_SCHEMA)
35         self.name = tp_config.get('name')
36         self.description = tp_config.get('description')
37         tprofile = tp_config['traffic_profile']
38         self.traffic_type = tprofile.get('traffic_type')
39         self.frame_rate, self.rate_unit = self._parse_rate(
40             tprofile.get('frame_rate', self.DEFAULT_FRAME_RATE))
41         self.test_precision = tprofile.get('test_precision')
42         self.packet_sizes = tprofile.get('packet_sizes')
43         self.duration = tprofile.get('duration', self.DEFAULT_DURATION)
44         self.lower_bound = tprofile.get('lower_bound')
45         self.upper_bound = tprofile.get('upper_bound')
46         self.step_interval = tprofile.get('step_interval')
47         self.enable_latency = tprofile.get('enable_latency', False)
48
49     def _parse_rate(self, rate):
50         """Parse traffic profile rate
51
52         The line rate can be defined in fps or percentage over the maximum line
53         rate:
54           - frame_rate = 5000 (by default, unit is 'fps')
55           - frame_rate = 5000fps
56           - frame_rate = 25%
57
58         :param rate: (string, int) line rate in fps or %
59         :return: (tuple: int, string) line rate number and unit
60         """
61         match = self.RATE_REGEX.match(str(rate))
62         if not match:
63             exceptions.TrafficProfileRate()
64         rate = float(match.group(1))
65         unit = match.group(2) if match.group(2) else self.RATE_FPS
66         if match.group(3):
67             raise exceptions.TrafficProfileRate()
68         return rate, unit
69
70
71 class TrafficProfile(object):
72     """
73     This class defines the behavior
74
75     """
76     UPLINK = "uplink"
77     DOWNLINK = "downlink"
78
79     @staticmethod
80     def get(tp_config):
81         """Get the traffic profile instance for the given traffic type
82
83         :param tp_config: loaded YAML file
84         :return:
85         """
86         profile_class = tp_config["traffic_profile"]["traffic_type"]
87         try:
88             return next(c for c in utils.itersubclasses(TrafficProfile)
89                         if c.__name__ == profile_class)(tp_config)
90         except StopIteration:
91             raise exceptions.TrafficProfileNotImplemented(
92                 profile_class=profile_class)
93
94     def __init__(self, tp_config):
95         # e.g. RFC2544 start_ip, stop_ip, drop_rate,
96         # IMIX = {"10K": 0.1, "100M": 0.5}
97         self.params = tp_config
98         self.config = TrafficProfileConfig(tp_config)
99
100     def is_ended(self):
101         return False
102
103     def execute_traffic(self, traffic_generator, **kawrgs):
104         """ This methods defines the behavior of the traffic generator.
105         It will be called in a loop until the traffic generator exits.
106
107         :param traffic_generator: TrafficGen instance
108         :return: None
109         """
110         raise NotImplementedError()
111
112
113 class DummyProfile(TrafficProfile):
114     """
115     This is an empty TrafficProfile implementation - if it is used,
116     the traffic will be completely handled by the Traffic Generator
117     implementation with no regard for the Traffic Profile.
118     """
119     def execute(self, traffic_generator):
120         pass