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, JOB_TYPE, JOB_MODEL_STATUS
27 from lcm.pub.utils.values import ignore_case_get
28 from lcm.ns_vnfs.const import JOB_ERROR
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)
44 self.nf_additional_params = {}
45 self.nf_heal_params = {}
46 self.m_nf_inst_id = ''
47 self.vnfm_inst_id = ''
52 except NSLCMException as e:
53 JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message)
55 logger.error(traceback.format_exc())
56 JobUtil.add_job_status(self.job_id, JOB_ERROR, 'nf heal fail')
59 self.update_job(1, desc='nf heal start')
60 self.get_and_check_params()
61 self.update_nf_status(VNF_STATUS.HEALING)
62 self.send_nf_healing_request()
63 self.update_nf_status(VNF_STATUS.ACTIVE)
64 self.update_job(100, desc='nf heal success')
66 def get_and_check_params(self):
67 nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id)
69 logger.error('NF instance[id=%s] does not exist' % self.vnf_instance_id)
70 raise NSLCMException('NF instance[id=%s] does not exist' % self.vnf_instance_id)
71 logger.debug('vnfd_model = %s, vnf_instance_id = %s' % (nf_info[0].vnfd_model, self.vnf_instance_id))
72 self.nf_model = nf_info[0].vnfd_model
73 self.m_nf_inst_id = nf_info[0].mnfinstid
74 self.vnfm_inst_id = nf_info[0].vnfm_inst_id
75 self.nf_additional_params = ignore_case_get(self.data, 'additionalParams')
77 if not self.nf_additional_params:
78 logger.error('additionalParams parameter does not exist or value incorrect')
79 raise NSLCMException('additionalParams parameter does not exist or value incorrect')
81 actionvminfo = ignore_case_get(self.nf_additional_params, 'actionvminfo')
82 vmid = ignore_case_get(actionvminfo, 'vmid')
83 self.nf_heal_params = {
87 "vduid": self.get_vudId(vmid),
88 "vmname": self.get_vmname(vmid)
92 while (retry_count > 0):
93 resp = restcall.call_req('http://%s:%s/events' % (MR_IP, MR_PORT),
96 restcall.rest_no_auth,
97 '/test/bins/1?timeout=15000',
99 if resp[2] == '200' and resp[1] != '[]':
100 for message in eval(resp[1]):
101 if 'powering-off' in message:
103 vm_info = json.loads(message)
104 if vmid == vm_info['instance_id']:
105 vduid = self.get_vudId(vm_info['instance_id'])
106 self.nf_heal_params = {
109 "vmid": vm_info['instance_id'],
111 "vmname": vm_info['display_name']
115 retry_count = retry_count - 1
117 def send_nf_healing_request(self):
118 req_param = json.JSONEncoder().encode(self.nf_heal_params)
119 rsp = send_nf_heal_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
120 vnfm_job_id = ignore_case_get(rsp, 'jobId')
123 ret = wait_job_finish(self.vnfm_inst_id, self.job_id, vnfm_job_id, progress_range=None, timeout=1200,
125 if ret != JOB_MODEL_STATUS.FINISHED:
126 logger.error('[NF heal] nf heal failed')
127 raise NSLCMException("nf heal failed")
129 # Gets vdu id according to the given vm id.
130 def get_vudId(self, vmid):
131 vnfcInstances = VNFCInstModel.objects.filter(vmid=vmid, nfinstid=self.vnf_instance_id)
132 if not vnfcInstances:
133 raise NSLCMException('VDU [vmid=%s, vnfInstanceId=%s] does not exist' % (vmid, self.vnf_instance_id))
135 return vnfcInstances.first().vduid
137 def get_vmname(self, vmid):
138 vms = VmInstModel.objects.filter(resouceid=vmid)
141 return vms.first().vmname
143 def update_job(self, progress, desc=''):
144 JobUtil.add_job_status(self.job_id, progress, desc)
146 def update_nf_status(self, status):
147 NfInstModel.objects.filter(nfinstid=self.vnf_instance_id).update(status=status)