update link to upper-constraints.txt
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / biz / update_vnfs.py
1 # Copyright (c) 2018, CMCC Technologies Co., Ltd.
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.pub.database.models import NfInstModel
21 from lcm.pub.exceptions import NSLCMException
22 from lcm.pub.msapi.vnfmdriver import send_nf_operate_request
23 from lcm.pub.utils.jobutil import JobUtil
24 from lcm.jobs.enum import JOB_MODEL_STATUS, JOB_TYPE, JOB_PROGRESS, JOB_ACTION
25 from lcm.pub.utils.values import ignore_case_get
26 from lcm.ns_vnfs.enum import VNF_STATUS
27 from lcm.ns_vnfs.biz.wait_job import wait_job_finish
28
29 logger = logging.getLogger(__name__)
30
31
32 class NFOperateService(threading.Thread):
33     def __init__(self, vnf_instance_id, data):
34         super(NFOperateService, self).__init__()
35         self.vnf_instance_id = vnf_instance_id
36         self.data = data
37         self.job_id = JobUtil.create_job(JOB_TYPE.VNF, JOB_ACTION.HEAL, vnf_instance_id)
38
39         self.nf_model = {}
40         self.nf_additional_params = {}
41         self.nf_operate_params = data
42         self.m_nf_inst_id = ''
43         self.vnfm_inst_id = ''
44
45     def run(self):
46         try:
47             self.do_biz()
48         except NSLCMException as e:
49             JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, e.args[0])
50         except:
51             logger.error(traceback.format_exc())
52             JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, 'nf update fail')
53
54     def do_biz(self):
55         self.update_job(1, desc='nf update start')
56         self.get_and_check_params()
57         self.update_nf_status(VNF_STATUS.UPDATING)
58         self.send_nf_operating_request()
59         self.update_nf_status(VNF_STATUS.ACTIVE)
60         self.update_job(100, desc='nf update success')
61
62     def get_and_check_params(self):
63         nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id)
64         if not nf_info:
65             logger.error('NF instance[id=%s] does not exist' % self.vnf_instance_id)
66             raise NSLCMException('NF instance[id=%s] does not exist' % self.vnf_instance_id)
67         logger.debug('vnfd_model = %s, vnf_instance_id = %s' % (nf_info[0].vnfd_model, self.vnf_instance_id))
68         self.nf_model = nf_info[0].vnfd_model
69         self.m_nf_inst_id = nf_info[0].mnfinstid
70         self.vnfm_inst_id = nf_info[0].vnfm_inst_id
71         self.nf_additional_params = ignore_case_get(self.data, 'additionalParams')
72
73     def send_nf_operating_request(self):
74         req_param = json.JSONEncoder().encode(self.nf_operate_params)
75         # rsp = send_nf_heal_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
76         rsp = send_nf_operate_request(self.vnfm_inst_id, self.m_nf_inst_id, req_param)
77         vnfm_job_id = ignore_case_get(rsp, 'jobId')
78         ret = wait_job_finish(self.vnfm_inst_id, self.job_id, vnfm_job_id, progress_range=None, timeout=1200,
79                               mode='1')
80         if ret != JOB_MODEL_STATUS.FINISHED:
81             logger.error('[NF update] nf update failed')
82             raise NSLCMException("nf update failed")
83
84     def update_job(self, progress, desc=''):
85         JobUtil.add_job_status(self.job_id, progress, desc)
86
87     def update_nf_status(self, status):
88         NfInstModel.objects.filter(nfinstid=self.vnf_instance_id).update(status=status)