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)
46 self.policies = self._get_policies(tosca.topology_template.policies)
47 self.vnf_exposed = self.get_all_endpoint_exposed(tosca.topology_template)
48 self.vnf_flavours = self.get_all_flavour(tosca.topology_template.groups)
50 def _get_all_services(self, nodeTemplates):
52 for node in nodeTemplates:
53 if self.isService(node):
55 service['serviceId'] = node['name']
56 if 'description' in node:
57 service['description'] = node['description']
58 service['properties'] = node['properties']
59 service['dependencies'] = map(lambda x: self.get_requirement_node_name(x),
60 self.getNodeDependencys(node))
61 service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
66 def _get_all_vcloud(self, nodeTemplates):
68 for node in nodeTemplates:
69 if self._isVcloud(node):
71 if 'vdc_name' in node['properties']:
72 ret['vdc_name'] = node['properties']['vdc_name']
75 if 'storage_clusters' in node['properties']:
76 ret['storage_clusters'] = node['properties']['storage_clusters']
78 ret['storage_clusters'] = []
83 def _isVcloud(self, node):
84 return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
86 def _get_all_vcenter(self, nodeTemplates):
88 for node in nodeTemplates:
89 if self._isVcenter(node):
91 if 'compute_clusters' in node['properties']:
92 ret['compute_clusters'] = node['properties']['compute_clusters']
94 ret['compute_clusters'] = []
95 if 'storage_clusters' in node['properties']:
96 ret['storage_clusters'] = node['properties']['storage_clusters']
98 ret['storage_clusters'] = []
99 if 'network_clusters' in node['properties']:
100 ret['network_clusters'] = node['properties']['network_clusters']
102 ret['network_clusters'] = []
107 def _isVcenter(self, node):
108 return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
110 def _get_all_image_file(self, nodeTemplates):
112 for node in nodeTemplates:
113 if self._isImageFile(node):
115 ret['image_file_id'] = node['name']
116 if 'description' in node:
117 ret['description'] = node['description']
118 ret['properties'] = node['properties']
123 def _isImageFile(self, node):
124 return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
126 def _get_all_local_storage(self, nodeTemplates):
128 for node in nodeTemplates:
129 if self._isLocalStorage(node):
131 ret['local_storage_id'] = node['name']
132 if 'description' in node:
133 ret['description'] = node['description']
134 ret['properties'] = node['properties']
139 def _isLocalStorage(self, node):
140 return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
143 def _get_all_volume_storage(self, nodeTemplates):
145 for node in nodeTemplates:
146 if self._isVolumeStorage(node):
148 ret['volume_storage_id'] = node['name']
149 if 'description' in node:
150 ret['description'] = node['description']
151 ret['properties'] = node['properties']
152 ret['image_file'] = map(lambda x: self.get_requirement_node_name(x),
153 self.getRequirementByName(node, 'image_file'))
158 def _isVolumeStorage(self, node):
159 return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
162 def _get_all_vdu(self, nodeTemplates):
164 for node in nodeTemplates:
167 ret['vdu_id'] = node['name']
168 if 'description' in node:
169 ret['description'] = node['description']
170 ret['properties'] = node['properties']
171 ret['image_file'] = self.get_node_image_file(node)
172 local_storages = self.getRequirementByName(node, 'local_storage')
173 ret['local_storages'] = map(lambda x: self.get_requirement_node_name(x), local_storages)
174 volume_storages = self.getRequirementByName(node, 'volume_storage')
175 ret['volume_storages'] = map(functools.partial(self._trans_volume_storage), volume_storages)
176 ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node))
178 nfv_compute = self.getCapabilityByName(node, 'nfv_compute')
179 if nfv_compute is not None and 'properties' in nfv_compute:
180 ret['nfv_compute'] = nfv_compute['properties']
182 ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)
184 scalable = self.getCapabilityByName(node, 'scalable')
185 if scalable is not None and 'properties' in scalable:
186 ret['scalable'] = scalable['properties']
188 ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
189 ret['artifacts'] = self._build_artifacts(node)
194 def get_node_image_file(self, node):
195 rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
200 def _trans_volume_storage(self, volume_storage):
201 if isinstance(volume_storage, str):
202 return {"volume_storage_id": volume_storage}
205 ret['volume_storage_id'] = self.get_requirement_node_name(volume_storage)
206 if 'relationship' in volume_storage and 'properties' in volume_storage['relationship']:
207 if 'location' in volume_storage['relationship']['properties']:
208 ret['location'] = volume_storage['relationship']['properties']['location']
209 if 'device' in volume_storage['relationship']['properties']:
210 ret['device'] = volume_storage['relationship']['properties']['device']
214 def get_linked_vl_ids(self, node, node_templates):
216 cps = self.getVirtalBindingCps(node, node_templates)
218 vl_reqs = self.getVirtualLinks(cp)
219 for vl_req in vl_reqs:
220 vl_ids.append(self.get_requirement_node_name(vl_req))
223 def _build_artifacts(self, node):
225 if 'artifacts' in node and len(node['artifacts']) > 0:
226 artifacts = node['artifacts']
227 for name, value in artifacts.items():
229 if isinstance(value, dict):
230 ret['artifact_name'] = name
231 ret['type'] = value.get('type', '')
232 ret['file'] = value.get('file', '')
233 ret['repository'] = value.get('repository', '')
234 ret['deploy_path'] = value.get('deploy_path', '')
236 ret['artifact_name'] = name
239 ret['repository'] = ''
240 ret['deploy_path'] = ''
244 def get_all_cp(self, nodeTemplates):
246 for node in nodeTemplates:
249 cp['cp_id'] = node['name']
250 cp['cpd_id'] = node['name']
251 cp['description'] = node['description']
252 cp['properties'] = node['properties']
253 cp['vl_id'] = self.get_node_vl_id(node)
254 cp['vdu_id'] = self.get_node_vdu_id(node)
255 vls = self.buil_cp_vls(node)
261 def get_all_plugin(self, node_templates):
263 for node in node_templates:
264 if self._isPlugin(node):
266 plugin['plugin_id'] = node['name']
267 plugin['description'] = node['description']
268 plugin['properties'] = node['properties']
269 if 'interfaces' in node:
270 plugin['interfaces'] = node['interfaces']
272 plugins.append(plugin)
275 def _isPlugin(self, node):
276 return node['nodeType'].lower().find('.plugin.') >= 0 or node['nodeType'].lower().endswith('.plugin')
278 def _get_all_element_group(self, groups):
281 if self._isVnfdElementGroup(group):
283 ret['group_id'] = group.name
284 ret['description'] = group.description
285 if 'properties' in group.tpl:
286 ret['properties'] = group.tpl['properties']
287 ret['members'] = group.members
291 def _isVnfdElementGroup(self, group):
292 return group.type.upper().find('.VNFDELEMENTGROUP.') >= 0 or group.type.upper().endswith('.VNFDELEMENTGROUP')
294 def _get_policies(self, top_policies):
296 scaling_policies = self.get_scaling_policies(top_policies)
297 healing_policies = self.get_healing_policies(top_policies)
298 policies.append({"scaling": scaling_policies, 'healing': healing_policies})
301 def get_healing_policies(self, top_policies):
302 return self.get_policies_by_keyword(top_policies, '.HEALING')