summaryrefslogtreecommitdiffstats
path: root/lcm/lcm/nf/vnfs/vnf_cancel/term_vnf.py
blob: ccf2c327a5b2a568d5b024e7e2d0484f98f8b297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Copyright 2017 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import traceback
from threading import Thread

from lcm.pub.database.models import JobStatusModel, NfInstModel
from lcm.pub.exceptions import NFLCMException
from lcm.pub.utils.jobutil import JobUtil
from lcm.pub.utils.timeutil import now_time
from lcm.pub.utils.values import ignore_case_get

logger = logging.getLogger(__name__)


class TermVnf(Thread):
    def __init__(self, data, nf_inst_id, job_id):
        super(TermVnf, self).__init__()
        self.data = data
        self.nf_inst_id = nf_inst_id
        self.job_id = job_id
        self.terminationType = ignore_case_get(self.data, "terminationType")
        self.gracefulTerminationTimeout = ignore_case_get(self.data, "gracefulTerminationTimeout")

    def run(self):
        try:
            self.term_pre()
            JobUtil.add_job_status(self.job_id, 100, "Terminate Vnf success.")
            is_exist = JobStatusModel.objects.filter(jobid=self.job_id).exists()
            logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
        except NFLCMException as e:
            logger.error('VNF instantiation failed, detail message: %s' % e.message)
            # NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='failed', lastuptime=now_time())
            JobUtil.add_job_status(self.job_id, 255, e.message)
            # self.vnf_term_failed_handle(e.message)
        except:
            # self.vnf_term_failed_handle('unexpected exception')
            logger.error(traceback.format_exc())

    def term_pre(self):
        vnf_insts = NfInstModel.objects.filter(pk=self.nf_inst_id)
        if not vnf_insts.exists():
            raise NFLCMException('VnfInst(%s) does not exist' % self.nf_inst_id)
        sel_vnf = vnf_insts[0]
        if sel_vnf.instantiationState != 'VNF_INSTANTIATED':
            raise NFLCMException("No instantiated vnf")
        if self.terminationType == 'GRACEFUL' and not self.gracefulTerminationTimeout:
            raise NFLCMException("Graceful termination must set timeout")
        JobUtil.add_job_status(self.job_id, 10, 'Nf terminating pre-check finish')
        logger.info("Nf terminating pre-check finish")