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 JOB_ERROR
26 from lcm.ns_vnfs.enum import VNF_STATUS
27 from lcm.ns_vnfs.biz.wait_job import wait_job_finish
29 logger = logging.getLogger(__name__)
32 class NFOperateService(threading.Thread):
33 def __init__(self, vnf_instance_id, data):
34 super(NFOperateService, self).__init__()
35 self.vnf_instance_id = vnf_instance_id
37 self.job_id = JobUtil.create_job("NF", JOB_TYPE.HEAL_VNF, vnf_instance_id)
40 self.nf_additional_params = {}
41 self.nf_operate_params = data
42 self.m_nf_inst_id = ''
43 self.vnfm_inst_id = ''
48 except NSLCMException as e:
49 JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message)
51 logger.error(traceback.format_exc())
52 JobUtil.add_job_status(self.job_id, JOB_ERROR, 'nf update fail')
55 self.update_job(1, desc='nf update start')
56 self.get_and_check_params()
57 self.update_nf_status(VNF_STATUS.UPDATING)
58 self.send_nf_operating_request()
59 self.update_nf_status(VNF_STATUS.ACTIVE)
60 self.update_job(100, desc='nf update success')
62 def get_and_check_params(self):
63 nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id)
65 logger.error('NF instance[id=%s] does not exist' % self.vnf_instance_id)
66 raise NSLCMException('NF instance[id=%s] does not exist' % self.vnf_instance_id)
67 logger.debug('vnfd_model = %s, vnf_instance_id = %s' % (nf_info[0].vnfd_model, self.vnf_instance_id))
68 self.nf_model = nf_info[0].vnfd_model
69 self.m_nf_inst_id = nf_info[0].mnfinstid
70 self.vnfm_inst_id = nf_info[0].vnfm_inst_id
71 self.nf_additional_params = ignore_case_get(self.data, 'additionalParams')
73 def send_nf_operating_request(self):
74 req_param = json.JSONEncoder().encode(self.nf_operate_params)
75 # rsp = send_nf_heal_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
76 rsp = send_nf_operate_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
77 vnfm_job_id = ignore_case_get(rsp, 'jobId')
78 ret = wait_job_finish(self.vnfm_inst_id, self.job_id, vnfm_job_id, progress_range=None, timeout=1200,
80 if ret != JOB_MODEL_STATUS.FINISHED:
81 logger.error('[NF update] nf update failed')
82 raise NSLCMException("nf update failed")
84 def update_job(self, progress, desc=''):
85 JobUtil.add_job_status(self.job_id, progress, desc)
87 def update_nf_status(self, status):
88 NfInstModel.objects.filter(nfinstid=self.vnf_instance_id).update(status=status)