Add lcm op occ to upd vnf
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / update_vnf.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 import logging
15 import traceback
16 from threading import Thread
17
18 from lcm.pub.database.models import NfInstModel
19 from lcm.pub.exceptions import NFLCMException
20 from lcm.nf.const import OPERATION_STATE_TYPE
21 from lcm.nf.const import OPERATION_TYPE
22 from lcm.nf.const import OPERATION_TASK
23 from lcm.pub.utils.notificationsutil import NotificationsUtil
24 from lcm.pub.utils.notificationsutil import prepare_notification
25 from lcm.pub.utils.jobutil import JobUtil
26 from lcm.pub.utils.timeutil import now_time
27 from .operate_vnf_lcm_op_occ import VnfLcmOpOcc
28
29 logger = logging.getLogger(__name__)
30
31
32 class UpdateVnf(Thread):
33     def __init__(self, data, instanceid, job_id):
34         super(UpdateVnf, self).__init__()
35         self.data = data
36         self.nf_inst_id = instanceid
37         self.job_id = job_id
38         self.vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid)
39         self.lcm_op_occ = VnfLcmOpOcc(
40             vnf_inst_id=instanceid,
41             lcm_op_id=job_id,
42             operation=OPERATION_TYPE.MODIFY_INFO,
43             task=OPERATION_TASK.MODIFY
44         )
45
46     def run(self):
47         logger.debug("start update for vnf %s", self.nf_inst_id)
48         key = "vnfInstanceName"
49         try:
50             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
51             JobUtil.add_job_status(self.job_id, 50, "Start updating VNF.")
52
53             if key in self.data and self.data[key] is not None:
54                 self.vnf_insts.update(nf_name=self.data[key],
55                                       lastuptime=now_time())
56
57             key = "vnfInstanceDescription"
58             if key in self.data and self.data[key] is not None:
59                 self.vnf_insts.update(nf_desc=self.data[key],
60                                       lastuptime=now_time())
61
62             key = "vnfPkgId"
63             if key in self.data:
64                 self.vnf_insts.update(vnfdid=self.data[key],
65                                       lastuptime=now_time())
66
67             JobUtil.add_job_status(self.job_id, 75, "Start sending notification.")
68             self.send_notification()
69
70             JobUtil.add_job_status(self.job_id, 100, "Update VNF success.")
71         except NFLCMException as e:
72             logger.error(e.message)
73             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, e.message)
74             JobUtil.add_job_status(self.job_id, 255, e.message)
75         except Exception as e:
76             logger.error(e.message)
77             logger.error(traceback.format_exc())
78             self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, e.message)
79             JobUtil.add_job_status(self.job_id, 255, e.message)
80
81     def send_notification(self):
82         notify_data = prepare_notification(nfinstid=self.nf_inst_id,
83                                            jobid=self.job_id,
84                                            operation=OPERATION_TYPE.MODIFY_INFO,
85                                            operation_state=OPERATION_STATE_TYPE.COMPLETED)
86
87         notify_data["changedInfo"] = {}
88         for key in ("vnfInstanceName", "vnfInstanceDescription"):
89             if key in self.data and self.data[key] is not None:
90                 notify_data["changedInfo"][key] = self.data[key]
91
92         logger.debug('Notification data: %s' % notify_data)
93         NotificationsUtil().send_notification(notify_data)