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;
 
  23 import java.util.concurrent.CompletableFuture;
 
  24 import java.util.concurrent.Future;
 
  25 import javax.ws.rs.client.InvocationCallback;
 
  26 import lombok.AccessLevel;
 
  28 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  29 import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
 
  30 import org.slf4j.Logger;
 
  31 import org.slf4j.LoggerFactory;
 
  34  * Handler for a <i>single</i> asynchronous response.
 
  36  * @param <T> response type
 
  39 public abstract class AsyncResponseHandler<T> implements InvocationCallback<T> {
 
  41     private static final Logger logger = LoggerFactory.getLogger(AsyncResponseHandler.class);
 
  43     @Getter(AccessLevel.NONE)
 
  44     private final PipelineControllerFuture<OperationOutcome> result = new PipelineControllerFuture<>();
 
  45     private final ControlLoopOperationParams params;
 
  46     private final OperationOutcome outcome;
 
  49      * Constructs the object.
 
  51      * @param params operation parameters
 
  52      * @param outcome outcome to be populated based on the response
 
  54     public AsyncResponseHandler(ControlLoopOperationParams params, OperationOutcome outcome) {
 
  56         this.outcome = outcome;
 
  60      * Handles the given future, arranging to cancel it when the response is received.
 
  62      * @param future future to be handled
 
  63      * @return a future to be used to cancel or wait for the response
 
  65     public CompletableFuture<OperationOutcome> handle(Future<T> future) {
 
  71      * Invokes {@link #doComplete()} and then completes "this" with the returned value.
 
  74     public void completed(T rawResponse) {
 
  76             logger.trace("{}.{}: response completed for {}", params.getActor(), params.getOperation(),
 
  77                             params.getRequestId());
 
  78             result.complete(doComplete(rawResponse));
 
  80         } catch (RuntimeException e) {
 
  81             logger.trace("{}.{}: response handler threw an exception for {}", params.getActor(), params.getOperation(),
 
  82                             params.getRequestId());
 
  83             result.completeExceptionally(e);
 
  88      * Invokes {@link #doFailed()} and then completes "this" with the returned value.
 
  91     public void failed(Throwable throwable) {
 
  93             logger.trace("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
 
  94                             params.getRequestId());
 
  95             result.complete(doFailed(throwable));
 
  97         } catch (RuntimeException e) {
 
  98             logger.trace("{}.{}: response failure handler threw an exception for {}", params.getActor(),
 
  99                             params.getOperation(), params.getRequestId());
 
 100             result.completeExceptionally(e);
 
 105      * Completes the processing of a response.
 
 107      * @param rawResponse raw response that was received
 
 108      * @return the outcome
 
 110     protected abstract OperationOutcome doComplete(T rawResponse);
 
 113      * Handles a response exception.
 
 115      * @param thrown exception that was thrown
 
 116      * @return the outcome
 
 118     protected abstract OperationOutcome doFailed(Throwable thrown);