Add onap log spec 1.2 for osdf
[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 from osdf.utils.mdc_utils import mdc_from_json
28
29
30 def process_placement_opt(request_json, policies, osdf_config):
31     """Perform the work for placement optimization (e.g. call SDC artifact and make conductor request)
32     NOTE: there is scope to make the requests to policy asynchronous to speed up overall performance
33     :param request_json: json content from original request
34     :param policies: flattened policies corresponding to this request
35     :param osdf_config: configuration specific to OSDF app
36     :param prov_status: provStatus retrieved from Subscriber policy
37     :return: None, but make a POST to callback URL
38     """
39     
40     try:
41         mdc_from_json(request_json)
42         rc = get_rest_client(request_json, service="so")
43         req_id = request_json["requestInfo"]["requestId"]
44         transaction_id = request_json['requestInfo']['transactionId']
45
46         metrics_log.info(MH.inside_worker_thread(req_id))
47         license_info = None
48         if request_json.get('licenseInfo', {}).get('licenseDemands'):
49             license_info = license_optim(request_json)
50
51         # Conductor only handles placement, only call Conductor if placementDemands exist
52         if request_json.get('placementInfo', {}).get('placementDemands'):
53             metrics_log.info(MH.requesting("placement/conductor", req_id))
54             placement_response = conductor.request(request_json, osdf_config, policies)
55             if license_info:  # Attach license solution if it exists
56                 placement_response['solutionInfo']['licenseInfo'] = license_info
57         else:  # License selection only scenario
58             placement_response = {
59                 "transactionId": transaction_id,
60                 "requestId": req_id,
61                 "requestStatus": "completed",
62                 "statusMessage": "License selection completed successfully",
63                 "solutionInfo": {"licenseInfo": license_info}
64             }
65     except Exception as err:
66         error_log.error("Error for {} {}".format(req_id, traceback.format_exc()))
67
68         try:
69             body = build_json_error_body(err)
70             metrics_log.info(MH.sending_response(req_id, "ERROR"))
71             rc.request(json=body, noresponse=True)
72         except RequestException:
73             error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
74         return
75
76     try:
77         metrics_log.info(MH.calling_back_with_body(req_id, rc.url,placement_response))
78         rc.request(json=placement_response, noresponse=True)
79     except RequestException :  # can't do much here but log it and move on
80         error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
81