update version of lcm
[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(ignore_case_get(job, 'progress'),
40                                  progress_range)
41         JobUtil.add_job_status(vnfo_job_id, progress,
42                                ignore_case_get(job, 'statusdescription'),
43                                ignore_case_get(job, 'errorcode'))
44     latest_progress = calc_progress(ignore_case_get(job_status, 'progress'),
45                                     progress_range)
46     JobUtil.add_job_status(vnfo_job_id, latest_progress,
47                            ignore_case_get(job_status, 'statusdescription'),
48                            ignore_case_get(job_status, 'errorcode'))
49     jobstatus = ignore_case_get(job_status, 'status')
50     if jobstatus in (JOB_MODEL_STATUS.ERROR, JOB_MODEL_STATUS.FINISHED):
51         return True, jobstatus
52     return False, JOB_MODEL_STATUS.PROCESSING
53
54
55 def wait_job_finish(vnfm_id, vnfo_job_id, vnfm_job_id, progress_range=None, timeout=600, job_callback=default_callback, **kwargs):
56     progress_range = [0, 100] if not progress_range else progress_range
57     response_id = 0
58     query_interval = 2
59     start_time = end_time = datetime.datetime.now()
60     while (end_time - start_time).seconds < timeout:
61         query_status, result = query_vnfm_job(vnfm_id, vnfm_job_id, response_id)
62         time.sleep(query_interval)
63         end_time = datetime.datetime.now()
64         if not query_status:
65             continue
66         job_status = ignore_case_get(result, 'responsedescriptor')
67         response_id_new = ignore_case_get(job_status, 'responseid')
68         if response_id_new == response_id:
69             continue
70         response_id = response_id_new
71         jobs = ignore_case_get(job_status, 'responsehistorylist', [])
72         if jobs:
73             jobs.reverse()
74         is_end, status = job_callback(vnfo_job_id, vnfm_job_id, job_status, jobs, progress_range, **kwargs)
75         if is_end:
76             return status
77     return JOB_MODEL_STATUS.TIMEOUT