Fix for placement call check and sync response
[optf/osdf.git] / osdf / optimizers / placementopt / conductor / remote_opt_processor.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
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
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 from requests import RequestException
20
21 import traceback
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
27
28
29 def process_placement_opt(request_json, policies, osdf_config):
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
37     """
38     
39     try:
40         rc = get_rest_client(request_json, service="so")
41         req_id = request_json["requestInfo"]["requestId"]
42         transaction_id = request_json['requestInfo']['transactionId']
43
44         metrics_log.info(MH.inside_worker_thread(req_id))
45         license_info = None
46         if request_json.get('licenseInfo', {}).get('licenseDemands'):
47             license_info = license_optim(request_json)
48
49         # Conductor only handles placement, only call Conductor if placementDemands exist
50         if request_json.get('placementInfo', {}).get('placementDemands'):
51             metrics_log.info(MH.requesting("placement/conductor", req_id))
52             placement_response = conductor.request(request_json, osdf_config, policies)
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,
58                 "requestId": req_id,
59                 "requestStatus": "completed",
60                 "statusMessage": "License selection completed successfully",
61                 "solutionInfo": {"licenseInfo": license_info}
62             }
63     except Exception as err:
64         error_log.error("Error for {} {}".format(req_id, traceback.format_exc()))
65
66         try:
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()))
72         return
73
74     try:
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()))
79