Notification stuffs.
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / instantiate_vnf.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 json
16 import logging
17 import traceback
18 import uuid
19 from threading import Thread
20
21 from lcm.pub.database.models import NfInstModel, VmInstModel, NetworkInstModel, \
22     SubNetworkInstModel, PortInstModel, StorageInstModel, FlavourInstModel, VNFCInstModel
23 from lcm.pub.exceptions import NFLCMException
24 from lcm.pub.msapi.gvnfmdriver import prepare_notification_data
25 # from lcm.pub.msapi.gvnfmdriver import notify_lcm_to_nfvo
26 from lcm.pub.msapi.sdc_run_catalog import query_vnfpackage_by_id
27 from lcm.pub.utils.jobutil import JobUtil
28 from lcm.pub.utils.timeutil import now_time
29 from lcm.pub.utils.notificationsutil import NotificationsUtil
30 from lcm.pub.utils.values import ignore_case_get, get_none, get_boolean, get_integer
31 from lcm.pub.vimapi import adaptor
32 from lcm.nf.biz.grant_vnf import grant_resource
33 from lcm.nf.const import GRANT_TYPE
34
35 logger = logging.getLogger(__name__)
36
37
38 class InstantiateVnf(Thread):
39     def __init__(self, data, nf_inst_id, job_id):
40         super(InstantiateVnf, self).__init__()
41         self.data = data
42         self.nf_inst_id = nf_inst_id
43         self.job_id = job_id
44         self.vim_id = ignore_case_get(ignore_case_get(self.data, "additionalParams"), "vimId")
45         self.grant_type = GRANT_TYPE.INSTANTIATE
46
47     def run(self):
48         try:
49             self.inst_pre()
50             self.apply_grant()
51             self.create_res()
52             self.lcm_notify()
53             JobUtil.add_job_status(self.job_id, 100, "Instantiate Vnf success.")
54         except NFLCMException as e:
55             self.vnf_inst_failed_handle(e.message)
56         except Exception as e:
57             logger.error(e.message)
58             logger.error(traceback.format_exc())
59             self.vnf_inst_failed_handle('unexpected exception')
60
61     def inst_pre(self):
62         vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
63         if not vnf_insts.exists():
64             raise NFLCMException('VNF nf_inst_id is not exist.')
65
66         if vnf_insts[0].status != 'NOT_INSTANTIATED':
67             raise NFLCMException('VNF instantiationState is not NOT_INSTANTIATED.')
68
69         JobUtil.add_job_status(self.job_id, 5, 'Get packageinfo by vnfd_id')
70         self.vnfd_id = vnf_insts[0].vnfdid
71         JobUtil.add_job_status(self.job_id, 10, 'Get vnf package info from catalog by csar_id')
72         input_parameters = []
73         inputs = ignore_case_get(self.data, "additionalParams")
74         if inputs:
75             if isinstance(inputs, (str, unicode)):
76                 inputs = json.loads(inputs)
77             for key, val in inputs.items():
78                 input_parameters.append({"key": key, "value": val})
79         vnf_package = query_vnfpackage_by_id(self.vnfd_id)
80         pkg_info = ignore_case_get(vnf_package, "packageInfo")
81         self.vnfd_info = json.loads(ignore_case_get(pkg_info, "vnfdModel"))
82
83         self.update_cps()
84         metadata = ignore_case_get(self.vnfd_info, "metadata")
85         csar_id = ignore_case_get(metadata, "id")
86         version = ignore_case_get(metadata, "vnfdVersion")
87         vendor = ignore_case_get(metadata, "vendor")
88         netype = ignore_case_get(metadata, "type")
89         vnfsoftwareversion = ignore_case_get(metadata, "version")
90         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).\
91             update(package_id=csar_id,
92                    flavour_id=ignore_case_get(self.data, "flavourId"),
93                    version=version,
94                    vendor=vendor,
95                    netype=netype,
96                    vnfd_model=self.vnfd_info,
97                    status='NOT_INSTANTIATED',
98                    vnfdid=self.vnfd_id,
99                    localizationLanguage=ignore_case_get(self.data, 'localizationLanguage'),
100                    input_params=input_parameters,
101                    vnfSoftwareVersion=vnfsoftwareversion,
102                    lastuptime=now_time())
103
104         logger.info("VimId = %s" % self.vim_id)
105         '''
106         is_exist = NfvoRegInfoModel.objects.filter(nfvoid=self.nf_inst_id).exists()
107         if not is_exist:
108             NfvoRegInfoModel.objects.create(
109                 nfvoid=self.nf_inst_id,
110                 vnfminstid=ignore_case_get(self.data, "vnfmId"),
111                 apiurl=self.vim_id)
112         '''
113         JobUtil.add_job_status(self.job_id, 15, 'Nf instancing pre-check finish')
114         logger.info("Nf instancing pre-check finish")
115
116     def apply_grant(self):
117         vdus = ignore_case_get(self.vnfd_info, "vdus")
118         apply_result = grant_resource(data=self.data, nf_inst_id=self.nf_inst_id, job_id=self.job_id,
119                                       grant_type=self.grant_type, vdus=vdus)
120         self.set_location(apply_result)
121
122         logger.info('VnfdInfo = %s' % self.vnfd_info)
123         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='INSTANTIATED', lastuptime=now_time())
124         JobUtil.add_job_status(self.job_id, 20, 'Nf instancing apply grant finish')
125         logger.info("Nf instancing apply grant finish")
126
127     def create_res(self):
128         logger.info("Create resource start")
129         adaptor.create_vim_res(self.vnfd_info, self.do_notify)
130         JobUtil.add_job_status(self.job_id, 70, '[NF instantiation] create resource finish')
131         logger.info("Create resource finish")
132
133     def lcm_notify(self):
134         notification_content = prepare_notification_data(self.nf_inst_id, self.job_id, "ADDED")
135         logger.info('Notify request data = %s' % notification_content)
136         # resp = notify_lcm_to_nfvo(json.dumps(notification_content))
137         # logger.info('Lcm notify end, response %s' % resp)
138         NotificationsUtil().send_notification(notification_content)
139
140     def vnf_inst_failed_handle(self, error_msg):
141         logger.error('VNF instantiation failed, detail message: %s' % error_msg)
142         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='NOT_INSTANTIATED', lastuptime=now_time())
143         JobUtil.add_job_status(self.job_id, 255, error_msg)
144
145     def do_notify(self, res_type, ret):
146         logger.info('Creating [%s] resource' % res_type)
147         resource_save_method = globals().get(res_type + '_save')
148         resource_save_method(self.job_id, self.nf_inst_id, ret)
149
150     def update_cps(self):
151         for extlink in ignore_case_get(self.data, "extVirtualLinks"):
152             for ext_cp in ignore_case_get(extlink, "extCps"):
153                 cpdid = ignore_case_get(ext_cp, "cpdId")
154                 for cp in ignore_case_get(self.vnfd_info, "cps"):
155                     if cpdid == ignore_case_get(cp, "cp_id"):
156                         cp["networkId"] = ignore_case_get(extlink, "resourceId")
157                         cp["subnetId"] = ignore_case_get(extlink, "resourceSubnetId")
158                         break
159
160     def set_location(self, apply_result):
161         vim_connections = ignore_case_get(apply_result, "vimConnections")
162         vnfid = ignore_case_get(apply_result, "vnfInstanceId")
163         directive = ignore_case_get(apply_result, "directive")
164         vim_assets = ignore_case_get(apply_result, "vimAssets")
165         access_info = ignore_case_get(vim_connections[0], "accessInfo")
166         tenant = ignore_case_get(access_info, "tenant")
167         vimid = ignore_case_get(vim_connections[0], "vimId")
168         cloud_owner, cloud_regionid = vimid.split("_")
169         vdu_info = []
170
171         for flavor in ignore_case_get(vim_assets, "vimComputeResourceFlavour"):
172             vdu_info.append({"vduName": flavor["resourceProviderId"],
173                              "flavorName": flavor["vimFlavourId"],
174                              "directive": directive})
175
176         for resource_type in ['vdus', 'vls']:
177             for resource in ignore_case_get(self.vnfd_info, resource_type):
178                 if "location_info" in resource["properties"]:
179                     resource["properties"]["location_info"]["vimid"] = vimid
180                     resource["properties"]["location_info"]["tenant"] = tenant
181                     resource["properties"]["location_info"]["vnfId"] = vnfid
182                     resource["properties"]["location_info"]["cloudOwner"] = cloud_owner
183                     resource["properties"]["location_info"]["cloudRegionId"] = cloud_regionid
184                     resource["properties"]["location_info"]["vduInfo"] = vdu_info
185                 else:
186                     resource["properties"]["location_info"] = {
187                         "vimid": vimid,
188                         "tenant": tenant,
189                         "vnfId": vnfid,
190                         "cloudOwner": cloud_owner,
191                         "cloudRegionId": cloud_regionid,
192                         "vduInfo": vdu_info}
193
194     '''
195     def get_subnet_ids(self, ext_cp):
196         subnet_ids = []
197         for cp_conf in ignore_case_get(ext_cp, "cpConfig"):
198             for cp_protocol in ignore_case_get(ext_cp, "cpProtocolData"):
199                 ip_over_ethernet = ignore_case_get(cp_protocol, "ipOverEthernet")
200                 for ip_address in ignore_case_get(ip_over_ethernet, "ipAddresses"):
201                     subnet_ids.append(ignore_case_get(ip_address, "subnetId"))
202         return subnet_ids
203     '''
204
205
206 def volume_save(job_id, nf_inst_id, ret):
207     JobUtil.add_job_status(job_id, 25, 'Create vloumns!')
208     StorageInstModel.objects.create(
209         storageid=str(uuid.uuid4()),
210         vimid=ignore_case_get(ret, "vimId"),
211         resourceid=ignore_case_get(ret, "id"),
212         name=ignore_case_get(ret, "name"),
213         tenant=ignore_case_get(ret, "tenantId"),
214         create_time=ignore_case_get(ret, "createTime"),
215         storagetype=get_none(ignore_case_get(ret, "type")),
216         size=ignore_case_get(ret, "size"),
217         insttype=0,
218         is_predefined=ignore_case_get(ret, "returnCode"),
219         nodeId=ignore_case_get(ret, "nodeId"),
220         instid=nf_inst_id)
221
222
223 def network_save(job_id, nf_inst_id, ret):
224     JobUtil.add_job_status(job_id, 35, 'Create networks!')
225     NetworkInstModel.objects.create(
226         networkid=str(uuid.uuid4()),
227         name=ignore_case_get(ret, "name"),
228         vimid=ignore_case_get(ret, "vimId"),
229         resourceid=ignore_case_get(ret, "id"),
230         tenant=ignore_case_get(ret, "tenantId"),
231         segmentid=str(ignore_case_get(ret, "segmentationId")),
232         network_type=ignore_case_get(ret, "networkType"),
233         physicalNetwork=ignore_case_get(ret, "physicalNetwork"),
234         vlantrans=get_boolean(ignore_case_get(ret, "vlanTransparent")),
235         is_shared=get_boolean(ignore_case_get(ret, "shared")),
236         routerExternal=get_boolean(ignore_case_get(ret, "routerExternal")),
237         insttype=0,
238         is_predefined=ignore_case_get(ret, "returnCode"),
239         nodeId=ignore_case_get(ret, "nodeId"),
240         instid=nf_inst_id)
241
242
243 def subnet_save(job_id, nf_inst_id, ret):
244     JobUtil.add_job_status(job_id, 40, 'Create subnets!')
245     SubNetworkInstModel.objects.create(
246         subnetworkid=str(uuid.uuid4()),
247         name=ignore_case_get(ret, "name"),
248         vimid=ignore_case_get(ret, "vimId"),
249         resourceid=ignore_case_get(ret, "id"),
250         tenant=ignore_case_get(ret, "tenantId"),
251         networkid=ignore_case_get(ret, "networkId"),
252         cidr=ignore_case_get(ret, "cidr"),
253         ipversion=ignore_case_get(ret, "ipversion"),
254         isdhcpenabled=ignore_case_get(ret, "enableDhcp"),
255         gatewayip=ignore_case_get(ret, "gatewayIp"),
256         dnsNameservers=ignore_case_get(ret, "dnsNameservers"),
257         hostRoutes=ignore_case_get(ret, "hostRoutes"),
258         allocationPools=ignore_case_get(ret, "allocationPools"),
259         insttype=0,
260         is_predefined=ignore_case_get(ret, "returnCode"),
261         instid=nf_inst_id)
262
263
264 def port_save(job_id, nf_inst_id, ret):
265     JobUtil.add_job_status(job_id, 50, 'Create ports!')
266     PortInstModel.objects.create(
267         portid=str(uuid.uuid4()),
268         networkid=ignore_case_get(ret, "networkId"),
269         subnetworkid=ignore_case_get(ret, "subnetId"),
270         name=ignore_case_get(ret, "name"),
271         vimid=ignore_case_get(ret, "vimId"),
272         resourceid=ignore_case_get(ret, "id"),
273         tenant=ignore_case_get(ret, "tenantId"),
274         macaddress=ignore_case_get(ret, "macAddress"),
275         ipaddress=ignore_case_get(ret, "ip"),
276         typevirtualnic=ignore_case_get(ret, "vnicType"),
277         securityGroups=ignore_case_get(ret, "securityGroups"),
278         insttype=0,
279         is_predefined=ignore_case_get(ret, "returnCode"),
280         nodeId=ignore_case_get(ret, "nodeId"),
281         instid=nf_inst_id)
282
283
284 def flavor_save(job_id, nf_inst_id, ret):
285     JobUtil.add_job_status(job_id, 60, 'Create flavors!')
286     FlavourInstModel.objects.create(
287         flavourid=str(uuid.uuid4()),
288         name=ignore_case_get(ret, "name"),
289         vimid=ignore_case_get(ret, "vimId"),
290         resourceid=ignore_case_get(ret, "id"),
291         tenant=ignore_case_get(ret, "tenantId"),
292         vcpu=get_integer(ignore_case_get(ret, "vcpu")),
293         memory=get_integer(ignore_case_get(ret, "memory")),
294         disk=get_integer(ignore_case_get(ret, "disk")),
295         ephemeral=get_integer(ignore_case_get(ret, "ephemeral")),
296         swap=get_integer(ignore_case_get(ret, "swap")),
297         isPublic=get_boolean(ignore_case_get(ret, "isPublic")),
298         extraspecs=ignore_case_get(ret, "extraSpecs"),
299         is_predefined=ret.get("returnCode", int(0)),
300         instid=nf_inst_id)
301
302
303 def vm_save(job_id, nf_inst_id, ret):
304     JobUtil.add_job_status(job_id, 70, 'Create vms!')
305     vm_id = str(uuid.uuid4())
306     VmInstModel.objects.create(
307         vmid=vm_id,
308         vmname=ignore_case_get(ret, "name"),
309         vimid=ignore_case_get(ret, "vimId"),
310         resourceid=ignore_case_get(ret, "id"),
311         tenant=ignore_case_get(ret, "tenantId"),
312         nic_array=ignore_case_get(ret, "nicArray"),
313         metadata=ignore_case_get(ret, "metadata"),
314         volume_array=ignore_case_get(ret, "volumeArray"),
315         server_group=ignore_case_get(ret, "serverGroup"),
316         availability_zone=str(ignore_case_get(ret, "availabilityZone", "undefined")),
317         flavor_id=ignore_case_get(ret, "flavorId"),
318         security_groups=ignore_case_get(ret, "securityGroups"),
319         operationalstate=ignore_case_get(ret, "status"),
320         insttype=0,
321         is_predefined=ignore_case_get(ret, "returnCode"),
322         instid=nf_inst_id)
323     VNFCInstModel.objects.create(
324         vnfcinstanceid=str(uuid.uuid4()),
325         vduid=ignore_case_get(ret, "id"),
326         is_predefined=ignore_case_get(ret, "returnCode"),
327         instid=nf_inst_id,
328         vmid=vm_id)