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