Merge "Bugfix: load_images cleanup, shellcheck fixes"
[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 """ Base class for the generic traffic profile implementation """
15
16 from __future__ import absolute_import
17 from yardstick.common.utils import import_modules_from_package, itersubclasses
18
19
20 class TrafficProfile(object):
21     """
22     This class defines the behavior
23
24     """
25
26     @staticmethod
27     def get(tp_config):
28         """Get the traffic profile instance for the given traffic type
29
30         :param tp_config: loaded YAML file
31         :return:
32         """
33         profile_class = tp_config["traffic_profile"]["traffic_type"]
34         import_modules_from_package(
35             "yardstick.network_services.traffic_profile")
36         try:
37             return next(c for c in itersubclasses(TrafficProfile)
38                         if c.__name__ == profile_class)(tp_config)
39         except StopIteration:
40             raise RuntimeError("No implementation for %s", profile_class)
41
42     def __init__(self, tp_config):
43         # e.g. RFC2544 start_ip, stop_ip, drop_rate,
44         # IMIX = {"10K": 0.1, "100M": 0.5}
45         self.params = tp_config
46
47     def execute(self, traffic_generator):
48         """ This methods defines the behavior of the traffic generator.
49         It will be called in a loop until the traffic generator exits.
50
51         :param traffic_generator: TrafficGen instance
52         :return: None
53         """
54         raise NotImplementedError()
55
56
57 class DummyProfile(TrafficProfile):
58     """
59     This is an empty TrafficProfile implementation - if it is used,
60     the traffic will be completely handled by the Traffic Generator
61     implementation with no regard for the Traffic Profile.
62     """
63     def execute(self, traffic_generator):
64         pass