f4871688d3aaf7c4711e19b13cb6361c81c54a18
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / change_vnf_flavour.py
1 # Copyright 2019 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import logging
16 import traceback
17 import json
18 from threading import Thread
19
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
29
30 logger = logging.getLogger(__name__)
31
32
33 class ChangeVnfFlavour(Thread):
34     def __init__(self, data, nf_inst_id, job_id):
35         super(ChangeVnfFlavour, self).__init__()
36         self.data = data
37         self.nf_inst_id = nf_inst_id
38         self.job_id = job_id
39         self.vnf_insts = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
40
41     def run(self):
42         try:
43             JobUtil.add_job_status(self.job_id,
44                                    10,
45                                    "Start to apply grant.")
46             self.apply_grant()
47             JobUtil.add_job_status(self.job_id,
48                                    50,
49                                    "Start to change vnf flavour.")
50             self.do_operation()
51             self.vnf_insts.update(
52                 status='INSTANTIATED',
53                 lastuptime=now_time()
54             )
55             self.send_notification()
56             JobUtil.add_job_status(self.job_id,
57                                    100,
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)
66
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,
72                                       job_id=self.job_id,
73                                       grant_type=GRANT_TYPE.CHANGE_FLAVOUR,
74                                       vdus=vdus)
75         logger.debug("Change flavour Grant result: %s", grant_result)
76
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
81
82     def send_notification(self):
83         data = prepare_notification_data(nfinstid=self.nf_inst_id,
84                                          jobid=self.job_id,
85                                          changetype=CHANGE_TYPE.MODIFIED,
86                                          operation=self.op_type)
87         logger.debug('Notify request data = %s' % data)
88         NotificationsUtil().send_notification(data)
89
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)