1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from genericparser.pub.utils.toscaparsers.basemodel import BaseInfoModel
19 # from genericparser.pub.exceptions import CatalogException
21 logger = logging.getLogger(__name__)
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')
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"]]
30 class EtsiVnfdInfoModel(BaseInfoModel):
32 def __init__(self, path, params):
33 super(EtsiVnfdInfoModel, self).__init__(path, params)
35 def parseModel(self, tosca):
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),
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)
51 def _get_all_volume_storage(self, nodeTemplates, node_types):
53 for node in nodeTemplates:
54 if self.isNodeTypeX(node, node_types, VDU_STORAGE_TYPE):
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)
65 def _get_all_vdu(self, nodeTemplates, node_types):
68 for node in nodeTemplates:
69 logger.debug("nodeTemplates :%s", node)
70 if self.isNodeTypeX(node, node_types, VDU_COMPUTE_TYPE):
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)
103 logger.debug("rets:%s", rets)
106 def _trans_virtual_storage(self, virtual_storage):
107 if isinstance(virtual_storage, str):
108 return {"virtual_storage_id": virtual_storage}
111 ret['virtual_storage_id'] = self.get_requirement_node_name(virtual_storage)
114 def _get_linked_vl_ids(self, node, node_templates):
116 cps = self._get_virtal_binding_cps(node, node_templates)
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))
123 def _get_virtal_binding_cp_ids(self, node, nodeTemplates):
124 return map(lambda x: x['name'], self._get_virtal_binding_cps(node, nodeTemplates))
126 def _get_virtal_binding_cps(self, node, nodeTemplates):
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']:
138 def _get_all_vl(self, nodeTemplates, node_types):
140 for node in nodeTemplates:
141 if self.isNodeTypeX(node, node_types, VNF_VL_TYPE):
143 vl['vl_id'] = node['name']
144 vl['description'] = node['description']
145 vl['properties'] = node['properties']
149 def _get_all_cp(self, nodeTemplates, node_types):
151 for node in nodeTemplates:
152 if self.isNodeTypeX(node, node_types, VDU_CP_TYPE):
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)
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'))
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'))
178 def _buil_cp_vls(self, node):
179 return map(lambda x: self._build_cp_vl(x), self.getRequirementByName(node, 'virtual_link'))
181 def _build_cp_vl(self, req):
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():
192 def _get_all_endpoint_exposed(self):
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}
199 def _get_external_cps(self, 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]})
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]})
214 external_cps.append({"key_name": key, "cpd_id": value})
217 def _get_forward_cps(self, 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]})
224 forward_cps.append({"key_name": key, "cpd_id": value})
227 # def get_substitution_mappings(self, tosca):
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']
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
247 if properties.get("descriptor_verison", "") == "":
248 version = metadata.get("template_version", "")
250 version = metadata.get("version", "")
251 properties["descriptor_verison"] = version
253 if properties.get("provider", "") == "":
254 provider = metadata.get("template_author", "")
256 provider = metadata.get("provider", "")
257 properties["provider"] = provider
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