Add ut cases for update 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.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
26
27 logger = logging.getLogger(__name__)
28
29
30 class UpdateVnf(Thread):
31     def __init__(self, data, instanceid, job_id):
32         self.data = data
33         self.nf_inst_id = instanceid
34         self.job_id = job_id
35         self.vnf_insts = NfInstModel.objects.filter(nfinstid=instanceid)
36
37     def run(self):
38         logger.debug("start update for vnf %s", self.nf_inst_id)
39         key = "vnfInstanceName"
40         try:
41             JobUtil.add_job_status(self.job_id, 50, "Start updating VNF.")
42
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())
46
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())
51
52             key = "vnfPkgId"
53             if key in self.data:
54                 self.vnf_insts.update(vnfdid=self.data[key],
55                                       lastuptime=now_time())
56
57             JobUtil.add_job_status(self.job_id, 75, "Start sending notification.")
58             self.send_notification()
59
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)
68
69     def send_notification(self):
70         notify_data = prepare_notification(nfinstid=self.nf_inst_id,
71                                            jobid=self.job_id,
72                                            operation=OPERATION_TYPE.MODIFY_INFO,
73                                            operation_state=OPERATION_STATE_TYPE.COMPLETED)
74
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]
79
80         logger.debug('Notification data: %s' % notify_data)
81         NotificationsUtil().send_notification(notify_data)