update link to upper-constraints.txt
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / biz / 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
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
24
25 logger = logging.getLogger(__name__)
26
27
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
31     if progress > 100:
32         return progress
33     floor_progress = int(math.floor(float(target_range[1] - target_range[0]) / 100 * progress))
34     target_range = floor_progress + target_range[0]
35     return target_range
36
37
38 def default_callback(vnfo_job_id, vnfm_job_id, job_status, jobs, progress_range, **kwargs):
39     for job in jobs:
40         progress = calc_progress(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(ignore_case_get(job_status, 'progress'),
46                                     progress_range)
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
54
55
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
58     response_id = 0
59     query_interval = 2
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()
65         if not query_status:
66             continue
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:
70             continue
71         response_id = response_id_new
72         jobs = ignore_case_get(job_status, 'responsehistorylist', [])
73         if jobs:
74             jobs.reverse()
75         is_end, status = job_callback(vnfo_job_id, vnfm_job_id, job_status, jobs, progress_range, **kwargs)
76         if is_end:
77             return status
78     return JOB_MODEL_STATUS.TIMEOUT