7b4423df501f280b9e37326e45725380485310b4
[modeling/etsicatalog.git] / genericparser / pub / utils / toscaparsers / vnfdmodel.py
1 # Copyright 2017 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 from genericparser.pub.utils.toscaparsers.basemodel import BaseInfoModel
19 # from genericparser.pub.exceptions import CatalogException
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 NFV_VNF_RELATIONSHIPS = [["tosca.relationships.nfv.VirtualLinksTo", "tosca.relationships.nfv.VduAttachesTo", "tosca.relationships.nfv.AttachesTo", "tosca.relationships.nfv.Vdu.AttachedTo", "tosca.relationships.DependsOn"],
27                          ["tosca.nodes.relationships.VirtualBindsTo", "tosca.relationships.nfv.VirtualBindsTo"]]
28
29
30 class EtsiVnfdInfoModel(BaseInfoModel):
31
32     def __init__(self, path, params):
33         super(EtsiVnfdInfoModel, self).__init__(path, params)
34
35     def parseModel(self, tosca):
36         self.vnf = {}
37         self.vnf = self._build_vnf(tosca)
38         self.metadata = self.buildMetadata(tosca)
39         self.inputs = self.buildInputs(tosca)
40         nodeTemplates = map(functools.partial(self.buildNode, tosca=tosca),
41                             tosca.nodetemplates)
42         node_types = tosca.topology_template.custom_defs
43         self.basepath = self.get_base_path(tosca)
44         self.volume_storages = self._get_all_volume_storage(nodeTemplates, node_types)
45         self.vdus = self._get_all_vdu(nodeTemplates, node_types)
46         self.vls = self._get_all_vl(nodeTemplates, node_types)
47         self.cps = self._get_all_cp(nodeTemplates, node_types)
48         self.vnf_exposed = self._get_all_endpoint_exposed()
49         self.graph = self.get_deploy_graph(tosca, NFV_VNF_RELATIONSHIPS)
50
51     def _get_all_volume_storage(self, nodeTemplates, node_types):
52         rets = []
53         for node in nodeTemplates:
54             if self.isNodeTypeX(node, node_types, VDU_STORAGE_TYPE):
55                 ret = {}
56                 ret['volume_storage_id'] = node['name']
57                 if 'description' in node:
58                     ret['description'] = node['description']
59                 ret['properties'] = node['properties']
60                 # image_file should be gotten form artifacts TODO
61                 # ret['artifacts'] = self._build_artifacts(node)
62                 rets.append(ret)
63         return rets
64
65     def _get_all_vdu(self, nodeTemplates, node_types):
66         rets = []
67         inject_files = []
68         for node in nodeTemplates:
69             logger.debug("nodeTemplates :%s", node)
70             if self.isNodeTypeX(node, node_types, VDU_COMPUTE_TYPE):
71                 ret = {}
72                 ret['vdu_id'] = node['name']
73                 ret['type'] = node['nodeType']
74                 if 'description' in node:
75                     ret['description'] = node['description']
76                 ret['properties'] = node['properties']
77                 if 'inject_files' in node['properties']:
78                     inject_files = node['properties']['inject_files']
79                 if inject_files is not None:
80                     if isinstance(inject_files, list):
81                         for inject_file in inject_files:
82                             source_path = os.path.join(self.basepath, inject_file['source_path'])
83                             with open(source_path, "rb") as f:
84                                 source_data = f.read()
85                                 source_data_base64 = source_data.encode("base64")
86                                 inject_file["source_data_base64"] = source_data_base64
87                     if isinstance(inject_files, dict):
88                         source_path = os.path.join(self.basepath, inject_files['source_path'])
89                         with open(source_path, "rb") as f:
90                             source_data = f.read()
91                             source_data_base64 = source_data.encode("base64")
92                             inject_files["source_data_base64"] = source_data_base64
93                 virtual_storages = self.getRequirementByName(node, 'virtual_storage')
94                 ret['virtual_storages'] = map(functools.partial(self._trans_virtual_storage), virtual_storages)
95                 ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node))
96                 virtual_compute = self.getCapabilityByName(node, 'virtual_compute')
97                 if virtual_compute is not None and 'properties' in virtual_compute:
98                     ret['virtual_compute'] = virtual_compute['properties']
99                 ret['vls'] = self._get_linked_vl_ids(node, nodeTemplates)
100                 ret['cps'] = self._get_virtal_binding_cp_ids(node, nodeTemplates)
101                 ret['artifacts'] = self.build_artifacts(node)
102                 rets.append(ret)
103         logger.debug("rets:%s", rets)
104         return rets
105
106     def _trans_virtual_storage(self, virtual_storage):
107         if isinstance(virtual_storage, str):
108             return {"virtual_storage_id": virtual_storage}
109         else:
110             ret = {}
111             ret['virtual_storage_id'] = self.get_requirement_node_name(virtual_storage)
112             return ret
113
114     def _get_linked_vl_ids(self, node, node_templates):
115         vl_ids = []
116         cps = self._get_virtal_binding_cps(node, node_templates)
117         for cp in cps:
118             vl_reqs = self.getRequirementByName(cp, 'virtual_link')
119             for vl_req in vl_reqs:
120                 vl_ids.append(self.get_requirement_node_name(vl_req))
121         return vl_ids
122
123     def _get_virtal_binding_cp_ids(self, node, nodeTemplates):
124         return map(lambda x: x['name'], self._get_virtal_binding_cps(node, nodeTemplates))
125
126     def _get_virtal_binding_cps(self, node, nodeTemplates):
127         cps = []
128         for tmpnode in nodeTemplates:
129             if 'requirements' in tmpnode:
130                 for item in tmpnode['requirements']:
131                     for key, value in item.items():
132                         if key.upper().startswith('VIRTUAL_BINDING'):
133                             req_node_name = self.get_requirement_node_name(value)
134                             if req_node_name is not None and req_node_name == node['name']:
135                                 cps.append(tmpnode)
136         return cps
137
138     def _get_all_vl(self, nodeTemplates, node_types):
139         vls = []
140         for node in nodeTemplates:
141             if self.isNodeTypeX(node, node_types, VNF_VL_TYPE):
142                 vl = dict()
143                 vl['vl_id'] = node['name']
144                 vl['description'] = node['description']
145                 vl['properties'] = node['properties']
146                 vls.append(vl)
147         return vls
148
149     def _get_all_cp(self, nodeTemplates, node_types):
150         cps = []
151         for node in nodeTemplates:
152             if self.isNodeTypeX(node, node_types, VDU_CP_TYPE):
153                 cp = {}
154                 cp['cp_id'] = node['name']
155                 cp['cpd_id'] = node['name']
156                 cp['description'] = node['description']
157                 cp['properties'] = node['properties']
158                 cp['vl_id'] = self._get_node_vl_id(node)
159                 cp['vdu_id'] = self._get_node_vdu_id(node)
160                 vls = self._buil_cp_vls(node)
161                 if len(vls) > 1:
162                     cp['vls'] = vls
163                 cps.append(cp)
164         return cps
165
166     def _get_node_vdu_id(self, node):
167         vdu_ids = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'virtual_binding'))
168         if len(vdu_ids) > 0:
169             return vdu_ids[0]
170         return ""
171
172     def _get_node_vl_id(self, node):
173         vl_ids = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'virtual_link'))
174         if len(vl_ids) > 0:
175             return vl_ids[0]
176         return ""
177
178     def _buil_cp_vls(self, node):
179         return map(lambda x: self._build_cp_vl(x), self.getRequirementByName(node, 'virtual_link'))
180
181     def _build_cp_vl(self, req):
182         cp_vl = {}
183         cp_vl['vl_id'] = self.get_prop_from_obj(req, 'node')
184         relationship = self.get_prop_from_obj(req, 'relationship')
185         if relationship is not None:
186             properties = self.get_prop_from_obj(relationship, 'properties')
187             if properties is not None and isinstance(properties, dict):
188                 for key, value in properties.items():
189                     cp_vl[key] = value
190         return cp_vl
191
192     def _get_all_endpoint_exposed(self):
193         if self.vnf:
194             external_cps = self._get_external_cps(self.vnf.get('requirements', None))
195             forward_cps = self._get_forward_cps(self.vnf.get('capabilities', None))
196             return {"external_cps": external_cps, "forward_cps": forward_cps}
197         return {}
198
199     def _get_external_cps(self, vnf_requirements):
200         external_cps = []
201         if vnf_requirements:
202             if isinstance(vnf_requirements, dict):
203                 for key, value in vnf_requirements.items():
204                     if isinstance(value, list) and len(value) > 0:
205                         external_cps.append({"key_name": key, "cpd_id": value[0]})
206                     else:
207                         external_cps.append({"key_name": key, "cpd_id": value})
208             elif isinstance(vnf_requirements, list):
209                 for vnf_requirement in vnf_requirements:
210                     for key, value in vnf_requirement.items():
211                         if isinstance(value, list) and len(value) > 0:
212                             external_cps.append({"key_name": key, "cpd_id": value[0]})
213                         else:
214                             external_cps.append({"key_name": key, "cpd_id": value})
215         return external_cps
216
217     def _get_forward_cps(self, vnf_capabilities):
218         forward_cps = []
219         if vnf_capabilities:
220             for key, value in vnf_capabilities.items():
221                 if isinstance(value, list) and len(value) > 0:
222                     forward_cps.append({"key_name": key, "cpd_id": value[0]})
223                 else:
224                     forward_cps.append({"key_name": key, "cpd_id": value})
225         return forward_cps
226
227     # def get_substitution_mappings(self, tosca):
228     #    node = {}
229     #    substitution_mappings = tosca.tpl['topology_template'].get('substitution_mappings', None)
230     #    if substitution_mappings:
231     #        node = substitution_mappings.get('properties', {})
232     #        node['type'] = substitution_mappings['node_type']
233     #    return node
234
235     def _build_vnf(self, tosca):
236         vnf = self.get_substitution_mappings(tosca)
237         properties = vnf.get("properties", {})
238         metadata = vnf.get("metadata", {})
239         if properties.get("descriptor_id", "") == "":
240             descriptor_id = metadata.get("descriptor_id", "")
241             if descriptor_id == "":
242                 descriptor_id = metadata.get("id", "")
243             if descriptor_id == "":
244                 descriptor_id = metadata.get("UUID", "")
245             properties["descriptor_id"] = descriptor_id
246
247         if properties.get("descriptor_verison", "") == "":
248             version = metadata.get("template_version", "")
249             if version == "":
250                 version = metadata.get("version", "")
251             properties["descriptor_verison"] = version
252
253         if properties.get("provider", "") == "":
254             provider = metadata.get("template_author", "")
255             if provider == "":
256                 provider = metadata.get("provider", "")
257             properties["provider"] = provider
258
259         if properties.get("template_name", "") == "":
260             template_name = metadata.get("template_name", "")
261             if template_name == "":
262                 template_name = metadata.get("template_name", "")
263             properties["template_name"] = template_name
264
265         return vnf