Add lcm op occ to vnf heal
[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.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
32
33 logger = logging.getLogger(__name__)
34
35
36 class ChangeVnfFlavour(Thread):
37     def __init__(self, data, nf_inst_id, job_id):
38         super(ChangeVnfFlavour, self).__init__()
39         self.data = data
40         self.nf_inst_id = nf_inst_id
41         self.job_id = job_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,
45             lcm_op_id=job_id,
46             operation=OPERATION_TYPE.CHANGE_FLAVOUR,
47             task=OPERATION_TASK.CHANGE_FLAVOUR
48         )
49
50     def run(self):
51         try:
52             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING)
53             JobUtil.add_job_status(self.job_id,
54                                    10,
55                                    "Start to apply grant.")
56             self.apply_grant()
57             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
58             JobUtil.add_job_status(self.job_id,
59                                    50,
60                                    "Start to change vnf flavour.")
61             self.do_operation()
62             self.vnf_insts.update(
63                 status='INSTANTIATED',
64                 lastuptime=now_time()
65             )
66             self.send_notification()
67             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.COMPLETED)
68             JobUtil.add_job_status(self.job_id,
69                                    100,
70                                    "Change vnf flavour success.")
71         except NFLCMException as e:
72             logger.error(e.message)
73             self.change_vnf_flavour_failed_handle(e.message)
74         except Exception as e:
75             logger.error(e.message)
76             logger.error(traceback.format_exc())
77             self.change_vnf_flavour_failed_handle(e.message)
78
79     def apply_grant(self):
80         logger.debug("Start change flavour apply grant")
81         vdus = ignore_case_get(self.vnfd_info, "vdus")
82         grant_result = grant_resource(data=self.data,
83                                       nf_inst_id=self.nf_inst_id,
84                                       job_id=self.job_id,
85                                       grant_type=GRANT_TYPE.CHANGE_FLAVOUR,
86                                       vdus=vdus)
87         logger.debug("Change flavour Grant result: %s", grant_result)
88
89     def do_operation(self):
90         logger.info('Operation resource begin')
91         self.vnfd_info = json.loads(self.vnf_insts[0].vnfd_model)
92         # TODO: Add operation logic
93
94     def send_notification(self):
95         data = prepare_notification_data(nfinstid=self.nf_inst_id,
96                                          jobid=self.job_id,
97                                          changetype=CHANGE_TYPE.MODIFIED,
98                                          operation=self.op_type)
99         logger.debug('Notify request data = %s' % data)
100         NotificationsUtil().send_notification(data)
101
102     def change_vnf_flavour_failed_handle(self, error_msg):
103         logger.error('Chnage vnf flavour failed, detail message: %s', error_msg)
104         self.vnf_insts.update(status=VNF_STATUS.FAILED,
105                               lastuptime=now_time())
106         self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg)
107         JobUtil.add_job_status(self.job_id, 255, error_msg)