2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.impl;
23 import java.util.HashMap;
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;
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.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
42 import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
43 import org.onap.policy.controlloop.policy.PolicyResult;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
50 * @param <T> response type
53 public abstract class HttpOperation<T> extends OperationPartial {
54 private static final Logger logger = LoggerFactory.getLogger(HttpOperation.class);
55 private static final Coder coder = new StandardCoder();
58 * Operator that created this operation.
60 protected final HttpOperator operator;
65 private final Class<T> responseClass;
69 * Constructs the object.
71 * @param params operation parameters
72 * @param operator operator that created this operation
73 * @param clazz response class
75 public HttpOperation(ControlLoopOperationParams params, HttpOperator operator, Class<T> clazz) {
76 super(params, operator);
77 this.operator = operator;
78 this.responseClass = clazz;
82 * If no timeout is specified, then it returns the operator's configured timeout.
85 protected long getTimeoutMs(Integer timeoutSec) {
86 return (timeoutSec == null || timeoutSec == 0 ? operator.getTimeoutMs() : super.getTimeoutMs(timeoutSec));
90 * Makes the request headers. This simply returns an empty map.
92 * @return request headers, a non-null, modifiable map
94 protected Map<String, Object> makeHeaders() {
95 return new HashMap<>();
99 * Gets the path to be used when performing the request; this is typically appended to
100 * the base URL. This method simply invokes {@link #getPath()}.
102 * @return the path URI suffix
104 public String makePath() {
105 return operator.getPath();
109 * Makes the URL to which the "get" request should be posted. This ir primarily used
110 * for logging purposes. This particular method returns the base URL appended with the
111 * return value from {@link #makePath()}.
113 * @return the URL to which from which to get
115 public String makeUrl() {
116 return (operator.getClient().getBaseUrl() + makePath());
120 * Arranges to handle a response.
122 * @param outcome outcome to be populate
123 * @param url URL to which to request was sent
124 * @param requester function to initiate the request and invoke the given callback
126 * @return a future for the response
128 protected CompletableFuture<OperationOutcome> handleResponse(OperationOutcome outcome, String url,
129 Function<InvocationCallback<Response>, Future<Response>> requester) {
131 final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
132 final CompletableFuture<Response> future = new CompletableFuture<>();
133 final Executor executor = params.getExecutor();
135 // arrange for the callback to complete "future"
136 InvocationCallback<Response> callback = new InvocationCallback<>() {
138 public void completed(Response response) {
139 future.complete(response);
143 public void failed(Throwable throwable) {
144 logger.warn("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
145 params.getRequestId());
146 future.completeExceptionally(throwable);
150 // start the request and arrange to cancel it if the controller is canceled
151 controller.add(requester.apply(callback));
153 // once "future" completes, process the response, and then complete the controller
154 future.thenApplyAsync(response -> processResponse(outcome, url, response), executor)
155 .whenCompleteAsync(controller.delayedComplete(), executor);
161 * Processes a response. This method simply sets the outcome to SUCCESS.
163 * @param outcome outcome to be populate
164 * @param url URL to which to request was sent
165 * @param response raw response to process
166 * @return the outcome
168 protected OperationOutcome processResponse(OperationOutcome outcome, String url, Response rawResponse) {
170 logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId());
172 String strResponse = HttpClient.getBody(rawResponse, String.class);
174 logRestResponse(url, strResponse);
177 if (responseClass == String.class) {
178 response = responseClass.cast(strResponse);
182 response = makeCoder().decode(strResponse, responseClass);
183 } catch (CoderException e) {
184 logger.warn("{}.{} cannot decode response for {}", params.getActor(), params.getOperation(),
185 params.getRequestId(), e);
186 throw new IllegalArgumentException("cannot decode response");
190 if (!isSuccess(rawResponse, response)) {
191 logger.info("{}.{} request failed with http error code {} for {}", params.getActor(), params.getOperation(),
192 rawResponse.getStatus(), params.getRequestId());
193 return setOutcome(outcome, PolicyResult.FAILURE);
196 logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(), params.getRequestId());
197 setOutcome(outcome, PolicyResult.SUCCESS);
198 postProcessResponse(outcome, url, rawResponse, response);
204 * Processes a successful response.
206 * @param outcome outcome to be populate
207 * @param url URL to which to request was sent
208 * @param rawResponse raw response
209 * @param response decoded response
211 protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse, T response) {
216 * Determines if the response indicates success. This method simply checks the HTTP
219 * @param rawResponse raw response
220 * @param response decoded response
221 * @return {@code true} if the response indicates success, {@code false} otherwise
223 protected boolean isSuccess(Response rawResponse, T response) {
224 return (rawResponse.getStatus() == 200);
228 * Logs a REST request. If the request is not of type, String, then it attempts to
229 * pretty-print it into JSON before logging.
231 * @param url request URL
232 * @param request request to be logged
234 public <Q> void logRestRequest(String url, Q request) {
237 if (request == null) {
239 } else if (request instanceof String) {
240 json = request.toString();
242 json = makeCoder().encode(request, true);
245 } catch (CoderException e) {
246 logger.warn("cannot pretty-print request", e);
247 json = request.toString();
250 NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, json);
251 logger.info("[OUT|{}|{}|]{}{}", CommInfrastructure.REST, url, NetLoggerUtil.SYSTEM_LS, json);
255 * Logs a REST response. If the response is not of type, String, then it attempts to
256 * pretty-print it into JSON before logging.
258 * @param url request URL
259 * @param response response to be logged
261 public <S> void logRestResponse(String url, S response) {
264 if (response == null) {
266 } else if (response instanceof String) {
267 json = response.toString();
269 json = makeCoder().encode(response, true);
272 } catch (CoderException e) {
273 logger.warn("cannot pretty-print response", e);
274 json = response.toString();
277 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, json);
278 logger.info("[IN|{}|{}|]{}{}", CommInfrastructure.REST, url, NetLoggerUtil.SYSTEM_LS, json);
281 // these may be overridden by junit tests
283 protected Coder makeCoder() {