1 # Copyright 2017 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.
18 from threading import Thread
20 from lcm.nf.const import VNF_STATUS, RESOURCE_MAP
21 from lcm.pub.database.models import (
22 NfInstModel, VmInstModel, NetworkInstModel,
23 StorageInstModel, PortInstModel, VNFCInstModel,
24 FlavourInstModel, SubNetworkInstModel
26 from lcm.pub.exceptions import NFLCMException
27 from lcm.pub.msapi.gvnfmdriver import prepare_notification_data, notify_lcm_to_nfvo
28 from lcm.pub.utils.jobutil import JobUtil
29 from lcm.pub.utils.timeutil import now_time
30 from lcm.pub.utils.values import ignore_case_get
31 from lcm.pub.vimapi import adaptor
32 from lcm.nf.biz.grant_vnf import grant_resource
33 from lcm.nf.const import GRANT_TYPE
35 logger = logging.getLogger(__name__)
38 class TerminateVnf(Thread):
39 def __init__(self, data, nf_inst_id, job_id):
40 super(TerminateVnf, self).__init__()
42 self.nf_inst_id = nf_inst_id
44 self.terminationType = ignore_case_get(self.data, "terminationType")
45 self.gracefulTerminationTimeout = ignore_case_get(self.data, "gracefulTerminationTimeout")
46 self.inst_resource = {'volumn': [], 'network': [], 'subnet': [], 'port': [], 'flavor': [], 'vm': []}
47 self.grant_type = GRANT_TYPE.TERMINATE
52 vdus = VmInstModel.objects.filter(instid=self.nf_inst_id, is_predefined=1)
53 apply_result = grant_resource(data=self.data, nf_inst_id=self.nf_inst_id, job_id=self.job_id,
54 grant_type=self.grant_type, vdus=vdus)
55 logger.info("Grant resource end, response: %s" % apply_result)
56 JobUtil.add_job_status(self.job_id, 20, 'Nf terminating grant_resource finish')
57 self.query_inst_resource()
58 self.query_notify_data()
59 self.delete_resource()
61 JobUtil.add_job_status(self.job_id, 100, "Terminate Vnf success.")
62 except NFLCMException as e:
63 self.vnf_term_failed_handle(e.message)
64 except Exception as e:
65 logger.error(e.message)
66 self.vnf_term_failed_handle(traceback.format_exc())
69 vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
70 if not vnf_insts.exists():
71 logger.warn('VnfInst(%s) does not exist' % self.nf_inst_id)
73 if self.terminationType == 'GRACEFUL' and not self.gracefulTerminationTimeout:
74 logger.warn("Set Graceful default termination timeout = 60")
75 self.gracefulTerminationTimeout = 60
76 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.TERMINATING)
77 JobUtil.add_job_status(self.job_id, 10, 'Nf terminating pre-check finish')
78 logger.info("Nf terminating pre-check finish")
81 def query_inst_resource(self):
82 logger.info('Query resource begin')
83 for resource_type in RESOURCE_MAP.keys():
84 resource_table = globals().get(resource_type + 'InstModel')
85 resource_insts = resource_table.objects.filter(instid=self.nf_inst_id)
86 for resource_inst in resource_insts:
87 if not resource_inst.resourceid:
89 self.inst_resource[RESOURCE_MAP.get(resource_type)].append(self.get_resource(resource_inst))
90 logger.info('Query resource end, resource=%s' % self.inst_resource)
92 def get_resource(self, resource):
94 "vim_id": resource.vimid,
95 "tenant_id": resource.tenant,
96 "res_id": resource.resourceid,
97 "is_predefined": resource.is_predefined
100 def query_notify_data(self):
101 self.notify_data = prepare_notification_data(self.nf_inst_id, self.job_id, "RMOVED")
102 NetworkInstModel.objects.filter(instid=self.nf_inst_id)
103 StorageInstModel.objects.filter(instid=self.nf_inst_id)
104 PortInstModel.objects.filter(instid=self.nf_inst_id)
105 VNFCInstModel.objects.filter(instid=self.nf_inst_id)
106 FlavourInstModel.objects.filter(instid=self.nf_inst_id)
107 SubNetworkInstModel.objects.filter(instid=self.nf_inst_id)
109 def delete_resource(self):
110 logger.info('Rollback resource begin')
111 adaptor.delete_vim_res(self.inst_resource, self.do_notify_delete)
112 logger.info('Rollback resource complete')
114 def do_notify_delete(self, res_type, res_id):
115 logger.error('Deleting [%s] resource, resourceid [%s]' % (res_type, res_id))
116 resource_type = RESOURCE_MAP.keys()[RESOURCE_MAP.values().index(res_type)]
117 resource_table = globals().get(resource_type + 'InstModel')
118 resource_table.objects.filter(instid=self.nf_inst_id, resourceid=res_id).delete()
120 def lcm_notify(self):
121 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='NOT_INSTANTIATED', lastuptime=now_time())
122 logger.info('Send notify request to nfvo')
123 resp = notify_lcm_to_nfvo(json.dumps(self.notify_data))
124 logger.info('Lcm notify end, response: %s' % resp)
126 def vnf_term_failed_handle(self, error_msg):
127 logger.error('VNF termination failed, detail message: %s' % error_msg)
128 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='failed', lastuptime=now_time())
129 JobUtil.add_job_status(self.job_id, 255, error_msg)