Add code of Terminate VNF instance
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / vnf_cancel / term_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 logging
15 import traceback
16 from threading import Thread
17
18 from lcm.pub.database.models import JobStatusModel, NfInstModel
19 from lcm.pub.exceptions import NFLCMException
20 from lcm.pub.utils.jobutil import JobUtil
21 from lcm.pub.utils.timeutil import now_time
22 from lcm.pub.utils.values import ignore_case_get
23
24 logger = logging.getLogger(__name__)
25
26
27 class TermVnf(Thread):
28     def __init__(self, data, nf_inst_id, job_id):
29         super(TermVnf, self).__init__()
30         self.data = data
31         self.nf_inst_id = nf_inst_id
32         self.job_id = job_id
33         self.terminationType = ignore_case_get(self.data, "terminationType")
34         self.gracefulTerminationTimeout = ignore_case_get(self.data, "gracefulTerminationTimeout")
35
36     def run(self):
37         try:
38             self.term_pre()
39             JobUtil.add_job_status(self.job_id, 100, "Terminate Vnf success.")
40             is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists()
41             logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
42         except NFLCMException as e:
43             logger.error('VNF instantiation failed, detail message: %s' % e.message)
44             # NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='failed', lastuptime=now_time())
45             JobUtil.add_job_status(self.job_id, 255, e.message)
46             # self.vnf_term_failed_handle(e.message)
47         except:
48             # self.vnf_term_failed_handle('unexpected exception')
49             logger.error(traceback.format_exc())
50
51     def term_pre(self):
52         vnf_insts = NfInstModel.objects.filter(pk=self.nf_inst_id)
53         if not vnf_insts.exists():
54             raise NFLCMException('VnfInst(%s) does not exist' % self.nf_inst_id)
55         sel_vnf = vnf_insts[0]
56         if sel_vnf.instantiationState != 'VNF_INSTANTIATED':
57             raise NFLCMException("No instantiated vnf")
58         if self.terminationType == 'GRACEFUL' and not self.gracefulTerminationTimeout:
59             raise NFLCMException("Graceful termination must set timeout")
60         JobUtil.add_job_status(self.job_id, 10, 'Nf terminating pre-check finish')
61         logger.info("Nf terminating pre-check finish")