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