1 # Copyright 2016-2018 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.pub.config.config import REPORT_TO_AAI
21 from lcm.pub.database.models import NfInstModel, VmInstModel
22 from lcm.pub.exceptions import NSLCMException
23 from lcm.pub.msapi import resmgr
24 from lcm.pub.msapi.aai import query_vnf_aai, delete_vnf_aai, query_vserver_aai, delete_vserver_aai
25 from lcm.pub.msapi.extsys import split_vim_to_owner_region, get_vim_by_id
26 from lcm.pub.msapi.vnfmdriver import send_nf_terminate_request
27 from lcm.pub.utils.jobutil import JOB_MODEL_STATUS, JobUtil
28 from lcm.pub.utils.values import ignore_case_get
29 from lcm.ns_vnfs.const import VNF_STATUS, NFVO_VNF_INST_TIMEOUT_SECOND, INST_TYPE
30 from lcm.ns_vnfs.biz.wait_job import wait_job_finish
31 from lcm.ns_vnfs.biz.subscribe import SubscriptionDeletion
33 logger = logging.getLogger(__name__)
36 class TerminateVnfs(threading.Thread):
37 def __init__(self, data, vnf_inst_id, job_id):
38 threading.Thread.__init__(self)
39 self.vnf_inst_id = vnf_inst_id
41 self.vnfm_inst_id = ''
44 self.terminationType = data['terminationType']
45 self.gracefulTerminationTimeout = data['gracefulTerminationTimeout']
51 self.send_nf_terminate_to_vnfmDriver()
52 self.wait_vnfm_job_finish()
53 self.send_terminate_vnf_to_resMgr()
55 self.delete_vserver_in_aai()
56 self.delete_vnf_in_aai()
57 self.delete_subscription()
58 self.delete_data_from_db()
59 except NSLCMException as e:
60 self.set_job_err(e.message)
61 except Exception as ex:
62 logger.error(traceback.format_exc())
63 self.set_job_err(ex.message)
65 def set_vnf_status(self, vnf_inst_info):
66 vnf_status = vnf_inst_info.status
67 if (vnf_status == VNF_STATUS.TERMINATING):
68 logger.info('[VNF terminate] VNF is dealing by other application,try again later.')
69 raise NSLCMException('[VNF terminate] VNF is dealing by other application,try again later.')
71 vnf_inst_info.status = VNF_STATUS.TERMINATING
74 def get_vnf_inst(self):
75 vnf_inst = NfInstModel.objects.filter(nfinstid=self.vnf_inst_id)
76 if not vnf_inst.exists():
77 logger.warning('[VNF terminate] Vnf terminate [%s] does not exist.' % self.vnf_inst_id)
81 def add_progress(self, progress, status_decs, error_code=""):
82 JobUtil.add_job_status(self.job_id, progress, status_decs, error_code)
85 vnf_inst_info = self.get_vnf_inst()
87 self.add_progress(100, "TERM_VNF_NOT_EXIST_SUCCESS", "finished")
88 self.add_progress(2, "GET_VNF_INST_SUCCESS")
89 self.vnfm_inst_id = vnf_inst_info.vnfm_inst_id
90 self.vnf_uuid = vnf_inst_info.mnfinstid
92 self.add_progress(100, "TERM_VNF_NOT_EXIST_SUCCESS", "finished")
94 def check_nf_valid(self):
95 vnf_inst = NfInstModel.objects.filter(nfinstid=self.vnf_inst_id)
96 if not vnf_inst.exists():
97 logger.warning('[VNF terminate] Vnf instance [%s] is not exist.' % self.vnf_inst_id)
98 raise NSLCMException('[VNF terminate] Vnf instance is not exist.')
100 self.add_progress(100, "TERM_VNF_NOT_EXIST_SUCCESS", "finished")
101 raise NSLCMException('[VNF terminate] Vnf instance is not exist.')
102 self.set_vnf_status(vnf_inst[0])
104 def set_job_err(self, error_msg):
105 logger.error('VNF Terminate failed, detail message: %s' % error_msg)
106 NfInstModel.objects.filter(nfinstid=self.vnf_inst_id).update(status=VNF_STATUS.FAILED)
107 JobUtil.add_job_status(self.job_id, 255, 'VNF Terminate failed, detail message: %s' % error_msg, 0)
109 def send_nf_terminate_to_vnfmDriver(self):
110 req_param = json.JSONEncoder().encode({
111 'terminationType': self.terminationType,
112 'gracefulTerminationTimeout': self.gracefulTerminationTimeout})
113 rsp = send_nf_terminate_request(self.vnfm_inst_id, self.vnf_uuid, req_param)
114 self.vnfm_job_id = ignore_case_get(rsp, 'jobId')
116 def send_terminate_vnf_to_resMgr(self):
117 resmgr.terminate_vnf(self.vnf_inst_id)
119 def wait_vnfm_job_finish(self):
120 if not self.vnfm_job_id:
121 logger.warn("No Job, need not wait")
123 ret = wait_job_finish(vnfm_id=self.vnfm_inst_id,
124 vnfo_job_id=self.job_id,
125 vnfm_job_id=self.vnfm_job_id,
126 progress_range=[10, 90],
127 timeout=NFVO_VNF_INST_TIMEOUT_SECOND)
129 if ret != JOB_MODEL_STATUS.FINISHED:
130 logger.error('VNF terminate failed on VNFM side.')
131 raise NSLCMException('VNF terminate failed on VNFM side.')
133 def delete_subscription(self):
134 SubscriptionDeletion(self.vnfm_inst_id, self.vnf_inst_id).do_biz()
136 def delete_data_from_db(self):
137 NfInstModel.objects.filter(nfinstid=self.vnf_inst_id).delete()
138 JobUtil.add_job_status(self.job_id, 100, 'vnf terminate success', 0)
140 def delete_vnf_in_aai(self):
141 logger.debug("TerminateVnfs::delete_vnf_in_aai::delete vnf instance[%s] in aai." % self.vnf_inst_id)
143 # query vnf instance in aai, get resource_version
144 customer_info = query_vnf_aai(self.vnf_inst_id)
145 resource_version = customer_info["resource-version"]
147 # delete vnf instance from aai
148 resp_data, resp_status = delete_vnf_aai(self.vnf_inst_id, resource_version)
150 "Success to delete vnf[%s] from aai, resp_status: [%s]." % (self.vnf_inst_id, resp_status))
151 except NSLCMException as e:
152 logger.debug("Fail to delete vnf from aai[%s], detail message: %s" % (self.vnf_inst_id, e.message))
154 logger.error(traceback.format_exc())
156 def delete_vserver_in_aai(self):
157 logger.debug("delete_vserver_in_aai start!")
159 vm_inst_infos = VmInstModel.objects.filter(insttype=INST_TYPE.VNF, instid=self.vnf_inst_id)
160 for vm_inst_info in vm_inst_infos:
161 vserver_id = vm_inst_info.resouceid
162 vim_id = vm_inst_info.vimid
163 cloud_owner, cloud_region_id = split_vim_to_owner_region(vim_id)
164 # query vim_info from aai, get tenant
165 vim_info = get_vim_by_id(vim_id)
166 tenant_id = vim_info["tenantId"]
168 # query vserver instance in aai, get resource_version
169 vserver_info = query_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id)
170 resource_version = vserver_info["resource-version"]
172 # delete vserver instance from aai
173 resp_data, resp_status = delete_vserver_aai(cloud_owner, cloud_region_id,
174 tenant_id, vserver_id, resource_version)
176 "Success to delete vserver instance[%s] from aai, resp_status: [%s]." %
177 (vserver_id, resp_status))
178 logger.debug("delete_vserver_in_aai end!")
179 except NSLCMException as e:
180 logger.debug("Fail to delete vserver from aai, detail message: %s" % e.message)
182 logger.error(traceback.format_exc())