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.
17 from lcm.pub.utils.toscaparser import EtsiNsdInfoModel
20 class EtsiVnfdInfoModel(EtsiNsdInfoModel):
22 def __init__(self, path, params):
23 super(EtsiVnfdInfoModel, self).__init__(path, params)
25 def parseModel(self, tosca):
26 self.buidMetadata(tosca)
27 if hasattr(tosca, 'topology_template') and hasattr(tosca.topology_template, 'inputs'):
28 self.inputs = self.buildInputs(tosca.topology_template.inputs)
30 nodeTemplates = map(functools.partial(self.buildNode, inputs=tosca.inputs, parsed_params=tosca.parsed_params),
33 self.services = self._get_all_services(nodeTemplates)
34 self.vcloud = self._get_all_vcloud(nodeTemplates)
35 self.vcenter = self._get_all_vcenter(nodeTemplates)
36 self.image_files = self._get_all_image_file(nodeTemplates)
37 self.local_storages = self._get_all_local_storage(nodeTemplates)
38 self.volume_storages = self._get_all_volume_storage(nodeTemplates)
39 self.vdus = self._get_all_vdu(nodeTemplates)
40 self.vls = self.get_all_vl(nodeTemplates)
41 self.cps = self.get_all_cp(nodeTemplates)
42 self.plugins = self.get_all_plugin(nodeTemplates)
43 self.routers = self.get_all_router(nodeTemplates)
44 self.server_groups = self.get_all_server_group(tosca.topology_template.groups)
45 self.element_groups = self._get_all_element_group(tosca.topology_template.groups)
48 def _get_all_services(self, nodeTemplates):
50 for node in nodeTemplates:
51 if self.isService(node):
53 service['serviceId'] = node['name']
54 if 'description' in node:
55 service['description'] = node['description']
56 service['properties'] = node['properties']
57 service['dependencies'] = map(lambda x: self.get_requirement_node_name(x),
58 self.getNodeDependencys(node))
59 service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
64 def _get_all_vcloud(self, nodeTemplates):
66 for node in nodeTemplates:
67 if self._isVcloud(node):
69 if 'vdc_name' in node['properties']:
70 ret['vdc_name'] = node['properties']['vdc_name']
73 if 'storage_clusters' in node['properties']:
74 ret['storage_clusters'] = node['properties']['storage_clusters']
76 ret['storage_clusters'] = []
81 def _isVcloud(self, node):
82 return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
84 def _get_all_vcenter(self, nodeTemplates):
86 for node in nodeTemplates:
87 if self._isVcenter(node):
89 if 'compute_clusters' in node['properties']:
90 ret['compute_clusters'] = node['properties']['compute_clusters']
92 ret['compute_clusters'] = []
93 if 'storage_clusters' in node['properties']:
94 ret['storage_clusters'] = node['properties']['storage_clusters']
96 ret['storage_clusters'] = []
97 if 'network_clusters' in node['properties']:
98 ret['network_clusters'] = node['properties']['network_clusters']
100 ret['network_clusters'] = []
105 def _isVcenter(self, node):
106 return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
108 def _get_all_image_file(self, nodeTemplates):
110 for node in nodeTemplates:
111 if self._isImageFile(node):
113 ret['image_file_id'] = node['name']
114 if 'description' in node:
115 ret['description'] = node['description']
116 ret['properties'] = node['properties']
121 def _isImageFile(self, node):
122 return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
124 def _get_all_local_storage(self, nodeTemplates):
126 for node in nodeTemplates:
127 if self._isLocalStorage(node):
129 ret['local_storage_id'] = node['name']
130 if 'description' in node:
131 ret['description'] = node['description']
132 ret['properties'] = node['properties']
137 def _isLocalStorage(self, node):
138 return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
141 def _get_all_volume_storage(self, nodeTemplates):
143 for node in nodeTemplates:
144 if self._isVolumeStorage(node):
146 ret['volume_storage_id'] = node['name']
147 if 'description' in node:
148 ret['description'] = node['description']
149 ret['properties'] = node['properties']
150 ret['image_file'] = map(lambda x: self.get_requirement_node_name(x),
151 self.getRequirementByName(node, 'image_file'))
156 def _isVolumeStorage(self, node):
157 return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
160 def _get_all_vdu(self, nodeTemplates):
162 for node in nodeTemplates:
165 ret['vdu_id'] = node['name']
166 if 'description' in node:
167 ret['description'] = node['description']
168 ret['properties'] = node['properties']
169 ret['image_file'] = self.get_node_image_file(node)
170 local_storages = self.getRequirementByName(node, 'local_storage')
171 ret['local_storages'] = map(lambda x: self.get_requirement_node_name(x), local_storages)
172 volume_storages = self.getRequirementByName(node, 'volume_storage')
173 ret['volume_storages'] = map(functools.partial(self._trans_volume_storage), volume_storages)
174 ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node))
176 nfv_compute = self.getCapabilityByName(node, 'nfv_compute')
177 if nfv_compute != None and 'properties' in nfv_compute:
178 ret['nfv_compute'] = nfv_compute['properties']
180 ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)
182 scalable = self.getCapabilityByName(node, 'scalable')
183 if scalable != None and 'properties' in scalable:
184 ret['scalable'] = scalable['properties']
186 ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
187 ret['artifacts'] = self._build_artifacts(node)
192 def get_node_image_file(self, node):
193 rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
198 def _trans_volume_storage(self, volume_storage):
199 if isinstance(volume_storage, str):
200 return {"volume_storage_id": volume_storage}
203 ret['volume_storage_id'] = self.get_requirement_node_name(volume_storage)
204 if 'relationship' in volume_storage and 'properties' in volume_storage['relationship']:
205 if 'location' in volume_storage['relationship']['properties']:
206 ret['location'] = volume_storage['relationship']['properties']['location']
207 if 'device' in volume_storage['relationship']['properties']:
208 ret['device'] = volume_storage['relationship']['properties']['device']
212 def get_linked_vl_ids(self, node, node_templates):
214 cps = self.getVirtalBindingCps(node, node_templates)
216 vl_reqs = self.getVirtualLinks(cp)
217 for vl_req in vl_reqs:
218 vl_ids.append(self.get_requirement_node_name(vl_req))
221 def _build_artifacts(self, node):
223 if 'artifacts' in node and len(node['artifacts']) > 0:
224 artifacts = node['artifacts']
225 for name, value in artifacts.items():
227 if isinstance(value, dict):
228 ret['artifact_name'] = name
229 ret['type'] = value.get('type', '')
230 ret['file'] = value.get('file', '')
231 ret['repository'] = value.get('repository', '')
232 ret['deploy_path'] = value.get('deploy_path', '')
234 ret['artifact_name'] = name
237 ret['repository'] = ''
238 ret['deploy_path'] = ''
242 def get_all_cp(self, nodeTemplates):
244 for node in nodeTemplates:
247 cp['cp_id'] = node['name']
248 cp['cpd_id'] = node['name']
249 cp['description'] = node['description']
250 cp['properties'] = node['properties']
251 cp['vl_id'] = self.get_node_vl_id(node)
252 cp['vdu_id'] = self.get_node_vdu_id(node)
253 vls = self.buil_cp_vls(node)
259 def get_all_plugin(self, node_templates):
261 for node in node_templates:
262 if self._isPlugin(node):
264 plugin['plugin_id'] = node['name']
265 plugin['description'] = node['description']
266 plugin['properties'] = node['properties']
267 if 'interfaces' in node:
268 plugin['interfaces'] = node['interfaces']
270 plugins.append(plugin)
273 def _isPlugin(self, node):
274 return node['nodeType'].lower().find('.plugin.') >= 0 or node['nodeType'].lower().endswith('.plugin')
276 def _get_all_element_group(self, groups):
279 if self._isVnfdElementGroup(group):
281 ret['group_id'] = group.name
282 ret['description'] = group.description
283 if 'properties' in group.tpl:
284 ret['properties'] = group.tpl['properties']
285 ret['members'] = group.members
289 def _isVnfdElementGroup(self, group):
290 return group.type.upper().find('.VNFDELEMENTGROUP.') >= 0 or group.type.upper().endswith('.VNFDELEMENTGROUP')