aboutsummaryrefslogtreecommitdiffstats
path: root/ice_validator/preload.py
blob: 8f3e0d5910b2a385802ce55b2a716392048ecc95 (plain)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# -*- 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 importlib
import inspect
import json
import os
import pkgutil
import shutil
from abc import ABC, abstractmethod
from itertools import chain
from typing import Set

from tests.helpers import (
    get_param,
    get_environment_pair,
    prop_iterator,
    get_output_dir,
    is_base_module,
)
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


# 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


def get_json_template(template_dir, template_name):
    template_name = template_name + ".json"
    with open(os.path.join(template_dir, template_name)) as f:
        return json.loads(f.read())


def remove(sequence, exclude, key=None):
    """
    Remove a copy of sequence that items occur in exclude.

    :param sequence: sequence of objects
    :param exclude:  objects to excluded (must support ``in`` check)
    :param key:      optional function to extract key from item in sequence
    :return:         list of items not in the excluded
    """
    key_func = key if key else lambda x: x
    result = (s for s in sequence if key_func(s) not in exclude)
    return set(result) if isinstance(sequence, Set) else list(result)


def get_or_create_template(template_dir, key, value, sequence, template_name):
    """
    Search a sequence of dicts where a given key matches value.  If
    found, then it returns that item.  If not, then it loads the
    template identified by template_name, adds it ot the sequence, and
    returns the template
    """
    for item in sequence:
        if item[key] == value:
            return item
    new_template = get_json_template(template_dir, template_name)
    sequence.append(new_template)
    return new_template


def replace(param):
    """
    Optionally used by the preload generator to wrap items in the preload
    that need to be replaced by end users
    :param param: p
    """
    return "VALUE FOR: {}".format(param) if param else ""


class AbstractPreloadGenerator(ABC):
    """
    All preload generators must inherit from this class and implement the
    abstract methods.

    Preload generators are automatically discovered at runtime via a plugin
    architecture.  The system path is scanned looking for modules with the name
    preload_*, then all non-abstract classes that inherit from AbstractPreloadGenerator
    are registered as preload plugins

    Attributes:
        :param vnf:             Instance of Vnf that contains the preload data
        :param base_output_dir: Base directory to house the preloads.  All preloads
                                must be written to a subdirectory under this directory
    """

    def __init__(self, vnf, base_output_dir):
        self.vnf = vnf
        self.base_output_dir = base_output_dir
        os.makedirs(self.output_dir, exist_ok=True)

    @classmethod
    @abstractmethod
    def format_name(cls):
        """
        String name to identify the format (ex: VN-API, GR-API)
        """
        raise NotImplementedError()

    @classmethod
    @abstractmethod
    def output_sub_dir(cls):
        """
        String sub-directory name that will appear under ``base_output_dir``
        """
        raise NotImplementedError()

    @classmethod
    @abstractmethod
    def supports_output_passing(cls):
        """
        Some preload methods allow automatically mapping output parameters in the
        base module to the input parameter of other modules.  This means these
        that the incremental modules do not need these base module outputs in their
        preloads.

        At this time, VNF-API does not support output parameter passing, but
        GR-API does.

        If this is true, then the generator will call Vnf#filter_output_params
        after the preload module for the base module has been created
        """
        raise NotImplementedError()

    @abstractmethod
    def generate_module(self, module):
        """
        Create the preloads and write them to ``self.output_dir``.  This
        method is responsible for generating the content of the preload and
        writing the file to disk.
        """
        raise NotImplementedError()

    @property
    def output_dir(self):
        return os.path.join(self.base_output_dir, self.output_sub_dir())

    def generate(self):
        # handle the base module first
        print("\nGenerating {} preloads".format(self.format_name()))
        self.generate_module(self.vnf.base_module)
        print("... generated template for {}".format(self.vnf.base_module))
        if self.supports_output_passing():
            self.vnf.filter_base_outputs()
        for mod in self.vnf.incremental_modules:
            self.generate_module(mod)
            print("... generated for {}".format(mod))


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 yield_by_count(sequence):
    """
    Iterates through sequence and yields each item according to its __count__
    attribute.  If an item has a __count__ of it will be returned 3 times
    before advancing to the next item in the sequence.

    :param sequence: sequence of dicts (must contain __count__)
    :returns:        generator of tuple key, value pairs
    """
    for key, value in sequence.items():
        for i in range(value["__count__"]):
            yield (key, value)


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 = 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 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)
        return {k: v for k, v in self.parameters.items() if k not in excluded}

    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)
    heat_templates = get_heat_templates(config)
    vnf = None
    for gen_class in get_generator_plugins():
        vnf = Vnf(heat_templates)
        generator = gen_class(vnf, preload_dir)
        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."
        )


def is_preload_generator(class_):
    """
    Returns True if the class is an implementation of AbstractPreloadGenerator
    """
    return (
        inspect.isclass(class_)
        and not inspect.isabstract(class_)
        and issubclass(class_, AbstractPreloadGenerator)
    )


def get_generator_plugins():
    """
    Scan the system path for modules that are preload plugins and discover
    and return the classes that implement AbstractPreloadGenerator in those
    modules
    """
    preload_plugins = (
        importlib.import_module(name)
        for finder, name, ispkg in pkgutil.iter_modules()
        if name.startswith("preload_")
    )
    members = chain.from_iterable(
        inspect.getmembers(mod, is_preload_generator) for mod in preload_plugins
    )
    return [m[1] for m in members]