Merge "Add docker image and build for 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
40
41     def _get_all_services(self, nodeTemplates):
42         ret = []
43         for node in nodeTemplates:
44             if self.isService(node):
45                 service = {}
46                 service['serviceId'] = node['name']
47                 if 'description' in node:
48                     service['description'] = node['description']
49                 service['properties'] = node['properties']
50                 service['dependencies'] = map(lambda x: self.get_requirement_node_name(x),
51                                               self.getNodeDependencys(node))
52                 service['networks'] = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
53
54                 ret.append(service)
55         return ret
56
57     def _get_all_vcloud(self, nodeTemplates):
58         rets = []
59         for node in nodeTemplates:
60             if self._isVcloud(node):
61                 ret = {}
62                 if 'vdc_name' in node['properties']:
63                     ret['vdc_name'] = node['properties']['vdc_name']
64                 else:
65                     ret['vdc_name'] = ""
66                 if 'storage_clusters' in node['properties']:
67                     ret['storage_clusters'] = node['properties']['storage_clusters']
68                 else:
69                     ret['storage_clusters'] = []
70
71                 rets.append(ret)
72         return rets
73
74     def _isVcloud(self, node):
75         return node['nodeType'].upper().find('.VCLOUD.') >= 0 or node['nodeType'].upper().endswith('.VCLOUD')
76
77     def _get_all_vcenter(self, nodeTemplates):
78         rets = []
79         for node in nodeTemplates:
80             if self._isVcenter(node):
81                 ret = {}
82                 if 'compute_clusters' in node['properties']:
83                     ret['compute_clusters'] = node['properties']['compute_clusters']
84                 else:
85                     ret['compute_clusters'] = []
86                 if 'storage_clusters' in node['properties']:
87                     ret['storage_clusters'] = node['properties']['storage_clusters']
88                 else:
89                     ret['storage_clusters'] = []
90                 if 'network_clusters' in node['properties']:
91                     ret['network_clusters'] = node['properties']['network_clusters']
92                 else:
93                     ret['network_clusters'] = []
94
95                 rets.append(ret)
96         return rets
97
98     def _isVcenter(self, node):
99         return node['nodeType'].upper().find('.VCENTER.') >= 0 or node['nodeType'].upper().endswith('.VCENTER')
100
101     def _get_all_image_file(self, nodeTemplates):
102         rets = []
103         for node in nodeTemplates:
104             if self._isImageFile(node):
105                 ret = {}
106                 ret['image_file_id'] = node['name']
107                 if 'description' in node:
108                     ret['description'] = node['description']
109                 ret['properties'] = node['properties']
110
111                 rets.append(ret)
112         return rets
113
114     def _isImageFile(self, node):
115         return node['nodeType'].upper().find('.IMAGEFILE.') >= 0 or node['nodeType'].upper().endswith('.IMAGEFILE')
116
117     def _get_all_local_storage(self, nodeTemplates):
118         rets = []
119         for node in nodeTemplates:
120             if self._isLocalStorage(node):
121                 ret = {}
122                 ret['local_storage_id'] = node['name']
123                 if 'description' in node:
124                     ret['description'] = node['description']
125                 ret['properties'] = node['properties']
126
127                 rets.append(ret)
128         return rets
129
130     def _isLocalStorage(self, node):
131         return node['nodeType'].upper().find('.LOCALSTORAGE.') >= 0 or node['nodeType'].upper().endswith(
132             '.LOCALSTORAGE')
133
134     def _get_all_volume_storage(self, nodeTemplates):
135         rets = []
136         for node in nodeTemplates:
137             if self._isVolumeStorage(node):
138                 ret = {}
139                 ret['volume_storage_id'] = node['name']
140                 if 'description' in node:
141                     ret['description'] = node['description']
142                 ret['properties'] = node['properties']
143                 ret['image_file'] = map(lambda x: self.get_requirement_node_name(x),
144                                         self.getRequirementByName(node, 'image_file'))
145
146                 rets.append(ret)
147         return rets
148
149     def _isVolumeStorage(self, node):
150         return node['nodeType'].upper().find('.VOLUMESTORAGE.') >= 0 or node['nodeType'].upper().endswith(
151             '.VOLUMESTORAGE')