Add lcm op occ for chg vnf flavour
[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.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
34
35 logger = logging.getLogger(__name__)
36
37
38 class ChangeVnfFlavour(Thread):
39     def __init__(self, data, nf_inst_id, job_id):
40         super(ChangeVnfFlavour, self).__init__()
41         self.data = data
42         self.nf_inst_id = nf_inst_id
43         self.job_id = job_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,
47             lcm_op_id=job_id,
48             operation=OPERATION_TYPE.CHANGE_FLAVOUR,
49             task=OPERATION_TASK.CHANGE_FLAVOUR
50         )
51
52     def run(self):
53         try:
54             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.STARTING)
55             JobUtil.add_job_status(
56                 self.job_id,
57                 10,
58                 "Start to apply grant."
59             )
60             self.apply_grant()
61             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
62             JobUtil.add_job_status(
63                 self.job_id,
64                 50,
65                 "Start to change vnf flavour."
66             )
67             self.lcm_op_occ.upd(
68                 sub_operation=SUB_OPERATION_TASK.GRANTED,
69                 operation_state=OPERATION_STATE_TYPE.PROCESSING
70             )
71             self.do_operation()
72             self.vnf_insts.update(
73                 status='INSTANTIATED',
74                 lastuptime=now_time()
75             )
76             self.send_notification()
77             JobUtil.add_job_status(
78                 self.job_id,
79                 100,
80                 "Change vnf flavour success."
81             )
82             self.lcm_op_occ.upd(
83                 sub_operation=SUB_OPERATION_TASK.SUCCESS,
84                 operation_state=OPERATION_STATE_TYPE.COMPLETED
85             )
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)
93
94     def pre_deal(self):
95         logger.debug("Start pre deal for VNF change_vnf_flavour task")
96
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
101             ))
102         self.lcm_op_occ.add()
103
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(
108             data=self.data,
109             nf_inst_id=self.nf_inst_id,
110             job_id=self.job_id,
111             grant_type=GRANT_TYPE.CHANGE_FLAVOUR,
112             vdus=vdus
113         )
114         logger.debug("Change flavour Grant result: %s", grant_result)
115
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
120
121     def send_notification(self):
122         data = prepare_notification_data(
123             nfinstid=self.nf_inst_id,
124             jobid=self.job_id,
125             changetype=CHANGE_TYPE.MODIFIED,
126             operation=OPERATION_TYPE.CHANGE_FLAVOUR
127         )
128         logger.debug('Notify request data = %s' % data)
129         NotificationsUtil().send_notification(data)
130
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()
136         )
137         self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, error_msg)
138         JobUtil.add_job_status(self.job_id, 255, error_msg)
139         self.lcm_op_occ.upd(
140             sub_operation=SUB_OPERATION_TASK.ERROR,
141             operation_state=OPERATION_STATE_TYPE.FAILED,
142             error={
143                 "status": 500,
144                 "detail": error_msg
145             }
146         )