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.pub.utils.notificationsutil import NotificationsUtil
23 from lcm.pub.utils.notificationsutil import prepare_notification
24 from lcm.pub.utils.jobutil import JobUtil
25 from lcm.pub.utils.timeutil import now_time
27 logger = logging.getLogger(__name__)
30 class UpdateVnf(Thread):
31 def __init__(self, data, instanceid, job_id):
33 self.nf_inst_id = instanceid
35 self.vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid)
38 logger.debug("start update for vnf %s", self.nf_inst_id)
39 key = "vnfInstanceName"
41 JobUtil.add_job_status(self.job_id, 50, "Start updating VNF.")
43 if key in self.data and self.data[key] is not None:
44 self.vnf_insts.update(nf_name=self.data[key],
45 lastuptime=now_time())
47 key = "vnfInstanceDescription"
48 if key in self.data and self.data[key] is not None:
49 self.vnf_insts.update(nf_desc=self.data[key],
50 lastuptime=now_time())
54 self.vnf_insts.update(vnfdid=self.data[key],
55 lastuptime=now_time())
57 JobUtil.add_job_status(self.job_id, 75, "Start sending notification.")
58 self.send_notification()
60 JobUtil.add_job_status(self.job_id, 100, "Update VNF success.")
61 except NFLCMException as e:
62 logger.error(e.message)
63 JobUtil.add_job_status(self.job_id, 255, e.message)
64 except Exception as e:
65 logger.error(e.message)
66 logger.error(traceback.format_exc())
67 JobUtil.add_job_status(self.job_id, 255, e.message)
69 def send_notification(self):
70 notify_data = prepare_notification(nfinstid=self.nf_inst_id,
72 operation=OPERATION_TYPE.MODIFY_INFO,
73 operation_state=OPERATION_STATE_TYPE.COMPLETED)
75 notify_data["changedInfo"] = {}
76 for key in ("vnfInstanceName", "vnfInstanceDescription"):
77 if key in self.data and self.data[key] is not None:
78 notify_data["changedInfo"][key] = self.data[key]
80 logger.debug('Notification data: %s' % notify_data)
81 NotificationsUtil().send_notification(notify_data)