1 # -------------------------------------------------------------------------
2 # Copyright (c) 2015-2017 AT&T Intellectual Property
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # -------------------------------------------------------------------------
20 This application generates conductor API calls using the information received from SO and Policy platform.
26 from jinja2 import Template
27 from requests import RequestException
29 from osdf.logging.osdf_logging import debug_log
30 from osdf.optimizers.placementopt.conductor.api_builder import conductor_api_builder
31 from osdf.utils.interfaces import RestClient
32 from osdf.operation.exceptions import BusinessException
35 def request(req_object, osdf_config, grouped_policies, prov_status):
37 Process a placement request from a Client (build Conductor API call, make the call, return result)
38 :param req_object: Request parameters from the client
39 :param osdf_config: Configuration specific to SNIRO application (core + deployment)
40 :param grouped_policies: policies related to placement (fetched based on request, and grouped by policy type)
41 :param prov_status: provStatus retrieved from Subscriber policy
42 :return: response from Conductor (accounting for redirects from Conductor service
44 config = osdf_config.deployment
45 local_config = osdf_config.core
46 uid, passwd = config['conductorUsername'], config['conductorPassword']
47 conductor_url = config['conductorUrl']
48 req_id = req_object['requestInfo']['requestId']
49 transaction_id = req_object['requestInfo']['transactionId']
50 headers = dict(transaction_id=transaction_id)
52 max_retries = config.get('conductorMaxRetries', 30)
53 ping_wait_time = config.get('conductorPingWaitTime', 60)
55 rc = RestClient(userid=uid, passwd=passwd, method="GET", log_func=debug_log.debug, headers=headers)
56 conductor_req_json_str = conductor_api_builder(req_object, grouped_policies, local_config, prov_status)
57 conductor_req_json = json.loads(conductor_req_json_str)
59 debug_log.debug("Sending first Conductor request for request_id {}".format(req_id))
60 resp, raw_resp = initial_request_to_conductor(rc, conductor_url, conductor_req_json)
61 # Very crude way of keeping track of time.
62 # We are not counting initial request time, first call back, or time for HTTP request
63 total_time, ctr = 0, 2
64 client_timeout = req_object['requestInfo']['timeout']
65 configured_timeout = max_retries * ping_wait_time
66 max_timeout = min(client_timeout, configured_timeout)
68 while True: # keep requesting conductor till we get a result or we run out of time
70 if resp["plans"][0].get("status") in ["error"]:
71 raise RequestException(response=raw_resp, request=raw_resp.request)
73 if resp["plans"][0].get("status") in ["done", "not found"]:
74 if resp["plans"][0].get("recommendations"):
75 return conductor_response_processor(resp, raw_resp, req_id)
76 else: # "solved" but no solutions found
77 return conductor_no_solution_processor(resp, raw_resp, req_id)
78 new_url = resp['plans'][0]['links'][0][0]['href'] # TODO: check why a list of lists
80 if total_time >= max_timeout:
81 raise BusinessException("Conductor could not provide a solution within {} seconds,"
82 "this transaction is timing out".format(max_timeout))
83 time.sleep(ping_wait_time)
85 debug_log.debug("Attempt number {} url {}; prior status={}".format(ctr, new_url, resp['plans'][0]['status']))
86 total_time += ping_wait_time
89 raw_resp = rc.request(new_url, raw_response=True)
90 resp = raw_resp.json()
91 except RequestException as e:
92 debug_log.debug("Conductor attempt {} for request_id {} has failed because {}".format(ctr, req_id, str(e)))
95 def initial_request_to_conductor(rc, conductor_url, conductor_req_json):
96 """First steps in the request-redirect chain in making a call to Conductor
97 :param rc: REST client object for calling conductor
98 :param conductor_url: conductor's base URL to submit a placement request
99 :param conductor_req_json: request json object to send to Conductor
100 :return: URL to check for follow up (similar to redirects); we keep checking these till we get a result/error
102 debug_log.debug("Payload to Conductor: {}".format(json.dumps(conductor_req_json)))
103 raw_resp = rc.request(url=conductor_url, raw_response=True, method="POST", json=conductor_req_json)
104 resp = raw_resp.json()
105 if resp["status"] != "template":
106 raise RequestException(response=raw_resp, request=raw_resp.request)
107 time.sleep(10) # 10 seconds wait time to avoid being too quick!
108 plan_url = resp["links"][0][0]["href"]
109 debug_log.debug("Attemping to read the plan from the conductor provided url {}".format(plan_url))
110 raw_resp = rc.request(raw_response=True, url=plan_url) # TODO: check why a list of lists for links
111 resp = raw_resp.json()
113 if resp["plans"][0]["status"] in ["error"]:
114 raise RequestException(response=raw_resp, request=raw_resp.request)
115 return resp, raw_resp # now the caller of this will handle further follow-ups
118 def conductor_response_processor(conductor_response, raw_response, req_id):
119 """Build a response object to be sent to client's callback URL from Conductor's response
120 This includes Conductor's placement optimization response, and required ASDC license artifacts
122 :param conductor_response: JSON response from Conductor
123 :param raw_response: Raw HTTP response corresponding to above
124 :param req_id: Id of a request
125 :return: JSON object that can be sent to the client's callback URL
127 composite_solutions = []
128 name_map = {"physical-location-id": "cloudClli", "host_id": "vnfHostName",
129 "cloud_version": "cloudVersion", "cloud_owner": "cloudOwner"}
130 for reco in conductor_response['plans'][0]['recommendations']:
131 for resource in reco.keys():
132 c = reco[resource]['candidate']
134 'resourceModuleName': resource,
135 'serviceResourceId': reco[resource]['service_resource_id'],
136 'inventoryType': c['inventory_type'],
137 'serviceInstanceId': c['candidate_id'] if c['inventory_type'] == "service" else "",
138 'cloudRegionId': c['location_id'],
142 for key, value in reco[resource]['attributes'].items():
144 solution['assignmentInfo'].append({"variableName": name_map[key], "variableValue": value})
146 debug_log.debug("The key[{}] is not mapped and will not be returned in assignment info".format(key))
149 solution['assignmentInfo'].append({'variableName': name_map['host_id'], 'variableValue': c['host_id']})
150 composite_solutions.append(solution)
152 request_state = conductor_response['plans'][0]['status']
153 transaction_id = raw_response.headers.get('transaction_id', "")
154 status_message = conductor_response.get('plans')[0].get('message', "")
157 if composite_solutions:
158 solution_info['placementInfo'] = composite_solutions
161 "transactionId": transaction_id,
163 "requestState": request_state,
164 "statusMessage": status_message,
165 "solutionInfo": solution_info
170 def conductor_no_solution_processor(conductor_response, raw_response, request_id,
171 template_placement_response="templates/plc_opt_response.jsont"):
172 """Build a response object to be sent to client's callback URL from Conductor's response
173 This is for case where no solution is found
175 :param conductor_response: JSON response from Conductor
176 :param raw_response: Raw HTTP response corresponding to above
177 :param request_id: request Id associated with the client request (same as conductor response's "name")
178 :param template_placement_response: the template for generating response to client (plc_opt_response.jsont)
179 :return: JSON object that can be sent to the client's callback URL
181 status_message = conductor_response["plans"][0].get("message")
182 templ = Template(open(template_placement_response).read())
183 return json.loads(templ.render(composite_solutions=[], requestId=request_id,
184 transactionId=raw_response.headers.get('transaction_id', ""),
185 statusMessage=status_message, json=json))