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):
32 super(UpdateVnf, self).__init__()
34 self.nf_inst_id = instanceid
36 self.vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid)
39 logger.debug("start update for vnf %s", self.nf_inst_id)
40 key = "vnfInstanceName"
42 JobUtil.add_job_status(self.job_id, 50, "Start updating VNF.")
44 if key in self.data and self.data[key] is not None:
45 self.vnf_insts.update(nf_name=self.data[key],
46 lastuptime=now_time())
48 key = "vnfInstanceDescription"
49 if key in self.data and self.data[key] is not None:
50 self.vnf_insts.update(nf_desc=self.data[key],
51 lastuptime=now_time())
55 self.vnf_insts.update(vnfdid=self.data[key],
56 lastuptime=now_time())
58 JobUtil.add_job_status(self.job_id, 75, "Start sending notification.")
59 self.send_notification()
61 JobUtil.add_job_status(self.job_id, 100, "Update VNF success.")
62 except NFLCMException as e:
63 logger.error(e.message)
64 JobUtil.add_job_status(self.job_id, 255, e.message)
65 except Exception as e:
66 logger.error(e.message)
67 logger.error(traceback.format_exc())
68 JobUtil.add_job_status(self.job_id, 255, e.message)
70 def send_notification(self):
71 notify_data = prepare_notification(nfinstid=self.nf_inst_id,
73 operation=OPERATION_TYPE.MODIFY_INFO,
74 operation_state=OPERATION_STATE_TYPE.COMPLETED)
76 notify_data["changedInfo"] = {}
77 for key in ("vnfInstanceName", "vnfInstanceDescription"):
78 if key in self.data and self.data[key] is not None:
79 notify_data["changedInfo"][key] = self.data[key]
81 logger.debug('Notification data: %s' % notify_data)
82 NotificationsUtil().send_notification(notify_data)