change lcm notify code and test case
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / vnf_create / inst_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 import json
15 import logging
16 import traceback
17 from threading import Thread
18
19 from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, SubNetworkInstModel, \
20     PortInstModel, StorageInstModel, FlavourInstModel, VNFCInstModel, VLInstModel, CPInstModel
21 from lcm.pub.exceptions import NFLCMException
22 from lcm.pub.msapi.nfvolcm import vnfd_rawdata_get, apply_grant_to_nfvo, notify_lcm_to_nfvo
23 from lcm.pub.utils.jobutil import JobUtil
24 from lcm.pub.utils.timeutil import now_time
25 from lcm.pub.utils.values import ignore_case_get
26 from lcm.pub.vimapi import adaptor
27
28 logger = logging.getLogger(__name__)
29
30
31 class InstVnf(Thread):
32     def __init__(self, data, nf_inst_id, job_id):
33         super(InstVnf, self).__init__()
34         self.data = data
35         self.nf_inst_id = nf_inst_id
36         self.job_id = job_id
37         self.nfvo_inst_id = ''
38         self.vnfm_inst_id = ''
39         self.vnfd_info = []
40         self.inst_resource = {'volumn':[],#'volumn':[{"vim_id": "1"}, {"res_id": "2"}]
41                               'network':[],
42                               'subnet':[],
43                               'port':[],
44                               'flavor':[],
45                               'vm':[],
46                               }
47         # self.create_res_result = {
48         #     'jobid': 'res_001',
49         #     'resourceResult': [{'name': 'vm01'}, {'name': 'vm02'}],
50         #     'resource_result':{
51         #         'affectedvnfc':[
52         #             {
53         #                 'status':'success',
54         #                 'vnfcinstanceid':'1',
55         #                 'computeresource':{'resourceid':'11'},
56         #                 'vduid':'111',
57         #                 'vdutype':'1111'
58         #             }
59         #         ],
60         #         'affectedvirtuallink':[
61         #             {
62         #                 'status': 'success',
63         #                 'virtuallinkinstanceid':'',
64         #                 'networkresource':{'resourceid':'1'},
65         #                 'subnetworkresource':{'resourceid':'1'},
66         #                 'virtuallinkdescid': '',
67         #             }
68         #         ],
69         #         'affectedcp':[{
70         #             'status': 'success',
71         #             'portresource':{'resourceid':'1'},
72         #             'cpinstanceid':'2',
73         #             'cpdid':'22',
74         #             'ownertype':'222',
75         #             'ownerid':'2222',
76         #             'virtuallinkinstanceid':'22222',
77         #
78         #         }],
79         #
80         #     }
81         # }
82
83     def run(self):
84         try:
85             self.inst_pre()
86             self.apply_grant()
87             self.create_res()
88             # self.check_res_status()
89             # self.wait_inst_finish(args)
90             self.lcm_notify()
91             JobUtil.add_job_status(self.job_id, 100, "Instantiate Vnf success.")
92             is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists()
93             logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
94         except NFLCMException as e:
95             self.vnf_inst_failed_handle(e.message)
96             # self.rollback(e.message)
97         except:
98             self.vnf_inst_failed_handle('unexpected exception')
99             logger.error(traceback.format_exc())
100             # self.rollback('unexpected exception')
101
102     def inst_pre(self):
103         vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
104         if not vnf_insts.exists():
105             raise NFLCMException('VNF nf_inst_id is not exist.')
106
107         self.vnfm_inst_id = vnf_insts[0].vnfm_inst_id
108         if vnf_insts[0].instantiationState != 'NOT_INSTANTIATED':
109             raise NFLCMException('VNF instantiationState is not NOT_INSTANTIATED.')
110
111         #get rawdata by vnfd_id
112         ret = vnfd_rawdata_get(vnf_insts[0].vnfdid)
113         if ret[0] != 0:
114             raise NFLCMException("Get vnfd_raw_data failed.")
115         self.vnfd_info = json.JSONDecoder().decode(ret[1])
116         #checkParameterExist
117         for cp in self.data:
118             if cp not in self.vnfd_info:
119                 raise NFLCMException('Input parameter is not defined in vnfd_info.')
120         #get nfvo info
121         JobUtil.add_job_status(self.job_id, 5, 'GET_NFVO_CONNECTION_INFO')
122         self.load_nfvo_config()
123
124         #update NfInstModel
125         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(flavour_id=ignore_case_get(self.data, "flavourId"),
126                                                                     vnf_level=ignore_case_get(self.data, 'instantiationLevelId'),
127                                                                     input_params=ignore_case_get(self.data, 'additionalParams'),
128                                                                     extension=ignore_case_get(self.data, ''),
129                                                                     initallocatedata=self.vnfd_info,
130                                                                     localizationLanguage=ignore_case_get(self.data, 'localizationLanguage'),
131                                                                     lastuptime=now_time())
132         JobUtil.add_job_status(self.job_id, 10, 'Nf instancing pre-check finish')
133         logger.info("Nf instancing pre-check finish")
134
135     def apply_grant(self):
136         logger.info('[NF instantiation] send resource grand request to nfvo start')
137         #self.check_vm_capacity()
138         content_args = {'nfvoInstanceId': self.nfvo_inst_id, 'vnfmInstanceId': self.vnfm_inst_id,
139                         'nfInstanceId': self.nf_inst_id, 'nfDescriptorId': '',
140                         'lifecycleOperation': 'Instantiate', 'jobId': self.job_id, 'addResource': [],
141                         'removeResource': [], 'placementConstraint': [], 'exVimIdList': [], 'additionalParam': {}}
142
143         vdus = self.vnfd_info['vdus']
144         res_index = 1
145         for vdu in vdus:
146             res_def = {'type': 'VDU', 'resourceDefinitionId': str(res_index), 'vduId': vdu['vdu_id'],
147                        'vimid': '', 'tenant': ''}
148             if self.vnfd_info['metadata']['cross_dc']:
149                 res_def['vimid'] = vdu['properties']['location_info']['vimId']
150                 res_def['tenant'] = vdu['properties']['location_info']['tenant']
151             content_args['addResource'].append(res_def)
152             res_index += 1
153         logger.info('content_args=%s' % content_args)
154         resp = apply_grant_to_nfvo(content_args)
155         logger.info("[NF instantiation] get grant response = %s" % resp)
156         if resp[0] != 0:
157             raise NFLCMException('Nf instancing apply grant exception')
158
159         #update_resources_table()
160         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(instantiationState='INSTANTIATED', lastuptime=now_time())
161         JobUtil.add_job_status(self.job_id, 20, 'Nf instancing apply grant finish')
162         logger.info("Nf instancing apply grant finish")
163
164     def create_res(self):
165         logger.info("[NF instantiation] create resource start")
166         adaptor.create_vim_res('', self.do_notify, self.do_rollback)
167
168         JobUtil.add_job_status(self.job_id, 70, '[NF instantiation] create resource finish')
169         logger.info("[NF instantiation] create resource finish")
170
171     # def check_res_status(self):
172     #     logger.info("[NF instantiation] confirm all vms are active start")
173     #     vnfcs = self.create_res_result['resource_result']['affectedvnfc']
174     #     for vnfc in vnfcs:
175     #         if 'success' != vnfc['status']:
176     #             logger.error("VNFC_STATUS_IS_NOT_ACTIVE[vduid=%s]" % vnfc['vduId'])
177     #             raise NFLCMException(msgid="VNFC_STATUS_IS_NOT_ACTIVE[vduid=%s]", args=vnfc['vduId'])
178     #
179     #     JobUtil.add_job_status(self.job_id, 80, 'SAVE_VNFC_TO_DB')
180     #     vls = self.create_res_result['resource_result']['affectedvirtuallink']
181     #     cps = self.create_res_result['resource_result']['affectedcp']
182     #
183     #     for vnfc in vnfcs:
184     #         if 'failed' == vnfc['status']:
185     #             continue
186     #         compute_resource = vnfc['computeresource']
187     #         vminst = VmInstModel.objects.filter(resouceid=compute_resource['resourceid']).first()
188     #         VNFCInstModel.objects.create(
189     #             vnfcinstanceid=vnfc['vnfcinstanceid'],
190     #             vduid=vnfc['vduid'],
191     #             vdutype=vnfc['vdutype'],
192     #             nfinstid=self.nf_inst_id,
193     #             vmid=vminst.vmid)
194     #     for vl in vls:
195     #         if 'failed' == vl['status']:
196     #             continue
197     #         network_resource = vl['networkresource']
198     #         subnet_resource = vl['subnetworkresource']
199     #         networkinst = NetworkInstModel.objects.filter(resouceid=network_resource['resourceid']).first()
200     #         subnetinst = SubNetworkInstModel.objects.filter(resouceid=subnet_resource['resourceid']).first()
201     #         VLInstModel.objects.create(
202     #             vlinstanceid=vl['virtuallinkinstanceid'],
203     #             vldid=vl['virtuallinkdescid'],
204     #             ownertype='0',
205     #             ownerid=self.nf_inst_id,
206     #             relatednetworkid=networkinst.networkid,
207     #             relatedsubnetworkid=subnetinst.subnetworkid)
208     #     # # for vs in vss:
209     #     for cp in cps:
210     #         if 'failed' == cp['status']:
211     #             continue
212     #         port_resource = cp['portresource']
213     #         portinst = PortInstModel.objects.filter(resouceid=port_resource['resourceid']).first()
214     #         ttt = portinst.portid
215     #         CPInstModel.objects.create(
216     #             cpinstanceid=cp['cpinstanceid'],
217     #             cpdid=cp['cpdid'],
218     #             relatedtype='2',
219     #             relatedport=portinst.portid,
220     #             ownertype=cp['ownertype'],
221     #             ownerid=cp['ownerid'],
222     #             vlinstanceid=cp['virtuallinkinstanceid'])
223     #     # self.add_job(43, 'INST_DPLY_VM_PRGS')
224     #     logger.info("[NF instantiation] confirm all vms are active end")
225
226     # def wait_inst_finish(self, args):
227     #     try:
228     #         logger.info('wait_inst_finish, args=%s' % args)
229     #         # WaitInstFinishTask(args).do_biz()
230     #         return {'result': '100', 'msg': 'Nf instancing wait finish', 'context': {}}
231     #     except Exception as e:
232     #         logger.error('Nf instancing wait exception=%s' % e.message)
233     #         logger.error(traceback.format_exc())
234     #         return {'result': '255', 'msg': 'Nf instancing wait exception', 'context': {}}
235
236     def lcm_notify(self):
237         logger.info('[NF instantiation] send notify request to nfvo start')
238         reg_info = NfvoRegInfoModel.objects.filter(vnfminstid=self.vnfm_inst_id).first()
239         # vm_info = VmInstModel.objects.filter(nfinstid=self.nf_inst_id)
240         vmlist = []
241         nfs = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
242         nf = nfs[0]
243         # allocate_data = json.loads(nf.initallocatedata)
244         # vmlist = json.loads(nf.predefinedvm)
245         addition_param = {'vmList': vmlist}
246         affected_vnfc = []
247         vnfcs = VNFCInstModel.objects.filter(nfinstid=self.nf_inst_id)
248         for vnfc in vnfcs:
249             compute_resource = {}
250             if vnfc.vmid:
251                 vm = VmInstModel.objects.filter(vmid=vnfc.vmid)
252                 if vm:
253                     compute_resource = {'vimId': vm[0].vimid, 'resourceId': vm[0].resouceid,
254                                         'resourceName': vm[0].vmname, 'tenant': vm[0].tenant}
255             affected_vnfc.append(
256                 {'vnfcInstanceId': vnfc.vnfcinstanceid, 'vduId': vnfc.vduid, 'changeType': 'added',
257                  'computeResource': compute_resource, 'storageResource': [], 'vduType': vnfc.vdutype})
258         affected_vl = []
259         vls = VLInstModel.objects.filter(ownerid=self.nf_inst_id)
260         for vl in vls:
261             network_resource = {}
262             subnet_resource = {}
263             if vl.relatednetworkid:
264                 network = NetworkInstModel.objects.filter(networkid=vl.relatednetworkid)
265                 subnet = SubNetworkInstModel.objects.filter(subnetworkid=vl.relatedsubnetworkid)
266                 if network:
267                     network_resource = {'vimId': network[0].vimid, 'resourceId': network[0].resouceid,
268                                         'resourceName': network[0].name, 'tenant': network[0].tenant}
269                 if subnet:
270                     subnet_resource = {'vimId': subnet[0].vimid, 'resourceId': subnet[0].resouceid,
271                                        'resourceName': subnet[0].name, 'tenant': subnet[0].tenant}
272             affected_vl.append(
273                 {'virtualLinkInstanceId': vl.vlinstanceid, 'virtualLinkDescId': vl.vldid, 'changeType': 'added',
274                  'networkResource': network_resource, 'subnetworkResource': subnet_resource, 'tenant': vl.tenant})
275         affected_vs = []
276         vss = StorageInstModel.objects.filter(instid=self.nf_inst_id)
277         for vs in vss:
278             affected_vs.append(
279                 {'virtualStorageInstanceId': vs.storageid, 'virtualStorageDescId': '', 'changeType': 'added',
280                  'storageResource': {'vimId': vs.vimid, 'resourceId': vs.resouceid,
281                                      'resourceName': vs.name, 'tenant': vs.tenant}})
282         affected_cp = []
283         #vnfc cps
284         for vnfc in vnfcs:
285             cps = CPInstModel.objects.filter(ownerid=vnfc.vnfcinstanceid, ownertype=3)
286             for cp in cps:
287                 port_resource = {}
288                 if cp.relatedport:
289                     port = PortInstModel.objects.filter(portid=cp.relatedport)
290                     if port:
291                         port_resource = {'vimId': port[0].vimid, 'resourceId': port[0].resouceid,
292                                          'resourceName': port[0].name, 'tenant': port[0].tenant}
293                 affected_cp.append(
294                     {'cPInstanceId': cp.cpinstanceid, 'cpdId': cp.cpdid, 'ownerid': cp.ownerid,
295                      'ownertype': cp.ownertype, 'changeType': 'added', 'portResource': port_resource,
296                      'virtualLinkInstanceId': cp.vlinstanceid})
297         #nf cps
298         cps = CPInstModel.objects.filter(ownerid=self.nf_inst_id, ownertype=0)
299         logger.info('vnf_inst_id=%s, cps size=%s' % (self.nf_inst_id, cps.count()))
300         for cp in cps:
301             port_resource = {}
302             if cp.relatedport:
303                 port = PortInstModel.objects.filter(portid=cp.relatedport)
304                 if port:
305                     port_resource = {'vimId': port[0].vimid, 'resourceId': port[0].resouceid,
306                                      'resourceName': port[0].name, 'tenant': port[0].tenant}
307             affected_cp.append(
308                 {'cPInstanceId': cp.cpinstanceid, 'cpdId': cp.cpdid, 'ownerid': cp.ownerid, 'ownertype': cp.ownertype,
309                  'changeType': 'added', 'portResource': port_resource,
310                  'virtualLinkInstanceId': cp.vlinstanceid})
311         affectedcapacity = {}
312         # reserved_total = allocate_data.get('reserved_total', {})
313         # affectedcapacity['vm'] = str(reserved_total.get('vmnum', 0))
314         # affectedcapacity['vcpu'] = str(reserved_total.get('vcpunum', 0))
315         # affectedcapacity['vMemory'] = str(reserved_total.get('memorysize', 0))
316         # affectedcapacity['port'] = str(reserved_total.get('portnum', 0))
317         # affectedcapacity['localStorage'] = str(reserved_total.get('hdsize', 0))
318         # affectedcapacity['sharedStorage'] = str(reserved_total.get('shdsize', 0))
319         content_args = {
320             # "vnfdmodule": allocate_data,
321             "additionalParam": addition_param,
322             "nfvoInstanceId": reg_info.nfvoid,
323             "vnfmInstanceId": self.vnfm_inst_id,
324             "status": 'finished',
325             "nfInstanceId": self.nf_inst_id,
326             "operation": 'instantiate',
327             "jobId": '',
328             # 'affectedcapacity': affectedcapacity,
329             'affectedService': [],
330             'affectedVnfc': affected_vnfc,
331             'affectedVirtualLink': affected_vl,
332             'affectedVirtualStorage': affected_vs,
333             'affectedCp': affected_cp}
334         logger.info('content_args=%s' % content_args)
335         #call rest api
336         resp = notify_lcm_to_nfvo(content_args, self.nf_inst_id)
337         logger.info('[NF instantiation] get lcm response %s' % resp)
338         if resp[0] != 0:
339             logger.error("notify lifecycle to nfvo failed.[%s]" % resp[1])
340             raise NFLCMException("send notify request to nfvo failed")
341         logger.info('[NF instantiation] send notify request to nfvo end')
342
343     # def rollback(self, args):
344     #     try:
345     #         logger.info('inst_exception, args=%s' % args)
346     #         # InstExceptionTask(args).do_biz()
347     #         return {'result': '100', 'msg': 'Nf instancing exception process finish', 'context': {}}
348     #     except Exception as e:
349     #         logger.error('Nf instancing exception process exception=%s' % e.message)
350     #         logger.error(traceback.format_exc())
351     #         return {'result': '255', 'msg': 'Nf instancing exception process exception', 'context': {}}
352
353     def load_nfvo_config(self):
354         logger.info("[NF instantiation]get nfvo connection info start")
355         reg_info = NfvoRegInfoModel.objects.filter(vnfminstid='vnfm111').first()
356         if reg_info:
357             self.vnfm_inst_id = reg_info.vnfminstid
358             self.nfvo_inst_id = reg_info.nfvoid
359             logger.info("[NF instantiation] Registered nfvo id is [%s]"%self.nfvo_inst_id)
360         else:
361             raise NFLCMException("Nfvo was not registered")
362         logger.info("[NF instantiation]get nfvo connection info end")
363
364     def vnf_inst_failed_handle(self, error_msg):
365         logger.error('VNF instantiation failed, detail message: %s' % error_msg)
366         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='failed', lastuptime=now_time())
367         JobUtil.add_job_status(self.job_id, 255, error_msg)
368
369     def do_notify(self, res_type, progress, ret):
370         logger.info('creating [%s] resource'%res_type)
371         progress = 20 + int(progress/2)     #20-70
372         if res_type == adaptor.RES_VOLUME:
373             logger.info('Create vloumns!')
374             if ret["returnCode"] == adaptor.RES_NEW:#new create
375                 self.inst_resource['volumn'].append({"vim_id": ignore_case_get(ret, "vim_id")}, {"res_id": ignore_case_get(ret, "res_id")})
376             JobUtil.add_job_status(self.job_id, progress, 'Create vloumns!')
377             StorageInstModel.objects.create(
378                 storageid='1',
379                 vimid='1',
380                 resouceid='1',
381                 name='40G',
382                 tenant='admin',
383                 insttype=0,
384                 instid=self.nf_inst_id)
385         elif res_type == adaptor.RES_NETWORK:
386             logger.info('Create networks!')
387             if ret["returnCode"] == adaptor.RES_NEW:
388                 self.inst_resource['network'].append({"vim_id": ignore_case_get(ret, "vim_id")}, {"res_id": ignore_case_get(ret, "res_id")})
389             # self.inst_resource['network'].append({"vim_id": "1"}, {"res_id": "2"})
390             JobUtil.add_job_status(self.job_id, progress, 'Create networks!')
391             NetworkInstModel.objects.create(
392                 networkid='1',
393                 vimid='1',
394                 resouceid='1',
395                 name='pnet_network',
396                 tenant='admin',
397                 insttype=0,
398                 instid=self.nf_inst_id)
399         elif res_type == adaptor.RES_SUBNET:
400             logger.info('Create subnets!')
401             if ret["returnCode"] == adaptor.RES_NEW:
402                 self.inst_resource['subnet'].append({"vim_id": ignore_case_get(ret, "vim_id")}, {"res_id": ignore_case_get(ret, "res_id")})
403             # self.inst_resource['subnet'].append({"vim_id": "1"}, {"res_id": "2"})
404             JobUtil.add_job_status(self.job_id, progress, 'Create subnets!')
405             SubNetworkInstModel.objects.create(
406                 subnetworkid='1',
407                 vimid='1',
408                 resouceid='1',
409                 networkid='1',
410                 name='sub_pnet',
411                 tenant='admin',
412                 insttype=0,
413                 instid=self.nf_inst_id)
414         elif res_type == adaptor.RES_PORT:
415             logger.info('Create ports!')
416             if ret["returnCode"] == adaptor.RES_NEW:
417                 self.inst_resource['port'].append({"vim_id": ignore_case_get(ret, "vim_id")}, {"res_id": ignore_case_get(ret, "res_id")})
418             # self.inst_resource['port'].append({"vim_id": "1"}, {"res_id": "2"})
419             JobUtil.add_job_status(self.job_id, progress, 'Create ports!')
420             PortInstModel.objects.create(
421                 portid='1',
422                 networkid='1',
423                 subnetworkid='1',
424                 vimid='1',
425                 resouceid='1',
426                 name='aaa_pnet_cp',
427                 tenant='admin',
428                 insttype=0,
429                 instid=self.nf_inst_id)
430         elif res_type == adaptor.RES_FLAVOR:
431             logger.info('Create flavors!')
432             if ret["returnCode"] == adaptor.RES_NEW:
433                 self.inst_resource['flavor'].append({"vim_id": ignore_case_get(ret, "vim_id")}, {"res_id": ignore_case_get(ret, "res_id")})
434             # self.inst_resource['flavor'].append({"vim_id": "1"}, {"res_id": "2"})
435             JobUtil.add_job_status(self.job_id, progress, 'Create flavors!')
436             FlavourInstModel.objects.create(
437                 falavourid='1',
438                 name='1',
439                 vcpu='1',
440                 extraspecs='1',
441                 instid=self.nf_inst_id)
442         elif res_type == adaptor.RES_VM:
443             logger.info('Create vms!')
444             if ret["returnCode"] == adaptor.RES_NEW:
445                 self.inst_resource['vm'].append({"vim_id": ignore_case_get(ret, "vim_id")}, {"res_id": ignore_case_get(ret, "res_id")})
446             # self.inst_resource['vm'].append({"vim_id": "1"}, {"res_id": "2"})
447             JobUtil.add_job_status(self.job_id, progress, 'Create vms!')
448             VmInstModel.objects.create(
449                 vmid="1",
450                 vimid="1",
451                 resouceid="11",
452                 insttype=0,
453                 instid=self.nf_inst_id,
454                 vmname="test_01",
455                 operationalstate=1)
456
457     def do_rollback(self, args_=None):
458         logger.error('error info : %s'%(args_))
459         adaptor.delete_vim_res(self.inst_resource, self.do_notify_delete)
460         logger.error('rollback resource complete')
461
462         StorageInstModel.objects.filter(instid=self.nf_inst_id).delete()
463         NetworkInstModel.objects.filter(instid=self.nf_inst_id).delete()
464         SubNetworkInstModel.objects.filter(instid=self.nf_inst_id).delete()
465         PortInstModel.objects.filter(instid=self.nf_inst_id).delete()
466         FlavourInstModel.objects.filter(instid=self.nf_inst_id).delete()
467         VmInstModel.objects.filter(instid=self.nf_inst_id).delete()
468         logger.error('delete table complete')
469         raise NFLCMException("Create resource failed")
470
471     def do_notify_delete(ret):
472         logger.error('Deleting [%s] resource'%ret)
473
474