update version of lcm
[vfc/nfvo/lcm.git] / lcm / ns / ns_heal.py
1 # Copyright 2017 Intel Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import logging
15 import threading
16 import traceback
17 import datetime
18 import time
19
20 from lcm.ns.const import NS_INST_STATUS
21 from lcm.pub.database.models import JobModel, NSInstModel
22 from lcm.ns.vnfs.heal_vnfs import NFHealService
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
27 JOB_ERROR = 255
28 logger = logging.getLogger(__name__)
29
30
31 class NSHealService(threading.Thread):
32     def __init__(self, ns_instance_id, request_data, job_id):
33         super(NSHealService, self).__init__()
34         self.ns_instance_id = ns_instance_id
35         self.request_data = request_data
36         self.job_id = job_id
37
38         self.heal_vnf_data = ''
39
40     def run(self):
41         try:
42             self.do_biz()
43         except NSLCMException as e:
44             JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message)
45         except:
46             logger.error(traceback.format_exc())
47             JobUtil.add_job_status(self.job_id, JOB_ERROR, 'ns heal fail')
48
49     def do_biz(self):
50         self.update_job(1, desc='ns heal start')
51         self.get_and_check_params()
52         self.update_ns_status(NS_INST_STATUS.HEALING)
53         self.do_vnfs_heal()
54         self.update_ns_status(NS_INST_STATUS.ACTIVE)
55         self.update_job(100, desc='ns heal success')
56
57     def get_and_check_params(self):
58         ns_info = NSInstModel.objects.filter(id=self.ns_instance_id)
59         if not ns_info:
60             logger.error('NS [id=%s] does not exist' % self.ns_instance_id)
61             raise NSLCMException('NS [id=%s] does not exist' % self.ns_instance_id)
62         self.heal_vnf_data = ignore_case_get(self.request_data, 'healVnfData')
63         if not self.heal_vnf_data:
64             logger.error('healVnfData parameter does not exist or value is incorrect.')
65             raise NSLCMException('healVnfData parameter does not exist or value incorrect.')
66
67     def do_vnfs_heal(self):
68         vnf_heal_params = self.prepare_vnf_heal_params(self.heal_vnf_data)
69         # count = len(self.heal_vnf_data)
70         # Only one VNF is supported to heal.
71         status = self.do_vnf_heal(vnf_heal_params, 15)
72         if status is JOB_MODEL_STATUS.FINISHED:
73             logger.info('nf[%s] heal handle end' % vnf_heal_params.get('vnfInstanceId'))
74             self.update_job(90,
75                             desc='nf[%s] heal handle end' % vnf_heal_params.get('vnfInstanceId'))
76         else:
77             logger.error('nf heal failed')
78             raise NSLCMException('nf heal failed')
79
80     def do_vnf_heal(self, vnf_heal_params, progress):
81         vnf_instance_id = vnf_heal_params.get('vnfInstanceId')
82         nf_service = NFHealService(vnf_instance_id, vnf_heal_params)
83         nf_service.start()
84         self.update_job(progress, desc='nf[%s] heal handle start' % vnf_instance_id)
85         status = self.wait_job_finish(nf_service.job_id)
86         return status
87
88     def prepare_vnf_heal_params(self, vnf_data):
89         vnf_instance_id = ignore_case_get(vnf_data, 'vnfInstanceId')
90         cause = ignore_case_get(vnf_data, "cause")
91         additional_params = ignore_case_get(vnf_data, "additionalParams")
92         result = {
93             "vnfInstanceId": vnf_instance_id,
94             "cause": cause,
95             "additionalParams": additional_params
96         }
97         return result
98
99     @staticmethod
100     def wait_job_finish(sub_job_id, timeout=3600):
101         query_interval = 2
102         start_time = end_time = datetime.datetime.now()
103         while (end_time - start_time).seconds < timeout:
104             job_result = JobModel.objects.get(jobid=sub_job_id)
105             time.sleep(query_interval)
106             end_time = datetime.datetime.now()
107             if job_result.progress == 100:
108                 return JOB_MODEL_STATUS.FINISHED
109             elif job_result.progress > 100:
110                 return JOB_MODEL_STATUS.ERROR
111             else:
112                 continue
113         return JOB_MODEL_STATUS.TIMEOUT
114
115     def update_job(self, progress, desc=''):
116         JobUtil.add_job_status(self.job_id, progress, desc)
117
118     def update_ns_status(self, status):
119         NSInstModel.objects.filter(id=self.ns_instance_id).update(status=status)