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.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;
47 * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
49 * @param <T> response type
52 public abstract class HttpOperation<T> extends OperationPartial {
53 private static final Logger logger = LoggerFactory.getLogger(HttpOperation.class);
56 * Configuration for this operation.
58 private final HttpConfig config;
63 private final Class<T> responseClass;
67 * Constructs the object.
69 * @param params operation parameters
70 * @param config configuration for this operation
71 * @param clazz response class
73 public HttpOperation(ControlLoopOperationParams params, HttpConfig config, Class<T> clazz) {
74 super(params, config);
76 this.responseClass = clazz;
79 public HttpClient getClient() {
80 return config.getClient();
83 public String getPath() {
84 return config.getPath();
87 public long getTimeoutMs() {
88 return config.getTimeoutMs();
92 * If no timeout is specified, then it returns the operator's configured timeout.
95 protected long getTimeoutMs(Integer timeoutSec) {
96 return (timeoutSec == null || timeoutSec == 0 ? getTimeoutMs() : super.getTimeoutMs(timeoutSec));
100 * Makes the request headers. This simply returns an empty map.
102 * @return request headers, a non-null, modifiable map
104 protected Map<String, Object> makeHeaders() {
105 return new HashMap<>();
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()}.
112 * @return the path URI suffix
114 public String makePath() {
119 * Makes the URL to which the "get" request should be posted. This is primarily used
120 * for logging purposes. This particular method returns the base URL appended with the
121 * return value from {@link #makePath()}.
123 * @return the URL to which from which to get
125 public String makeUrl() {
126 return (getClient().getBaseUrl() + makePath());
130 * Arranges to handle a response.
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
136 * @return a future for the response
138 protected CompletableFuture<OperationOutcome> handleResponse(OperationOutcome outcome, String url,
139 Function<InvocationCallback<Response>, Future<Response>> requester) {
141 final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
142 final CompletableFuture<Response> future = new CompletableFuture<>();
143 final Executor executor = params.getExecutor();
145 // arrange for the callback to complete "future"
146 InvocationCallback<Response> callback = new InvocationCallback<>() {
148 public void completed(Response response) {
149 future.complete(response);
153 public void failed(Throwable throwable) {
154 logger.warn("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
155 params.getRequestId());
156 future.completeExceptionally(throwable);
160 // start the request and arrange to cancel it if the controller is canceled
161 controller.add(requester.apply(callback));
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);
171 * Processes a response. This method decodes the response, sets the outcome based on
172 * the response, and then returns a completed future.
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
179 protected CompletableFuture<OperationOutcome> processResponse(OperationOutcome outcome, String url,
180 Response rawResponse) {
182 logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId());
184 String strResponse = HttpClient.getBody(rawResponse, String.class);
186 logMessage(EventType.IN, CommInfrastructure.REST, url, strResponse);
189 if (responseClass == String.class) {
190 response = responseClass.cast(strResponse);
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");
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));
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);
213 * Sets an operation's outcome and default message based on the result.
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
221 public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
224 return setOutcome(outcome, result);
228 * Processes a successful response. This method simply returns the outcome wrapped in
229 * a completed future.
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
237 protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
238 Response rawResponse, T response) {
240 return CompletableFuture.completedFuture(outcome);
244 * Determines if the response indicates success. This method simply checks the HTTP
247 * @param rawResponse raw response
248 * @param response decoded response
249 * @return {@code true} if the response indicates success, {@code false} otherwise
251 protected boolean isSuccess(Response rawResponse, T response) {
252 return (rawResponse.getStatus() == 200);
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);