Get the tree of parent entities for an entity in a container
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / AsyncResponseHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider;
22
23 import java.util.concurrent.CompletableFuture;
24 import java.util.concurrent.Future;
25 import javax.ws.rs.client.InvocationCallback;
26 import lombok.AccessLevel;
27 import lombok.Getter;
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;
32
33 /**
34  * Handler for a <i>single</i> asynchronous response.
35  *
36  * @param <T> response type
37  */
38 @Getter
39 public abstract class AsyncResponseHandler<T> implements InvocationCallback<T> {
40
41     private static final Logger logger = LoggerFactory.getLogger(AsyncResponseHandler.class);
42
43     @Getter(AccessLevel.NONE)
44     private final PipelineControllerFuture<OperationOutcome> result = new PipelineControllerFuture<>();
45     private final ControlLoopOperationParams params;
46     private final OperationOutcome outcome;
47
48     /**
49      * Constructs the object.
50      *
51      * @param params operation parameters
52      * @param outcome outcome to be populated based on the response
53      */
54     public AsyncResponseHandler(ControlLoopOperationParams params, OperationOutcome outcome) {
55         this.params = params;
56         this.outcome = outcome;
57     }
58
59     /**
60      * Handles the given future, arranging to cancel it when the response is received.
61      *
62      * @param future future to be handled
63      * @return a future to be used to cancel or wait for the response
64      */
65     public CompletableFuture<OperationOutcome> handle(Future<T> future) {
66         result.add(future);
67         return result;
68     }
69
70     /**
71      * Invokes {@link #doComplete()} and then completes "this" with the returned value.
72      */
73     @Override
74     public void completed(T rawResponse) {
75         try {
76             logger.trace("{}.{}: response completed for {}", params.getActor(), params.getOperation(),
77                             params.getRequestId());
78             result.complete(doComplete(rawResponse));
79
80         } catch (RuntimeException e) {
81             logger.trace("{}.{}: response handler threw an exception for {}", params.getActor(), params.getOperation(),
82                             params.getRequestId());
83             result.completeExceptionally(e);
84         }
85     }
86
87     /**
88      * Invokes {@link #doFailed()} and then completes "this" with the returned value.
89      */
90     @Override
91     public void failed(Throwable throwable) {
92         try {
93             logger.trace("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
94                             params.getRequestId());
95             result.complete(doFailed(throwable));
96
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);
101         }
102     }
103
104     /**
105      * Completes the processing of a response.
106      *
107      * @param rawResponse raw response that was received
108      * @return the outcome
109      */
110     protected abstract OperationOutcome doComplete(T rawResponse);
111
112     /**
113      * Handles a response exception.
114      *
115      * @param thrown exception that was thrown
116      * @return the outcome
117      */
118     protected abstract OperationOutcome doFailed(Throwable thrown);
119 }