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.
21 from lcm.ns.enum import NS_INST_STATUS
22 from lcm.pub.database.models import JobModel, NSInstModel, NfInstModel, VNFCInstModel, VmInstModel
23 from lcm.pub.exceptions import NSLCMException
24 from lcm.pub.utils.jobutil import JobUtil, JOB_MODEL_STATUS
25 from lcm.pub.utils.values import ignore_case_get
26 from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
27 from lcm.ns.biz.ns_lcm_op_occ import NsLcmOpOcc
30 logger = logging.getLogger(__name__)
33 class NSHealService(threading.Thread):
34 def __init__(self, ns_instance_id, request_data, job_id):
35 super(NSHealService, self).__init__()
36 self.ns_instance_id = ns_instance_id
37 self.request_data = request_data
39 self.occ_id = NsLcmOpOcc.create(ns_instance_id, "HEAL", "PROCESSING", False, request_data)
40 self.heal_vnf_data = ''
41 self.heal_ns_data = ''
46 except NSLCMException as e:
47 JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message)
48 NsLcmOpOcc.update(self.occ_id, operationState="FAILED", error=e.message)
49 except Exception as e:
50 logger.error(traceback.format_exc())
51 JobUtil.add_job_status(self.job_id, JOB_ERROR, 'ns heal fail')
52 NsLcmOpOcc.update(self.occ_id, operationState="FAILED", error=e.message)
55 self.update_job(1, desc='ns heal start')
56 self.get_and_check_params()
57 self.update_ns_status(NS_INST_STATUS.HEALING)
59 self.update_ns_status(NS_INST_STATUS.ACTIVE)
60 self.update_job(100, desc='ns heal success')
61 NsLcmOpOcc.update(self.occ_id, "COMPLETED")
63 def get_and_check_params(self):
64 ns_info = NSInstModel.objects.filter(id=self.ns_instance_id)
66 errmsg = 'NS [id=%s] does not exist' % self.ns_instance_id
68 raise NSLCMException(errmsg)
70 self.heal_ns_data = ignore_case_get(self.request_data, 'healNsData')
71 self.heal_vnf_data = ignore_case_get(self.request_data, 'healVnfData')
73 if self.heal_ns_data and self.heal_vnf_data:
74 errmsg = 'healNsData and healVnfData can not exist together'
76 raise NSLCMException(errmsg)
78 if not self.heal_ns_data and not self.heal_vnf_data:
79 errmsg = 'healNsData and healVnfData parameters does not exist or value is incorrect.'
81 raise NSLCMException(errmsg)
84 if self.heal_vnf_data:
85 vnf_heal_params = self.prepare_vnf_heal_params(self.heal_vnf_data)
86 status = self.do_vnf_or_ns_heal(vnf_heal_params, 15)
87 if status is JOB_MODEL_STATUS.FINISHED:
88 logger.info('nf[%s] heal handle end' %
89 vnf_heal_params.get('vnfInstanceId'))
91 desc='nf[%s] heal handle end' % vnf_heal_params.get('vnfInstanceId'))
93 errmsg = 'nf heal failed'
95 raise NSLCMException(errmsg)
97 ns_heal_params = self.prepare_ns_heal_params(self.heal_ns_data)
98 for ns_heal_param in ns_heal_params:
99 status = self.do_vnf_or_ns_heal(ns_heal_param, 15)
100 if status is JOB_MODEL_STATUS.FINISHED:
101 logger.info('nf[%s] heal handle end' %
102 ns_heal_param.get('vnfInstanceId'))
104 desc='nf[%s] heal handle end' % ns_heal_param.get('vnfInstanceId'))
106 errmsg = 'nf heal failed'
108 raise NSLCMException(errmsg)
110 def do_vnf_or_ns_heal(self, heal_param, progress):
111 instance_id = heal_param.get('vnfInstanceId')
112 nf_service = NFHealService(instance_id, heal_param)
115 progress, desc='nf[%s] heal handle start' % instance_id)
116 status = self.wait_job_finish(nf_service.job_id)
119 def prepare_ns_heal_params(self, ns_data):
120 degree_healing = ignore_case_get(ns_data, 'degreeHealing')
121 if not degree_healing:
122 errmsg = 'degreeHealing does not exist.'
124 raise NSLCMException(errmsg)
125 ns_instance_id = self.ns_instance_id
127 action = ignore_case_get(ns_data, 'actionsHealing')
128 if degree_healing == "HEAL_RESTORE":
129 ns_inst_infos = NfInstModel.objects.filter(
130 ns_inst_id=self.ns_instance_id)
131 if not ns_inst_infos.exists():
132 raise NSLCMException(
133 'NSInsts(%s) does not exist' % self.ns_instance_id)
136 for ns_inst_info in ns_inst_infos:
137 vnfc_insts = VNFCInstModel.objects.filter(
138 nfinstid=ns_inst_info.nfinstid)
139 # If a condition is not met, will it all terminate?
140 if not vnfc_insts.exists():
141 raise NSLCMException(
142 'vnfcinsts(%s) does not exist' % ns_inst_info.nfinstid)
143 for vnfc_inst in vnfc_insts:
144 vm_id = vnfc_inst.vmid
145 vdu_id = vnfc_inst.vduid
146 vm_inst_info = VmInstModel.objects.filter(vmid=vm_id)
147 if not vm_inst_info.exists():
148 raise NSLCMException(
149 'vminstinfo(%s) does not exist' % vm_id)
150 vm_name = vm_inst_info[0].vmname
153 "vnfInstanceId": ns_instance_id,
155 "additionalParams": {
164 result_arr.append(result)
167 errmsg = 'The degree of healing dose not exist or value is incorrect.'
169 raise NSLCMException(errmsg)
171 def prepare_vnf_heal_params(self, vnf_data):
172 vnf_instance_id = ignore_case_get(vnf_data, 'vnfInstanceId')
173 if not vnf_instance_id:
174 errmsg = 'vnfinstanceid does not exist or value is incorrect.'
176 raise NSLCMException(errmsg)
177 cause = ignore_case_get(vnf_data, 'cause')
178 additional_params = ignore_case_get(vnf_data, 'additionalParams')
179 action = ignore_case_get(additional_params, 'action')
180 action_vm_info = ignore_case_get(additional_params, 'actionvminfo')
181 vm_id = ignore_case_get(action_vm_info, 'vmid')
182 vdu_id = ignore_case_get(action_vm_info, 'vduid')
183 vm_name = ignore_case_get(action_vm_info, 'vmname')
186 "vnfInstanceId": vnf_instance_id,
188 "additionalParams": {
200 def wait_job_finish(sub_job_id, timeout=3600):
202 start_time = end_time = datetime.datetime.now()
203 while (end_time - start_time).seconds < timeout:
204 job_result = JobModel.objects.get(jobid=sub_job_id)
205 time.sleep(query_interval)
206 end_time = datetime.datetime.now()
207 if job_result.progress == 100:
208 return JOB_MODEL_STATUS.FINISHED
209 elif job_result.progress > 100:
210 return JOB_MODEL_STATUS.ERROR
213 return JOB_MODEL_STATUS.TIMEOUT
215 def update_job(self, progress, desc=''):
216 JobUtil.add_job_status(self.job_id, progress, desc)
218 def update_ns_status(self, status):
219 NSInstModel.objects.filter(
220 id=self.ns_instance_id).update(status=status)