1 # Copyright 2016 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.
20 from lcm.pub.utils.jobutil import JobUtil, JOB_MODEL_STATUS
21 from lcm.pub.msapi.vnfmdriver import query_vnfm_job
22 from lcm.pub.utils.values import ignore_case_get
24 logger = logging.getLogger(__name__)
27 def calc_progress(vnfm_progress, target_range=None):
28 target_range = [0, 100] if not target_range else target_range
29 progress = int(vnfm_progress) if vnfm_progress else 0
32 floor_progress = int(math.floor(float(target_range[1] - target_range[0]) / 100 * progress))
33 target_range = floor_progress + target_range[0]
37 def default_callback(vnfo_job_id, vnfm_job_id, job_status, jobs, progress_range, **kwargs):
39 progress = calc_progress(
40 ignore_case_get(job, 'progress'),
42 JobUtil.add_job_status(vnfo_job_id, progress,
43 ignore_case_get(job, 'statusdescription'),
44 ignore_case_get(job, 'errorcode'))
45 latest_progress = calc_progress(
46 ignore_case_get(job_status, 'progress'),
48 JobUtil.add_job_status(vnfo_job_id, latest_progress,
49 ignore_case_get(job_status, 'statusdescription'),
50 ignore_case_get(job_status, 'errorcode'))
51 jobstatus = ignore_case_get(job_status, 'status')
52 if jobstatus in (JOB_MODEL_STATUS.ERROR, JOB_MODEL_STATUS.FINISHED):
53 return True, jobstatus
54 return False, JOB_MODEL_STATUS.PROCESSING
57 def wait_job_finish(vnfm_id, vnfo_job_id, vnfm_job_id, progress_range=None, timeout=600,
58 job_callback=default_callback, **kwargs):
59 progress_range = [0, 100] if not progress_range else progress_range
62 start_time = end_time = datetime.datetime.now()
63 while (end_time - start_time).seconds < timeout:
64 query_status, result = query_vnfm_job(vnfm_id, vnfm_job_id, response_id)
65 time.sleep(query_interval)
66 end_time = datetime.datetime.now()
69 job_status = ignore_case_get(result, 'responsedescriptor')
70 response_id_new = ignore_case_get(job_status, 'responseid')
71 if response_id_new == response_id:
73 response_id = response_id_new
74 jobs = ignore_case_get(job_status, 'responsehistorylist', [])
77 is_end, status = job_callback(vnfo_job_id, vnfm_job_id, job_status,
78 jobs, progress_range, **kwargs)
81 return JOB_MODEL_STATUS.TIMEOUT