4c007ea19a170195d996f1892c0e9ea45c9e66c4
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.impl;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.concurrent.CompletableFuture;
26 import java.util.concurrent.Executor;
27 import java.util.concurrent.Future;
28 import java.util.function.Function;
29 import javax.ws.rs.client.InvocationCallback;
30 import javax.ws.rs.core.Response;
31 import lombok.Getter;
32 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
33 import org.onap.policy.common.endpoints.http.client.HttpClient;
34 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
35 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
41 import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
42 import org.onap.policy.controlloop.policy.PolicyResult;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
48  *
49  * @param <T> response type
50  */
51 @Getter
52 public abstract class HttpOperation<T> extends OperationPartial {
53     private static final Logger logger = LoggerFactory.getLogger(HttpOperation.class);
54
55     /**
56      * Configuration for this operation.
57      */
58     private final HttpConfig config;
59
60     /**
61      * Response class.
62      */
63     private final Class<T> responseClass;
64
65
66     /**
67      * Constructs the object.
68      *
69      * @param params operation parameters
70      * @param config configuration for this operation
71      * @param clazz response class
72      */
73     public HttpOperation(ControlLoopOperationParams params, HttpConfig config, Class<T> clazz) {
74         super(params, config);
75         this.config = config;
76         this.responseClass = clazz;
77     }
78
79     public HttpClient getClient() {
80         return config.getClient();
81     }
82
83     public String getPath() {
84         return config.getPath();
85     }
86
87     public long getTimeoutMs() {
88         return config.getTimeoutMs();
89     }
90
91     /**
92      * If no timeout is specified, then it returns the operator's configured timeout.
93      */
94     @Override
95     protected long getTimeoutMs(Integer timeoutSec) {
96         return (timeoutSec == null || timeoutSec == 0 ? getTimeoutMs() : super.getTimeoutMs(timeoutSec));
97     }
98
99     /**
100      * Makes the request headers. This simply returns an empty map.
101      *
102      * @return request headers, a non-null, modifiable map
103      */
104     protected Map<String, Object> makeHeaders() {
105         return new HashMap<>();
106     }
107
108     /**
109      * Gets the path to be used when performing the request; this is typically appended to
110      * the base URL. This method simply invokes {@link #getPath()}.
111      *
112      * @return the path URI suffix
113      */
114     public String makePath() {
115         return getPath();
116     }
117
118     /**
119      * Makes the URL to which the "get" request should be posted. This ir primarily used
120      * for logging purposes. This particular method returns the base URL appended with the
121      * return value from {@link #makePath()}.
122      *
123      * @return the URL to which from which to get
124      */
125     public String makeUrl() {
126         return (getClient().getBaseUrl() + makePath());
127     }
128
129     /**
130      * Arranges to handle a response.
131      *
132      * @param outcome outcome to be populate
133      * @param url URL to which to request was sent
134      * @param requester function to initiate the request and invoke the given callback
135      *        when it completes
136      * @return a future for the response
137      */
138     protected CompletableFuture<OperationOutcome> handleResponse(OperationOutcome outcome, String url,
139                     Function<InvocationCallback<Response>, Future<Response>> requester) {
140
141         final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
142         final CompletableFuture<Response> future = new CompletableFuture<>();
143         final Executor executor = params.getExecutor();
144
145         // arrange for the callback to complete "future"
146         InvocationCallback<Response> callback = new InvocationCallback<>() {
147             @Override
148             public void completed(Response response) {
149                 future.complete(response);
150             }
151
152             @Override
153             public void failed(Throwable throwable) {
154                 logger.warn("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
155                                 params.getRequestId());
156                 future.completeExceptionally(throwable);
157             }
158         };
159
160         // start the request and arrange to cancel it if the controller is canceled
161         controller.add(requester.apply(callback));
162
163         // once "future" completes, process the response, and then complete the controller
164         future.thenComposeAsync(response -> processResponse(outcome, url, response), executor)
165                         .whenCompleteAsync(controller.delayedComplete(), executor);
166
167         return controller;
168     }
169
170     /**
171      * Processes a response. This method decodes the response, sets the outcome based on
172      * the response, and then returns a completed future.
173      *
174      * @param outcome outcome to be populate
175      * @param url URL to which to request was sent
176      * @param response raw response to process
177      * @return a future to cancel or await the outcome
178      */
179     protected CompletableFuture<OperationOutcome> processResponse(OperationOutcome outcome, String url,
180                     Response rawResponse) {
181
182         logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId());
183
184         String strResponse = HttpClient.getBody(rawResponse, String.class);
185
186         logMessage(EventType.IN, CommInfrastructure.REST, url, strResponse);
187
188         T response;
189         if (responseClass == String.class) {
190             response = responseClass.cast(strResponse);
191         } else {
192             try {
193                 response = makeCoder().decode(strResponse, responseClass);
194             } catch (CoderException e) {
195                 logger.warn("{}.{} cannot decode response for {}", params.getActor(), params.getOperation(),
196                                 params.getRequestId(), e);
197                 throw new IllegalArgumentException("cannot decode response");
198             }
199         }
200
201         if (!isSuccess(rawResponse, response)) {
202             logger.info("{}.{} request failed with http error code {} for {}", params.getActor(), params.getOperation(),
203                             rawResponse.getStatus(), params.getRequestId());
204             return CompletableFuture.completedFuture(setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response));
205         }
206
207         logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(), params.getRequestId());
208         setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response);
209         return postProcessResponse(outcome, url, rawResponse, response);
210     }
211
212     /**
213      * Sets an operation's outcome and default message based on the result.
214      *
215      * @param outcome operation to be updated
216      * @param result result of the operation
217      * @param rawResponse raw response
218      * @param response decoded response
219      * @return the updated operation
220      */
221     public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
222                     T response) {
223
224         return setOutcome(outcome, result);
225     }
226
227     /**
228      * Processes a successful response. This method simply returns the outcome wrapped in
229      * a completed future.
230      *
231      * @param outcome outcome to be populate
232      * @param url URL to which to request was sent
233      * @param rawResponse raw response
234      * @param response decoded response
235      * @return a future to cancel or await the outcome
236      */
237     protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
238                     Response rawResponse, T response) {
239
240         return CompletableFuture.completedFuture(outcome);
241     }
242
243     /**
244      * Determines if the response indicates success. This method simply checks the HTTP
245      * status code.
246      *
247      * @param rawResponse raw response
248      * @param response decoded response
249      * @return {@code true} if the response indicates success, {@code false} otherwise
250      */
251     protected boolean isSuccess(Response rawResponse, T response) {
252         return (rawResponse.getStatus() == 200);
253     }
254
255     @Override
256     public <Q> String logMessage(EventType direction, CommInfrastructure infra, String sink, Q request) {
257         String json = super.logMessage(direction, infra, sink, request);
258         NetLoggerUtil.log(direction, infra, sink, json);
259         return json;
260     }
261 }