1 # Copyright 2016 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 from lcm.ns.vnfs.wait_job import wait_job_finish
21 from lcm.pub.config.config import REPORT_TO_AAI
22 from lcm.pub.database.models import NfInstModel
23 from lcm.ns.vnfs.const import VNF_STATUS, NFVO_VNF_INST_TIMEOUT_SECOND
24 from lcm.pub.msapi.aai import query_vnf_aai, delete_vnf_aai
25 from lcm.pub.utils.values import ignore_case_get
26 from lcm.pub.utils.jobutil import JOB_MODEL_STATUS, JobUtil
27 from lcm.pub.exceptions import NSLCMException
28 from lcm.pub.msapi.vnfmdriver import send_nf_terminate_request
29 from lcm.pub.msapi import resmgr
31 logger = logging.getLogger(__name__)
34 class TerminateVnfs(threading.Thread):
35 def __init__(self, data, vnf_inst_id, job_id):
36 threading.Thread.__init__(self)
37 self.vnf_inst_id = vnf_inst_id
39 self.vnfm_inst_id = ''
42 self.terminationType = data['terminationType']
43 self.gracefulTerminationTimeout = data['gracefulTerminationTimeout']
49 self.send_nf_terminate_to_vnfmDriver()
50 self.wait_vnfm_job_finish()
51 self.send_terminate_vnf_to_resMgr()
52 self.delete_data_from_db()
54 self.delete_vnf_in_aai()
55 except NSLCMException as e:
56 self.exception(e.message)
58 logger.error(traceback.format_exc())
59 self.exception('unexpected exception')
61 def set_vnf_status(self, vnf_inst_info):
62 vnf_status = vnf_inst_info.status
63 if (vnf_status == VNF_STATUS.TERMINATING):
64 logger.info('[VNF terminate] VNF is dealing by other application,try again later.')
65 raise NSLCMException('[VNF terminate] VNF is dealing by other application,try again later.')
67 vnf_inst_info.status = VNF_STATUS.TERMINATING
70 def check_vnf_is_exist(self):
71 vnf_inst = NfInstModel.objects.filter(nfinstid=self.vnf_inst_id)
72 if not vnf_inst.exists():
73 logger.warning('[VNF terminate] Vnf terminate [%s] is not exist.' % self.vnf_inst_id)
77 def add_progress(self, progress, status_decs, error_code=""):
78 JobUtil.add_job_status(self.job_id, progress, status_decs, error_code)
81 vnf_inst_info = self.check_vnf_is_exist()
83 self.add_progress(100, "TERM_VNF_NOT_EXIST_SUCCESS", "finished")
84 self.add_progress(2, "GET_VNF_INST_SUCCESS")
85 self.vnfm_inst_id = vnf_inst_info.vnfm_inst_id
86 self.vnf_uuid = vnf_inst_info.mnfinstid
88 self.add_progress(100, "TERM_VNF_NOT_EXIST_SUCCESS", "finished")
90 def check_nf_valid(self):
91 vnf_inst = NfInstModel.objects.filter(nfinstid=self.vnf_inst_id)
92 if not vnf_inst.exists():
93 logger.warning('[VNF terminate] Vnf instance [%s] is not exist.' % self.vnf_inst_id)
94 raise NSLCMException('[VNF terminate] Vnf instance is not exist.')
96 self.add_progress(100, "TERM_VNF_NOT_EXIST_SUCCESS", "finished")
97 raise NSLCMException('[VNF terminate] Vnf instance is not exist.')
98 self.set_vnf_status(vnf_inst[0])
100 def exception(self, error_msg):
101 logger.error('VNF Terminate failed, detail message: %s' % error_msg)
102 NfInstModel.objects.filter(nfinstid=self.vnf_inst_id).update(status=VNF_STATUS.FAILED)
103 JobUtil.add_job_status(self.job_id, 255, 'VNF Terminate failed, detail message: %s' % error_msg, 0)
105 def send_nf_terminate_to_vnfmDriver(self):
106 req_param = json.JSONEncoder().encode({
107 'terminationType': self.terminationType,
108 'gracefulTerminationTimeout': self.gracefulTerminationTimeout})
109 rsp = send_nf_terminate_request(self.vnfm_inst_id, self.vnf_uuid, req_param)
110 self.vnfm_job_id = ignore_case_get(rsp, 'jobId')
112 def send_terminate_vnf_to_resMgr(self):
113 resmgr.terminate_vnf(self.vnf_inst_id)
115 def wait_vnfm_job_finish(self):
116 if not self.vnfm_job_id:
117 logger.warn("No Job, need not wait")
119 ret = wait_job_finish(vnfm_id=self.vnfm_inst_id, vnfo_job_id=self.job_id,
120 vnfm_job_id=self.vnfm_job_id, progress_range=[10, 90],
121 timeout=NFVO_VNF_INST_TIMEOUT_SECOND)
123 if ret != JOB_MODEL_STATUS.FINISHED:
124 logger.error('VNF terminate failed on VNFM side.')
125 raise NSLCMException('VNF terminate failed on VNFM side.')
127 def delete_data_from_db(self):
128 NfInstModel.objects.filter(nfinstid=self.vnf_inst_id).delete()
129 JobUtil.add_job_status(self.job_id, 100, 'vnf terminate success', 0)
131 def delete_vnf_in_aai(self):
132 logger.debug("TerminateVnfs::delete_vnf_in_aai::delete vnf instance[%s] in aai." % self.vnf_inst_id)
134 # query vnf instance in aai, get resource_version
135 customer_info = query_vnf_aai(self.vnf_inst_id)
136 resource_version = customer_info["resource-version"]
138 # delete vnf instance from aai
139 resp_data, resp_status = delete_vnf_aai(self.vnf_inst_id, resource_version)
141 logger.debug("Fail to delete vnf instance[%s] from aai, resp_status: [%s]." % (self.vnf_inst_id, resp_status))
144 "Success to delete vnf instance[%s] from aai, resp_status: [%s]." % (self.vnf_inst_id, resp_status))