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.pub.utils.notificationsutil import NotificationsUtil, prepare_notification_data
26 from lcm.pub.utils.values import ignore_case_get
27 from lcm.pub.utils.timeutil import now_time
28 from lcm.pub.utils.jobutil import JobUtil
29 from lcm.pub.exceptions import NFLCMException
30 from lcm.pub.database.models import NfInstModel
31 from .operate_vnf_lcm_op_occ import VnfLcmOpOcc
33 logger = logging.getLogger(__name__)
36 class ChangeVnfFlavour(Thread):
37 def __init__(self, data, nf_inst_id, job_id):
38 super(ChangeVnfFlavour, self).__init__()
40 self.nf_inst_id = nf_inst_id
42 self.vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
43 self.lcm_op_occ = VnfLcmOpOcc(
44 vnf_inst_id=nf_inst_id,
46 operation=OPERATION_TYPE.CHANGE_FLAVOUR,
47 task=OPERATION_TASK.CHANGE_FLAVOUR
52 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING)
53 JobUtil.add_job_status(self.job_id,
55 "Start to apply grant.")
57 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
58 JobUtil.add_job_status(self.job_id,
60 "Start to change vnf flavour.")
62 self.vnf_insts.update(
63 status='INSTANTIATED',
66 self.send_notification()
67 JobUtil.add_job_status(self.job_id,
69 "Change vnf flavour success.")
70 except NFLCMException as e:
71 logger.error(e.message)
72 self.change_vnf_flavour_failed_handle(e.message)
73 except Exception as e:
74 logger.error(e.message)
75 logger.error(traceback.format_exc())
76 self.change_vnf_flavour_failed_handle(e.message)
78 def apply_grant(self):
79 logger.debug("Start change flavour apply grant")
80 vdus = ignore_case_get(self.vnfd_info, "vdus")
81 grant_result = grant_resource(data=self.data,
82 nf_inst_id=self.nf_inst_id,
84 grant_type=GRANT_TYPE.CHANGE_FLAVOUR,
86 logger.debug("Change flavour Grant result: %s", grant_result)
88 def do_operation(self):
89 logger.info('Operation resource begin')
90 self.vnfd_info = json.loads(self.vnf_insts[0].vnfd_model)
91 # TODO: Add operation logic
93 def send_notification(self):
94 data = prepare_notification_data(nfinstid=self.nf_inst_id,
96 changetype=CHANGE_TYPE.MODIFIED,
97 operation=OPERATION_TYPE.CHANGE_FLAVOUR)
98 logger.debug('Notify request data = %s' % data)
99 NotificationsUtil().send_notification(data)
101 def change_vnf_flavour_failed_handle(self, error_msg):
102 logger.error('Chnage vnf flavour failed, detail message: %s', error_msg)
103 self.vnf_insts.update(status=VNF_STATUS.FAILED,
104 lastuptime=now_time())
105 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg)
106 JobUtil.add_job_status(self.job_id, 255, error_msg)