SOL003 API Align
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / biz / scale_vnfs.py
1 # Copyright 2017 ZTE 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 json
15 import logging
16 import threading
17 import traceback
18
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, JOB_TYPE, JOB_MODEL_STATUS
23 from lcm.pub.utils.values import ignore_case_get
24 from lcm.ns_vnfs.const import VNF_STATUS
25 from lcm.ns_vnfs.biz.wait_job import wait_job_finish
26
27 JOB_ERROR = 255
28 SCALE_TYPE = ("SCALE_OUT", "SCALE_IN")
29 logger = logging.getLogger(__name__)
30
31
32 class NFManualScaleService(threading.Thread):
33     def __init__(self, vnf_instance_id, data):
34         super(NFManualScaleService, self).__init__()
35         self.vnf_instance_id = vnf_instance_id
36         self.data = data
37         self.job_id = JobUtil.create_job(
38             "NF", JOB_TYPE.MANUAL_SCALE_VNF, vnf_instance_id)
39         self.nf_scale_params = []
40         self.m_nf_inst_id = ''
41         self.vnfm_inst_id = ''
42
43     def run(self):
44         try:
45             self.do_biz()
46         except NSLCMException as e:
47             JobUtil.add_job_status(self.job_id, JOB_ERROR, e.message)
48         except Exception as ex:
49             logger.error(ex.message)
50             logger.error(traceback.format_exc())
51             JobUtil.add_job_status(self.job_id, JOB_ERROR, 'VNF scale failed')
52         finally:
53             self.update_nf_status()
54
55     def do_biz(self):
56         self.update_job(1, desc='VNF scale start')
57         self.update_nf_status(VNF_STATUS.SCALING)
58         self.get_and_check_params()
59         self.send_nf_scaling_requests()
60         self.update_nf_status(VNF_STATUS.ACTIVE)
61         self.update_job(100, desc='VNF scale 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             raise NSLCMException(
67                 'NF instance[id=%s] does not exist' %
68                 self.vnf_instance_id)
69         logger.debug('vnfd_model = %s, vnf_instance_id = %s' %
70                      (nf_info[0].vnfd_model, self.vnf_instance_id))
71         nf_model = nf_info[0].vnfd_model
72         self.m_nf_inst_id = nf_info[0].mnfinstid
73         self.vnfm_inst_id = nf_info[0].vnfm_inst_id
74         scale_vnf_data = ignore_case_get(self.data, 'scaleVnfData')
75         if not scale_vnf_data:
76             raise NSLCMException('scaleVnfData parameter does not exist')
77
78         self.nf_scale_params.append({
79             'type': ignore_case_get(scale_vnf_data, 'type'),
80             'aspectId': ignore_case_get(scale_vnf_data, 'aspectId'),
81             'numberOfSteps': ignore_case_get(scale_vnf_data, 'numberOfSteps'),
82             'additionalParam': {'vnfdModel': nf_model}
83         })
84
85     def send_nf_scaling_requests(self):
86         nf_scale_num = len(self.nf_scale_params)
87         for i in range(nf_scale_num):
88             progress_range = [10 + 80 / nf_scale_num * i,
89                               10 + 80 / nf_scale_num * (i + 1)]
90             self.send_nf_scaling_request(
91                 self.nf_scale_params[i], progress_range)
92
93     def send_nf_scaling_request(self, scale_param, progress_range):
94         req_param = json.JSONEncoder().encode(scale_param)
95         rsp = send_nf_scaling_request(
96             self.vnfm_inst_id, self.m_nf_inst_id, req_param)
97         vnfm_job_id = ignore_case_get(rsp, 'jobId')
98         ret = wait_job_finish(
99             self.vnfm_inst_id,
100             self.job_id,
101             vnfm_job_id,
102             progress_range=progress_range,
103             timeout=1200,
104             mode='1')
105         if ret != JOB_MODEL_STATUS.FINISHED:
106             raise NSLCMException("VNF scale failed")
107
108     def update_nf_status(self, status=VNF_STATUS.ACTIVE):
109         NfInstModel.objects.filter(
110             nfinstid=self.vnf_instance_id).update(
111             status=status)
112
113     def update_job(self, progress, desc=''):
114         JobUtil.add_job_status(self.job_id, progress, desc)