Update gitreview and info.yaml
[modeling/etsicatalog.git] / catalog / pub / utils / toscaparser / vnfdparser / vnfd_sol_251.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 logging
16 import os
17 import base64
18
19
20 logger = logging.getLogger(__name__)
21
22 SECTIONS = (VDU_COMPUTE_TYPE, VNF_VL_TYPE, VDU_CP_TYPE, VDU_STORAGE_TYPE) = \
23            ('tosca.nodes.nfv.Vdu.Compute', 'tosca.nodes.nfv.VnfVirtualLink', 'tosca.nodes.nfv.VduCp', 'tosca.nodes.nfv.Vdu.VirtualStorage')
24
25
26 class VnfdSOL251():
27
28     def __init__(self, model):
29         self.model = model
30
31     def build_vnf(self, tosca):
32         vnf = self.model.get_substitution_mappings(tosca)
33         properties = vnf.get("properties", {})
34         metadata = vnf.get("metadata", {})
35
36         for key, value in list(properties.items()):
37             if isinstance(value, dict):
38                 if value["type"] == "string":
39                     properties[key] = value.get("default", "")
40                 elif value["type"] == "list":
41                     properties[key] = value.get("default", {})
42                 else:
43                     properties[key] = value.get("default", "")
44         ptype = "descriptor_id"
45         meta_types = ["descriptor_id", "id", "UUID"]
46         self._get_property(properties, metadata, ptype, meta_types)
47
48         ptype = "descriptor_version"
49         meta_types = ["template_version", "version"]
50         self._get_property(properties, metadata, ptype, meta_types)
51
52         ptype = "provider"
53         meta_types = ["template_author", "provider"]
54         self._get_property(properties, metadata, ptype, meta_types)
55
56         ptype = "template_name"
57         meta_types = ["template_name"]
58         self._get_property(properties, metadata, ptype, meta_types)
59
60         ptype = "software_version"
61         meta_types = ["software_version"]
62         self._get_property(properties, metadata, ptype, meta_types)
63
64         ptype = "product_name"
65         meta_types = ["product_name"]
66         self._get_property(properties, metadata, ptype, meta_types)
67
68         ptype = "flavour_description"
69         meta_types = ["flavour_description"]
70         self._get_property(properties, metadata, ptype, meta_types)
71
72         ptype = "vnfm_info"
73         meta_types = ["vnfm_info"]
74         self._get_property(properties, metadata, ptype, meta_types)
75
76         ptype = "flavour_id"
77         meta_types = ["flavour_id"]
78         self._get_property(properties, metadata, ptype, meta_types)
79
80         logger.debug("vnf:%s", vnf)
81
82         return vnf
83
84     def get_all_vl(self, nodeTemplates, node_types):
85         vls = []
86         for node in nodeTemplates:
87             if self.model.isNodeTypeX(node, node_types, VNF_VL_TYPE):
88                 vl = dict()
89                 vl['vl_id'] = node['name']
90                 vl['description'] = node['description']
91                 vl['properties'] = node['properties']
92                 vlp = vl['properties']
93                 nodep = node['properties']
94                 vlp['connectivity_type']['layer_protocol'] = nodep['connectivity_type']['layer_protocols'][0]
95                 vlp['vl_profile']['max_bit_rate_requirements'] = nodep['vl_profile']['max_bitrate_requirements']
96                 vlp['vl_profile']['min_bit_rate_requirements'] = nodep['vl_profile']['min_bitrate_requirements']
97                 if 'virtual_link_protocol_data' in nodep['vl_profile']:
98                     protocol_data = nodep['vl_profile']['virtual_link_protocol_data'][0]
99                     vlp['vl_profile']['associated_layer_protocol'] = protocol_data['associated_layer_protocol']
100                     if 'l3_protocol_data' in protocol_data:
101                         l3 = protocol_data['l3_protocol_data']
102                         vlp['vl_profile']['networkName'] = l3.get("name", "")
103                         vlp['vl_profile']['cidr'] = l3.get("cidr", "")
104                         vlp['vl_profile']['dhcpEnabled'] = l3.get("dhcp_enabled", "")
105                         vlp['vl_profile']['ip_version'] = l3.get("ip_version", "")
106                     if 'l2_protocol_data' in protocol_data:
107                         l2 = protocol_data['l2_protocol_data']
108                         vlp['vl_profile']['physicalNetwork'] = l2.get("physical_network", "")
109                 vls.append(vl)
110         return vls
111
112     def get_all_cp(self, nodeTemplates, node_types):
113         cps = []
114         for node in nodeTemplates:
115             if self.model.isNodeTypeX(node, node_types, VDU_CP_TYPE):
116                 cp = {}
117                 cp['cp_id'] = node['name']
118                 cp['cpd_id'] = node['name']
119                 cp['description'] = node['description']
120                 cp['properties'] = {}
121                 nodep = node['properties']
122                 cp['properties']['trunk_mode'] = nodep.get("trunk_mode", "")
123                 cp['properties']['layer_protocol'] = nodep.get("layer_protocols", "")
124                 if 'vnic_type' in nodep:
125                     cp['properties']['vnic_type'] = nodep.get("vnic_type", "normal")
126                 if 'virtual_network_interface_requirements' in nodep:
127                     cp['properties']['virtual_network_interface_requirements'] = nodep.get("virtual_network_interface_requirements", "")
128                 if "protocol" in nodep:
129                     node_protocol = nodep['protocol'][0]
130                     cp['properties']['protocol_data'] = nodep['protocol']
131                     cp_protocol = cp['properties']['protocol_data'][0]
132                     cp_protocol['asscociated_layer_protocol'] = node_protocol['associated_layer_protocol']
133                     if "address_data" in node_protocol:
134                         cp_protocol['address_data'] = node_protocol['address_data'][0]
135
136                 cp['vl_id'] = self._get_node_vl_id(node)
137                 cp['vdu_id'] = self._get_node_vdu_id(node)
138                 vls = self._buil_cp_vls(node)
139                 if len(vls) > 1:
140                     cp['vls'] = vls
141                 cps.append(cp)
142         return cps
143
144     def get_all_volume_storage(self, nodeTemplates, node_types):
145         rets = []
146         for node in nodeTemplates:
147             if self.model.isNodeTypeX(node, node_types, VDU_STORAGE_TYPE):
148                 ret = {}
149                 ret['volume_storage_id'] = node['name']
150                 if 'description' in node:
151                     ret['description'] = node['description']
152                 ret['properties'] = node['properties']
153                 rets.append(ret)
154         return rets
155
156     def get_all_vdu(self, nodeTemplates, node_types):
157         rets = []
158         inject_files = []
159         for node in nodeTemplates:
160             logger.debug("nodeTemplates :%s", node)
161             if self.model.isNodeTypeX(node, node_types, VDU_COMPUTE_TYPE):
162                 ret = {}
163                 ret['vdu_id'] = node['name']
164                 ret['type'] = node['nodeType']
165                 if 'description' in node:
166                     ret['description'] = node['description']
167                 ret['properties'] = node['properties']
168                 if 'boot_data' in node['properties']:
169                     ret['properties']['user_data'] = node['properties']['boot_data']
170                     del ret['properties']['boot_data']
171                 if 'inject_files' in node['properties']:
172                     inject_files = node['properties']['inject_files']
173                 if inject_files is not None:
174                     if isinstance(inject_files, list):
175                         for inject_file in inject_files:
176                             source_path = os.path.join(self.model.basepath, inject_file['source_path'])
177                             with open(source_path, "rb") as f:
178                                 source_data = f.read()
179                                 source_data_base64 = base64.b64encode(source_data)
180                                 inject_file["source_data_base64"] = source_data_base64.decode()
181                     if isinstance(inject_files, dict):
182                         source_path = os.path.join(self.model.basepath, inject_files['source_path'])
183                         with open(source_path, "rb") as f:
184                             source_data = f.read()
185                             source_data_base64 = base64.b64encode(source_data)
186                             inject_files["source_data_base64"] = source_data_base64.decode()
187                 ret['dependencies'] = [self.model.get_requirement_node_name(x) for x in self.model.getNodeDependencys(node)]
188                 virtual_compute = self.model.getCapabilityByName(node, 'virtual_compute')
189                 if virtual_compute is not None and 'properties' in virtual_compute:
190                     vc = {}
191                     vc['virtual_cpu'] = virtual_compute['properties']['virtual_cpu']
192                     vc['virtual_memory'] = virtual_compute['properties']['virtual_memory']
193                     vc['virtual_storages'] = virtual_compute['properties'].get("virtual_local_storage", {})
194                     ret['virtual_compute'] = vc
195                 ret['vls'] = self._get_linked_vl_ids(node, nodeTemplates)
196                 ret['cps'] = self._get_virtal_binding_cp_ids(node, nodeTemplates)
197                 ret['artifacts'] = self.model.build_artifacts(node)
198                 rets.append(ret)
199         logger.debug("rets:%s", rets)
200         return rets
201
202     def get_all_endpoint_exposed(self):
203         if self.model.vnf:
204             external_cps = self._get_external_cps(self.model.vnf.get('requirements', None))
205             forward_cps = self._get_forward_cps(self.model.vnf.get('capabilities', None))
206             return {"external_cps": external_cps, "forward_cps": forward_cps}
207         return {}
208
209     def _get_property(self, properties, metadata, ptype, meta_types):
210         if ptype not in properties or properties[ptype] == "":
211             for mtype in meta_types:
212                 data = metadata.get(mtype, "")
213                 if data != "":
214                     properties[ptype] = data
215
216     def _trans_virtual_storage(self, virtual_storage):
217         if isinstance(virtual_storage, str):
218             return {"virtual_storage_id": virtual_storage}
219         else:
220             ret = {}
221             ret['virtual_storage_id'] = self.model.get_requirement_node_name(virtual_storage)
222             return ret
223
224     def _get_linked_vl_ids(self, node, node_templates):
225         vl_ids = []
226         cps = self._get_virtal_binding_cps(node, node_templates)
227         for cp in cps:
228             vl_reqs = self.model.getRequirementByName(cp, 'virtual_link')
229             for vl_req in vl_reqs:
230                 vl_ids.append(self.model.get_requirement_node_name(vl_req))
231         return vl_ids
232
233     def _get_virtal_binding_cp_ids(self, node, nodeTemplates):
234         return [x['name'] for x in self._get_virtal_binding_cps(node, nodeTemplates)]
235
236     def _get_virtal_binding_cps(self, node, nodeTemplates):
237         cps = []
238         for tmpnode in nodeTemplates:
239             if 'requirements' in tmpnode:
240                 for item in tmpnode['requirements']:
241                     for key, value in list(item.items()):
242                         if key.upper().startswith('VIRTUAL_BINDING'):
243                             req_node_name = self.model.get_requirement_node_name(value)
244                             if req_node_name is not None and req_node_name == node['name']:
245                                 cps.append(tmpnode)
246         return cps
247
248     def _get_node_vdu_id(self, node):
249         vdu_ids = [self.model.get_requirement_node_name(x) for x in self.model.getRequirementByName(node, 'virtual_binding')]
250         if len(vdu_ids) > 0:
251             return vdu_ids[0]
252         return ""
253
254     def _get_node_vl_id(self, node):
255         vl_ids = [self.model.get_requirement_node_name(x) for x in self.model.getRequirementByName(node, 'virtual_link')]
256         if len(vl_ids) > 0:
257             return vl_ids[0]
258         return ""
259
260     def _buil_cp_vls(self, node):
261         return [self._build_cp_vl(x) for x in self.model.getRequirementByName(node, 'virtual_link')]
262
263     def _build_cp_vl(self, req):
264         cp_vl = {}
265         cp_vl['vl_id'] = self.model.get_prop_from_obj(req, 'node')
266         relationship = self.model.get_prop_from_obj(req, 'relationship')
267         if relationship is not None:
268             properties = self.model.get_prop_from_obj(relationship, 'properties')
269             if properties is not None and isinstance(properties, dict):
270                 for key, value in list(properties.items()):
271                     cp_vl[key] = value
272         return cp_vl
273
274     def _get_external_cps(self, vnf_requirements):
275         external_cps = []
276         if vnf_requirements:
277             if isinstance(vnf_requirements, dict):
278                 for key, value in list(vnf_requirements.items()):
279                     if isinstance(value, list) and len(value) > 0:
280                         external_cps.append({"key_name": key, "cpd_id": value[0]})
281                     else:
282                         external_cps.append({"key_name": key, "cpd_id": value})
283             elif isinstance(vnf_requirements, list):
284                 for vnf_requirement in vnf_requirements:
285                     for key, value in list(vnf_requirement.items()):
286                         if isinstance(value, list) and len(value) > 0:
287                             external_cps.append({"key_name": key, "cpd_id": value[0]})
288                         else:
289                             external_cps.append({"key_name": key, "cpd_id": value})
290         return external_cps
291
292     def _get_forward_cps(self, vnf_capabilities):
293         forward_cps = []
294         if vnf_capabilities:
295             for key, value in list(vnf_capabilities.items()):
296                 if isinstance(value, list) and len(value) > 0:
297                     forward_cps.append({"key_name": key, "cpd_id": value[0]})
298                 else:
299                     forward_cps.append({"key_name": key, "cpd_id": value})
300         return forward_cps