def run(self): try: self.inst_pre() self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING) self.apply_grant() self.lcm_op_occ.upd( sub_operation=SUB_OPERATION_TASK.GRANTED, operation_state=OPERATION_STATE_TYPE.PROCESSING ) self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING) self.create_res() self.lcm_notify() JobUtil.add_job_status( self.job_id, 100, "Instantiate Vnf success." ) self.lcm_op_occ.upd( sub_operation=SUB_OPERATION_TASK.SUCCESS, operation_state=OPERATION_STATE_TYPE.COMPLETED ) except NFLCMException as e: self.vnf_inst_failed_handle(e.args[0]) except NFLCMExceptionConflict as e: self.vnf_inst_failed_handle(e.args[0]) except Exception as e: logger.error(str(e)) logger.error(traceback.format_exc()) self.vnf_inst_failed_handle('unexpected exception')
def cancel_vl_list(self): array_vlinst = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=self.ns_inst_id) if not array_vlinst: logger.info( "[cancel_vl_list] no vlinst attatch to ns_inst_id: %s" % self.ns_inst_id) return step_progress = 20 / len(array_vlinst) cur_progress = 70 for vlinst in array_vlinst: delete_result = "failed" cur_progress += step_progress try: ret = call_from_ns_cancel_resource('vl', vlinst.vlinstanceid) if ret[0] == 0: result = json.JSONDecoder().decode(ret[1]).get( "result", "") if str(result) == '0': delete_result = "success" except Exception as e: logger.error("[cancel_vl_list] error[%s]!" % e.args[0]) logger.error(traceback.format_exc()) job_msg = "Delete vlinst:[%s] %s." % (vlinst.vlinstanceid, delete_result) JobUtil.add_job_status(self.job_id, cur_progress, job_msg)
def run(self): logger.debug("start update for vnf %s", self.nf_inst_id) key = "vnfInstanceName" try: self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING) JobUtil.add_job_status(self.job_id, 50, "Start updating VNF.") if key in self.data and self.data[key] is not None: self.vnf_insts.update(nf_name=self.data[key], lastuptime=now_time()) key = "vnfInstanceDescription" if key in self.data and self.data[key] is not None: self.vnf_insts.update(nf_desc=self.data[key], lastuptime=now_time()) key = "vnfPkgId" if key in self.data: self.vnf_insts.update(vnfdid=self.data[key], lastuptime=now_time()) JobUtil.add_job_status(self.job_id, 75, "Start sending notification.") self.send_notification() JobUtil.add_job_status(self.job_id, 100, "Update VNF success.") except NFLCMException as e: logger.error(e.args[0]) self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, e.args[0]) JobUtil.add_job_status(self.job_id, 255, e.args[0]) except Exception as e: logger.error(e.args[0]) logger.error(traceback.format_exc()) self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, e.args[0]) JobUtil.add_job_status(self.job_id, 255, e.args[0])
def cancel_vnf_list(self): array_vnfinst = NfInstModel.objects.filter(ns_inst_id=self.ns_inst_id) if not array_vnfinst: logger.info( "[cancel_vnf_list] no vnfinst attatch to ns_inst_id: %s" % self.ns_inst_id) return step_progress = 10 / len(array_vnfinst) cur_progress = 50 vnf_jobs = [] for vnfinst in array_vnfinst: cur_progress += step_progress delete_result = "failed" vnf_job_id = '' try: vnf_job_id = self.delete_vnf(vnfinst.nfinstid) if vnf_job_id: delete_result = "deleting" except Exception as e: logger.error("[cancel_vnf_list] error[%s]!" % e.args[0]) logger.error(traceback.format_exc()) job_msg = "Delete vnfinst:[%s] %s." % (vnfinst.nfinstid, delete_result) JobUtil.add_job_status(self.job_id, cur_progress, job_msg) vnf_jobs.append((vnfinst.nfinstid, vnf_job_id)) self.wait_delete_vnfs(vnf_jobs, cur_progress, step_progress)
def cancel_sfc_list(self): array_sfcinst = FPInstModel.objects.filter(nsinstid=self.ns_inst_id) if not array_sfcinst: logger.info( "[cancel_sfc_list] no sfcinst attatch to ns_inst_id: %s" % self.ns_inst_id) return step_progress = 20 / len(array_sfcinst) cur_progress = 30 for sfcinst in array_sfcinst: cur_progress += step_progress delete_result = "failed" try: ret = call_from_ns_cancel_resource('sfc', sfcinst.sfcid) if ret[0] == 0: result = json.JSONDecoder().decode(ret[1]).get( "result", "") if str(result) == '0': delete_result = "success" except Exception as e: logger.error("[cancel_sfc_list] error[%s]!" % e.args[0]) logger.error(traceback.format_exc()) job_msg = "Delete sfcinst:[%s] %s." % (sfcinst.sfcid, delete_result) JobUtil.add_job_status(self.job_id, cur_progress, job_msg)
def run(self): try: self.heal_pre() self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING) self.apply_grant() self.lcm_op_occ.upd( sub_operation=SUB_OPERATION_TASK.GRANTED, operation_state=OPERATION_STATE_TYPE.PROCESSING ) self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING) self.heal_resource() JobUtil.add_job_status(self.job_id, 100, "Heal Vnf success.") NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update( status='INSTANTIATED', lastuptime=now_time() ) self.lcm_notify( LCM_NOTIFICATION_STATUS.RESULT, OPERATION_STATE_TYPE.COMPLETED ) self.lcm_op_occ.upd( sub_operation=SUB_OPERATION_TASK.SUCCESS, operation_state=OPERATION_STATE_TYPE.COMPLETED ) except NFLCMException as e: logger.error(e.args[0]) self.vnf_heal_failed_handle(e.args[0]) except Exception as e: logger.error(e.args[0]) logger.error(traceback.format_exc()) self.vnf_heal_failed_handle(e.args[0])
def exception(self, error_msg): logger.error('VNF Terminate failed, detail message: %s' % error_msg) NfInstModel.objects.filter(nfinstid=self.vnf_inst_id).update( status=VNF_STATUS.FAILED) JobUtil.add_job_status( self.job_id, 255, 'VNF Terminate failed, detail message: %s' % error_msg, 0)
def test_instantiate_vnf_when_get_packageinfo_by_csarid_failed( self, mock_call_req): NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222', version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED', nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time()) r1_get_vnfpackage_by_vnfdid = [ 1, json.JSONEncoder().encode(vnfpackage_info), '200' ] mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid] self.nf_inst_id = '1111' self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id) JobUtil.add_job_status(self.job_id, 0, 'INST_VNF_READY') data = inst_req_data InstantiateVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run() self.assert_job_result(self.job_id, 255, 'Failed to query vnf CSAR(111) from catalog.')
def vm_save(job_id, nf_inst_id, ret): JobUtil.add_job_status(job_id, 70, 'Create vms!') vm_id = str(uuid.uuid4()) nics = ignore_case_get(ret, "nicArray") volumes = ignore_case_get(ret, "volumeArray") VmInstModel.objects.create( vmid=vm_id, vmname=ignore_case_get(ret, "name"), vimid=ignore_case_get(ret, "vimId"), resourceid=ignore_case_get(ret, "id"), tenant=ignore_case_get(ret, "tenantId"), nic_array=nics if nics else "null", metadata=ignore_case_get(ret, "metadata"), volume_array=volumes if volumes else "null", server_group=ignore_case_get(ret, "serverGroup"), availability_zone=str(ignore_case_get(ret, "availabilityZone", "undefined")), flavor_id=ignore_case_get(ret, "flavorId"), security_groups=ignore_case_get(ret, "securityGroups"), operationalstate=ignore_case_get(ret, "status"), insttype=0, is_predefined=ignore_case_get(ret, "returnCode"), instid=nf_inst_id) VNFCInstModel.objects.create( vnfcinstanceid=str(uuid.uuid4()), vduid=ignore_case_get(ret, "id"), is_predefined=ignore_case_get(ret, "returnCode"), instid=nf_inst_id, vmid=vm_id) for portid in ignore_case_get(ret, "ports"): PortInstModel.objects.filter( resourceid=portid ).update( vmid=ignore_case_get(ret, "id") )
def test_instantiate_vnf_when_already_instantiated(self): NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222', version='', vendor='', netype='', vnfd_model='', status='INSTANTIATED', nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time()) self.nf_inst_id = '1111' self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id) JobUtil.add_job_status(self.job_id, 0, 'INST_VNF_READY') jobs = JobStatusModel.objects.filter(jobid=self.job_id, progress=0, descp='INST_VNF_READY') self.assertEqual(1, len(jobs)) data = inst_req_data InstantiateVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run() self.assert_job_result( self.job_id, 255, 'VNF instantiationState is not NOT_INSTANTIATED.')
def test_clear_job(self): job_id = "1" JobModel.objects.filter().delete() JobModel(jobid=job_id, jobtype="1", jobaction="2", resid="3", status=0).save() JobUtil.clear_job(job_id) self.assertEqual(0, len(JobModel.objects.filter(jobid=job_id)))
def test_clear_job_status(self): job_id = "1" JobStatusModel.objects.filter().delete() JobStatusModel(indexid=1, jobid=job_id, status="success", progress=10).save() JobUtil.clear_job_status(job_id) self.assertEqual(0, len(JobStatusModel.objects.filter(jobid=job_id)))
def cancel_vnf_list(self): array_vnfinst = NfInstModel.objects.filter(ns_inst_id=self.ns_inst_id) if not array_vnfinst: logger.error( "[cancel_vnf_list] no vnfinst attatch to ns_inst_id:%s" % self.ns_inst_id) return step_progress = 20 / len(array_vnfinst) cur_progress = 50 for vnfinst in array_vnfinst: tmp_msg = vnfinst.nfinstid try: self.delete_vnf(tmp_msg) cur_progress += step_progress JobUtil.add_job_status( self.job_id, cur_progress, "Delete vnfinst:[%s] success." % tmp_msg, '') except Exception as e: logger.error("[cancel_vnf_list] error[%s]!" % e.message) logger.error(traceback.format_exc()) JobUtil.add_job_status(self.job_id, cur_progress, "Delete vnfinst:[%s] Failed." % tmp_msg, '') return 'false' return 'true'
def test_instantiate_operating_conflict(self, mock_call_req): NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222', version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED', nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time()) r1_get_vnfpackage_by_vnfdid = [ 0, json.JSONEncoder().encode(vnfpackage_info), '200' ] mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid] self.nf_inst_id = '1111' self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id) JobUtil.add_job_status(self.job_id, 0, 'INST_VNF_READY') VNFLcmOpOccModel.objects.create( vnf_instance_id=self.nf_inst_id, id=self.job_id, operation=const.OPERATION_TYPE.INSTANTIATE, operation_state=const.OPERATION_STATE_TYPE.PROCESSING) response = self.client.post( '/api/vnflcm/v1/vnf_instances/1111/instantiate', data=inst_req_data, format='json') self.assertEqual(status.HTTP_409_CONFLICT, response.status_code)
def run(self): try: if self.term_pre(): vdus = VmInstModel.objects.filter(instid=self.nf_inst_id, is_predefined=1) self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING) apply_result = grant_resource(data=self.data, nf_inst_id=self.nf_inst_id, job_id=self.job_id, grant_type=self.grant_type, vdus=vdus) self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING) logger.info("Grant resource end, response: %s" % apply_result) JobUtil.add_job_status(self.job_id, 20, 'Nf terminating grant_resource finish') self.query_inst_resource() self.query_notify_data() self.delete_resource() self.lcm_notify() JobUtil.add_job_status(self.job_id, 100, "Terminate Vnf success.") except NFLCMException as e: self.vnf_term_failed_handle(e.args[0]) except Exception as e: logger.error(e.args[0]) logger.error(traceback.format_exc()) self.vnf_term_failed_handle(e.args[0])
def vnf_operate_failed_handle(self, error_msg): logger.error('VNF Operation failed, detail message: %s' % error_msg) NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update( # status=VNF_STATUS.FAILED, lastuptime=now_time()) self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg) JobUtil.add_job_status(self.job_id, 255, error_msg)
def wait_job_finish_common_call_back(vnfo_job_id, vnfm_job_id, job_status, jobs, progress_range, **kwargs): error_254 = False for job in jobs: progress = TerminateNsService.calc_progress_over_100( job['progress'], progress_range) if 254 == progress: logger.debug("=========254==============") progress = 255 error_254 = True JobUtil.add_job_status(vnfo_job_id, progress, job.get('statusdescription', ""), job.get('errorcode', "")) latest_progress = TerminateNsService.calc_progress_over_100( job_status['progress'], progress_range) if 254 == latest_progress: logger.debug("=========254==============") latest_progress = 255 error_254 = True JobUtil.add_job_status(vnfo_job_id, latest_progress, job_status.get('statusdescription', ""), job_status.get('errorcode', "")) # return error_254 if error_254: logger.debug("return 254") return True, 'error_254' if job_status['status'] in ('error', 'finished'): return True, job_status['status'] return False, 'processing'
def test_instantiate_vnf_when_unexpected_exception(self, mock_call, mock_call_req): NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222', version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED', nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time()) r1_get_vnfpackage_by_vnfdid = [ 0, json.JSONEncoder().encode(vnfpackage_info), '200' ] r2_apply_grant_result = [ 0, json.JSONEncoder().encode(self.grant_result), '200' ] mock_call_req.side_effect = [ r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result ] mock_call.side_effect = [ c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume ] self.nf_inst_id = '1111' self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id) JobUtil.add_job_status(self.job_id, 0, 'INST_VNF_READY') data = inst_req_data InstantiateVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run() self.assert_job_result(self.job_id, 255, 'unexpected exception')
def start_buildin_grapflow(self, job_id, plan_input, occ_id): JobUtil.add_job_status( job_id, 10, 'NS inst(%s) buildin grap workflow started.' % self.ns_inst_id) run_ns_instantiate(plan_input, occ_id) return dict(data={'jobId': job_id}, status=status.HTTP_200_OK, occ_id=occ_id)
def vnf_inst_failed_handle(self, error_msg): logger.error('VNF instantiation failed, detail message: %s' % error_msg) NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update( status=VNF_STATUS.FAILED, lastuptime=now_time()) JobUtil.add_job_status( self.job_id, 255, 'VNF instantiation failed, detail message: %s' % error_msg, 0)
def start_buildin_workflow(self, job_id, plan_input, occ_id): JobUtil.add_job_status( job_id, 10, 'NS inst(%s) buildin workflow started.' % self.ns_inst_id) BuildInWorkflowThread(plan_input, occ_id).start() return dict(data={'jobId': job_id}, status=status.HTTP_200_OK, occ_id=occ_id)
def run(self): try: self.do_biz() except NSLCMException as e: JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, e.args[0]) except: logger.error(traceback.format_exc()) JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, 'nf update fail')
def save_info_to_db(self): logger.debug("save_info_to_db start") do_biz_with_share_lock("set-vnflist-in-vnffginst-%s" % self.ns_inst_id, self.save_vnf_inst_id_in_vnffg) NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update( status=VNF_STATUS.ACTIVE, lastuptime=now_time()) JobUtil.add_job_status(self.job_id, 100, 'vnf instantiation success', 0) logger.debug("save_info_to_db end")
def check_data(self): JobUtil.add_job_status(self.job_id, 0, "TERMINATING...", '') ns_inst = NSInstModel.objects.filter(id=self.ns_inst_id) if not ns_inst.exists(): logger.warn('ns instance [%s] does not exist.' % self.ns_inst_id) return False JobUtil.add_job_status(self.job_id, 10, "Ns cancel: check ns_inst_id success", '') return True
def run(self): try: self.do_biz() except NSLCMException as e: JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message) except: logger.error(traceback.format_exc()) JobUtil.add_job_status(self.job_id, JOB_ERROR, "ns terminate fail.", '')
def run(self): try: self.do_biz() except NSLCMException as e: JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message) except: logger.error(traceback.format_exc()) JobUtil.add_job_status(self.job_id, JOB_ERROR, 'nf scale fail') finally: self.update_nf_status()
def run(self): try: self.do_biz() except NSLCMException as e: JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, e.args[0]) NsLcmOpOcc.update(self.occ_id, operationState="FAILED", error=e.args[0]) except Exception as e: logger.error(traceback.format_exc()) JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, 'ns heal fail') NsLcmOpOcc.update(self.occ_id, operationState="FAILED", error=e.args[0])
def apply_grant(self): vdus = VmInstModel.objects.filter(instid=self.nf_inst_id) apply_result = grant_resource(data=self.data, nf_inst_id=self.nf_inst_id, job_id=self.job_id, grant_type=self.grant_type, vdus=vdus) logger.info("Grant resource, response: %s" % apply_result) JobUtil.add_job_status(self.job_id, 20, 'Nf Operate grant_resource finish')
def prepare_create_params(): nf_inst_id = str(uuid.uuid4()) NfInstModel(nfinstid=nf_inst_id, status=VNF_STATUS.INSTANTIATING, create_time=now_time(), lastuptime=now_time()).save() job_id = JobUtil.create_job(INST_TYPE_NAME.VNF, JOB_TYPE.CREATE_VNF, nf_inst_id) JobUtil.add_job_status(job_id, 0, 'create vnf record in database.', 0) return nf_inst_id, job_id
def do_biz(self): if not self.check_data(): JobUtil.add_job_status(self.job_id, 100, "Need not terminate.", '') return self.cancel_sfc_list() self.cancel_vnf_list() time.sleep(4) self.cancel_vl_list() self.finaldata()