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)
51 def _get_all_services(self, nodeTemplates):
53 for node in nodeTemplates:
54 if self.isService(node):
56 service['serviceId'] = node['name']
57 if 'description' in node:
58 service['description'] = node['description']
59 service['properties'] = node['properties']
60 service['dependencies'] = map(lambda x: self.get_requirement_node_name(x),
61 self.getNodeDependencys(node))
62 service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
67 def _get_all_vcloud(self, nodeTemplates):
69 for node in nodeTemplates:
70 if self._isVcloud(node):
72 if 'vdc_name' in node['properties']:
73 ret['vdc_name'] = node['properties']['vdc_name']
76 if 'storage_clusters' in node['properties']:
77 ret['storage_clusters'] = node['properties']['storage_clusters']
79 ret['storage_clusters'] = []
84 def _isVcloud(self, node):
85 return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
87 def _get_all_vcenter(self, nodeTemplates):
89 for node in nodeTemplates:
90 if self._isVcenter(node):
92 if 'compute_clusters' in node['properties']:
93 ret['compute_clusters'] = node['properties']['compute_clusters']
95 ret['compute_clusters'] = []
96 if 'storage_clusters' in node['properties']:
97 ret['storage_clusters'] = node['properties']['storage_clusters']
99 ret['storage_clusters'] = []
100 if 'network_clusters' in node['properties']:
101 ret['network_clusters'] = node['properties']['network_clusters']
103 ret['network_clusters'] = []
108 def _isVcenter(self, node):
109 return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
111 def _get_all_image_file(self, nodeTemplates):
113 for node in nodeTemplates:
114 if self._isImageFile(node):
116 ret['image_file_id'] = node['name']
117 if 'description' in node:
118 ret['description'] = node['description']
119 ret['properties'] = node['properties']
124 def _isImageFile(self, node):
125 return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
127 def _get_all_local_storage(self, nodeTemplates):
129 for node in nodeTemplates:
130 if self._isLocalStorage(node):
132 ret['local_storage_id'] = node['name']
133 if 'description' in node:
134 ret['description'] = node['description']
135 ret['properties'] = node['properties']
140 def _isLocalStorage(self, node):
141 return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
144 def _get_all_volume_storage(self, nodeTemplates):
146 for node in nodeTemplates:
147 if self._isVolumeStorage(node):
149 ret['volume_storage_id'] = node['name']
150 if 'description' in node:
151 ret['description'] = node['description']
152 ret['properties'] = node['properties']
153 ret['image_file'] = map(lambda x: self.get_requirement_node_name(x),
154 self.getRequirementByName(node, 'image_file'))
159 def _isVolumeStorage(self, node):
160 return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
163 def _get_all_vdu(self, nodeTemplates):
165 for node in nodeTemplates:
168 ret['vdu_id'] = node['name']
169 if 'description' in node:
170 ret['description'] = node['description']
171 ret['properties'] = node['properties']
172 ret['image_file'] = self.get_node_image_file(node)
173 local_storages = self.getRequirementByName(node, 'local_storage')
174 ret['local_storages'] = map(lambda x: self.get_requirement_node_name(x), local_storages)
175 volume_storages = self.getRequirementByName(node, 'volume_storage')
176 ret['volume_storages'] = map(functools.partial(self._trans_volume_storage), volume_storages)
177 ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node))
179 nfv_compute = self.getCapabilityByName(node, 'nfv_compute')
180 if nfv_compute != None and 'properties' in nfv_compute:
181 ret['nfv_compute'] = nfv_compute['properties']
183 ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)
185 scalable = self.getCapabilityByName(node, 'scalable')
186 if scalable != None and 'properties' in scalable:
187 ret['scalable'] = scalable['properties']
189 ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
190 ret['artifacts'] = self._build_artifacts(node)
195 def get_node_image_file(self, node):
196 rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
201 def _trans_volume_storage(self, volume_storage):
202 if isinstance(volume_storage, str):
203 return {"volume_storage_id": volume_storage}
206 ret['volume_storage_id'] = self.get_requirement_node_name(volume_storage)
207 if 'relationship' in volume_storage and 'properties' in volume_storage['relationship']:
208 if 'location' in volume_storage['relationship']['properties']:
209 ret['location'] = volume_storage['relationship']['properties']['location']
210 if 'device' in volume_storage['relationship']['properties']:
211 ret['device'] = volume_storage['relationship']['properties']['device']
215 def get_linked_vl_ids(self, node, node_templates):
217 cps = self.getVirtalBindingCps(node, node_templates)
219 vl_reqs = self.getVirtualLinks(cp)
220 for vl_req in vl_reqs:
221 vl_ids.append(self.get_requirement_node_name(vl_req))
224 def _build_artifacts(self, node):
226 if 'artifacts' in node and len(node['artifacts']) > 0:
227 artifacts = node['artifacts']
228 for name, value in artifacts.items():
230 if isinstance(value, dict):
231 ret['artifact_name'] = name
232 ret['type'] = value.get('type', '')
233 ret['file'] = value.get('file', '')
234 ret['repository'] = value.get('repository', '')
235 ret['deploy_path'] = value.get('deploy_path', '')
237 ret['artifact_name'] = name
240 ret['repository'] = ''
241 ret['deploy_path'] = ''
245 def get_all_cp(self, nodeTemplates):
247 for node in nodeTemplates:
250 cp['cp_id'] = node['name']
251 cp['cpd_id'] = node['name']
252 cp['description'] = node['description']
253 cp['properties'] = node['properties']
254 cp['vl_id'] = self.get_node_vl_id(node)
255 cp['vdu_id'] = self.get_node_vdu_id(node)
256 vls = self.buil_cp_vls(node)
262 def get_all_plugin(self, node_templates):
264 for node in node_templates:
265 if self._isPlugin(node):
267 plugin['plugin_id'] = node['name']
268 plugin['description'] = node['description']
269 plugin['properties'] = node['properties']
270 if 'interfaces' in node:
271 plugin['interfaces'] = node['interfaces']
273 plugins.append(plugin)
276 def _isPlugin(self, node):
277 return node['nodeType'].lower().find('.plugin.') >= 0 or node['nodeType'].lower().endswith('.plugin')
279 def _get_all_element_group(self, groups):
282 if self._isVnfdElementGroup(group):
284 ret['group_id'] = group.name
285 ret['description'] = group.description
286 if 'properties' in group.tpl:
287 ret['properties'] = group.tpl['properties']
288 ret['members'] = group.members
292 def _isVnfdElementGroup(self, group):
293 return group.type.upper().find('.VNFDELEMENTGROUP.') >= 0 or group.type.upper().endswith('.VNFDELEMENTGROUP')
295 def _get_policies(self, top_policies):
297 scaling_policies = self.get_scaling_policies(top_policies)
298 healing_policies = self.get_healing_policies(top_policies)
299 policies.append({"scaling":scaling_policies, 'healing':healing_policies})
302 def get_healing_policies(self, top_policies):
303 return self.get_policies_by_keyword(top_policies,'.HEALING')