Merge "Update pom file of vfc-nfvo-lcm"
[vfc/nfvo/lcm.git] / lcm / pub / utils / toscaparser / vnfdmodel.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import functools
16
17 from lcm.pub.utils.toscaparser import EtsiNsdInfoModel
18
19
20 class EtsiVnfdInfoModel(EtsiNsdInfoModel):
21
22     def __init__(self, path, params):
23         super(EtsiVnfdInfoModel, self).__init__(path, params)
24
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)
29
30         nodeTemplates = map(functools.partial(self.buildNode, inputs=tosca.inputs, parsed_params=tosca.parsed_params),
31                             tosca.nodetemplates)
32
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
41
42     def _get_all_services(self, nodeTemplates):
43         ret = []
44         for node in nodeTemplates:
45             if self.isService(node):
46                 service = {}
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))
54
55                 ret.append(service)
56         return ret
57
58     def _get_all_vcloud(self, nodeTemplates):
59         rets = []
60         for node in nodeTemplates:
61             if self._isVcloud(node):
62                 ret = {}
63                 if 'vdc_name' in node['properties']:
64                     ret['vdc_name'] = node['properties']['vdc_name']
65                 else:
66                     ret['vdc_name'] = ""
67                 if 'storage_clusters' in node['properties']:
68                     ret['storage_clusters'] = node['properties']['storage_clusters']
69                 else:
70                     ret['storage_clusters'] = []
71
72                 rets.append(ret)
73         return rets
74
75     def _isVcloud(self, node):
76         return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
77
78     def _get_all_vcenter(self, nodeTemplates):
79         rets = []
80         for node in nodeTemplates:
81             if self._isVcenter(node):
82                 ret = {}
83                 if 'compute_clusters' in node['properties']:
84                     ret['compute_clusters'] = node['properties']['compute_clusters']
85                 else:
86                     ret['compute_clusters'] = []
87                 if 'storage_clusters' in node['properties']:
88                     ret['storage_clusters'] = node['properties']['storage_clusters']
89                 else:
90                     ret['storage_clusters'] = []
91                 if 'network_clusters' in node['properties']:
92                     ret['network_clusters'] = node['properties']['network_clusters']
93                 else:
94                     ret['network_clusters'] = []
95
96                 rets.append(ret)
97         return rets
98
99     def _isVcenter(self, node):
100         return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
101
102     def _get_all_image_file(self, nodeTemplates):
103         rets = []
104         for node in nodeTemplates:
105             if self._isImageFile(node):
106                 ret = {}
107                 ret['image_file_id'] = node['name']
108                 if 'description' in node:
109                     ret['description'] = node['description']
110                 ret['properties'] = node['properties']
111
112                 rets.append(ret)
113         return rets
114
115     def _isImageFile(self, node):
116         return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
117
118     def _get_all_local_storage(self, nodeTemplates):
119         rets = []
120         for node in nodeTemplates:
121             if self._isLocalStorage(node):
122                 ret = {}
123                 ret['local_storage_id'] = node['name']
124                 if 'description' in node:
125                     ret['description'] = node['description']
126                 ret['properties'] = node['properties']
127
128                 rets.append(ret)
129         return rets
130
131     def _isLocalStorage(self, node):
132         return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
133             '.LOCALSTORAGE')
134
135     def _get_all_volume_storage(self, nodeTemplates):
136         rets = []
137         for node in nodeTemplates:
138             if self._isVolumeStorage(node):
139                 ret = {}
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'))
146
147                 rets.append(ret)
148         return rets
149
150     def _isVolumeStorage(self, node):
151         return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
152             '.VOLUMESTORAGE')
153
154     def _get_all_vdu(self, nodeTemplates):
155         rets = []
156         for node in nodeTemplates:
157             if self.isVdu(node):
158                 ret = {}
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))
169
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']
173
174                 ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)
175
176                 scalable = self.getCapabilityByName(node, 'scalable')
177                 if scalable != None and 'properties' in scalable:
178                     ret['scalable'] = scalable['properties']
179
180                 ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
181                 ret['artifacts'] = self._build_artifacts(node)
182
183                 rets.append(ret)
184         return rets
185
186     def get_node_image_file(self, node):
187         rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
188         if len(rets) > 0:
189             return rets[0]
190         return ""
191
192     def _trans_volume_storage(self, volume_storage):
193         if isinstance(volume_storage, str):
194             return {"volume_storage_id": volume_storage}
195         else:
196             ret = {}
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']
203
204             return ret
205
206     def get_linked_vl_ids(self, node, node_templates):
207         vl_ids = []
208         cps = self.getVirtalBindingCps(node, node_templates)
209         for cp in cps:
210             vl_reqs = self.getVirtualLinks(cp)
211             for vl_req in vl_reqs:
212                 vl_ids.append(self.get_requirement_node_name(vl_req))
213         return vl_ids
214
215     def _build_artifacts(self, node):
216         rets = []
217         if 'artifacts' in node and len(node['artifacts']) > 0:
218             artifacts = node['artifacts']
219             for name, value in artifacts.items():
220                 ret = {}
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', '')
227                 else:
228                     ret['artifact_name'] = name
229                     ret['type'] = ''
230                     ret['file'] = value
231                     ret['repository'] = ''
232                     ret['deploy_path'] = ''
233                 rets.append(ret)
234         return rets