1 # Copyright 2019 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.biz.grant_vnf import grant_resource
21 from lcm.nf.const import GRANT_TYPE, CHANGE_TYPE
22 from lcm.nf.const import VNF_STATUS
23 from lcm.nf.const import OPERATION_TYPE, OPERATION_TASK
24 from lcm.nf.const import OPERATION_STATE_TYPE
25 from lcm.nf.const import SUB_OPERATION_TASK
26 from lcm.pub.utils.notificationsutil import NotificationsUtil, prepare_notification_data
27 from lcm.pub.utils.values import ignore_case_get
28 from lcm.pub.utils.timeutil import now_time
29 from lcm.pub.utils.jobutil import JobUtil
30 from lcm.pub.exceptions import NFLCMException
31 from lcm.pub.exceptions import NFLCMExceptionConflict
32 from lcm.pub.database.models import NfInstModel
33 from .operate_vnf_lcm_op_occ import VnfLcmOpOcc
35 logger = logging.getLogger(__name__)
38 class ChangeVnfFlavour(Thread):
39 def __init__(self, data, nf_inst_id, job_id):
40 super(ChangeVnfFlavour, self).__init__()
42 self.nf_inst_id = nf_inst_id
44 self.vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
45 self.lcm_op_occ = VnfLcmOpOcc(
46 vnf_inst_id=nf_inst_id,
48 operation=OPERATION_TYPE.CHANGE_FLAVOUR,
49 task=OPERATION_TASK.CHANGE_FLAVOUR
54 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING)
55 JobUtil.add_job_status(
58 "Start to apply grant."
61 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
62 JobUtil.add_job_status(
65 "Start to change vnf flavour."
68 sub_operation=SUB_OPERATION_TASK.GRANTED,
69 operation_state=OPERATION_STATE_TYPE.PROCESSING
72 self.vnf_insts.update(
73 status='INSTANTIATED',
76 self.send_notification()
77 JobUtil.add_job_status(
80 "Change vnf flavour success."
83 sub_operation=SUB_OPERATION_TASK.SUCCESS,
84 operation_state=OPERATION_STATE_TYPE.COMPLETED
86 except NFLCMException as e:
87 logger.error(e.message)
88 self.change_vnf_flavour_failed_handle(e.message)
89 except Exception as e:
90 logger.error(e.message)
91 logger.error(traceback.format_exc())
92 self.change_vnf_flavour_failed_handle(e.message)
95 logger.debug("Start pre deal for VNF change_vnf_flavour task")
97 vnf_is_in_processing, vnf_op = self.lcm_op_occ.is_in_processing()
98 if vnf_is_in_processing:
99 raise NFLCMExceptionConflict('VNF(%s) %s in processing.' % (
100 self.nf_inst_id, vnf_op
102 self.lcm_op_occ.add()
104 def apply_grant(self):
105 logger.debug("Start change flavour apply grant")
106 vdus = ignore_case_get(self.vnfd_info, "vdus")
107 grant_result = grant_resource(
109 nf_inst_id=self.nf_inst_id,
111 grant_type=GRANT_TYPE.CHANGE_FLAVOUR,
114 logger.debug("Change flavour Grant result: %s", grant_result)
116 def do_operation(self):
117 logger.info('Operation resource begin')
118 self.vnfd_info = json.loads(self.vnf_insts[0].vnfd_model)
119 # TODO: Add operation logic
121 def send_notification(self):
122 data = prepare_notification_data(
123 nfinstid=self.nf_inst_id,
125 changetype=CHANGE_TYPE.MODIFIED,
126 operation=OPERATION_TYPE.CHANGE_FLAVOUR
128 logger.debug('Notify request data = %s' % data)
129 NotificationsUtil().send_notification(data)
131 def change_vnf_flavour_failed_handle(self, error_msg):
132 logger.error('Chnage vnf flavour failed, detail message: %s', error_msg)
133 self.vnf_insts.update(
134 status=VNF_STATUS.FAILED,
135 lastuptime=now_time()
137 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg)
138 JobUtil.add_job_status(self.job_id, 255, error_msg)
140 sub_operation=SUB_OPERATION_TASK.ERROR,
141 operation_state=OPERATION_STATE_TYPE.FAILED,