Merge "Fix log file name 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         self.vls = self.get_all_vl(nodeTemplates)
41         self.cps = self.get_all_cp(nodeTemplates)
42         self.plugins = self.get_all_plugin(nodeTemplates)
43
44
45     def _get_all_services(self, nodeTemplates):
46         ret = []
47         for node in nodeTemplates:
48             if self.isService(node):
49                 service = {}
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))
57
58                 ret.append(service)
59         return ret
60
61     def _get_all_vcloud(self, nodeTemplates):
62         rets = []
63         for node in nodeTemplates:
64             if self._isVcloud(node):
65                 ret = {}
66                 if 'vdc_name' in node['properties']:
67                     ret['vdc_name'] = node['properties']['vdc_name']
68                 else:
69                     ret['vdc_name'] = ""
70                 if 'storage_clusters' in node['properties']:
71                     ret['storage_clusters'] = node['properties']['storage_clusters']
72                 else:
73                     ret['storage_clusters'] = []
74
75                 rets.append(ret)
76         return rets
77
78     def _isVcloud(self, node):
79         return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
80
81     def _get_all_vcenter(self, nodeTemplates):
82         rets = []
83         for node in nodeTemplates:
84             if self._isVcenter(node):
85                 ret = {}
86                 if 'compute_clusters' in node['properties']:
87                     ret['compute_clusters'] = node['properties']['compute_clusters']
88                 else:
89                     ret['compute_clusters'] = []
90                 if 'storage_clusters' in node['properties']:
91                     ret['storage_clusters'] = node['properties']['storage_clusters']
92                 else:
93                     ret['storage_clusters'] = []
94                 if 'network_clusters' in node['properties']:
95                     ret['network_clusters'] = node['properties']['network_clusters']
96                 else:
97                     ret['network_clusters'] = []
98
99                 rets.append(ret)
100         return rets
101
102     def _isVcenter(self, node):
103         return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
104
105     def _get_all_image_file(self, nodeTemplates):
106         rets = []
107         for node in nodeTemplates:
108             if self._isImageFile(node):
109                 ret = {}
110                 ret['image_file_id'] = node['name']
111                 if 'description' in node:
112                     ret['description'] = node['description']
113                 ret['properties'] = node['properties']
114
115                 rets.append(ret)
116         return rets
117
118     def _isImageFile(self, node):
119         return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
120
121     def _get_all_local_storage(self, nodeTemplates):
122         rets = []
123         for node in nodeTemplates:
124             if self._isLocalStorage(node):
125                 ret = {}
126                 ret['local_storage_id'] = node['name']
127                 if 'description' in node:
128                     ret['description'] = node['description']
129                 ret['properties'] = node['properties']
130
131                 rets.append(ret)
132         return rets
133
134     def _isLocalStorage(self, node):
135         return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
136             '.LOCALSTORAGE')
137
138     def _get_all_volume_storage(self, nodeTemplates):
139         rets = []
140         for node in nodeTemplates:
141             if self._isVolumeStorage(node):
142                 ret = {}
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'))
149
150                 rets.append(ret)
151         return rets
152
153     def _isVolumeStorage(self, node):
154         return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
155             '.VOLUMESTORAGE')
156
157     def _get_all_vdu(self, nodeTemplates):
158         rets = []
159         for node in nodeTemplates:
160             if self.isVdu(node):
161                 ret = {}
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))
172
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']
176
177                 ret['vls'] = self.get_linked_vl_ids(node, nodeTemplates)
178
179                 scalable = self.getCapabilityByName(node, 'scalable')
180                 if scalable != None and 'properties' in scalable:
181                     ret['scalable'] = scalable['properties']
182
183                 ret['cps'] = self.getVirtalBindingCpIds(node, nodeTemplates)
184                 ret['artifacts'] = self._build_artifacts(node)
185
186                 rets.append(ret)
187         return rets
188
189     def get_node_image_file(self, node):
190         rets = map(lambda x: self.get_requirement_node_name(x), self.getRequirementByName(node, 'guest_os'))
191         if len(rets) > 0:
192             return rets[0]
193         return ""
194
195     def _trans_volume_storage(self, volume_storage):
196         if isinstance(volume_storage, str):
197             return {"volume_storage_id": volume_storage}
198         else:
199             ret = {}
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']
206
207             return ret
208
209     def get_linked_vl_ids(self, node, node_templates):
210         vl_ids = []
211         cps = self.getVirtalBindingCps(node, node_templates)
212         for cp in cps:
213             vl_reqs = self.getVirtualLinks(cp)
214             for vl_req in vl_reqs:
215                 vl_ids.append(self.get_requirement_node_name(vl_req))
216         return vl_ids
217
218     def _build_artifacts(self, node):
219         rets = []
220         if 'artifacts' in node and len(node['artifacts']) > 0:
221             artifacts = node['artifacts']
222             for name, value in artifacts.items():
223                 ret = {}
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', '')
230                 else:
231                     ret['artifact_name'] = name
232                     ret['type'] = ''
233                     ret['file'] = value
234                     ret['repository'] = ''
235                     ret['deploy_path'] = ''
236                 rets.append(ret)
237         return rets
238
239     def get_all_cp(self, nodeTemplates):
240         cps = []
241         for node in nodeTemplates:
242             if self.isCp(node):
243                 cp = {}
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)
251                 if len(vls) > 1:
252                     cp['vls'] = vls
253                 cps.append(cp)
254         return cps
255
256     def get_all_plugin(self, node_templates):
257         plugins = []
258         for node in node_templates:
259             if self._isPlugin(node):
260                 plugin = {}
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']
266
267                 plugins.append(plugin)
268         return plugins
269
270     def _isPlugin(self, node):
271         return node['nodeType'].lower().find('.plugin.') >= 0 or node['nodeType'].lower().endswith('.plugin')