1 # Copyright 2017 Intel 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.
20 from lcm.ns.vnfs.const import VNF_STATUS
21 from lcm.ns.vnfs.wait_job import wait_job_finish
22 from lcm.pub.database.models import NfInstModel
23 from lcm.pub.exceptions import NSLCMException
24 from lcm.pub.msapi.vnfmdriver import send_nf_heal_request
25 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
26 from lcm.pub.utils.values import ignore_case_get
30 logger = logging.getLogger(__name__)
33 class NFHealService(threading.Thread):
34 def __init__(self, vnf_instance_id, data):
35 super(NFHealService, 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_heal_params = {}
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 heal fail')
56 self.update_job(1, desc='nf heal start')
57 self.update_nf_status(VNF_STATUS.HEALING)
58 self.get_and_check_params()
59 self.send_nf_healing_request()
60 self.update_nf_status(VNF_STATUS.ACTIVE)
61 self.update_job(100, desc='nf heal 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 = json.loads(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 if not self.nf_additional_params:
75 logger.error('additionalParams parameter does not exist or value incorrect')
76 raise NSLCMException('additionalParams parameter does not exist or value incorrect')
78 action = ignore_case_get(self.nf_additional_params, 'action')
79 if action is "restartvm":
81 vmid = ignore_case_get(self.nf_additional_params, 'vmid')
82 vmname = ignore_case_get(self.nf_additional_params, 'vmname')
83 # TODO(sshank): Find how to get 'vduid'
86 self.nf_heal_params = {
95 def send_nf_healing_request(self):
96 req_param = json.JSONEncoder().encode(self.nf_heal_params)
97 rsp = send_nf_heal_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
98 vnfm_job_id = ignore_case_get(rsp, 'jobId')
99 ret = wait_job_finish(self.vnfm_inst_id, self.job_id, vnfm_job_id, progress_range=None, timeout=1200,
101 if ret != JOB_MODEL_STATUS.FINISHED:
102 logger.error('[NF heal] nf heal failed')
103 raise NSLCMException("nf heal failed")
105 def update_job(self, progress, desc=''):
106 JobUtil.add_job_status(self.job_id, progress, desc)
108 def update_nf_status(self, status):
109 NfInstModel.objects.filter(nfinstid=self.vnf_instance_id).update(status=status)