refactor job url const
[vfc/nfvo/lcm.git] / lcm / workflows / graphflow / task / lcm_async_rest_task.py
1 # Copyright 2018 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
15 import logging
16 import json
17 from lcm.jobs.const import JOB_INSTANCE_URI
18 from lcm.workflows.graphflow.task.async_rest_task import ASyncRestTask
19 from lcm.pub.utils import restcall
20
21 logger = logging.getLogger(__name__)
22
23
24 class LcmASyncRestTask(ASyncRestTask):
25
26     METHOD_GET_JOB_STATUS = "status"
27
28     def __init__(self, *args):
29         super(LcmASyncRestTask, self).__init__(*args)
30         # self.job_url = JOB_INSTANCE_URI
31
32     def call_rest(self, url, method, content=None):
33         ret = restcall.req_by_msb(url, method, content)
34         return ret[2], json.JSONDecoder().decode(ret[1])
35
36     def get_ext_status(self):
37         job_id = self.parse_job_id()
38         logger.debug("get_ext_status %s", self.key)
39         job_status = None
40         if job_id:
41             url = JOB_INSTANCE_URI % job_id
42             status, job_result = self.call_rest(url, self.GET)
43             if status in self.STATUS_OK:
44                 progress = job_result["responseDescriptor"]["progress"]
45                 if progress == 100:
46                     job_status = self.FINISHED
47                 elif progress == 255:
48                     job_status = self.ERROR
49                 else:
50                     job_status = self.PROCESSING
51         return job_status
52
53     def parse_job_id(self):
54         if self.output:
55             return self.output["jobId"] if "jobId" in self.output else None
56         else:
57             return None