1 # Copyright (c) 2018, CMCC Technologies Co., Ltd.
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.
20 from lcm.pub.database.models import NfInstModel
21 from lcm.pub.exceptions import NSLCMException
22 from lcm.pub.msapi.vnfmdriver import send_nf_operate_request
23 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
24 from lcm.pub.utils.values import ignore_case_get
25 from lcm.ns_vnfs.const import VNF_STATUS
26 from lcm.ns_vnfs.biz.wait_job import wait_job_finish
30 logger = logging.getLogger(__name__)
33 class NFOperateService(threading.Thread):
34 def __init__(self, vnf_instance_id, data):
35 super(NFOperateService, self).__init__()
36 self.vnf_instance_id = vnf_instance_id
38 self.job_id = JobUtil.create_job("NF", JOB_TYPE.HEAL_VNF, vnf_instance_id)
41 self.nf_additional_params = {}
42 self.nf_operate_params = data
43 self.m_nf_inst_id = ''
44 self.vnfm_inst_id = ''
49 except NSLCMException as e:
50 JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message)
52 logger.error(traceback.format_exc())
53 JobUtil.add_job_status(self.job_id, JOB_ERROR, 'nf update fail')
56 self.update_job(1, desc='nf update start')
57 self.get_and_check_params()
58 self.update_nf_status(VNF_STATUS.UPDATING)
59 self.send_nf_operating_request()
60 self.update_nf_status(VNF_STATUS.ACTIVE)
61 self.update_job(100, desc='nf update success')
63 def get_and_check_params(self):
64 nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id)
66 logger.error('NF instance[id=%s] does not exist' % self.vnf_instance_id)
67 raise NSLCMException('NF instance[id=%s] does not exist' % self.vnf_instance_id)
68 logger.debug('vnfd_model = %s, vnf_instance_id = %s' % (nf_info[0].vnfd_model, self.vnf_instance_id))
69 self.nf_model = nf_info[0].vnfd_model
70 self.m_nf_inst_id = nf_info[0].mnfinstid
71 self.vnfm_inst_id = nf_info[0].vnfm_inst_id
72 self.nf_additional_params = ignore_case_get(self.data, 'additionalParams')
74 def send_nf_operating_request(self):
75 req_param = json.JSONEncoder().encode(self.nf_operate_params)
76 # rsp = send_nf_heal_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
77 rsp = send_nf_operate_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
78 vnfm_job_id = ignore_case_get(rsp, 'jobId')
79 ret = wait_job_finish(self.vnfm_inst_id, self.job_id, vnfm_job_id, progress_range=None, timeout=1200,
81 if ret != JOB_MODEL_STATUS.FINISHED:
82 logger.error('[NF update] nf update failed')
83 raise NSLCMException("nf update failed")
85 def update_job(self, progress, desc=''):
86 JobUtil.add_job_status(self.job_id, progress, desc)
88 def update_nf_status(self, status):
89 NfInstModel.objects.filter(nfinstid=self.vnf_instance_id).update(status=status)