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();
84 * Gets the path to be used when performing the request; this is typically appended to
85 * the base URL. This method simply invokes {@link #getPath()}.
87 * @return the path URI suffix
89 public String getPath() {
90 return config.getPath();
93 public long getTimeoutMs() {
94 return config.getTimeoutMs();
98 * If no timeout is specified, then it returns the operator's configured timeout.
101 protected long getTimeoutMs(Integer timeoutSec) {
102 return (timeoutSec == null || timeoutSec == 0 ? getTimeoutMs() : super.getTimeoutMs(timeoutSec));
106 * Makes the request headers. This simply returns an empty map.
108 * @return request headers, a non-null, modifiable map
110 protected Map<String, Object> makeHeaders() {
111 return new HashMap<>();
115 * Makes the URL to which the HTTP request should be posted. This is primarily used
116 * for logging purposes. This particular method returns the base URL appended with the
117 * return value from {@link #getPath()}.
119 * @return the URL to which from which to get
121 public String getUrl() {
122 return (getClient().getBaseUrl() + getPath());
126 * Arranges to handle a response.
128 * @param outcome outcome to be populate
129 * @param url URL to which to request was sent
130 * @param requester function to initiate the request and invoke the given callback
132 * @return a future for the response
134 protected CompletableFuture<OperationOutcome> handleResponse(OperationOutcome outcome, String url,
135 Function<InvocationCallback<Response>, Future<Response>> requester) {
137 final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
138 final CompletableFuture<Response> future = new CompletableFuture<>();
139 final Executor executor = params.getExecutor();
141 // arrange for the callback to complete "future"
142 InvocationCallback<Response> callback = new InvocationCallback<>() {
144 public void completed(Response response) {
145 future.complete(response);
149 public void failed(Throwable throwable) {
150 logger.warn("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
151 params.getRequestId());
152 future.completeExceptionally(throwable);
156 // start the request and arrange to cancel it if the controller is canceled
157 controller.add(requester.apply(callback));
159 // once "future" completes, process the response, and then complete the controller
160 future.thenComposeAsync(response -> processResponse(outcome, url, response), executor)
161 .whenCompleteAsync(controller.delayedComplete(), executor);
167 * Processes a response. This method decodes the response, sets the outcome based on
168 * the response, and then returns a completed future.
170 * @param outcome outcome to be populate
171 * @param url URL to which to request was sent
172 * @param response raw response to process
173 * @return a future to cancel or await the outcome
175 protected CompletableFuture<OperationOutcome> processResponse(OperationOutcome outcome, String url,
176 Response rawResponse) {
178 logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId());
180 String strResponse = rawResponse.readEntity(String.class);
182 logMessage(EventType.IN, CommInfrastructure.REST, url, strResponse);
185 if (responseClass == String.class) {
186 response = responseClass.cast(strResponse);
189 response = getCoder().decode(strResponse, responseClass);
190 } catch (CoderException e) {
191 logger.warn("{}.{} cannot decode response for {}", params.getActor(), params.getOperation(),
192 params.getRequestId(), e);
193 throw new IllegalArgumentException("cannot decode response");
197 if (!isSuccess(rawResponse, response)) {
198 logger.info("{}.{} request failed with http error code {} for {}", params.getActor(), params.getOperation(),
199 rawResponse.getStatus(), params.getRequestId());
200 return CompletableFuture.completedFuture(setOutcome(outcome, PolicyResult.FAILURE, rawResponse, response));
203 logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(), params.getRequestId());
204 setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response);
205 return postProcessResponse(outcome, url, rawResponse, response);
209 * Sets an operation's outcome and default message based on the result.
211 * @param outcome operation to be updated
212 * @param result result of the operation
213 * @param rawResponse raw response
214 * @param response decoded response
215 * @return the updated operation
217 public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
220 outcome.setResponse(response);
221 return setOutcome(outcome, result);
225 * Processes a successful response. This method simply returns the outcome wrapped in
226 * a completed future.
228 * @param outcome outcome to be populate
229 * @param url URL to which to request was sent
230 * @param rawResponse raw response
231 * @param response decoded response
232 * @return a future to cancel or await the outcome
234 protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
235 Response rawResponse, T response) {
237 return CompletableFuture.completedFuture(outcome);
241 * Determines if the response indicates success. This method simply checks the HTTP
244 * @param rawResponse raw response
245 * @param response decoded response
246 * @return {@code true} if the response indicates success, {@code false} otherwise
248 protected boolean isSuccess(Response rawResponse, T response) {
249 return (rawResponse.getStatus() == 200);
253 public <Q> String logMessage(EventType direction, CommInfrastructure infra, String sink, Q request) {
254 String json = super.logMessage(direction, infra, sink, request);
255 NetLoggerUtil.log(direction, infra, sink, json);