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