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)
42 def _get_all_services(self, nodeTemplates):
44 for node in nodeTemplates:
45 if self.isService(node):
47 service['serviceId'] = node['name']
48 if 'description' in node:
49 service['description'] = node['description']
50 service['properties'] = node['properties']
51 service['dependencies'] = map(lambda x: self.get_requirement_node_name(x),
52 self.getNodeDependencys(node))
53 service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
58 def _get_all_vcloud(self, nodeTemplates):
60 for node in nodeTemplates:
61 if self._isVcloud(node):
63 if 'vdc_name' in node['properties']:
64 ret['vdc_name'] = node['properties']['vdc_name']
67 if 'storage_clusters' in node['properties']:
68 ret['storage_clusters'] = node['properties']['storage_clusters']
70 ret['storage_clusters'] = []
75 def _isVcloud(self, node):
76 return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
78 def _get_all_vcenter(self, nodeTemplates):
80 for node in nodeTemplates:
81 if self._isVcenter(node):
83 if 'compute_clusters' in node['properties']:
84 ret['compute_clusters'] = node['properties']['compute_clusters']
86 ret['compute_clusters'] = []
87 if 'storage_clusters' in node['properties']:
88 ret['storage_clusters'] = node['properties']['storage_clusters']
90 ret['storage_clusters'] = []
91 if 'network_clusters' in node['properties']:
92 ret['network_clusters'] = node['properties']['network_clusters']
94 ret['network_clusters'] = []
99 def _isVcenter(self, node):
100 return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
102 def _get_all_image_file(self, nodeTemplates):
104 for node in nodeTemplates:
105 if self._isImageFile(node):
107 ret['image_file_id'] = node['name']
108 if 'description' in node:
109 ret['description'] = node['description']
110 ret['properties'] = node['properties']
115 def _isImageFile(self, node):
116 return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
118 def _get_all_local_storage(self, nodeTemplates):
120 for node in nodeTemplates:
121 if self._isLocalStorage(node):
123 ret['local_storage_id'] = node['name']
124 if 'description' in node:
125 ret['description'] = node['description']
126 ret['properties'] = node['properties']
131 def _isLocalStorage(self, node):
132 return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
135 def _get_all_volume_storage(self, nodeTemplates):
137 for node in nodeTemplates:
138 if self._isVolumeStorage(node):
140 ret['volume_storage_id'] = node['name']
141 if 'description' in node:
142 ret['description'] = node['description']
143 ret['properties'] = node['properties']
144 ret['image_file'] = map(lambda x: self.get_requirement_node_name(x),
145 self.getRequirementByName(node, 'image_file'))
150 def _isVolumeStorage(self, node):
151 return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
154 def _get_all_vdu(self, nodeTemplates):
156 for node in nodeTemplates:
159 ret['vdu_id'] = node['name']
160 if 'description' in node:
161 ret['description'] = node['description']
162 ret['properties'] = node['properties']
163 ret['image_file'] = self.get_node_image_file(node)
164 local_storages = self.getRequirementByName(node, 'local_storage')
165 ret['local_storages'] = map(lambda x: self.get_requirement_node_name(x), local_storages)
166 volume_storages = self.getRequirementByName(node, 'volume_storage')
167 ret['volume_storages'] = map(functools.partial(self._trans_volume_storage), volume_storages)
168 ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node))
170 nfv_compute = self.getCapabilityByName(node, 'nfv_compute')
171 if nfv_compute != None and 'properties' in nfv_compute:
172 ret['nfv_compute'] = nfv_compute['properties']
174 ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)
176 scalable = self.getCapabilityByName(node, 'scalable')
177 if scalable != None and 'properties' in scalable:
178 ret['scalable'] = scalable['properties']
180 ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
181 ret['artifacts'] = self._build_artifacts(node)
186 def get_node_image_file(self, node):
187 rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
192 def _trans_volume_storage(self, volume_storage):
193 if isinstance(volume_storage, str):
194 return {"volume_storage_id": volume_storage}
197 ret['volume_storage_id'] = self.get_requirement_node_name(volume_storage)
198 if 'relationship' in volume_storage and 'properties' in volume_storage['relationship']:
199 if 'location' in volume_storage['relationship']['properties']:
200 ret['location'] = volume_storage['relationship']['properties']['location']
201 if 'device' in volume_storage['relationship']['properties']:
202 ret['device'] = volume_storage['relationship']['properties']['device']
206 def get_linked_vl_ids(self, node, node_templates):
208 cps = self.getVirtalBindingCps(node, node_templates)
210 vl_reqs = self.getVirtualLinks(cp)
211 for vl_req in vl_reqs:
212 vl_ids.append(self.get_requirement_node_name(vl_req))
215 def _build_artifacts(self, node):
217 if 'artifacts' in node and len(node['artifacts']) > 0:
218 artifacts = node['artifacts']
219 for name, value in artifacts.items():
221 if isinstance(value, dict):
222 ret['artifact_name'] = name
223 ret['type'] = value.get('type', '')
224 ret['file'] = value.get('file', '')
225 ret['repository'] = value.get('repository', '')
226 ret['deploy_path'] = value.get('deploy_path', '')
228 ret['artifact_name'] = name
231 ret['repository'] = ''
232 ret['deploy_path'] = ''