2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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;
24 import java.util.List;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.Executor;
28 import java.util.concurrent.Future;
29 import java.util.concurrent.TimeUnit;
30 import java.util.function.Function;
31 import javax.ws.rs.client.InvocationCallback;
32 import javax.ws.rs.core.Response;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.onap.policy.common.endpoints.http.client.HttpClient;
36 import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
37 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
43 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
45 import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
52 * @param <T> response type
55 public abstract class HttpOperation<T> extends OperationPartial {
56 private static final Logger logger = LoggerFactory.getLogger(HttpOperation.class);
62 SUCCESS, FAILURE, STILL_WAITING
66 * Configuration for this operation.
68 private final HttpConfig config;
73 private final Class<T> responseClass;
76 * {@code True} to use polling, {@code false} otherwise.
79 private boolean usePolling;
82 * Number of polls issued so far, on the current operation attempt.
85 private int pollCount;
89 * Constructs the object.
91 * @param params operation parameters
92 * @param config configuration for this operation
93 * @param clazz response class
94 * @param propertyNames names of properties required by this operation
96 protected HttpOperation(ControlLoopOperationParams params, HttpConfig config, Class<T> clazz,
97 List<String> propertyNames) {
98 super(params, config, propertyNames);
100 this.responseClass = clazz;
104 * Indicates that polling should be used.
106 protected void setUsePolling() {
107 if (!(config instanceof HttpPollingConfig)) {
108 throw new IllegalStateException("cannot poll without polling parameters");
114 public HttpClient getClient() {
115 return config.getClient();
119 * Gets the path to be used when performing the request; this is typically appended to
120 * the base URL. This method simply invokes {@link #getPath()}.
122 * @return the path URI suffix
124 public String getPath() {
125 return config.getPath();
128 public long getTimeoutMs() {
129 return config.getTimeoutMs();
133 * If no timeout is specified, then it returns the operator's configured timeout.
136 protected long getTimeoutMs(Integer timeoutSec) {
137 return (timeoutSec == null || timeoutSec == 0 ? getTimeoutMs() : super.getTimeoutMs(timeoutSec));
141 * Makes the request headers. This simply returns an empty map.
143 * @return request headers, a non-null, modifiable map
145 protected Map<String, Object> makeHeaders() {
146 return new HashMap<>();
150 * Makes the URL to which the HTTP request should be posted. This is primarily used
151 * for logging purposes. This particular method returns the base URL appended with the
152 * return value from {@link #getPath()}.
154 * @return the URL to which from which to get
156 public String getUrl() {
157 return (getClient().getBaseUrl() + getPath());
161 * Resets the polling count
164 * Note: This should be invoked at the start of each operation (i.e., in
165 * {@link #startOperationAsync(int, OperationOutcome)}.
167 protected void resetPollCount() {
172 * Arranges to handle a response.
174 * @param outcome outcome to be populate
175 * @param url URL to which to request was sent
176 * @param requester function to initiate the request and invoke the given callback
178 * @return a future for the response
180 protected CompletableFuture<OperationOutcome> handleResponse(OperationOutcome outcome, String url,
181 Function<InvocationCallback<Response>, Future<Response>> requester) {
183 final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
184 final CompletableFuture<Response> future = new CompletableFuture<>();
185 final Executor executor = params.getExecutor();
187 // arrange for the callback to complete "future"
188 InvocationCallback<Response> callback = new InvocationCallback<>() {
190 public void completed(Response response) {
191 future.complete(response);
195 public void failed(Throwable throwable) {
196 logger.warn("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
197 params.getRequestId());
198 future.completeExceptionally(throwable);
202 // start the request and arrange to cancel it if the controller is canceled
203 controller.add(requester.apply(callback));
205 // once "future" completes, process the response, and then complete the controller
206 future.thenComposeAsync(response -> processResponse(outcome, url, response), executor)
207 .whenCompleteAsync(controller.delayedComplete(), executor);
213 * Processes a response. This method decodes the response, sets the outcome based on
214 * the response, and then returns a completed future.
216 * @param outcome outcome to be populate
217 * @param url URL to which to request was sent
218 * @param response raw response to process
219 * @return a future to cancel or await the outcome
221 protected CompletableFuture<OperationOutcome> processResponse(OperationOutcome outcome, String url,
222 Response rawResponse) {
224 logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId());
226 String strResponse = rawResponse.readEntity(String.class);
228 logMessage(EventType.IN, CommInfrastructure.REST, url, strResponse);
231 if (responseClass == String.class) {
232 response = responseClass.cast(strResponse);
235 response = getCoder().decode(strResponse, responseClass);
236 } catch (CoderException e) {
237 logger.warn("{}.{} cannot decode response for {}", params.getActor(), params.getOperation(),
238 params.getRequestId(), e);
239 throw new IllegalArgumentException("cannot decode response");
243 if (!isSuccess(rawResponse, response)) {
244 logger.info("{}.{} request failed with http error code {} for {}", params.getActor(), params.getOperation(),
245 rawResponse.getStatus(), params.getRequestId());
246 return CompletableFuture.completedFuture(
247 setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
250 logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(), params.getRequestId());
251 setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response);
252 return postProcessResponse(outcome, url, rawResponse, response);
256 * Sets an operation's outcome and default message based on the result.
258 * @param outcome operation to be updated
259 * @param result result of the operation
260 * @param rawResponse raw response
261 * @param response decoded response
262 * @return the updated operation
264 public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
267 outcome.setResponse(response);
268 return setOutcome(outcome, result);
272 * Processes a successful response. This method simply returns the outcome wrapped in
273 * a completed future.
275 * @param outcome outcome to be populate
276 * @param url URL to which to request was sent
277 * @param rawResponse raw response
278 * @param response decoded response
279 * @return a future to cancel or await the outcome
281 protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
282 Response rawResponse, T response) {
285 // doesn't use polling - just return the completed future
286 return CompletableFuture.completedFuture(outcome);
289 HttpPollingConfig cfg = (HttpPollingConfig) config;
291 switch (detmStatus(rawResponse, response)) {
293 logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(),
294 params.getRequestId());
295 return CompletableFuture
296 .completedFuture(setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response));
299 logger.info("{}.{} request failed for {}", params.getActor(), params.getOperation(),
300 params.getRequestId());
301 return CompletableFuture
302 .completedFuture(setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
306 logger.info("{}.{} request incomplete for {}", params.getActor(), params.getOperation(),
307 params.getRequestId());
313 // see if the limit for the number of polls has been reached
314 if (pollCount++ >= cfg.getMaxPolls()) {
315 logger.warn("{}: execeeded 'poll' limit {} for {}", getFullName(), cfg.getMaxPolls(),
316 params.getRequestId());
317 setOutcome(outcome, OperationResult.FAILURE_TIMEOUT);
318 return CompletableFuture.completedFuture(outcome);
321 // sleep and then poll
322 Function<Void, CompletableFuture<OperationOutcome>> doPoll = unused -> issuePoll(outcome);
323 return sleep(getPollWaitMs(), TimeUnit.MILLISECONDS).thenComposeAsync(doPoll);
327 * Polls to see if the original request is complete. This method polls using an HTTP
328 * "get" request whose URL is constructed by appending the extracted "poll ID" to the
329 * poll path from the configuration data.
331 * @param outcome outcome to be populated with the response
332 * @return a future that can be used to cancel the poll or await its response
334 protected CompletableFuture<OperationOutcome> issuePoll(OperationOutcome outcome) {
335 String path = getPollingPath();
336 String url = getClient().getBaseUrl() + path;
338 logger.debug("{}: 'poll' count {} for {}", getFullName(), pollCount, params.getRequestId());
340 logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
342 return handleResponse(outcome, url, callback -> getClient().get(callback, path, null));
346 * Determines the status of the response. This particular method simply throws an
349 * @param rawResponse raw response
350 * @param response decoded response
351 * @return the status of the response
353 protected Status detmStatus(Response rawResponse, T response) {
354 throw new UnsupportedOperationException("cannot determine response status");
358 * Gets the URL to use when polling. Typically, this is some unique ID appended to the
359 * polling path found within the configuration data. This particular method simply
360 * returns the polling path from the configuration data.
362 * @return the URL to use when polling
364 protected String getPollingPath() {
365 return ((HttpPollingConfig) config).getPollPath();
369 * Determines if the response indicates success. This method simply checks the HTTP
372 * @param rawResponse raw response
373 * @param response decoded response
374 * @return {@code true} if the response indicates success, {@code false} otherwise
376 protected boolean isSuccess(Response rawResponse, T response) {
377 return (rawResponse.getStatus() == 200);
381 public <Q> String logMessage(EventType direction, CommInfrastructure infra, String sink, Q request) {
382 String json = super.logMessage(direction, infra, sink, request);
383 NetLoggerUtil.log(direction, infra, sink, json);
387 // these may be overridden by junit tests
389 protected long getPollWaitMs() {
390 HttpPollingConfig cfg = (HttpPollingConfig) config;
392 return TimeUnit.MILLISECONDS.convert(cfg.getPollWaitSec(), TimeUnit.SECONDS);