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 # -------------------------------------------------------------------------
19 from requests import RequestException
22 from osdf.operation.error_handling import build_json_error_body
23 from osdf.logging.osdf_logging import metrics_log, MH, error_log
24 from osdf.optimizers.placementopt.conductor import conductor
25 from osdf.optimizers.licenseopt.simple_license_allocation import license_optim
26 from osdf.utils.interfaces import get_rest_client
29 def process_placement_opt(request_json, policies, osdf_config, prov_status):
30 """Perform the work for placement optimization (e.g. call SDC artifact and make conductor request)
31 NOTE: there is scope to make the requests to policy asynchronous to speed up overall performance
32 :param request_json: json content from original request
33 :param policies: flattened policies corresponding to this request
34 :param osdf_config: configuration specific to OSDF app
35 :param prov_status: provStatus retrieved from Subscriber policy
36 :return: None, but make a POST to callback URL
40 rc = get_rest_client(request_json, service="so")
41 req_id = request_json["requestInfo"]["requestId"]
42 transaction_id = request_json['requestInfo']['transactionId']
44 metrics_log.info(MH.inside_worker_thread(req_id))
46 if 'licenseDemand' in request_json['placementInfo']['demandInfo']:
47 license_info = license_optim(request_json)
49 # Conductor only handles placement, only call Conductor if placementDemands exist
50 if 'placementDemand' in request_json['placementInfo']['demandInfo']:
51 metrics_log.info(MH.requesting("placement/conductor", req_id))
52 placement_response = conductor.request(request_json, osdf_config, policies, prov_status)
53 if license_info: # Attach license solution if it exists
54 placement_response['solutionInfo']['licenseInfo'] = license_info
55 else: # License selection only scenario
56 placement_response = {
57 "transactionId": transaction_id,
59 "requestState": "complete",
60 "statusMessage": "License selection completed successfully",
61 "solutionInfo": {"licenseInfo": license_info}
63 except Exception as err:
64 error_log.error("Error for {} {}".format(req_id, traceback.format_exc()))
67 body = build_json_error_body(err)
68 metrics_log.info(MH.sending_response(req_id, "ERROR"))
69 rc.request(json=body, noresponse=True)
70 except RequestException:
71 error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
75 metrics_log.info(MH.calling_back_with_body(req_id, rc.url,placement_response))
76 rc.request(json=placement_response, noresponse=True)
77 except RequestException : # can't do much here but log it and move on
78 error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))