aboutsummaryrefslogtreecommitdiffstats
path: root/ice_validator/preload/environment.py
blob: 083be9bcf0531b42dd77b315f310fde1eed4e6c4 (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
import re
import tempfile
from pathlib import Path

from cached_property import cached_property

from tests.helpers import check, first, unzip, load_yaml

SERVICE_TEMPLATE_PATTERN = re.compile(r".*service-.*?-template.yml")
RESOURCE_TEMPLATE_PATTERN = re.compile(r".*resource-(.*?)-template.yml")


def yaml_files(path):
    """
    Return files that are YAML (end with .yml or .yaml)

    :param path: Directory path object
    :return: list of paths to YAML files
    """
    return [
        p
        for p in path.iterdir()
        if p.is_file() and p.suffix.lower() in (".yml", ".yaml")
    ]


class CloudServiceArchive:
    """
    Wrapper to extract information from a CSAR file.
    """

    def __init__(self, csar_path):
        self.csar_path = Path(csar_path)
        with tempfile.TemporaryDirectory() as csar_dir:
            csar_dir = Path(csar_dir)
            unzip(self.csar_path, csar_dir)
            self._service = self._get_service_template(csar_dir)
            self._resources = self._get_vf_module_resource_templates(csar_dir)

    def get_vf_module(self, vf_module):
        """
        Retrieve the VF Module definition from the CSAR for the given heat
        module name (should not include the file extension - ex: base)

        :param vf_module: name of Heat module (no path or file extension)
        :return: The definition of the module as a dict or None if not found
        """
        groups = self._service.get("topology_template", {}).get("groups", {})
        for props in groups.values():
            module_label = props.get("properties", {}).get("vf_module_label", "")
            if module_label.lower() == vf_module.lower():
                return props
        return None

    def get_vf_module_model_name(self, vf_module):
        """
        Retrieves the vfModuleModelName of the module or None if vf_module is not
        found (see get_vf_module)

        :param vf_module: name of Heat module (no path or file extension)
        :return: The value if vfModuleModelName as string or None if not found
        """
        module = self.get_vf_module(vf_module)
        return module.get("metadata", {}).get("vfModuleModelName") if module else None

    @property
    def topology_template(self):
        """
        Return dict representing the topology_template node of the service
        template
        """
        return self._service.get("topology_template") or {}

    @property
    def groups(self):
        """
        Return dict representing the groups node of the service
        template
        """
        return self.topology_template.get("groups") or {}

    @property
    def vf_modules(self):
        """
        Returns mapping of group ID to VfModule present in the service template
        """
        return {
            group_id: props
            for group_id, props in self.groups.items()
            if props.get("type") == "org.openecomp.groups.VfModule"
        }

    def get_vnf_type(self, module):
        """
        Concatenation of service and VF instance name
        """
        service_name = self.service_name
        instance_name = self.get_vf_module_resource_name(module)
        if service_name and instance_name:
            return "{}/{}".format(service_name, instance_name)

    @property
    def vf_module_resource_names(self):
        """
        Returns the resource names for all VfModules (these can be used
        to find the resource templates as they will be part of the filename)
        """
        names = (
            module.get("metadata", {}).get("vfModuleModelName")
            for module in self.vf_modules.values()
        )
        return [name.split(".")[0] for name in names if name]

    def get_vf_module_resource_name(self, vf_module):
        """
        Retrieves the resource name of the module or None if vf_module is not
        found (see get_vf_module)

        :param vf_module: name of Heat module (no path or file extension)
        :return: The value if resource nae as string or None if not found
        """
        vf_model_name = self.get_vf_module_model_name(vf_module)
        if not vf_model_name:
            return None
        resource_name = vf_model_name.split(".")[0]
        resource = self._resources.get(resource_name, {})
        return resource.get("metadata", {}).get("name")

    @staticmethod
    def _get_definition_files(csar_dir):
        """
        Returns a list of all files in the CSAR's Definitions directory
        """
        def_dir = csar_dir / "Definitions"
        check(
            def_dir.exists(),
            "CSAR is invalid. {} does not contain a Definitions directory.".format(
                csar_dir.as_posix()
            ),
        )
        return yaml_files(def_dir)

    def _get_service_template(self, csar_dir):
        """
        Returns the service template as a dict.  Assumes there is only one.
        """
        files = map(str, self._get_definition_files(csar_dir))
        service_template = first(files, SERVICE_TEMPLATE_PATTERN.match)
        return load_yaml(service_template) if service_template else {}

    def _get_vf_module_resource_templates(self, csar_dir):
        """
        Returns a mapping of resource name to resource definition (as a dict)
        (Only loads resource templates that correspond to VF Modules
        """
        def_dir = csar_dir / "Definitions"
        mapping = (
            (name, def_dir / "resource-{}-template.yml".format(name))
            for name in self.vf_module_resource_names
        )
        return {name: load_yaml(path) for name, path in mapping if path.exists()}

    @property
    def service_name(self):
        """
        Name of the service (extracted from the service template
        """
        return self._service.get("metadata", {}).get("name")

    def __repr__(self):
        return "CSAR (path={}, name={})".format(self.csar_path.name, self.service_name)

    def __str__(self):
        return repr(self)


class PreloadEnvironment:
    def __init__(self, env_dir, parent=None):
        self.base_dir = Path(env_dir)
        self.parent = parent
        self._modules = self._load_modules()
        self._sub_env = self._load_envs()
        self._defaults = self._load_defaults()

    def _load_defaults(self):
        defaults = self.base_dir / "defaults.yaml"
        return load_yaml(defaults) if defaults.exists() else {}

    def _load_modules(self):
        files = [
            p
            for p in self.base_dir.iterdir()
            if p.is_file() and p.suffix.lower().endswith(".env")
        ]
        return {f.name.lower(): load_yaml(f).get("parameters", {}) for f in files}

    def _load_envs(self):
        env_dirs = [
            p for p in self.base_dir.iterdir() if p.is_dir() and p.name != "preloads"
        ]
        return {d.name: PreloadEnvironment(d, self) for d in env_dirs}

    @cached_property
    def csar(self):
        csar_path = first(self.base_dir.iterdir(), lambda p: p.suffix == ".csar")
        if csar_path:
            return CloudServiceArchive(csar_path)
        else:
            return self.parent.csar if self.parent else None

    @property
    def defaults(self):
        result = {}
        if self.parent:
            result.update(self.parent.defaults)
        result.update(self._defaults)
        return result

    @property
    def environments(self):
        all_envs = [self]
        for env in self._sub_env.values():
            all_envs.append(env)
            all_envs.extend(env.environments)
        return [e for e in all_envs if e.is_leaf]

    def get_module(self, name):
        name = name if name.lower().endswith(".env") else "{}.env".format(name).lower()
        if name not in self.module_names:
            return {}
        result = {}
        parent_module = self.parent.get_module(name) if self.parent else None
        module = self._modules.get(name)
        for m in (parent_module, self.defaults, module):
            if m:
                result.update(m)
        if self.csar:
            vnf_type = self.csar.get_vnf_type(name)
            if vnf_type:
                result["vnf-type"] = vnf_type
            model_name = self.csar.get_vf_module_model_name(name)
            if model_name:
                result["vf-module-model-name"] = model_name
        return result

    @property
    def module_names(self):
        parent_modules = self.parent.module_names if self.parent else set()
        result = set()
        result.update(self._modules.keys())
        result.update(parent_modules)
        return result

    @property
    def modules(self):
        return {name: self.get_module(name) for name in self.module_names}

    def get_environment(self, env_name):
        for name, env in self._sub_env.items():
            if name == env_name:
                return env
            result = env.get_environment(env_name)
            if result:
                return result
        return None

    @property
    def is_base(self):
        return self.parent is None

    @property
    def is_leaf(self):
        return not self._sub_env

    @property
    def name(self):
        return self.base_dir.name

    def __repr__(self):
        return "PreloadEnvironment(name={})".format(self.name)