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
21 from lcm.jobs.enum import JOB_MODEL_STATUS
22 from lcm.pub.msapi.vnfmdriver import query_vnfm_job
23 from lcm.pub.utils.values import ignore_case_get
25 logger = logging.getLogger(__name__)
28 def calc_progress(vnfm_progress, target_range=None):
29 target_range = [0, 100] if not target_range else target_range
30 progress = int(vnfm_progress) if vnfm_progress else 0
33 floor_progress = int(math.floor(float(target_range[1] - target_range[0]) / 100 * progress))
34 target_range = floor_progress + target_range[0]
38 def default_callback(vnfo_job_id, vnfm_job_id, job_status, jobs, progress_range, **kwargs):
40 progress = calc_progress(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(ignore_case_get(job_status, 'progress'),
47 JobUtil.add_job_status(vnfo_job_id, latest_progress,
48 ignore_case_get(job_status, 'statusdescription'),
49 ignore_case_get(job_status, 'errorcode'))
50 jobstatus = ignore_case_get(job_status, 'status')
51 if jobstatus in (JOB_MODEL_STATUS.ERROR, JOB_MODEL_STATUS.FINISHED):
52 return True, jobstatus
53 return False, JOB_MODEL_STATUS.PROCESSING
56 def wait_job_finish(vnfm_id, vnfo_job_id, vnfm_job_id, progress_range=None, timeout=600, job_callback=default_callback, **kwargs):
57 progress_range = [0, 100] if not progress_range else progress_range
60 start_time = end_time = datetime.datetime.now()
61 while (end_time - start_time).seconds < timeout:
62 query_status, result = query_vnfm_job(vnfm_id, vnfm_job_id, response_id)
63 time.sleep(query_interval)
64 end_time = datetime.datetime.now()
67 job_status = ignore_case_get(result, 'responsedescriptor')
68 response_id_new = ignore_case_get(job_status, 'responseid')
69 if response_id_new == response_id:
71 response_id = response_id_new
72 jobs = ignore_case_get(job_status, 'responsehistorylist', [])
75 is_end, status = job_callback(vnfo_job_id, vnfm_job_id, job_status, jobs, progress_range, **kwargs)
78 return JOB_MODEL_STATUS.TIMEOUT