Modify the code of healing ns
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / heal_vnfs.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
15 import json
16 import logging
17 import threading
18 import traceback
19
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
27
28 JOB_ERROR = 255
29
30 logger = logging.getLogger(__name__)
31
32
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
37         self.data = data
38         self.job_id = JobUtil.create_job("NF", JOB_TYPE.HEAL_VNF, vnf_instance_id)
39
40         self.nf_model = {}
41         self.nf_additional_params = {}
42         self.nf_heal_params = {}
43         self.m_nf_inst_id = ''
44         self.vnfm_inst_id = ''
45
46     def run(self):
47         try:
48             self.do_biz()
49         except NSLCMException as e:
50             JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message)
51         except:
52             logger.error(traceback.format_exc())
53             JobUtil.add_job_status(self.job_id, JOB_ERROR, 'nf heal fail')
54
55     def do_biz(self):
56         self.update_job(1, desc='nf heal start')
57         self.get_and_check_params()
58         self.update_nf_status(VNF_STATUS.HEALING)
59         self.send_nf_healing_request()
60         self.update_nf_status(VNF_STATUS.ACTIVE)
61         self.update_job(100, desc='nf heal success')
62
63     def get_and_check_params(self):
64         nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id)
65         if not nf_info:
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')
73
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')
77
78         action = ignore_case_get(self.nf_additional_params, 'action')
79         if action is "restartvm":
80             action = "vmReset"
81
82         actionvminfo = ignore_case_get(self.nf_additional_params, 'actionvminfo')
83         vmid = ignore_case_get(actionvminfo, 'vmid')
84         vmname = ignore_case_get(actionvminfo, 'vmname')
85         # TODO(sshank): Find how to get 'vduid'
86         vduid = ""
87
88         self.nf_heal_params = {
89             "action": action,
90             "affectedvm": {
91                 "vmid": vmid,
92                 "vduid": vduid,
93                 "vmname": vmname,
94             }
95         }
96
97     def send_nf_healing_request(self):
98         req_param = json.JSONEncoder().encode(self.nf_heal_params)
99         rsp = send_nf_heal_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
100         vnfm_job_id = ignore_case_get(rsp, 'jobId')
101         ret = wait_job_finish(self.vnfm_inst_id, self.job_id, vnfm_job_id, progress_range=None, timeout=1200,
102                               mode='1')
103         if ret != JOB_MODEL_STATUS.FINISHED:
104             logger.error('[NF heal] nf heal failed')
105             raise NSLCMException("nf heal failed")
106
107     def update_job(self, progress, desc=''):
108         JobUtil.add_job_status(self.job_id, progress, desc)
109
110     def update_nf_status(self, status):
111         NfInstModel.objects.filter(nfinstid=self.vnf_instance_id).update(status=status)