Update gitreview and info.yaml
[modeling/etsicatalog.git] / genericparser / pub / utils / toscaparsers / vnfdparser / vnfd_sol_base.py
1 # Copyright 2019 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import functools
16 import logging
17 import os
18 import base64
19
20
21 logger = logging.getLogger(__name__)
22
23 SECTIONS = (VDU_COMPUTE_TYPE, VNF_VL_TYPE, VDU_CP_TYPE, VDU_STORAGE_TYPE) = \
24            ('tosca.nodes.nfv.Vdu.Compute', 'tosca.nodes.nfv.VnfVirtualLink', 'tosca.nodes.nfv.VduCp', 'tosca.nodes.nfv.Vdu.VirtualStorage')
25
26
27 class VnfdSOLBase():
28
29     def __init__(self, model):
30         self.model = model
31
32     def build_vnf(self, tosca):
33         vnf = self.model.get_substitution_mappings(tosca)
34         properties = vnf.get("properties", {})
35         metadata = vnf.get("metadata", {})
36         if properties.get("descriptor_id", "") == "":
37             descriptor_id = metadata.get("descriptor_id", "")
38             if descriptor_id == "":
39                 descriptor_id = metadata.get("id", "")
40             if descriptor_id == "":
41                 descriptor_id = metadata.get("UUID", "")
42             properties["descriptor_id"] = descriptor_id
43
44         if properties.get("descriptor_version", "") == "":
45             version = metadata.get("template_version", "")
46             if version == "":
47                 version = metadata.get("version", "")
48             properties["descriptor_version"] = version
49
50         if properties.get("provider", "") == "":
51             provider = metadata.get("template_author", "")
52             if provider == "":
53                 provider = metadata.get("provider", "")
54             properties["provider"] = provider
55
56         if properties.get("template_name", "") == "":
57             template_name = metadata.get("template_name", "")
58             if template_name == "":
59                 template_name = metadata.get("template_name", "")
60             properties["template_name"] = template_name
61         logger.debug("vnf:%s", vnf)
62         return vnf
63
64     def get_all_vl(self, nodeTemplates, node_types):
65         vls = []
66         for node in nodeTemplates:
67             if self.model.isNodeTypeX(node, node_types, VNF_VL_TYPE):
68                 vl = dict()
69                 vl['vl_id'] = node['name']
70                 vl['description'] = node['description']
71                 vl['properties'] = node['properties']
72                 vls.append(vl)
73         return vls
74
75     def get_all_cp(self, nodeTemplates, node_types):
76         cps = []
77         for node in nodeTemplates:
78             if self.model.isNodeTypeX(node, node_types, VDU_CP_TYPE):
79                 cp = {}
80                 cp['cp_id'] = node['name']
81                 cp['cpd_id'] = node['name']
82                 cp['description'] = node['description']
83                 cp['properties'] = node['properties']
84                 cp['vl_id'] = self._get_node_vl_id(node)
85                 cp['vdu_id'] = self._get_node_vdu_id(node)
86                 vls = self._buil_cp_vls(node)
87                 if len(vls) > 1:
88                     cp['vls'] = vls
89                 cps.append(cp)
90         return cps
91
92     def get_all_volume_storage(self, nodeTemplates, node_types):
93         rets = []
94         for node in nodeTemplates:
95             if self.model.isNodeTypeX(node, node_types, VDU_STORAGE_TYPE):
96                 ret = {}
97                 ret['volume_storage_id'] = node['name']
98                 if 'description' in node:
99                     ret['description'] = node['description']
100                 ret['properties'] = node['properties']
101                 rets.append(ret)
102         return rets
103
104     def get_all_vdu(self, nodeTemplates, node_types):
105         rets = []
106         inject_files = []
107         for node in nodeTemplates:
108             logger.debug("nodeTemplates :%s", node)
109             if self.model.isNodeTypeX(node, node_types, VDU_COMPUTE_TYPE):
110                 ret = {}
111                 ret['vdu_id'] = node['name']
112                 ret['type'] = node['nodeType']
113                 if 'description' in node:
114                     ret['description'] = node['description']
115                 ret['properties'] = node['properties']
116                 if 'inject_files' in node['properties']:
117                     inject_files = node['properties']['inject_files']
118                 if inject_files is not None:
119                     if isinstance(inject_files, list):
120                         for inject_file in inject_files:
121                             source_path = os.path.join(self.model.basepath, inject_file['source_path'])
122                             with open(source_path, "rb") as f:
123                                 source_data = f.read()
124                                 source_data_base64 = base64.b64encode(source_data)
125                                 inject_file["source_data_base64"] = source_data_base64.decode()
126                     if isinstance(inject_files, dict):
127                         source_path = os.path.join(self.model.basepath, inject_files['source_path'])
128                         with open(source_path, "rb") as f:
129                             source_data = f.read()
130                             source_data_base64 = base64.b64encode(source_data)
131                             inject_files["source_data_base64"] = source_data_base64.decode()
132                 virtual_storages = self.model.getRequirementByName(node, 'virtual_storage')
133                 ret['virtual_storages'] = list(map(functools.partial(self._trans_virtual_storage), virtual_storages))
134                 ret['dependencies'] = [self.model.get_requirement_node_name(x) for x in self.model.getNodeDependencys(node)]
135                 virtual_compute = self.model.getCapabilityByName(node, 'virtual_compute')
136                 if virtual_compute is not None and 'properties' in virtual_compute:
137                     ret['virtual_compute'] = virtual_compute['properties']
138                 ret['vls'] = self._get_linked_vl_ids(node, nodeTemplates)
139                 ret['cps'] = self._get_virtal_binding_cp_ids(node, nodeTemplates)
140                 ret['artifacts'] = self.model.build_artifacts(node)
141                 rets.append(ret)
142         logger.debug("rets:%s", rets)
143         return rets
144
145     def get_all_endpoint_exposed(self):
146         if self.model.vnf:
147             external_cps = self._get_external_cps(self.model.vnf.get('requirements', None))
148             forward_cps = self._get_forward_cps(self.model.vnf.get('capabilities', None))
149             return {"external_cps": external_cps, "forward_cps": forward_cps}
150         return {}
151
152     def _trans_virtual_storage(self, virtual_storage):
153         if isinstance(virtual_storage, str):
154             return {"virtual_storage_id": virtual_storage}
155         else:
156             ret = {}
157             ret['virtual_storage_id'] = self.model.get_requirement_node_name(virtual_storage)
158             return ret
159
160     def _get_linked_vl_ids(self, node, node_templates):
161         vl_ids = []
162         cps = self._get_virtal_binding_cps(node, node_templates)
163         for cp in cps:
164             vl_reqs = self.model.getRequirementByName(cp, 'virtual_link')
165             for vl_req in vl_reqs:
166                 vl_ids.append(self.model.get_requirement_node_name(vl_req))
167         return vl_ids
168
169     def _get_virtal_binding_cp_ids(self, node, nodeTemplates):
170         return [x['name'] for x in self._get_virtal_binding_cps(node, nodeTemplates)]
171
172     def _get_virtal_binding_cps(self, node, nodeTemplates):
173         cps = []
174         for tmpnode in nodeTemplates:
175             if 'requirements' in tmpnode:
176                 for item in tmpnode['requirements']:
177                     for key, value in list(item.items()):
178                         if key.upper().startswith('VIRTUAL_BINDING'):
179                             req_node_name = self.model.get_requirement_node_name(value)
180                             if req_node_name is not None and req_node_name == node['name']:
181                                 cps.append(tmpnode)
182         return cps
183
184     def _get_node_vdu_id(self, node):
185         vdu_ids = [self.model.get_requirement_node_name(x) for x in self.model.getRequirementByName(node, 'virtual_binding')]
186         if len(vdu_ids) > 0:
187             return vdu_ids[0]
188         return ""
189
190     def _get_node_vl_id(self, node):
191         vl_ids = [self.model.get_requirement_node_name(x) for x in self.model.getRequirementByName(node, 'virtual_link')]
192         if len(vl_ids) > 0:
193             return vl_ids[0]
194         return ""
195
196     def _buil_cp_vls(self, node):
197         return [self._build_cp_vl(x) for x in self.model.getRequirementByName(node, 'virtual_link')]
198
199     def _build_cp_vl(self, req):
200         cp_vl = {}
201         cp_vl['vl_id'] = self.model.get_prop_from_obj(req, 'node')
202         relationship = self.model.get_prop_from_obj(req, 'relationship')
203         if relationship is not None:
204             properties = self.model.get_prop_from_obj(relationship, 'properties')
205             if properties is not None and isinstance(properties, dict):
206                 for key, value in list(properties.items()):
207                     cp_vl[key] = value
208         return cp_vl
209
210     def _get_external_cps(self, vnf_requirements):
211         external_cps = []
212         if vnf_requirements:
213             if isinstance(vnf_requirements, dict):
214                 for key, value in list(vnf_requirements.items()):
215                     if isinstance(value, list) and len(value) > 0:
216                         external_cps.append({"key_name": key, "cpd_id": value[0]})
217                     else:
218                         external_cps.append({"key_name": key, "cpd_id": value})
219             elif isinstance(vnf_requirements, list):
220                 for vnf_requirement in vnf_requirements:
221                     for key, value in list(vnf_requirement.items()):
222                         if isinstance(value, list) and len(value) > 0:
223                             external_cps.append({"key_name": key, "cpd_id": value[0]})
224                         else:
225                             external_cps.append({"key_name": key, "cpd_id": value})
226         return external_cps
227
228     def _get_forward_cps(self, vnf_capabilities):
229         forward_cps = []
230         if vnf_capabilities:
231             for key, value in list(vnf_capabilities.items()):
232                 if isinstance(value, list) and len(value) > 0:
233                     forward_cps.append({"key_name": key, "cpd_id": value[0]})
234                 else:
235                     forward_cps.append({"key_name": key, "cpd_id": value})
236         return forward_cps