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.pub.enum import JOB_MODEL_STATUS, JOB_TYPE, JOB_PROGRESS
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(
37 "NF", JOB_TYPE.MANUAL_SCALE_VNF, vnf_instance_id)
38 self.nf_scale_params = []
39 self.m_nf_inst_id = ''
40 self.vnfm_inst_id = ''
45 except NSLCMException as e:
46 JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, e.args[0])
47 except Exception as ex:
48 logger.error(ex.args[0])
49 logger.error(traceback.format_exc())
50 JobUtil.add_job_status(self.job_id, JOB_PROGRESS.ERROR, 'VNF scale failed')
52 self.update_nf_status()
55 self.update_job(1, desc='VNF scale start')
56 self.update_nf_status(VNF_STATUS.SCALING)
57 self.get_and_check_params()
58 self.send_nf_scaling_requests()
59 self.update_nf_status(VNF_STATUS.ACTIVE)
60 self.update_job(100, desc='VNF scale success')
62 def get_and_check_params(self):
63 nf_info = NfInstModel.objects.filter(nfinstid=self.vnf_instance_id)
66 'NF instance[id=%s] does not exist' %
68 logger.debug('vnfd_model = %s, vnf_instance_id = %s' %
69 (nf_info[0].vnfd_model, self.vnf_instance_id))
70 nf_model = nf_info[0].vnfd_model
71 self.m_nf_inst_id = nf_info[0].mnfinstid
72 self.vnfm_inst_id = nf_info[0].vnfm_inst_id
73 scale_vnf_data = ignore_case_get(self.data, 'scaleVnfData')
74 if not scale_vnf_data:
75 raise NSLCMException('scaleVnfData parameter does not exist')
77 self.nf_scale_params.append({
78 'type': ignore_case_get(scale_vnf_data, 'type'),
79 'aspectId': ignore_case_get(scale_vnf_data, 'aspectId'),
80 'numberOfSteps': ignore_case_get(scale_vnf_data, 'numberOfSteps'),
81 'additionalParam': {'vnfdModel': nf_model}
84 def send_nf_scaling_requests(self):
85 nf_scale_num = len(self.nf_scale_params)
86 for i in range(nf_scale_num):
87 progress_range = [10 + 80 / nf_scale_num * i,
88 10 + 80 / nf_scale_num * (i + 1)]
89 self.send_nf_scaling_request(
90 self.nf_scale_params[i], progress_range)
92 def send_nf_scaling_request(self, scale_param, progress_range):
93 req_param = json.JSONEncoder().encode(scale_param)
94 rsp = send_nf_scaling_request(
95 self.vnfm_inst_id, self.m_nf_inst_id, req_param)
96 vnfm_job_id = ignore_case_get(rsp, 'jobId')
97 ret = wait_job_finish(
101 progress_range=progress_range,
104 if ret != JOB_MODEL_STATUS.FINISHED:
105 raise NSLCMException("VNF scale failed")
107 def update_nf_status(self, status=VNF_STATUS.ACTIVE):
108 NfInstModel.objects.filter(
109 nfinstid=self.vnf_instance_id).update(
112 def update_job(self, progress, desc=''):
113 JobUtil.add_job_status(self.job_id, progress, desc)