Adding SRIOV scenario
[apex.git] / apex / settings / deploy_settings.py
1 ##############################################################################
2 # Copyright (c) 2016 Michael Chapman (michapma@redhat.com) and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 import yaml
12
13 from apex.common import constants
14
15 REQ_DEPLOY_SETTINGS = ['sdn_controller',
16                        'odl_version',
17                        'tacker',
18                        'congress',
19                        'dataplane',
20                        'sfc',
21                        'vpn',
22                        'vpp',
23                        'ceph',
24                        'gluon',
25                        'rt_kvm',
26                        'os_version',
27                        'l2gw',
28                        'sriov']
29
30 OPT_DEPLOY_SETTINGS = ['performance',
31                        'vsperf',
32                        'ceph_device',
33                        'yardstick',
34                        'dovetail',
35                        'odl_vpp_routing_node',
36                        'dvr',
37                        'odl_vpp_netvirt',
38                        'barometer',
39                        'calipso']
40
41 VALID_ROLES = ['Controller', 'Compute', 'ObjectStorage']
42 VALID_PERF_OPTS = ['kernel', 'nova', 'vpp', 'ovs']
43 VALID_DATAPLANES = ['ovs', 'ovs_dpdk', 'fdio']
44 REQ_PATCH_CRITERIA = ['change-id', 'project']
45 OPT_PATCH_CRITERIA = ['branch']
46
47
48 class DeploySettings(dict):
49     """
50     This class parses a APEX deploy settings yaml file into an object
51     """
52     def __init__(self, filename):
53         if isinstance(filename, str):
54             with open(filename, 'r') as deploy_settings_file:
55                 init_dict = yaml.safe_load(deploy_settings_file)
56         else:
57             # assume input is a dict to build from
58             init_dict = filename
59
60         super().__init__(init_dict)
61         self._validate_settings()
62
63     def _validate_settings(self):
64         """
65         Validates the deploy settings file provided
66
67         DeploySettingsException will be raised if validation fails.
68         """
69
70         if 'deploy_options' not in self:
71             raise DeploySettingsException("No deploy options provided in"
72                                           " deploy settings file")
73         if 'global_params' not in self:
74             raise DeploySettingsException("No global options provided in"
75                                           " deploy settings file")
76
77         deploy_options = self['deploy_options']
78         if not isinstance(deploy_options, dict):
79             raise DeploySettingsException("deploy_options should be a list")
80
81         if ('gluon' in self['deploy_options'] and
82            'vpn' in self['deploy_options']):
83                 if (self['deploy_options']['gluon'] is True and
84                    self['deploy_options']['vpn'] is False):
85                         raise DeploySettingsException(
86                             "Invalid deployment configuration: "
87                             "If gluon is enabled, "
88                             "vpn also needs to be enabled")
89
90         for setting, value in deploy_options.items():
91             if setting not in REQ_DEPLOY_SETTINGS + OPT_DEPLOY_SETTINGS:
92                 raise DeploySettingsException("Invalid deploy_option {} "
93                                               "specified".format(setting))
94             if setting == 'dataplane':
95                 if value not in VALID_DATAPLANES:
96                     planes = ' '.join(VALID_DATAPLANES)
97                     raise DeploySettingsException(
98                         "Invalid dataplane {} specified. Valid dataplanes:"
99                         " {}".format(value, planes))
100
101         for req_set in REQ_DEPLOY_SETTINGS:
102             if req_set not in deploy_options:
103                 if req_set == 'dataplane':
104                     self['deploy_options'][req_set] = 'ovs'
105                 elif req_set == 'ceph':
106                     self['deploy_options'][req_set] = True
107                 elif req_set == 'odl_version':
108                     self['deploy_options'][req_set] = \
109                         constants.DEFAULT_ODL_VERSION
110                 elif req_set == 'os_version':
111                     self['deploy_options'][req_set] = \
112                         constants.DEFAULT_OS_VERSION
113                 else:
114                     self['deploy_options'][req_set] = False
115             elif req_set == 'odl_version' and self['deploy_options'][
116                     'odl_version'] not in constants.VALID_ODL_VERSIONS:
117                 raise DeploySettingsException(
118                     "Invalid ODL version: {}".format(self[deploy_options][
119                         'odl_version']))
120             elif req_set == 'sriov':
121                 if self['deploy_options'][req_set] is True:
122                     raise DeploySettingsException(
123                         "Invalid SRIOV interface name: {}".format(
124                             self['deploy_options']['sriov']))
125
126         if self['deploy_options']['odl_version'] == 'oxygen':
127             self['deploy_options']['odl_version'] = 'master'
128
129         if 'performance' in deploy_options:
130             if not isinstance(deploy_options['performance'], dict):
131                 raise DeploySettingsException("Performance deploy_option"
132                                               "must be a dictionary.")
133             for role, role_perf_sets in deploy_options['performance'].items():
134                 if role not in VALID_ROLES:
135                     raise DeploySettingsException("Performance role {}"
136                                                   "is not valid, choose"
137                                                   "from {}".format(
138                                                       role,
139                                                       " ".join(VALID_ROLES)
140                                                   ))
141
142                 for key in role_perf_sets:
143                     if key not in VALID_PERF_OPTS:
144                         raise DeploySettingsException("Performance option {} "
145                                                       "is not valid, choose"
146                                                       "from {}".format(
147                                                           key,
148                                                           " ".join(
149                                                               VALID_PERF_OPTS)
150                                                       ))
151         # validate global params
152         if 'ha_enabled' not in self['global_params']:
153
154             raise DeploySettingsException('ha_enabled is missing in global '
155                                           'parameters of deploy settings file')
156         if 'patches' not in self['global_params']:
157             self['global_params']['patches'] = dict()
158         for node in ('undercloud', 'overcloud'):
159             if node not in self['global_params']['patches']:
160                 self['global_params']['patches'][node] = list()
161             else:
162                 patches = self['global_params']['patches'][node]
163                 assert isinstance(patches, list)
164                 for patch in patches:
165                     assert isinstance(patch, dict)
166                     # Assert all required criteria exists for each patch
167                     assert all(i in patch.keys() for i in REQ_PATCH_CRITERIA)
168                     patch_criteria = REQ_PATCH_CRITERIA + OPT_PATCH_CRITERIA
169                     # Assert all patch keys are valid criteria
170                     assert all(i in patch_criteria for i in patch.keys())
171
172     def _dump_performance(self):
173         """
174         Creates performance settings string for bash consumption.
175         Output will be in the form of a list that can be iterated over in
176         bash, with each string being the direct input to the performance
177         setting script in the form <role> <category> <key> <value> to
178         facilitate modification of the correct image.
179         """
180         bash_str = 'performance_options=(\n'
181         deploy_options = self['deploy_options']
182         for role, settings in deploy_options['performance'].items():
183             for category, options in settings.items():
184                 for key, value in options.items():
185                     bash_str += "\"{} {} {} {}\"\n".format(role,
186                                                            category,
187                                                            key,
188                                                            value)
189         bash_str += ')\n'
190         bash_str += '\n'
191         bash_str += 'performance_roles=(\n'
192         for role in self['deploy_options']['performance']:
193             bash_str += role + '\n'
194         bash_str += ')\n'
195         bash_str += '\n'
196
197         return bash_str
198
199     def _dump_deploy_options_array(self):
200         """
201         Creates deploy settings array in bash syntax.
202         """
203         bash_str = ''
204         for key, value in self['deploy_options'].items():
205             if not isinstance(value, bool):
206                 bash_str += "deploy_options_array[{}]=\"{}\"\n".format(key,
207                                                                        value)
208             else:
209                 bash_str += "deploy_options_array[{}]={}\n".format(key,
210                                                                    value)
211         return bash_str
212
213
214 class DeploySettingsException(Exception):
215     def __init__(self, value):
216         self.value = value
217
218     def __str__(self):
219         return self.value