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)
45 def _get_all_services(self, nodeTemplates):
47 for node in nodeTemplates:
48 if self.isService(node):
50 service['serviceId'] = node['name']
51 if 'description' in node:
52 service['description'] = node['description']
53 service['properties'] = node['properties']
54 service['dependencies'] = map(lambda x: self.get_requirement_node_name(x),
55 self.getNodeDependencys(node))
56 service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
61 def _get_all_vcloud(self, nodeTemplates):
63 for node in nodeTemplates:
64 if self._isVcloud(node):
66 if 'vdc_name' in node['properties']:
67 ret['vdc_name'] = node['properties']['vdc_name']
70 if 'storage_clusters' in node['properties']:
71 ret['storage_clusters'] = node['properties']['storage_clusters']
73 ret['storage_clusters'] = []
78 def _isVcloud(self, node):
79 return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
81 def _get_all_vcenter(self, nodeTemplates):
83 for node in nodeTemplates:
84 if self._isVcenter(node):
86 if 'compute_clusters' in node['properties']:
87 ret['compute_clusters'] = node['properties']['compute_clusters']
89 ret['compute_clusters'] = []
90 if 'storage_clusters' in node['properties']:
91 ret['storage_clusters'] = node['properties']['storage_clusters']
93 ret['storage_clusters'] = []
94 if 'network_clusters' in node['properties']:
95 ret['network_clusters'] = node['properties']['network_clusters']
97 ret['network_clusters'] = []
102 def _isVcenter(self, node):
103 return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
105 def _get_all_image_file(self, nodeTemplates):
107 for node in nodeTemplates:
108 if self._isImageFile(node):
110 ret['image_file_id'] = node['name']
111 if 'description' in node:
112 ret['description'] = node['description']
113 ret['properties'] = node['properties']
118 def _isImageFile(self, node):
119 return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
121 def _get_all_local_storage(self, nodeTemplates):
123 for node in nodeTemplates:
124 if self._isLocalStorage(node):
126 ret['local_storage_id'] = node['name']
127 if 'description' in node:
128 ret['description'] = node['description']
129 ret['properties'] = node['properties']
134 def _isLocalStorage(self, node):
135 return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
138 def _get_all_volume_storage(self, nodeTemplates):
140 for node in nodeTemplates:
141 if self._isVolumeStorage(node):
143 ret['volume_storage_id'] = node['name']
144 if 'description' in node:
145 ret['description'] = node['description']
146 ret['properties'] = node['properties']
147 ret['image_file'] = map(lambda x: self.get_requirement_node_name(x),
148 self.getRequirementByName(node, 'image_file'))
153 def _isVolumeStorage(self, node):
154 return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
157 def _get_all_vdu(self, nodeTemplates):
159 for node in nodeTemplates:
162 ret['vdu_id'] = node['name']
163 if 'description' in node:
164 ret['description'] = node['description']
165 ret['properties'] = node['properties']
166 ret['image_file'] = self.get_node_image_file(node)
167 local_storages = self.getRequirementByName(node, 'local_storage')
168 ret['local_storages'] = map(lambda x: self.get_requirement_node_name(x), local_storages)
169 volume_storages = self.getRequirementByName(node, 'volume_storage')
170 ret['volume_storages'] = map(functools.partial(self._trans_volume_storage), volume_storages)
171 ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node))
173 nfv_compute = self.getCapabilityByName(node, 'nfv_compute')
174 if nfv_compute != None and 'properties' in nfv_compute:
175 ret['nfv_compute'] = nfv_compute['properties']
177 ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)
179 scalable = self.getCapabilityByName(node, 'scalable')
180 if scalable != None and 'properties' in scalable:
181 ret['scalable'] = scalable['properties']
183 ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
184 ret['artifacts'] = self._build_artifacts(node)
189 def get_node_image_file(self, node):
190 rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
195 def _trans_volume_storage(self, volume_storage):
196 if isinstance(volume_storage, str):
197 return {"volume_storage_id": volume_storage}
200 ret['volume_storage_id'] = self.get_requirement_node_name(volume_storage)
201 if 'relationship' in volume_storage and 'properties' in volume_storage['relationship']:
202 if 'location' in volume_storage['relationship']['properties']:
203 ret['location'] = volume_storage['relationship']['properties']['location']
204 if 'device' in volume_storage['relationship']['properties']:
205 ret['device'] = volume_storage['relationship']['properties']['device']
209 def get_linked_vl_ids(self, node, node_templates):
211 cps = self.getVirtalBindingCps(node, node_templates)
213 vl_reqs = self.getVirtualLinks(cp)
214 for vl_req in vl_reqs:
215 vl_ids.append(self.get_requirement_node_name(vl_req))
218 def _build_artifacts(self, node):
220 if 'artifacts' in node and len(node['artifacts']) > 0:
221 artifacts = node['artifacts']
222 for name, value in artifacts.items():
224 if isinstance(value, dict):
225 ret['artifact_name'] = name
226 ret['type'] = value.get('type', '')
227 ret['file'] = value.get('file', '')
228 ret['repository'] = value.get('repository', '')
229 ret['deploy_path'] = value.get('deploy_path', '')
231 ret['artifact_name'] = name
234 ret['repository'] = ''
235 ret['deploy_path'] = ''
239 def get_all_cp(self, nodeTemplates):
241 for node in nodeTemplates:
244 cp['cp_id'] = node['name']
245 cp['cpd_id'] = node['name']
246 cp['description'] = node['description']
247 cp['properties'] = node['properties']
248 cp['vl_id'] = self.get_node_vl_id(node)
249 cp['vdu_id'] = self.get_node_vdu_id(node)
250 vls = self.buil_cp_vls(node)
256 def get_all_plugin(self, node_templates):
258 for node in node_templates:
259 if self._isPlugin(node):
261 plugin['plugin_id'] = node['name']
262 plugin['description'] = node['description']
263 plugin['properties'] = node['properties']
264 if 'interfaces' in node:
265 plugin['interfaces'] = node['interfaces']
267 plugins.append(plugin)
270 def _isPlugin(self, node):
271 return node['nodeType'].lower().find('.plugin.') >= 0 or node['nodeType'].lower().endswith('.plugin')