Add parser convert vnfd element_group
[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         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
47
48     def _get_all_services(self, nodeTemplates):
49         ret = []
50         for node in nodeTemplates:
51             if self.isService(node):
52                 service = {}
53                 service['serviceId'] = node['name']
54                 if 'description' in node:
55                     service['description'] = node['description']
56                 service['properties'] = node['properties']
57                 service['dependencies'] = map(lambda x: self.get_requirement_node_name(x),
58                                               self.getNodeDependencys(node))
59                 service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
60
61                 ret.append(service)
62         return ret
63
64     def _get_all_vcloud(self, nodeTemplates):
65         rets = []
66         for node in nodeTemplates:
67             if self._isVcloud(node):
68                 ret = {}
69                 if 'vdc_name' in node['properties']:
70                     ret['vdc_name'] = node['properties']['vdc_name']
71                 else:
72                     ret['vdc_name'] = ""
73                 if 'storage_clusters' in node['properties']:
74                     ret['storage_clusters'] = node['properties']['storage_clusters']
75                 else:
76                     ret['storage_clusters'] = []
77
78                 rets.append(ret)
79         return rets
80
81     def _isVcloud(self, node):
82         return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
83
84     def _get_all_vcenter(self, nodeTemplates):
85         rets = []
86         for node in nodeTemplates:
87             if self._isVcenter(node):
88                 ret = {}
89                 if 'compute_clusters' in node['properties']:
90                     ret['compute_clusters'] = node['properties']['compute_clusters']
91                 else:
92                     ret['compute_clusters'] = []
93                 if 'storage_clusters' in node['properties']:
94                     ret['storage_clusters'] = node['properties']['storage_clusters']
95                 else:
96                     ret['storage_clusters'] = []
97                 if 'network_clusters' in node['properties']:
98                     ret['network_clusters'] = node['properties']['network_clusters']
99                 else:
100                     ret['network_clusters'] = []
101
102                 rets.append(ret)
103         return rets
104
105     def _isVcenter(self, node):
106         return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
107
108     def _get_all_image_file(self, nodeTemplates):
109         rets = []
110         for node in nodeTemplates:
111             if self._isImageFile(node):
112                 ret = {}
113                 ret['image_file_id'] = node['name']
114                 if 'description' in node:
115                     ret['description'] = node['description']
116                 ret['properties'] = node['properties']
117
118                 rets.append(ret)
119         return rets
120
121     def _isImageFile(self, node):
122         return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
123
124     def _get_all_local_storage(self, nodeTemplates):
125         rets = []
126         for node in nodeTemplates:
127             if self._isLocalStorage(node):
128                 ret = {}
129                 ret['local_storage_id'] = node['name']
130                 if 'description' in node:
131                     ret['description'] = node['description']
132                 ret['properties'] = node['properties']
133
134                 rets.append(ret)
135         return rets
136
137     def _isLocalStorage(self, node):
138         return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
139             '.LOCALSTORAGE')
140
141     def _get_all_volume_storage(self, nodeTemplates):
142         rets = []
143         for node in nodeTemplates:
144             if self._isVolumeStorage(node):
145                 ret = {}
146                 ret['volume_storage_id'] = node['name']
147                 if 'description' in node:
148                     ret['description'] = node['description']
149                 ret['properties'] = node['properties']
150                 ret['image_file'] = map(lambda x: self.get_requirement_node_name(x),
151                                         self.getRequirementByName(node, 'image_file'))
152
153                 rets.append(ret)
154         return rets
155
156     def _isVolumeStorage(self, node):
157         return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
158             '.VOLUMESTORAGE')
159
160     def _get_all_vdu(self, nodeTemplates):
161         rets = []
162         for node in nodeTemplates:
163             if self.isVdu(node):
164                 ret = {}
165                 ret['vdu_id'] = node['name']
166                 if 'description' in node:
167                     ret['description'] = node['description']
168                 ret['properties'] = node['properties']
169                 ret['image_file'] = self.get_node_image_file(node)
170                 local_storages = self.getRequirementByName(node, 'local_storage')
171                 ret['local_storages'] = map(lambda x: self.get_requirement_node_name(x), local_storages)
172                 volume_storages = self.getRequirementByName(node, 'volume_storage')
173                 ret['volume_storages'] = map(functools.partial(self._trans_volume_storage), volume_storages)
174                 ret['dependencies'] = map(lambda x: self.get_requirement_node_name(x), self.getNodeDependencys(node))
175
176                 nfv_compute = self.getCapabilityByName(node, 'nfv_compute')
177                 if nfv_compute != None and 'properties' in nfv_compute:
178                     ret['nfv_compute'] = nfv_compute['properties']
179
180                 ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)
181
182                 scalable = self.getCapabilityByName(node, 'scalable')
183                 if scalable != None and 'properties' in scalable:
184                     ret['scalable'] = scalable['properties']
185
186                 ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
187                 ret['artifacts'] = self._build_artifacts(node)
188
189                 rets.append(ret)
190         return rets
191
192     def get_node_image_file(self, node):
193         rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
194         if len(rets) > 0:
195             return rets[0]
196         return ""
197
198     def _trans_volume_storage(self, volume_storage):
199         if isinstance(volume_storage, str):
200             return {"volume_storage_id": volume_storage}
201         else:
202             ret = {}
203             ret['volume_storage_id'] = self.get_requirement_node_name(volume_storage)
204             if 'relationship' in volume_storage and 'properties' in volume_storage['relationship']:
205                 if 'location' in volume_storage['relationship']['properties']:
206                     ret['location'] = volume_storage['relationship']['properties']['location']
207                 if 'device' in volume_storage['relationship']['properties']:
208                     ret['device'] = volume_storage['relationship']['properties']['device']
209
210             return ret
211
212     def get_linked_vl_ids(self, node, node_templates):
213         vl_ids = []
214         cps = self.getVirtalBindingCps(node, node_templates)
215         for cp in cps:
216             vl_reqs = self.getVirtualLinks(cp)
217             for vl_req in vl_reqs:
218                 vl_ids.append(self.get_requirement_node_name(vl_req))
219         return vl_ids
220
221     def _build_artifacts(self, node):
222         rets = []
223         if 'artifacts' in node and len(node['artifacts']) > 0:
224             artifacts = node['artifacts']
225             for name, value in artifacts.items():
226                 ret = {}
227                 if isinstance(value, dict):
228                     ret['artifact_name'] = name
229                     ret['type'] = value.get('type', '')
230                     ret['file'] = value.get('file', '')
231                     ret['repository'] = value.get('repository', '')
232                     ret['deploy_path'] = value.get('deploy_path', '')
233                 else:
234                     ret['artifact_name'] = name
235                     ret['type'] = ''
236                     ret['file'] = value
237                     ret['repository'] = ''
238                     ret['deploy_path'] = ''
239                 rets.append(ret)
240         return rets
241
242     def get_all_cp(self, nodeTemplates):
243         cps = []
244         for node in nodeTemplates:
245             if self.isCp(node):
246                 cp = {}
247                 cp['cp_id'] = node['name']
248                 cp['cpd_id'] = node['name']
249                 cp['description'] = node['description']
250                 cp['properties'] = node['properties']
251                 cp['vl_id'] = self.get_node_vl_id(node)
252                 cp['vdu_id'] = self.get_node_vdu_id(node)
253                 vls = self.buil_cp_vls(node)
254                 if len(vls) > 1:
255                     cp['vls'] = vls
256                 cps.append(cp)
257         return cps
258
259     def get_all_plugin(self, node_templates):
260         plugins = []
261         for node in node_templates:
262             if self._isPlugin(node):
263                 plugin = {}
264                 plugin['plugin_id'] = node['name']
265                 plugin['description'] = node['description']
266                 plugin['properties'] = node['properties']
267                 if 'interfaces' in node:
268                     plugin['interfaces'] = node['interfaces']
269
270                 plugins.append(plugin)
271         return plugins
272
273     def _isPlugin(self, node):
274         return node['nodeType'].lower().find('.plugin.') >= 0 or node['nodeType'].lower().endswith('.plugin')
275
276     def _get_all_element_group(self, groups):
277         rets = []
278         for group in groups:
279             if self._isVnfdElementGroup(group):
280                 ret = {}
281                 ret['group_id'] = group.name
282                 ret['description'] = group.description
283                 if 'properties' in group.tpl:
284                     ret['properties'] = group.tpl['properties']
285                 ret['members'] = group.members
286                 rets.append(ret)
287         return rets
288
289     def _isVnfdElementGroup(self, group):
290         return group.type.upper().find('.VNFDELEMENTGROUP.') >= 0 or group.type.upper().endswith('.VNFDELEMENTGROUP')