Use BidirectionalTopicClient from policy-common
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperation.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.impl;
22
23 import java.util.HashMap;
24 import java.util.Map;
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;
31 import lombok.Getter;
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.HttpParams;
40 import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
41 import org.onap.policy.controlloop.policy.PolicyResult;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
47  *
48  * @param <T> response type
49  */
50 @Getter
51 public abstract class HttpOperation<T> extends OperationPartial {
52     private static final Logger logger = LoggerFactory.getLogger(HttpOperation.class);
53
54     /**
55      * Operator that created this operation.
56      */
57     protected final HttpOperator operator;
58
59     /**
60      * Response class.
61      */
62     private final Class<T> responseClass;
63
64
65     /**
66      * Constructs the object.
67      *
68      * @param params operation parameters
69      * @param operator operator that created this operation
70      * @param clazz response class
71      */
72     public HttpOperation(ControlLoopOperationParams params, HttpOperator operator, Class<T> clazz) {
73         super(params, operator);
74         this.operator = operator;
75         this.responseClass = clazz;
76     }
77
78     /**
79      * If no timeout is specified, then it returns the operator's configured timeout.
80      */
81     @Override
82     protected long getTimeoutMs(Integer timeoutSec) {
83         return (timeoutSec == null || timeoutSec == 0 ? operator.getTimeoutMs() : super.getTimeoutMs(timeoutSec));
84     }
85
86     /**
87      * Makes the request headers. This simply returns an empty map.
88      *
89      * @return request headers, a non-null, modifiable map
90      */
91     protected Map<String, Object> makeHeaders() {
92         return new HashMap<>();
93     }
94
95     /**
96      * Gets the path to be used when performing the request; this is typically appended to
97      * the base URL. This method simply invokes {@link #getPath()}.
98      *
99      * @return the path URI suffix
100      */
101     public String makePath() {
102         return operator.getPath();
103     }
104
105     /**
106      * Makes the URL to which the "get" request should be posted. This ir primarily used
107      * for logging purposes. This particular method returns the base URL appended with the
108      * return value from {@link #makePath()}.
109      *
110      * @return the URL to which from which to get
111      */
112     public String makeUrl() {
113         return (operator.getClient().getBaseUrl() + makePath());
114     }
115
116     /**
117      * Arranges to handle a response.
118      *
119      * @param outcome outcome to be populate
120      * @param url URL to which to request was sent
121      * @param requester function to initiate the request and invoke the given callback
122      *        when it completes
123      * @return a future for the response
124      */
125     protected CompletableFuture<OperationOutcome> handleResponse(OperationOutcome outcome, String url,
126                     Function<InvocationCallback<Response>, Future<Response>> requester) {
127
128         final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
129         final CompletableFuture<Response> future = new CompletableFuture<>();
130         final Executor executor = params.getExecutor();
131
132         // arrange for the callback to complete "future"
133         InvocationCallback<Response> callback = new InvocationCallback<>() {
134             @Override
135             public void completed(Response response) {
136                 future.complete(response);
137             }
138
139             @Override
140             public void failed(Throwable throwable) {
141                 logger.warn("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
142                                 params.getRequestId());
143                 future.completeExceptionally(throwable);
144             }
145         };
146
147         // start the request and arrange to cancel it if the controller is canceled
148         controller.add(requester.apply(callback));
149
150         // once "future" completes, process the response, and then complete the controller
151         future.thenApplyAsync(response -> processResponse(outcome, url, response), executor)
152                         .whenCompleteAsync(controller.delayedComplete(), executor);
153
154         return controller;
155     }
156
157     /**
158      * Processes a response. This method simply sets the outcome to SUCCESS.
159      *
160      * @param outcome outcome to be populate
161      * @param url URL to which to request was sent
162      * @param response raw response to process
163      * @return the outcome
164      */
165     protected OperationOutcome processResponse(OperationOutcome outcome, String url, Response rawResponse) {
166
167         logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId());
168
169         String strResponse = HttpClient.getBody(rawResponse, String.class);
170
171         logMessage(EventType.IN, CommInfrastructure.REST, url, strResponse);
172
173         T response;
174         if (responseClass == String.class) {
175             response = responseClass.cast(strResponse);
176
177         } else {
178             try {
179                 response = makeCoder().decode(strResponse, responseClass);
180             } catch (CoderException e) {
181                 logger.warn("{}.{} cannot decode response for {}", params.getActor(), params.getOperation(),
182                                 params.getRequestId(), e);
183                 throw new IllegalArgumentException("cannot decode response");
184             }
185         }
186
187         if (!isSuccess(rawResponse, response)) {
188             logger.info("{}.{} request failed with http error code {} for {}", params.getActor(), params.getOperation(),
189                             rawResponse.getStatus(), params.getRequestId());
190             return setOutcome(outcome, PolicyResult.FAILURE);
191         }
192
193         logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(), params.getRequestId());
194         setOutcome(outcome, PolicyResult.SUCCESS);
195         postProcessResponse(outcome, url, rawResponse, response);
196
197         return outcome;
198     }
199
200     /**
201      * Processes a successful response.
202      *
203      * @param outcome outcome to be populate
204      * @param url URL to which to request was sent
205      * @param rawResponse raw response
206      * @param response decoded response
207      */
208     protected void postProcessResponse(OperationOutcome outcome, String url, Response rawResponse, T response) {
209         // do nothing
210     }
211
212     /**
213      * Determines if the response indicates success. This method simply checks the HTTP
214      * status code.
215      *
216      * @param rawResponse raw response
217      * @param response decoded response
218      * @return {@code true} if the response indicates success, {@code false} otherwise
219      */
220     protected boolean isSuccess(Response rawResponse, T response) {
221         return (rawResponse.getStatus() == 200);
222     }
223
224     @Override
225     public <Q> String logMessage(EventType direction, CommInfrastructure infra, String sink, Q request) {
226         String json = super.logMessage(direction, infra, sink, request);
227         NetLoggerUtil.log(direction, infra, sink, json);
228         return json;
229     }
230 }