99b450763bc0293d35649ce41ecbb116e0df1699
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / wait_job.py
1 # Copyright 2016 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 time
15 import datetime
16 import logging
17
18 import math
19
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
23
24 logger = logging.getLogger(__name__)
25
26
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
30     if progress > 100:
31         return progress
32     floor_progress = int(math.floor(float(target_range[1] - target_range[0]) / 100 * progress))
33     target_range = floor_progress + target_range[0]
34     return target_range
35
36
37 def default_callback(vnfo_job_id, vnfm_job_id, job_status, jobs, progress_range, **kwargs):
38     for job in jobs:
39         progress = calc_progress(
40             ignore_case_get(job, 'progress'), 
41             progress_range)
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'), 
47         progress_range)
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
55
56
57 def wait_job_finish(vnfm_id, vnfo_job_id, vnfm_job_id, progress_range=None, timeout=600, job_callback=default_callback, **kwargs):
58     progress_range = [0, 100] if not progress_range else progress_range
59     response_id = 0
60     query_interval = 2
61     start_time = end_time = datetime.datetime.now()
62     while (end_time - start_time).seconds < timeout:
63         query_status, result = query_vnfm_job(vnfm_id, vnfm_job_id, response_id)
64         time.sleep(query_interval)
65         end_time = datetime.datetime.now()
66         if not query_status:
67             continue
68         job_status = ignore_case_get(result, 'responsedescriptor')
69         response_id_new = ignore_case_get(job_status, 'responseid')
70         if response_id_new == response_id:
71             continue
72         response_id = response_id_new
73         jobs = ignore_case_get(job_status, 'responsehistorylist', [])
74         if jobs:
75             jobs.reverse()
76         is_end, status = job_callback(vnfo_job_id, vnfm_job_id, job_status, 
77             jobs, progress_range, **kwargs)
78         if is_end:
79             return status
80     return JOB_MODEL_STATUS.TIMEOUT