summaryrefslogtreecommitdiffstats
path: root/catalog/pub/utils/toscaparser/vnfdmodel.py
blob: 7934a847234b13918bcd02ccefb17914c8562ac4 (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
# Copyright 2017 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file 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.

import functools
import logging
import os
from catalog.pub.utils.toscaparser import EtsiNsdInfoModel
logger = logging.getLogger(__name__)


class EtsiVnfdInfoModel(EtsiNsdInfoModel):

    def __init__(self, path, params):
        super(EtsiVnfdInfoModel, self).__init__(path, params)

    def parseModel(self, tosca):
        self.buidMetadata(tosca)
        if hasattr(tosca, 'topology_template') and hasattr(tosca.topology_template, 'inputs'):
            self.inputs = self.buildInputs(tosca.topology_template.inputs)

        nodeTemplates = map(functools.partial(self.buildNode, tosca=tosca),
                            tosca.nodetemplates)
        node_types = tosca.topology_template.custom_defs
        self.basepath = self._get_base_path(tosca)
        self.services = self._get_all_services(nodeTemplates)
        self.vcloud = self._get_all_vcloud(nodeTemplates)
        self.vcenter = self._get_all_vcenter(nodeTemplates)
        self.image_files = self._get_all_image_file(nodeTemplates)
        self.local_storages = self._get_all_local_storage(nodeTemplates)
        self.volume_storages = self._get_all_volume_storage(nodeTemplates)
        self.vdus = self._get_all_vdu(nodeTemplates, node_types)
        self.vls = self.get_all_vl(nodeTemplates, node_types)
        self.cps = self.get_all_cp(nodeTemplates, node_types)
        self.plugins = self.get_all_plugin(nodeTemplates)
        self.routers = self.get_all_router(nodeTemplates)
        self.server_groups = self.get_all_server_group(tosca.topology_template.groups)
        self.element_groups = self._get_all_element_group(tosca.topology_template.groups)
        self.policies = self._get_policies(tosca.topology_template.policies)
        self.vnf_exposed = self.get_all_endpoint_exposed(tosca.topology_template)
        self.vnf_flavours = self.get_all_flavour(tosca.topology_template.groups)

    def _get_base_path(self, tosca):
        fpath, fname = os.path.split(tosca.path)
        return fpath

    def _get_all_services(self, nodeTemplates):
        ret = []
        for node in nodeTemplates:
            if self.isService(node):
                service = {}
                service['serviceId'] = node['name']
                if 'description' in node:
                    service['description'] = node['description']
                service['properties'] = node['properties']
                service['dependencies'] = map(lambda x: self.get_requirement_node_name(x),
                                              self.getNodeDependencys(node))
                service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))

                ret.append(service)
        return ret

    def _get_all_vcloud(self, nodeTemplates):
        rets = []
        for node in nodeTemplates:
            if self._isVcloud(node):
                ret = {}
                if 'vdc_name' in node['properties']:
                    ret['vdc_name'] = node['properties']['vdc_name']
                else:
                    ret['vdc_name'] = ""
                if 'storage_clusters' in node['properties']:
                    ret['storage_clusters'] = node['properties']['storage_clusters']
                else:
                    ret['storage_clusters'] = []

                rets.append(ret)
        return rets

    def _isVcloud(self, node):
        return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')

    def _get_all_vcenter(self, nodeTemplates):
        rets = []
        for node in nodeTemplates:
            if self._isVcenter(node):
                ret = {}
                if 'compute_clusters' in node['properties']:
                    ret['compute_clusters'] = node['properties']['compute_clusters']
                else:
                    ret['compute_clusters'] = []
                if 'storage_clusters' in node['properties']:
                    ret['storage_clusters'] = node['properties']['storage_clusters']
                else:
                    ret['storage_clusters'] = []
                if 'network_clusters' in node['properties']:
                    ret['network_clusters'] = node['properties']['network_clusters']
                else:
                    ret['network_clusters'] = []

                rets.append(ret)
        return rets

    def _isVcenter(self, node):
        return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')

    def _get_all_image_file(self, nodeTemplates):
        rets = []
        for node in nodeTemplates:
            if self._isImageFile(node):
                ret = {}
                ret['image_file_id'] = node['name']
                if 'description' in node:
                    ret['description'] = node['description']
                ret['properties'] = node['properties']

                rets.append(ret)
        return rets

    def _isImageFile(self, node):
        return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')

    def _get_all_local_storage(self, nodeTemplates):
        rets = []
        for node in nodeTemplates:
            if self._isLocalStorage(node):
                ret = {}
                ret['local_storage_id'] = node['name']
                if 'description' in node:
                    ret['description'] = node['description']
                ret['properties'] = node['properties']

                rets.append(ret)
        return rets

    def _isLocalStorage(self, node):
        return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
            '.LOCALSTORAGE')

    def _get_all_volume_storage(self, nodeTemplates):
        rets = []
        for node in nodeTemplates:
            if self._isVolumeStorage(node):
                ret = {}
                ret['volume_storage_id'] = node['name']
                if 'description' in node:
                    ret['description'] = node['description']
                ret['properties'] = node['properties']
                ret['image_file'] = map(lambda x: self.get_requirement_node_name(x),
                                        self.getRequirementByName(node, 'image_file'))

                rets.append(ret)
        return rets

    def _isVolumeStorage(self, node):
        return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
            '.VOLUMESTORAGE')

    def _get_all_vdu(self, nodeTemplates, node_types):
        rets = []
        for node in nodeTemplates:
            if self.isVdu(node, node_types):
                ret = {}
                ret['vdu_id'] = node['name']
                if 'description' in node:
                    ret['description'] = node['description']
                ret['properties'] = node['properties']
                for inject_file in ret['properties']['inject_files']:
                    source_path = os.path.join(self.basepath, inject_file['source_path'])
                    with open(source_path, "rb") as f:
                        source_data = f.read()
                        source_data_base64 = source_data.encode("base64")
                        inject_file["source_data_base64"] = source_data_base64

                ret['image_file'] = self.get_node_image_file(node)
                local_storages = self.getRequirementByName(node, 'local_storage')
                ret['local_storages'] = map(lambda x: self.get_requirement_node_name(x), local_storages)
                volume_storages = self.getRequirementByName(node, 'volume_storage')
                ret['volume_storages'] = map(functools.partial(self._trans_volume_storage), volume_storages)
                ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node))

                virtual_compute = self.getCapabilityByName(node, 'virtual_compute')
                if virtual_compute is not None and 'properties' in virtual_compute:
                    ret['virtual_compute'] = virtual_compute['properties']
                virtual_storage_names = self.getRequirementByName(node, 'virtual_storage')
                virtual_storage = self.getRequirementByNodeName(nodeTemplates, virtual_storage_names[0], 'properties')
                if virtual_storage is not None:
                    ret['virtual_compute']['virtual_storage'] = virtual_storage

                ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)

                scalable = self.getCapabilityByName(node, 'scalable')
                if scalable is not None and 'properties' in scalable:
                    ret['scalable'] = scalable['properties']

                ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
                ret['artifacts'] = self._build_artifacts(node)

                rets.append(ret)
        logger.debug("rets:%s", rets)
        return rets

    def get_node_image_file(self, node):
        rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
        if len(rets) > 0:
            return rets[0]
        return ""

    def _trans_volume_storage(self, volume_storage):
        if isinstance(volume_storage, str):
            return {"volume_storage_id": volume_storage}
        else:
            ret = {}
            ret['volume_storage_id'] = self.get_requirement_node_name(volume_storage)
            if 'relationship' in volume_storage and 'properties' in volume_storage['relationship']:
                if 'location' in volume_storage['relationship']['properties']:
                    ret['location'] = volume_storage['relationship']['properties']['location']
                if 'device' in volume_storage['relationship']['properties']:
                    ret['device'] = volume_storage['relationship']['properties']['device']

            return ret

    def get_linked_vl_ids(self, node, node_templates):
        vl_ids = []
        cps = self.getVirtalBindingCps(node, node_templates)
        for cp in cps:
            vl_reqs = self.getVirtualLinks(cp)
            for vl_req in vl_reqs:
                vl_ids.append(self.get_requirement_node_name(vl_req))
        return vl_ids

    def _build_artifacts(self, node):
        rets = []
        if 'artifacts' in node and len(node['artifacts']) > 0:
            artifacts = node['artifacts']
            for name, value in artifacts.items():
                ret = {}
                if isinstance(value, dict):
                    ret['artifact_name'] = name
                    ret['type'] = value.get('type', '')
                    ret['file'] = value.get('file', '')
                    ret['repository'] = value.get('repository', '')
                    ret['deploy_path'] = value.get('deploy_path', '')
                else:
                    ret['artifact_name'] = name
                    ret['type'] = ''
                    ret['file'] = value
                    ret['repository'] = ''
                    ret['deploy_path'] = ''
                rets.append(ret)
        return rets

    def get_all_cp(self, nodeTemplates, node_types):
        cps = []
        for node in nodeTemplates:
            if self.isCp(node, node_types):
                cp = {}
                cp['cp_id'] = node['name']
                cp['cpd_id'] = node['name']
                cp['description'] = node['description']
                cp['properties'] = node['properties']
                cp['vl_id'] = self.get_node_vl_id(node)
                cp['vdu_id'] = self.get_node_vdu_id(node)
                vls = self.buil_cp_vls(node)
                if len(vls) > 1:
                    cp['vls'] = vls
                cps.append(cp)
        return cps

    def get_all_plugin(self, node_templates):
        plugins = []
        for node in node_templates:
            if self._isPlugin(node):
                plugin = {}
                plugin['plugin_id'] = node['name']
                plugin['description'] = node['description']
                plugin['properties'] = node['properties']
                if 'interfaces' in node:
                    plugin['interfaces'] = node['interfaces']

                plugins.append(plugin)
        return plugins

    def _isPlugin(self, node):
        return node['nodeType'].lower().find('.plugin.') >= 0 or node['nodeType'].lower().endswith('.plugin')

    def _get_all_element_group(self, groups):
        rets = []
        for group in groups:
            if self._isVnfdElementGroup(group):
                ret = {}
                ret['group_id'] = group.name
                ret['description'] = group.description
                if 'properties' in group.tpl:
                    ret['properties'] = group.tpl['properties']
                ret['members'] = group.members
                rets.append(ret)
        return rets

    def _isVnfdElementGroup(self, group):
        return group.type.upper().find('.VNFDELEMENTGROUP.') >= 0 or group.type.upper().endswith('.VNFDELEMENTGROUP')

    def _get_policies(self, top_policies):
        policies = []
        scaling_policies = self.get_scaling_policies(top_policies)
        healing_policies = self.get_healing_policies(top_policies)
        policies.append({"scaling": scaling_policies, 'healing': healing_policies})
        return policies

    def get_healing_policies(self, top_policies):
        return self.get_policies_by_keyword(top_policies, '.HEALING')