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.pub.utils.notificationsutil import NotificationsUtil, prepare_notification_data
24 from lcm.pub.utils.values import ignore_case_get
25 from lcm.pub.utils.timeutil import now_time
26 from lcm.pub.utils.jobutil import JobUtil
27 from lcm.pub.exceptions import NFLCMException
28 from lcm.pub.database.models import NfInstModel
30 logger = logging.getLogger(__name__)
33 class ChangeVnfFlavour(Thread):
34 def __init__(self, data, nf_inst_id, job_id):
35 super(ChangeVnfFlavour, self).__init__()
37 self.nf_inst_id = nf_inst_id
39 self.vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
43 JobUtil.add_job_status(self.job_id,
45 "Start to apply grant.")
47 JobUtil.add_job_status(self.job_id,
49 "Start to change vnf flavour.")
51 self.vnf_insts.update(
52 status='INSTANTIATED',
55 self.send_notification()
56 JobUtil.add_job_status(self.job_id,
58 "Change vnf flavour success.")
59 except NFLCMException as e:
60 logger.error(e.message)
61 self.change_vnf_flavour_failed_handle(e.message)
62 except Exception as e:
63 logger.error(e.message)
64 logger.error(traceback.format_exc())
65 self.change_vnf_flavour_failed_handle(e.message)
67 def apply_grant(self):
68 logger.debug("Start change flavour apply grant")
69 vdus = ignore_case_get(self.vnfd_info, "vdus")
70 grant_result = grant_resource(data=self.data,
71 nf_inst_id=self.nf_inst_id,
73 grant_type=GRANT_TYPE.CHANGE_FLAVOUR,
75 logger.debug("Change flavour Grant result: %s", grant_result)
77 def do_operation(self):
78 logger.info('Operation resource begin')
79 self.vnfd_info = json.loads(self.vnf_insts[0].vnfd_model)
80 # TODO: Add operation logic
82 def send_notification(self):
83 data = prepare_notification_data(nfinstid=self.nf_inst_id,
85 changetype=CHANGE_TYPE.MODIFIED,
86 operation=self.op_type)
87 logger.debug('Notify request data = %s' % data)
88 NotificationsUtil().send_notification(data)
90 def change_vnf_flavour_failed_handle(self, error_msg):
91 logger.error('Chnage vnf flavour failed, detail message: %s', error_msg)
92 self.vnf_insts.update(status=VNF_STATUS.FAILED,
93 lastuptime=now_time())
94 JobUtil.add_job_status(self.job_id, 255, error_msg)