1 # Copyright 2019 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.
20 logger = logging.getLogger(__name__)
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')
28 def __init__(self, model):
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 if properties.get("descriptor_id", "") == "":
36 descriptor_id = metadata.get("descriptor_id", "")
37 if descriptor_id == "":
38 descriptor_id = metadata.get("id", "")
39 if descriptor_id == "":
40 descriptor_id = metadata.get("UUID", "")
41 properties["descriptor_id"] = descriptor_id
43 if properties.get("descriptor_verison", "") == "":
44 version = metadata.get("template_version", "")
46 version = metadata.get("version", "")
47 properties["descriptor_verison"] = version
49 if properties.get("provider", "") == "":
50 provider = metadata.get("template_author", "")
52 provider = metadata.get("provider", "")
53 properties["provider"] = provider
55 if properties.get("template_name", "") == "":
56 template_name = metadata.get("template_name", "")
57 if template_name == "":
58 template_name = metadata.get("template_name", "")
59 properties["template_name"] = template_name
60 logger.debug("vnf:%s", vnf)
63 def get_all_vl(self, nodeTemplates, node_types):
65 for node in nodeTemplates:
66 if self.model.isNodeTypeX(node, node_types, VNF_VL_TYPE):
68 vl['vl_id'] = node['name']
69 vl['description'] = node['description']
70 vl['properties'] = node['properties']
74 def get_all_cp(self, nodeTemplates, node_types):
76 for node in nodeTemplates:
77 if self.model.isNodeTypeX(node, node_types, VDU_CP_TYPE):
79 cp['cp_id'] = node['name']
80 cp['cpd_id'] = node['name']
81 cp['description'] = node['description']
82 cp['properties'] = node['properties']
83 cp['vl_id'] = self._get_node_vl_id(node)
84 cp['vdu_id'] = self._get_node_vdu_id(node)
85 vls = self._buil_cp_vls(node)
91 def get_all_volume_storage(self, nodeTemplates, node_types):
93 for node in nodeTemplates:
94 if self.model.isNodeTypeX(node, node_types, VDU_STORAGE_TYPE):
96 ret['volume_storage_id'] = node['name']
97 if 'description' in node:
98 ret['description'] = node['description']
99 ret['properties'] = node['properties']
103 def get_all_vdu(self, nodeTemplates, node_types):
106 for node in nodeTemplates:
107 logger.debug("nodeTemplates :%s", node)
108 if self.model.isNodeTypeX(node, node_types, VDU_COMPUTE_TYPE):
110 ret['vdu_id'] = node['name']
111 ret['type'] = node['nodeType']
112 if 'description' in node:
113 ret['description'] = node['description']
114 ret['properties'] = node['properties']
115 if 'inject_files' in node['properties']:
116 inject_files = node['properties']['inject_files']
117 if inject_files is not None:
118 if isinstance(inject_files, list):
119 for inject_file in inject_files:
120 source_path = os.path.join(self.model.basepath, inject_file['source_path'])
121 with open(source_path, "rb") as f:
122 source_data = f.read()
123 source_data_base64 = source_data.encode("base64")
124 inject_file["source_data_base64"] = source_data_base64
125 if isinstance(inject_files, dict):
126 source_path = os.path.join(self.model.basepath, inject_files['source_path'])
127 with open(source_path, "rb") as f:
128 source_data = f.read()
129 source_data_base64 = source_data.encode("base64")
130 inject_files["source_data_base64"] = source_data_base64
131 virtual_storages = self.model.getRequirementByName(node, 'virtual_storage')
132 ret['virtual_storages'] = map(functools.partial(self._trans_virtual_storage), virtual_storages)
133 ret['dependencies'] = map(lambda x: self.model.get_requirement_node_name(x), self.model.getNodeDependencys(node))
134 virtual_compute = self.model.getCapabilityByName(node, 'virtual_compute')
135 if virtual_compute is not None and 'properties' in virtual_compute:
136 ret['virtual_compute'] = virtual_compute['properties']
137 ret['vls'] = self._get_linked_vl_ids(node, nodeTemplates)
138 ret['cps'] = self._get_virtal_binding_cp_ids(node, nodeTemplates)
139 ret['artifacts'] = self.model.build_artifacts(node)
141 logger.debug("rets:%s", rets)
144 def get_all_endpoint_exposed(self):
146 external_cps = self._get_external_cps(self.model.vnf.get('requirements', None))
147 forward_cps = self._get_forward_cps(self.model.vnf.get('capabilities', None))
148 return {"external_cps": external_cps, "forward_cps": forward_cps}
151 def _trans_virtual_storage(self, virtual_storage):
152 if isinstance(virtual_storage, str):
153 return {"virtual_storage_id": virtual_storage}
156 ret['virtual_storage_id'] = self.model.get_requirement_node_name(virtual_storage)
159 def _get_linked_vl_ids(self, node, node_templates):
161 cps = self._get_virtal_binding_cps(node, node_templates)
163 vl_reqs = self.model.getRequirementByName(cp, 'virtual_link')
164 for vl_req in vl_reqs:
165 vl_ids.append(self.model.get_requirement_node_name(vl_req))
168 def _get_virtal_binding_cp_ids(self, node, nodeTemplates):
169 return map(lambda x: x['name'], self._get_virtal_binding_cps(node, nodeTemplates))
171 def _get_virtal_binding_cps(self, node, nodeTemplates):
173 for tmpnode in nodeTemplates:
174 if 'requirements' in tmpnode:
175 for item in tmpnode['requirements']:
176 for key, value in item.items():
177 if key.upper().startswith('VIRTUAL_BINDING'):
178 req_node_name = self.model.get_requirement_node_name(value)
179 if req_node_name is not None and req_node_name == node['name']:
183 def _get_node_vdu_id(self, node):
184 vdu_ids = map(lambda x: self.model.get_requirement_node_name(x), self.model.getRequirementByName(node, 'virtual_binding'))
189 def _get_node_vl_id(self, node):
190 vl_ids = map(lambda x: self.model.get_requirement_node_name(x), self.model.getRequirementByName(node, 'virtual_link'))
195 def _buil_cp_vls(self, node):
196 return map(lambda x: self._build_cp_vl(x), self.model.getRequirementByName(node, 'virtual_link'))
198 def _build_cp_vl(self, req):
200 cp_vl['vl_id'] = self.model.get_prop_from_obj(req, 'node')
201 relationship = self.model.get_prop_from_obj(req, 'relationship')
202 if relationship is not None:
203 properties = self.model.get_prop_from_obj(relationship, 'properties')
204 if properties is not None and isinstance(properties, dict):
205 for key, value in properties.items():
209 def _get_external_cps(self, vnf_requirements):
212 if isinstance(vnf_requirements, dict):
213 for key, value in vnf_requirements.items():
214 if isinstance(value, list) and len(value) > 0:
215 external_cps.append({"key_name": key, "cpd_id": value[0]})
217 external_cps.append({"key_name": key, "cpd_id": value})
218 elif isinstance(vnf_requirements, list):
219 for vnf_requirement in vnf_requirements:
220 for key, value in vnf_requirement.items():
221 if isinstance(value, list) and len(value) > 0:
222 external_cps.append({"key_name": key, "cpd_id": value[0]})
224 external_cps.append({"key_name": key, "cpd_id": value})
227 def _get_forward_cps(self, vnf_capabilities):
230 for key, value in vnf_capabilities.items():
231 if isinstance(value, list) and len(value) > 0:
232 forward_cps.append({"key_name": key, "cpd_id": value[0]})
234 forward_cps.append({"key_name": key, "cpd_id": value})