1 # -------------------------------------------------------------------------
2 # Copyright (c) 2015-2017 AT&T Intellectual Property
3 # Copyright (C) 2020 Wipro Limited.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # -------------------------------------------------------------------------
22 from requests import RequestException
25 from osdf.adapters.conductor.api_builder import conductor_api_builder
26 from osdf.logging.osdf_logging import debug_log
27 from osdf.utils.interfaces import RestClient
28 from osdf.operation.exceptions import BusinessException
31 def request(req_info, demands, request_parameters, service_info, osdf_config, flat_policies):
32 config = osdf_config.deployment
33 local_config = osdf_config.core
34 uid, passwd = config['conductorUsername'], config['conductorPassword']
35 conductor_url = config['conductorUrl']
36 req_id = req_info["requestId"]
37 transaction_id = req_info['transactionId']
38 headers = dict(transaction_id=transaction_id)
39 placement_ver_enabled = config.get('placementVersioningEnabled', False)
41 if placement_ver_enabled:
42 cond_minor_version = config.get('conductorMinorVersion', None)
43 if cond_minor_version is not None:
44 x_minor_version = str(cond_minor_version)
45 headers.update({'X-MinorVersion': x_minor_version})
46 debug_log.debug("Versions set in HTTP header to conductor: X-MinorVersion: {} ".format(x_minor_version))
48 max_retries = config.get('conductorMaxRetries', 30)
49 ping_wait_time = config.get('conductorPingWaitTime', 60)
51 rc = RestClient(userid=uid, passwd=passwd, method="GET", log_func=debug_log.debug, headers=headers)
52 conductor_req_json_str = conductor_api_builder(req_info, demands, request_parameters, service_info, flat_policies,
54 conductor_req_json = json.loads(conductor_req_json_str)
56 debug_log.debug("Sending first Conductor request for request_id {}".format(req_id))
58 resp, raw_resp = initial_request_to_conductor(rc, conductor_url, conductor_req_json)
59 # Very crude way of keeping track of time.
60 # We are not counting initial request time, first call back, or time for HTTP request
61 total_time, ctr = 0, 2
62 client_timeout = req_info['timeout']
63 configured_timeout = max_retries * ping_wait_time
64 max_timeout = min(client_timeout, configured_timeout)
66 while True: # keep requesting conductor till we get a result or we run out of time
68 if resp["plans"][0].get("status") in ["error"]:
69 raise RequestException(response=raw_resp, request=raw_resp.request)
71 if resp["plans"][0].get("status") in ["done", "not found"]:
73 new_url = resp['plans'][0]['links'][0][0]['href'] # TODO: check why a list of lists
75 if total_time >= max_timeout:
76 raise BusinessException("Conductor could not provide a solution within {} seconds,"
77 "this transaction is timing out".format(max_timeout))
78 time.sleep(ping_wait_time)
80 debug_log.debug("Attempt number {} url {}; prior status={}".format(ctr, new_url, resp['plans'][0]['status']))
81 total_time += ping_wait_time
84 raw_resp = rc.request(new_url, raw_response=True)
85 resp = raw_resp.json()
86 except RequestException as e:
87 debug_log.debug("Conductor attempt {} for request_id {} has failed because {}".format(ctr, req_id, str(e)))
90 def initial_request_to_conductor(rc, conductor_url, conductor_req_json):
91 """First steps in the request-redirect chain in making a call to Conductor
92 :param rc: REST client object for calling conductor
93 :param conductor_url: conductor's base URL to submit a placement request
94 :param conductor_req_json: request json object to send to Conductor
95 :return: URL to check for follow up (similar to redirects); we keep checking these till we get a result/error
97 debug_log.debug("Payload to Conductor: {}".format(json.dumps(conductor_req_json)))
98 raw_resp = rc.request(url=conductor_url, raw_response=True, method="POST", json=conductor_req_json)
99 resp = raw_resp.json()
100 if resp["status"] != "template":
101 raise RequestException(response=raw_resp, request=raw_resp.request)
102 time.sleep(10) # 10 seconds wait time to avoid being too quick!
103 plan_url = resp["links"][0][0]["href"]
104 debug_log.debug("Attempting to read the plan from the conductor provided url {}".format(plan_url))
105 raw_resp = rc.request(raw_response=True, url=plan_url) # TODO: check why a list of lists for links
106 resp = raw_resp.json()
108 if resp["plans"][0]["status"] in ["error"]:
109 raise RequestException(response=raw_resp, request=raw_resp.request)
110 return resp, raw_resp # now the caller of this will handle further follow-ups