1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
# -*- coding: utf8 -*-
# ============LICENSE_START====================================================
# org.onap.vvp/validation-scripts
# ===================================================================
# Copyright © 2019 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
# https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
import os
import shutil
from abc import ABC, abstractmethod
from collections import OrderedDict
from preload.generator import yield_by_count
from preload.environment import PreloadEnvironment
from tests.helpers import (
get_param,
get_environment_pair,
prop_iterator,
get_output_dir,
is_base_module,
remove,
)
from tests.parametrizers import parametrize_heat_templates
from tests.structures import NeutronPortProcessor, Heat
from tests.test_environment_file_parameters import get_preload_excluded_parameters
from tests.utils import nested_dict
from tests.utils.vm_types import get_vm_type_for_nova_server
from config import Config, get_generator_plugins
from tests.test_environment_file_parameters import ENV_PARAMETER_SPEC
CHANGE = "CHANGEME"
# This is only used to fake out parametrizers
class DummyMetafunc:
def __init__(self, config):
self.inputs = {}
self.config = config
def parametrize(self, name, file_list):
self.inputs[name] = file_list
def get_heat_templates(config):
"""
Returns the Heat template paths discovered by the pytest parameterizers
:param config: pytest config
:return: list of heat template paths
"""
meta = DummyMetafunc(config)
parametrize_heat_templates(meta)
heat_templates = meta.inputs.get("heat_templates", [])
if isinstance(heat_templates, list) and len(heat_templates) > 0:
heat_templates = heat_templates[0]
else:
return
return heat_templates
class FilterBaseOutputs(ABC):
"""
Invoked to remove parameters in an object that appear in the base module.
Base output parameters can be passed to incremental modules
so they do not need to be defined in a preload. This method can be
invoked on a module to pre-filter the parameters before a preload is
created.
The method should remove the parameters that exist in the base module from
both itself and any sub-objects.
"""
@abstractmethod
def filter_output_params(self, base_outputs):
raise NotImplementedError()
class IpParam:
def __init__(self, ip_addr_param, port):
self.param = ip_addr_param or ""
self.port = port
@property
def ip_version(self):
return 6 if "_v6_" in self.param else 4
def __hash__(self):
return hash(self.param)
def __eq__(self, other):
return hash(self) == hash(other)
def __str__(self):
return "{}(v{})".format(self.param, self.ip_version)
def __repr(self):
return str(self)
class Network(FilterBaseOutputs):
def __init__(self, role, name_param):
self.network_role = role
self.name_param = name_param
self.subnet_params = set()
def filter_output_params(self, base_outputs):
self.subnet_params = remove(self.subnet_params, base_outputs)
def __hash__(self):
return hash(self.network_role)
def __eq__(self, other):
return hash(self) == hash(other)
class Port(FilterBaseOutputs):
def __init__(self, vm, network):
self.vm = vm
self.network = network
self.fixed_ips = []
self.floating_ips = []
self.uses_dhcp = True
def add_ips(self, props):
props = props.get("properties") or props
for fixed_ip in props.get("fixed_ips") or []:
if not isinstance(fixed_ip, dict):
continue
ip_address = get_param(fixed_ip.get("ip_address"))
subnet = get_param(fixed_ip.get("subnet") or fixed_ip.get("subnet_id"))
if ip_address:
self.uses_dhcp = False
self.fixed_ips.append(IpParam(ip_address, self))
if subnet:
self.network.subnet_params.add(subnet)
for ip in prop_iterator(props, "allowed_address_pairs", "ip_address"):
self.uses_dhcp = False
param = get_param(ip) if ip else ""
if param:
self.floating_ips.append(IpParam(param, self))
def filter_output_params(self, base_outputs):
self.fixed_ips = remove(self.fixed_ips, base_outputs, key=lambda ip: ip.param)
self.floating_ips = remove(
self.floating_ips, base_outputs, key=lambda ip: ip.param
)
class VirtualMachineType(FilterBaseOutputs):
def __init__(self, vm_type, vnf_module):
self.vm_type = vm_type
self.names = []
self.ports = []
self.vm_count = 0
self.vnf_module = vnf_module
def filter_output_params(self, base_outputs):
self.names = remove(self.names, base_outputs)
for port in self.ports:
port.filter_output_params(base_outputs)
@property
def networks(self):
return {port.network for port in self.ports}
@property
def floating_ips(self):
for port in self.ports:
for ip in port.floating_ips:
yield ip
@property
def fixed_ips(self):
for port in self.ports:
for ip in port.fixed_ips:
yield ip
def update_ports(self, network, props):
port = self.get_or_create_port(network)
port.add_ips(props)
def get_or_create_port(self, network):
for port in self.ports:
if port.network == network:
return port
port = Port(self, network)
self.ports.append(port)
return port
class Vnf:
def __init__(self, templates):
self.modules = [VnfModule(t, self) for t in templates]
self.uses_contrail = self._uses_contrail()
self.base_module = next(
(mod for mod in self.modules if mod.is_base_module), None
)
self.incremental_modules = [m for m in self.modules if not m.is_base_module]
def _uses_contrail(self):
for mod in self.modules:
resources = mod.heat.get_all_resources()
types = (r.get("type", "") for r in resources.values())
if any(t.startswith("OS::ContrailV2") for t in types):
return True
return False
@property
def base_output_params(self):
return self.base_module.heat.outputs
def filter_base_outputs(self):
non_base_modules = (m for m in self.modules if not m.is_base_module)
for mod in non_base_modules:
mod.filter_output_params(self.base_output_params)
def env_path(heat_path):
"""
Create the path to the env file for the give heat path.
:param heat_path: path to heat file
:return: path to env file (assumes it is present and named correctly)
"""
base_path = os.path.splitext(heat_path)[0]
return "{}.env".format(base_path)
class VnfModule(FilterBaseOutputs):
def __init__(self, template_file, vnf):
self.vnf = vnf
self.vnf_name = os.path.splitext(os.path.basename(template_file))[0]
self.template_file = template_file
self.heat = Heat(filepath=template_file, envpath=env_path(template_file))
env_pair = get_environment_pair(self.template_file)
env_yaml = env_pair.get("eyml") if env_pair else {}
self.parameters = {key: "" for key in self.heat.parameters}
self.parameters.update(env_yaml.get("parameters") or {})
self.networks = []
self.virtual_machine_types = self._create_vm_types()
self._add_networks()
self.outputs_filtered = False
def filter_output_params(self, base_outputs):
for vm in self.virtual_machine_types:
vm.filter_output_params(base_outputs)
for network in self.networks:
network.filter_output_params(base_outputs)
self.parameters = {
k: v for k, v in self.parameters.items() if k not in base_outputs
}
self.networks = [
network
for network in self.networks
if network.name_param not in base_outputs or network.subnet_params
]
self.outputs_filtered = True
def _create_vm_types(self):
servers = self.heat.get_resource_by_type("OS::Nova::Server", all_resources=True)
vm_types = {}
for _, props in yield_by_count(servers):
vm_type = get_vm_type_for_nova_server(props)
vm = vm_types.setdefault(vm_type, VirtualMachineType(vm_type, self))
vm.vm_count += 1
name = nested_dict.get(props, "properties", "name", default={})
vm_name = get_param(name) if name else ""
vm.names.append(vm_name)
return list(vm_types.values())
def _add_networks(self):
ports = self.heat.get_resource_by_type("OS::Neutron::Port", all_resources=True)
for rid, props in yield_by_count(ports):
resource_type, port_match = NeutronPortProcessor.get_rid_match_tuple(rid)
if resource_type != "external":
continue
network_role = port_match.group("network_role")
vm = self._get_vm_type(port_match.group("vm_type"))
network = self._get_network(network_role, props)
vm.update_ports(network, props)
@property
def is_base_module(self):
return is_base_module(self.template_file)
@property
def availability_zones(self):
"""Returns a list of all availability zone parameters found in the template"""
return sorted(
p for p in self.heat.parameters if p.startswith("availability_zone")
)
@property
def label(self):
"""
Label for the VF module that will appear in the CSAR
"""
return self.vnf_name
@property
def env_specs(self):
"""Return available Environment Spec definitions"""
try:
return Config().env_specs
except FileNotFoundError:
return [ENV_PARAMETER_SPEC]
@property
def platform_provided_params(self):
result = set()
for spec in self.env_specs:
for props in spec["PLATFORM PROVIDED"]:
result.add(props["property"][-1])
return result
@property
def env_template(self):
"""
Returns a a template .env file that can be completed to enable
preload generation.
"""
params = OrderedDict()
params["vnf-type"] = CHANGE
params["vf-module-model-name"] = CHANGE
params["vf_module_name"] = CHANGE
for az in self.availability_zones:
params[az] = CHANGE
for network in self.networks:
params[network.name_param] = CHANGE
for param in set(network.subnet_params):
params[param] = CHANGE
for vm in self.virtual_machine_types:
for name in set(vm.names):
params[name] = CHANGE
for ip in vm.floating_ips:
params[ip.param] = CHANGE
for ip in vm.fixed_ips:
params[ip.param] = CHANGE
excluded = get_preload_excluded_parameters(
self.template_file, persistent_only=True
)
excluded.update(self.platform_provided_params)
for name, value in self.parameters.items():
if name in excluded:
continue
params[name] = value if value else CHANGE
return {"parameters": params}
@property
def preload_parameters(self):
"""
Subset of parameters from the env file that can be overridden in
tag values. Per VNF Heat Guidelines, specific parameters such as
flavor, image, etc. must not be overridden so they are excluded.
:return: dict of parameters suitable for the preload
"""
excluded = get_preload_excluded_parameters(self.template_file)
params = {k: v for k, v in self.parameters.items() if k not in excluded}
return params
def _get_vm_type(self, vm_type):
for vm in self.virtual_machine_types:
if vm_type.lower() == vm.vm_type.lower():
return vm
raise RuntimeError("Encountered unknown VM type: {}".format(vm_type))
def _get_network(self, network_role, props):
network_prop = nested_dict.get(props, "properties", "network") or {}
name_param = get_param(network_prop) if network_prop else ""
for network in self.networks:
if network.network_role.lower() == network_role.lower():
return network
new_network = Network(network_role, name_param)
self.networks.append(new_network)
return new_network
def __str__(self):
return "VNF Module ({})".format(os.path.basename(self.template_file))
def __repr__(self):
return str(self)
def __hash__(self):
return hash(self.vnf_name)
def __eq__(self, other):
return hash(self) == hash(other)
def create_preloads(config, exitstatus):
"""
Create preloads in every format that can be discovered by get_generator_plugins
"""
if config.getoption("self_test"):
return
print("+===================================================================+")
print("| Preload Template Generation |")
print("+===================================================================+")
preload_dir = os.path.join(get_output_dir(config), "preloads")
if os.path.exists(preload_dir):
shutil.rmtree(preload_dir)
env_directory = config.getoption("env_dir")
preload_env = PreloadEnvironment(env_directory) if env_directory else None
plugins = get_generator_plugins()
available_formats = [p.format_name() for p in plugins]
selected_formats = config.getoption("preload_formats") or available_formats
heat_templates = get_heat_templates(config)
vnf = None
for plugin_class in plugins:
if plugin_class.format_name() not in selected_formats:
continue
vnf = Vnf(heat_templates)
generator = plugin_class(vnf, preload_dir, preload_env)
generator.generate()
if vnf and vnf.uses_contrail:
print(
"\nWARNING: Preload template generation does not support Contrail\n"
"at this time, but Contrail resources were detected. The preload \n"
"template may be incomplete."
)
if exitstatus != 0:
print(
"\nWARNING: Heat violations detected. Preload templates may be\n"
"incomplete."
)
|