Add support for Python 3
[yardstick.git] / yardstick / vTC / apexlake / experimental_framework / api.py
1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
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 __future__ import absolute_import
16 import experimental_framework.benchmarking_unit as b_unit
17 from experimental_framework import heat_template_generation, common
18
19
20 class FrameworkApi(object):
21
22     @staticmethod
23     def init():
24         """
25         Initializes the Framework
26
27         :return: None
28         """
29         common.init(api=True)
30
31     # @staticmethod
32     # def get_available_test_cases():
33     #     """
34     #     Returns a list of available test cases.
35     #     This list include eventual modules developed by the user, if any.
36     #     Each test case is returned as a string that represents the full name
37     #     of the test case and that can be used to get more information
38     #     calling get_test_case_features(test_case_name)
39     #
40     #     :return: list of strings
41     #     """
42     #     return b_unit.BenchmarkingUnit.get_available_test_cases()
43
44     @staticmethod
45     def get_test_case_features(test_case):
46         """
47         Returns a list of features (description, requested parameters,
48         allowed values, etc.) for a specified test case.
49
50         :param test_case: name of the test case (string)
51                           The string represents the test case and can be
52                           obtained  calling  "get_available_test_cases()"
53                           method.
54
55         :return: dict() containing the features of the test case
56         """
57         if not isinstance(test_case, str):
58             raise ValueError('The provided test_case parameter has to be '
59                              'a string')
60         benchmark = b_unit.BenchmarkingUnit.get_required_benchmarks(
61             [test_case])[0]
62         return benchmark.get_features()
63
64     @staticmethod
65     def execute_framework(
66             test_cases,
67             iterations,
68             heat_template,
69             heat_template_parameters,
70             deployment_configuration,
71             openstack_credentials
72     ):
73         """
74         Executes the framework according the inputs
75
76         :param test_cases: Test cases to be ran on the workload
77                             (dict() of dict())
78
79                             Example:
80                             test_case = dict()
81                             test_case['name'] = 'module.Class'
82                             test_case['params'] = dict()
83                             test_case['params']['throughput'] = '1'
84                             test_case['params']['vlan_sender'] = '1007'
85                             test_case['params']['vlan_receiver'] = '1006'
86                             test_cases = [test_case]
87
88         :param iterations: Number of cycles to be executed (int)
89
90         :param heat_template: (string) File name of the heat template of the
91                             workload to be deployed. It contains the
92                             parameters to be evaluated in the form of
93                             #parameter_name. (See heat_templates/vTC.yaml as
94                             example).
95
96         :param heat_template_parameters: (dict) Parameters to be provided
97                             as input to the heat template.
98                             See http://docs.openstack.org/developer/heat/
99                             template_guide/hot_guide.html - section
100                             "Template input parameters" for further info.
101
102         :param deployment_configuration: ( dict[string] = list(strings) ) )
103                             Dictionary of parameters representing the
104                             deployment configuration of the workload
105                             The key is a string corresponding to the name of
106                             the parameter, the value is a list of strings
107                             representing the value to be assumed by a specific
108                             param.
109                             The parameters are user defined: they have to
110                             correspond to the place holders (#parameter_name)
111                             specified in the heat template.
112
113         :return: dict() Containing results
114         """
115         common.init(api=True)
116
117         # Input Validation
118         common.InputValidation.validate_os_credentials(openstack_credentials)
119         credentials = openstack_credentials
120
121         msg = 'The provided heat_template does not exist'
122         if common.RELEASE == 'liberty':
123             heat_template = 'vTC_liberty.yaml'
124         else:
125             heat_template = 'vTC.yaml'
126         template = "{}{}".format(common.get_template_dir(), heat_template)
127         common.InputValidation.validate_file_exist(template, msg)
128
129         msg = 'The provided iterations variable must be an integer value'
130         common.InputValidation.validate_integer(iterations, msg)
131
132         msg = 'The provided heat_template_parameters variable must be a ' \
133               'dictionary'
134         common.InputValidation.validate_dictionary(heat_template_parameters,
135                                                    msg)
136         log_msg = "Generation of all the heat templates " \
137                   "required by the experiment"
138         common.LOG.info(log_msg)
139         heat_template_generation.generates_templates(heat_template,
140                                                      deployment_configuration)
141         benchmarking_unit = \
142             b_unit.BenchmarkingUnit(
143                 heat_template, credentials, heat_template_parameters,
144                 iterations, test_cases)
145         try:
146             common.LOG.info("Benchmarking Unit initialization")
147             benchmarking_unit.initialize()
148             common.LOG.info("Benchmarking Unit Running")
149             results = benchmarking_unit.run_benchmarks()
150         finally:
151             common.LOG.info("Benchmarking Unit Finalization")
152             benchmarking_unit.finalize()
153         return results