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