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