Merge "vpp: Initial support of VPP vSwitch"
[vswitchperf.git] / core / loader / loader.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 """Loader module definition.
16 """
17
18 from conf import settings
19 from core.loader.loader_servant import LoaderServant
20 from tools.collectors.collector import ICollector
21 from tools.pkt_fwd.pkt_fwd import IPktFwd
22 from tools.pkt_gen.trafficgen import ITrafficGenerator
23 from vswitches.vswitch import IVSwitch
24 from vnfs.vnf.vnf import IVnf
25
26 class Loader(object):
27     """Loader class - main object context holder.
28     """
29     _trafficgen_loader = None
30     _metrics_loader = None
31     _vswitch_loader = None
32     _vnf_loader = None
33
34     def __init__(self):
35         """Loader ctor - initialization method.
36
37         All data is read from configuration each time Loader instance is
38         created. It is up to creator to maintain object life cycle if this
39         behavior is unwanted.
40         """
41         self._trafficgen_loader = LoaderServant(
42             settings.getValue('TRAFFICGEN_DIR'),
43             settings.getValue('TRAFFICGEN'),
44             ITrafficGenerator)
45
46         self._metrics_loader = LoaderServant(
47             settings.getValue('COLLECTOR_DIR'),
48             settings.getValue('COLLECTOR'),
49             ICollector)
50
51         self._vswitch_loader = LoaderServant(
52             settings.getValue('VSWITCH_DIR'),
53             settings.getValue('VSWITCH'),
54             IVSwitch)
55
56         self._vnf_loader = LoaderServant(
57             settings.getValue('VNF_DIR'),
58             settings.getValue('VNF'),
59             IVnf)
60
61         self._pktfwd_loader = LoaderServant(
62             settings.getValue('PKTFWD_DIR'),
63             settings.getValue('PKTFWD'),
64             IPktFwd)
65
66     def get_trafficgen(self):
67         """Returns a new instance configured traffic generator.
68
69         :return: ITrafficGenerator implementation if available, None otherwise.
70         """
71         return self._trafficgen_loader.get_class()()
72
73     def get_trafficgen_class(self):
74         """Returns type of currently configured traffic generator.
75
76         :return: Type of ITrafficGenerator implementation if available.
77             None otherwise.
78         """
79         return self._trafficgen_loader.get_class()
80
81     def get_trafficgens(self):
82         """Returns dictionary of all available traffic generators.
83
84         :return: Dictionary of traffic generators.
85             - key: name of the class which implements ITrafficGenerator,
86             - value: Type of traffic generator which implements
87               ITrafficGenerator.
88         """
89         return self._trafficgen_loader.get_classes()
90
91     def get_trafficgens_printable(self):
92         """Returns all available traffic generators in printable format.
93
94         :return: String containing printable list of traffic generators.
95         """
96         return self._trafficgen_loader.get_classes_printable()
97
98     def get_collector(self):
99         """Returns instance of currently configured collector implementation.
100
101         :return: ICollector implementation if available, None otherwise.
102         """
103         return self._metrics_loader.get_class()()
104
105     def get_collector_class(self):
106         """Returns type of currently configured collector implementation.
107
108         :return: Type of ICollector implementation if available.
109             None otherwise.
110         """
111         return self._metrics_loader.get_class()
112
113     def get_collectors(self):
114         """Returns dictionary of all available collectors.
115
116         :return: Dictionary of collectors.
117             - key: name of the class which implements ICollector,
118             - value: Type of collector which implements ICollector.
119         """
120         return self._metrics_loader.get_classes()
121
122     def get_collectors_printable(self):
123         """Returns all available collectors in printable format.
124
125         :return: String containing printable list of collectors.
126         """
127         return self._metrics_loader.get_classes_printable()
128
129     def get_vswitch(self):
130         """Returns instance of currently configured vswitch implementation.
131
132         :return: IVSwitch implementation if available, None otherwise.
133         """
134         return self._vswitch_loader.get_class()()
135
136     def get_vswitch_class(self):
137         """Returns type of currently configured vswitch implementation.
138
139         :return: Type of IVSwitch implementation if available.
140             None otherwise.
141         """
142         return self._vswitch_loader.get_class()
143
144     def get_vswitches(self):
145         """Returns dictionary of all available vswitches.
146
147         :return: Dictionary of vswitches.
148             - key: name of the class which implements IVSwitch,
149             - value: Type of vswitch which implements IVSwitch.
150         """
151         return self._vswitch_loader.get_classes()
152
153     def get_vswitches_printable(self):
154         """Returns all available vswitches in printable format.
155
156         :return: String containing printable list of vswitches.
157         """
158         return self._vswitch_loader.get_classes_printable()
159
160     def get_vnf(self):
161         """Returns instance of currently configured vnf implementation.
162
163         :return: IVnf implementation if available, None otherwise.
164         """
165         return self._vnf_loader.get_class()()
166
167     def get_vnf_class(self):
168         """Returns type of currently configured vnf implementation.
169
170         :return: Type of IVnf implementation if available.
171             None otherwise.
172         """
173         return self._vnf_loader.get_class()
174
175     def get_vnfs(self):
176         """Returns dictionary of all available vnfs.
177
178         :return: Dictionary of vnfs.
179             - key: name of the class which implements IVnf,
180             - value: Type of vnf which implements IVnf.
181         """
182         return self._vnf_loader.get_classes()
183
184     def get_vnfs_printable(self):
185         """Returns all available vnfs in printable format.
186
187         :return: String containing printable list of vnfs.
188         """
189         return self._vnf_loader.get_classes_printable()
190
191     def get_pktfwd(self):
192         """Returns instance of currently configured packet forwarder implementation.
193
194         :return: IPktFwd implementation if available, None otherwise.
195         """
196         return self._pktfwd_loader.get_class()()
197
198     def get_pktfwd_class(self):
199         """Returns type of currently configured packet forwarder implementation.
200
201         :return: Type of IPktFwd implementation if available.
202             None otherwise.
203         """
204         return self._pktfwd_loader.get_class()
205
206     def get_pktfwds(self):
207         """Returns dictionary of all available packet forwarders.
208
209         :return: Dictionary of packet forwarders.
210             - key: name of the class which implements IPktFwd,
211             - value: Type of packet forwarder which implements IPktFwd.
212         """
213         return self._pktfwd_loader.get_classes()
214
215     def get_pktfwds_printable(self):
216         """Returns all available packet forwarders in printable format.
217
218         :return: String containing printable list of packet forwarders.
219         """
220         return self._pktfwd_loader.get_classes_printable()