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
28 from lcm.pub.msapi.gvnfmdriver import notify_lcm_to_nfvo
29 from lcm.pub.utils.jobutil import JobUtil
30 from lcm.pub.utils.timeutil import now_time
31 from lcm.pub.utils.notificationsutil import NotificationsUtil
32 from lcm.pub.utils.values import ignore_case_get
33 from lcm.pub.vimapi import adaptor
34 from lcm.nf.biz.grant_vnf import grant_resource
35 from lcm.nf.const import CHANGE_TYPE, GRANT_TYPE, OPERATION_TYPE
37 logger = logging.getLogger(__name__)
40 class TerminateVnf(Thread):
41 def __init__(self, data, nf_inst_id, job_id):
42 super(TerminateVnf, self).__init__()
44 self.nf_inst_id = nf_inst_id
46 self.terminationType = ignore_case_get(self.data, "terminationType")
47 self.gracefulTerminationTimeout = ignore_case_get(self.data, "gracefulTerminationTimeout")
48 self.inst_resource = {'volumn': [], 'network': [], 'subnet': [], 'port': [], 'flavor': [], 'vm': []}
49 self.grant_type = GRANT_TYPE.TERMINATE
54 vdus = VmInstModel.objects.filter(instid=self.nf_inst_id, is_predefined=1)
55 apply_result = grant_resource(data=self.data, nf_inst_id=self.nf_inst_id, job_id=self.job_id,
56 grant_type=self.grant_type, vdus=vdus)
57 logger.info("Grant resource end, response: %s" % apply_result)
58 JobUtil.add_job_status(self.job_id, 20, 'Nf terminating grant_resource finish')
59 self.query_inst_resource()
60 self.query_notify_data()
61 self.delete_resource()
63 JobUtil.add_job_status(self.job_id, 100, "Terminate Vnf success.")
64 except NFLCMException as e:
65 self.vnf_term_failed_handle(e.message)
66 except Exception as e:
67 logger.error(e.message)
68 self.vnf_term_failed_handle(traceback.format_exc())
71 vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
72 if not vnf_insts.exists():
73 logger.warn('VnfInst(%s) does not exist' % self.nf_inst_id)
75 if self.terminationType == 'GRACEFUL' and not self.gracefulTerminationTimeout:
76 logger.warn("Set Graceful default termination timeout = 60")
77 self.gracefulTerminationTimeout = 60
78 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status=VNF_STATUS.TERMINATING)
79 JobUtil.add_job_status(self.job_id, 10, 'Nf terminating pre-check finish')
80 logger.info("Nf terminating pre-check finish")
83 def query_inst_resource(self):
84 logger.info('Query resource begin')
85 for resource_type in RESOURCE_MAP.keys():
86 resource_table = globals().get(resource_type + 'InstModel')
87 resource_insts = resource_table.objects.filter(instid=self.nf_inst_id)
88 for resource_inst in resource_insts:
89 if not resource_inst.resourceid:
91 self.inst_resource[RESOURCE_MAP.get(resource_type)].append(self.get_resource(resource_inst))
92 logger.info('Query resource end, resource=%s' % self.inst_resource)
94 def get_resource(self, resource):
96 "vim_id": resource.vimid,
97 "tenant_id": resource.tenant,
98 "res_id": resource.resourceid,
99 "is_predefined": resource.is_predefined
102 def query_notify_data(self):
103 self.notify_data = prepare_notification_data(self.nf_inst_id, self.job_id, CHANGE_TYPE.REMOVED, OPERATION_TYPE.TERMINATE)
104 NetworkInstModel.objects.filter(instid=self.nf_inst_id)
105 StorageInstModel.objects.filter(instid=self.nf_inst_id)
106 PortInstModel.objects.filter(instid=self.nf_inst_id)
107 VNFCInstModel.objects.filter(instid=self.nf_inst_id)
108 FlavourInstModel.objects.filter(instid=self.nf_inst_id)
109 SubNetworkInstModel.objects.filter(instid=self.nf_inst_id)
111 def delete_resource(self):
112 logger.info('Rollback resource begin')
113 adaptor.delete_vim_res(self.inst_resource, self.do_notify_delete)
114 logger.info('Rollback resource complete')
116 def do_notify_delete(self, res_type, res_id):
117 logger.debug('Deleting [%s] resource, resourceid [%s]' % (res_type, res_id))
118 resource_type = RESOURCE_MAP.keys()[RESOURCE_MAP.values().index(res_type)]
119 resource_table = globals().get(resource_type + 'InstModel')
120 resource_table.objects.filter(instid=self.nf_inst_id, resourceid=res_id).delete()
122 def lcm_notify(self):
123 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(status='NOT_INSTANTIATED', lastuptime=now_time())
124 logger.info('Send notify request to nfvo')
126 resp = notify_lcm_to_nfvo(json.dumps(self.notify_data))
127 logger.info('Lcm notify end, response: %s' % resp)
128 except Exception as e:
129 logger.error("Lcm terminate notify failed: %s", e.message)
130 NotificationsUtil().send_notification(self.notify_data)
132 def vnf_term_failed_handle(self, error_msg):
133 logger.error('VNF termination failed, detail message: %s' % error_msg)
134 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).update(lastuptime=now_time())
135 JobUtil.add_job_status(self.job_id, 255, error_msg)