1 # Copyright 2017 ZTE 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.
19 from lcm.pub.database.models import NfInstModel
20 from lcm.pub.exceptions import NSLCMException
21 from lcm.pub.msapi.vnfmdriver import send_nf_scaling_request
22 from lcm.pub.utils.jobutil import JobUtil
23 from lcm.jobs.enum import JOB_MODEL_STATUS, JOB_TYPE, JOB_PROGRESS, JOB_ACTION
24 from lcm.pub.utils.values import ignore_case_get
25 from lcm.ns_vnfs.enum import VNF_STATUS
26 from lcm.ns_vnfs.biz.wait_job import wait_job_finish
28 logger = logging.getLogger(__name__)
31 class NFManualScaleService(threading.Thread):
32 def __init__(self, vnf_instance_id, data):
33 super(NFManualScaleService, self).__init__()
34 self.vnf_instance_id = vnf_instance_id
36 self.job_id = JobUtil.create_job(JOB_TYPE.VNF, JOB_ACTION.MANUAL_SCALE, vnf_instance_id)
37 self.nf_scale_params = []
38 self.m_nf_inst_id = ''
39 self.vnfm_inst_id = ''
44 except NSLCMException as e:
45 JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, e.args[0])
46 except Exception as ex:
47 logger.error(ex.args[0])
48 logger.error(traceback.format_exc())
49 JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, 'VNF scale failed')
51 self.update_nf_status()
54 self.update_job(1, desc='VNF scale start')
55 self.update_nf_status(VNF_STATUS.SCALING)
56 self.get_and_check_params()
57 self.send_nf_scaling_requests()
58 self.update_nf_status(VNF_STATUS.ACTIVE)
59 self.update_job(100, desc='VNF scale success')
61 def get_and_check_params(self):
62 nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id)
65 'NF instance[id=%s] does not exist' %
67 logger.debug('vnfd_model = %s, vnf_instance_id = %s' %
68 (nf_info[0].vnfd_model, self.vnf_instance_id))
69 nf_model = 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 scale_vnf_data = ignore_case_get(self.data, 'scaleVnfData')
73 if not scale_vnf_data:
74 raise NSLCMException('scaleVnfData parameter does not exist')
76 self.nf_scale_params.append({
77 'type': ignore_case_get(scale_vnf_data, 'type'),
78 'aspectId': ignore_case_get(scale_vnf_data, 'aspectId'),
79 'numberOfSteps': ignore_case_get(scale_vnf_data, 'numberOfSteps'),
80 'additionalParam': {'vnfdModel': nf_model}
83 def send_nf_scaling_requests(self):
84 nf_scale_num = len(self.nf_scale_params)
85 for i in range(nf_scale_num):
86 progress_range = [10 + 80 / nf_scale_num * i,
87 10 + 80 / nf_scale_num * (i + 1)]
88 self.send_nf_scaling_request(
89 self.nf_scale_params[i], progress_range)
91 def send_nf_scaling_request(self, scale_param, progress_range):
92 req_param = json.JSONEncoder().encode(scale_param)
93 rsp = send_nf_scaling_request(
94 self.vnfm_inst_id, self.m_nf_inst_id, req_param)
95 vnfm_job_id = ignore_case_get(rsp, 'jobId')
96 ret = wait_job_finish(
100 progress_range=progress_range,
103 if ret != JOB_MODEL_STATUS.FINISHED:
104 raise NSLCMException("VNF scale failed")
106 def update_nf_status(self, status=VNF_STATUS.ACTIVE):
107 NfInstModel.objects.filter(
108 nfinstid=self.vnf_instance_id).update(
111 def update_job(self, progress, desc=''):
112 JobUtil.add_job_status(self.job_id, progress, desc)