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.
16 from threading import Thread
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
29 logger = logging.getLogger(__name__)
32 class UpdateVnf(Thread):
33 def __init__(self, data, instanceid, job_id):
34 super(UpdateVnf, self).__init__()
36 self.nf_inst_id = instanceid
38 self.vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid)
39 self.lcm_op_occ = VnfLcmOpOcc(
40 vnf_inst_id=instanceid,
42 operation=OPERATION_TYPE.MODIFY_INFO,
43 task=OPERATION_TASK.MODIFY
47 logger.debug("start update for vnf %s", self.nf_inst_id)
48 key = "vnfInstanceName"
50 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.PROCESSING)
51 JobUtil.add_job_status(self.job_id, 50, "Start updating VNF.")
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())
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())
64 self.vnf_insts.update(vnfdid=self.data[key],
65 lastuptime=now_time())
67 JobUtil.add_job_status(self.job_id, 75, "Start sending notification.")
68 self.send_notification()
70 JobUtil.add_job_status(self.job_id, 100, "Update VNF success.")
71 except NFLCMException as e:
72 logger.error(e.args[0])
73 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, e.args[0])
74 JobUtil.add_job_status(self.job_id, 255, e.args[0])
75 except Exception as e:
76 logger.error(e.args[0])
77 logger.error(traceback.format_exc())
78 self.lcm_op_occ.notify_lcm(OPERATION_STATE_TYPE.FAILED, e.args[0])
79 JobUtil.add_job_status(self.job_id, 255, e.args[0])
81 def send_notification(self):
82 notify_data = prepare_notification(nfinstid=self.nf_inst_id,
84 operation=OPERATION_TYPE.MODIFY_INFO,
85 operation_state=OPERATION_STATE_TYPE.COMPLETED)
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]
92 logger.debug('Notification data: %s' % notify_data)
93 NotificationsUtil().send_notification(notify_data)