load_gen: Supporting loading of load_gen via loader.
[vswitchperf.git] / core / component_factory.py
1 # Copyright 2015-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 """Functions for creating controller objects based on deployment or traffic
16 """
17
18 from core.traffic_controller_rfc2544 import TrafficControllerRFC2544
19 from core.traffic_controller_rfc2889 import TrafficControllerRFC2889
20 from core.vswitch_controller_clean import VswitchControllerClean
21 from core.vswitch_controller_p2p import VswitchControllerP2P
22 from core.vswitch_controller_pxp import VswitchControllerPXP
23 from core.vswitch_controller_op2p import VswitchControllerOP2P
24 from core.vswitch_controller_ptunp import VswitchControllerPtunP
25 from core.vnf_controller import VnfController
26 from core.pktfwd_controller import PktFwdController
27
28
29 def __init__():
30     """Finds and loads all the modules required.
31
32     Very similar code to load_trafficgens().
33     """
34     pass
35
36 def create_traffic(traffic_type, trafficgen_class):
37     """Return a new IVSwitchController for the traffic type.
38
39     The returned traffic controller has the given traffic type and traffic
40     generator class.
41
42     traffic_types: 'rfc2544_throughput'
43
44     :param traffic_type: Name of traffic type
45     :param trafficgen_class: Reference to traffic generator class to be used.
46     :return: A new TrafficController
47     """
48     if traffic_type.lower().startswith('rfc2889'):
49         return TrafficControllerRFC2889(trafficgen_class)
50     else:
51         return TrafficControllerRFC2544(trafficgen_class)
52
53
54 def create_vswitch(deployment_scenario, vswitch_class, traffic,
55                    tunnel_operation=None):
56     """Return a new IVSwitchController for the deployment_scenario.
57
58     The returned controller is configured with the given vSwitch class.
59
60     Deployment scenarios: e.g. 'p2p', 'pvp', 'pvpv12', etc.
61
62     :param deployment_scenario: The deployment scenario name
63     :param vswitch_class: Reference to vSwitch class to be used.
64     :param traffic: Dictionary with traffic specific details
65     :param tunnel_operation encapsulation/decapsulation or None
66     :return: IVSwitchController for the deployment_scenario
67     """
68     # pylint: disable=too-many-return-statements
69     deployment_scenario = deployment_scenario.lower()
70     if deployment_scenario.startswith("p2p"):
71         return VswitchControllerP2P(vswitch_class, traffic)
72     elif deployment_scenario.startswith("pvp"):
73         return VswitchControllerPXP(deployment_scenario, vswitch_class, traffic)
74     elif deployment_scenario.startswith("pvvp"):
75         return VswitchControllerPXP(deployment_scenario, vswitch_class, traffic)
76     elif deployment_scenario.startswith("pvpv"):
77         return VswitchControllerPXP(deployment_scenario, vswitch_class, traffic)
78     elif deployment_scenario.startswith("op2p"):
79         return VswitchControllerOP2P(vswitch_class, traffic, tunnel_operation)
80     elif deployment_scenario.startswith("ptunp"):
81         return VswitchControllerPtunP(vswitch_class, traffic)
82     elif deployment_scenario.startswith("clean"):
83         return VswitchControllerClean(vswitch_class, traffic)
84     else:
85         raise RuntimeError("Unknown deployment scenario '{}'.".format(deployment_scenario))
86
87
88 def create_vnf(deployment_scenario, vnf_class, extra_vnfs):
89     """Return a new VnfController for the deployment_scenario.
90
91     The returned controller is configured with the given VNF class.
92
93     Deployment scenarios: 'p2p', 'pvp'
94
95     :param deployment_scenario: The deployment scenario name
96     :param vswitch_class: Reference to vSwitch class to be used.
97     :param extra_vnfs: The number of VNFs not involved in given
98         deployment scenario. It will be used to correctly expand
99         configuration values and initialize shared dirs. This parameter
100         is used in case, that additional VNFs are executed by TestSteps.
101     :return: VnfController for the deployment_scenario
102     """
103     return VnfController(deployment_scenario, vnf_class, extra_vnfs)
104
105 def create_collector(collector_class, result_dir, test_name):
106     """Return a new Collector of the given class
107
108     :param collector_class: The collector class to be used.
109     :param result_dir: Directory with test results
110     :param test_name: Test to be run
111     :return: A new CollectorController.
112     """
113     return collector_class(result_dir, test_name)
114
115 def create_loadgen(loadgen_class, loadgen_cfg):
116     """Return a new ILoadGenerator for the loadgen class.
117
118     The returned load generator is of given loadgen generator class.
119
120     :param loadgen_class: Name to load generator class to be used.
121     :param loadgen_cfg: Configuration for the loadgen
122     :return: A new ILoadGenerator class
123     """
124     # pylint: disable=too-many-function-args
125     return loadgen_class(loadgen_cfg)
126
127 def create_pktfwd(deployment, pktfwd_class):
128     """Return a new packet forwarder controller
129
130     The returned controller is configured with the given
131     packet forwarder class.
132
133     :param pktfwd_class: Reference to packet forwarder class to be used.
134     :param deployment: The deployment scenario name
135     :return: packet forwarder controller
136     """
137     return PktFwdController(deployment, pktfwd_class)