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.pub.config.config import MR_IP
21 from lcm.pub.config.config import MR_PORT
22 from lcm.pub.database.models import NfInstModel, VNFCInstModel, VmInstModel
23 from lcm.pub.exceptions import NSLCMException
24 from lcm.pub.msapi.vnfmdriver import send_nf_heal_request
25 from lcm.pub.utils import restcall
26 from lcm.pub.utils.jobutil import JobUtil
27 from lcm.pub.enum import JOB_MODEL_STATUS, JOB_TYPE, JOB_PROGRESS
28 from lcm.pub.utils.values import ignore_case_get
29 from lcm.ns_vnfs.enum import VNF_STATUS
30 from lcm.ns_vnfs.biz.wait_job import wait_job_finish
33 logger = logging.getLogger(__name__)
36 class NFHealService(threading.Thread):
37 def __init__(self, vnf_instance_id, data):
38 super(NFHealService, self).__init__()
39 self.vnf_instance_id = vnf_instance_id
41 self.job_id = JobUtil.create_job("NF", JOB_TYPE.HEAL_VNF, vnf_instance_id)
43 self.nf_additional_params = {}
44 self.nf_heal_params = {}
45 self.m_nf_inst_id = ''
46 self.vnfm_inst_id = ''
50 JobUtil.add_job_status(self.job_id, JOB_PROGRESS.STARTED, 'vnf heal start')
51 self.get_and_check_params()
52 self.update_nf_status(VNF_STATUS.HEALING)
53 self.send_nf_healing_request()
54 self.update_nf_status(VNF_STATUS.ACTIVE)
55 JobUtil.add_job_status(self.job_id, JOB_PROGRESS.FINISHED, 'vnf heal success')
56 except NSLCMException as e:
57 JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, e.message)
59 logger.error(traceback.format_exc())
60 JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, 'vnf heal fail')
62 def get_and_check_params(self):
63 nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id)
65 raise NSLCMException('VNF instance[id=%s] does not exist' % self.vnf_instance_id)
66 logger.debug('vnfd_model = %s, vnf_instance_id = %s' % (nf_info[0].vnfd_model, self.vnf_instance_id))
67 self.nf_model = nf_info[0].vnfd_model
68 self.m_nf_inst_id = nf_info[0].mnfinstid
69 self.vnfm_inst_id = nf_info[0].vnfm_inst_id
70 self.nf_additional_params = ignore_case_get(self.data, 'additionalParams')
71 if not self.nf_additional_params:
72 raise NSLCMException('additionalParams parameter does not exist or value incorrect')
74 actionvminfo = ignore_case_get(self.nf_additional_params, 'actionvminfo')
75 vmid = ignore_case_get(actionvminfo, 'vmid')
76 self.nf_heal_params = {
80 "vduid": self.get_vudId(vmid),
81 "vmname": self.get_vmname(vmid)
85 while (retry_count > 0):
86 resp = restcall.call_req('http://%s:%s/events' % (MR_IP, MR_PORT),
89 restcall.rest_no_auth,
90 '/test/bins/1?timeout=15000',
92 if resp[2] == '200' and resp[1] != '[]':
93 for message in eval(resp[1]):
94 if 'powering-off' in message:
96 vm_info = json.loads(message)
97 if vmid == vm_info['instance_id']:
98 vduid = self.get_vudId(vm_info['instance_id'])
99 self.nf_heal_params = {
102 "vmid": vm_info['instance_id'],
104 "vmname": vm_info['display_name']
108 retry_count = retry_count - 1
110 def send_nf_healing_request(self):
111 req_param = json.JSONEncoder().encode(self.nf_heal_params)
112 rsp = send_nf_heal_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
113 vnfm_job_id = ignore_case_get(rsp, 'jobId')
116 ret = wait_job_finish(self.vnfm_inst_id, self.job_id, vnfm_job_id, progress_range=None, timeout=1200, mode='1')
117 if ret != JOB_MODEL_STATUS.FINISHED:
118 logger.error('[NF heal] nf heal failed')
119 raise NSLCMException("nf heal failed")
121 # Gets vdu id according to the given vm id.
122 def get_vudId(self, vmid):
123 vnfcInstances = VNFCInstModel.objects.filter(vmid=vmid, nfinstid=self.vnf_instance_id)
124 if not vnfcInstances:
125 raise NSLCMException('VDU [vmid=%s, vnfInstanceId=%s] does not exist' % (vmid, self.vnf_instance_id))
127 return vnfcInstances.first().vduid
129 def get_vmname(self, vmid):
130 vms = VmInstModel.objects.filter(resouceid=vmid)
133 return vms.first().vmname
135 def update_nf_status(self, status):
136 NfInstModel.objects.filter(nfinstid=self.vnf_instance_id).update(status=status)