0721c5d92d23e5ebdd78d51eeace5d9e62b706e0
[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 from threading import Thread
16
17 from lcm.pub.database.models import NfInstModel
18 from lcm.nf.const import OPERATION_STATE_TYPE
19 from lcm.nf.const import OPERATION_TYPE
20 from lcm.pub.utils.notificationsutil import NotificationsUtil
21 from lcm.pub.utils.notificationsutil import prepare_notification
22 from lcm.pub.utils.timeutil import now_time
23
24 logger = logging.getLogger(__name__)
25
26
27 class UpdateVnf(Thread):
28     def __init__(self, data, instanceid, job_id):
29         self.data = data
30         self.nf_inst_id = instanceid
31         self.job_id = job_id
32         self.vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid)
33
34     def run(self):
35         logger.debug("start update for vnf %s", self.nf_inst_id)
36         key = "vnfInstanceName"
37         if key in self.data and self.data[key] is not None:
38             self.vnf_insts.update(nf_name=self.data[key],
39                                   lastuptime=now_time())
40
41         key = "vnfInstanceDescription"
42         if key in self.data and self.data[key] is not None:
43             self.vnf_insts.update(nf_desc=self.data[key],
44                                   lastuptime=now_time())
45
46         key = "vnfPkgId"
47         if key in self.data:
48             self.vnf_insts.update(vnfdid=self.data[key],
49                                   lastuptime=now_time())
50
51     def send_notification(self):
52         notify_data = prepare_notification(nfinstid=self.nf_inst_id,
53                                            jobid=self.job_id,
54                                            operation=OPERATION_TYPE.MODIFY_INFO,
55                                            operation_state=OPERATION_STATE_TYPE.COMPLETED)
56
57         notify_data["changedInfo"] = {}
58         for key in ("vnfInstanceName", "vnfInstanceDescription"):
59             if key in self.data and self.data[key] is not None:
60                 notify_data["changedInfo"][key] = self.data[key]
61
62         logger.debug('Notification data: %s' % notify_data)
63         NotificationsUtil().send_notification(notify_data)