aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-actors/actorServiceProvider/src/main/java/org/onap/policy/controlloop/actorserviceprovider/impl/HttpOperation.java
blob: 1e11bce4c80e0ddc7e740409f3e2cc5d6aa42b03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.controlloop.actorserviceprovider.impl;

import jakarta.ws.rs.client.InvocationCallback;
import jakarta.ws.rs.core.Response;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import lombok.Getter;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.common.endpoints.http.client.HttpClient;
import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
import org.onap.policy.controlloop.actorserviceprovider.pipeline.PipelineControllerFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Operator that uses HTTP. The operator's parameters must be an {@link HttpParams}.
 *
 * @param <T> response type
 */
@Getter
public abstract class HttpOperation<T> extends OperationPartial {
    private static final Logger logger = LoggerFactory.getLogger(HttpOperation.class);

    /**
     * Response status.
     */
    public enum Status {
        SUCCESS, FAILURE, STILL_WAITING
    }

    /**
     * Configuration for this operation.
     */
    private final HttpConfig config;

    /**
     * Response class.
     */
    private final Class<T> responseClass;

    /**
     * {@code True} to use polling, {@code false} otherwise.
     */
    @Getter
    private boolean usePolling;

    /**
     * Number of polls issued so far, on the current operation attempt.
     */
    @Getter
    private int pollCount;


    /**
     * Constructs the object.
     *
     * @param params operation parameters
     * @param config configuration for this operation
     * @param clazz response class
     * @param propertyNames names of properties required by this operation
     */
    protected HttpOperation(ControlLoopOperationParams params, HttpConfig config, Class<T> clazz,
                    List<String> propertyNames) {
        super(params, config, propertyNames);
        this.config = config;
        this.responseClass = clazz;
    }

    /**
     * Indicates that polling should be used.
     */
    protected void setUsePolling() {
        if (!(config instanceof HttpPollingConfig)) {
            throw new IllegalStateException("cannot poll without polling parameters");
        }

        usePolling = true;
    }

    public HttpClient getClient() {
        return config.getClient();
    }

    /**
     * Gets the path to be used when performing the request; this is typically appended to
     * the base URL. This method simply invokes {@link #getPath()}.
     *
     * @return the path URI suffix
     */
    public String getPath() {
        return config.getPath();
    }

    public long getTimeoutMs() {
        return config.getTimeoutMs();
    }

    /**
     * If no timeout is specified, then it returns the operator's configured timeout.
     */
    @Override
    protected long getTimeoutMs(Integer timeoutSec) {
        return (timeoutSec == null || timeoutSec == 0 ? getTimeoutMs() : super.getTimeoutMs(timeoutSec));
    }

    /**
     * Makes the request headers. This simply returns an empty map.
     *
     * @return request headers, a non-null, modifiable map
     */
    protected Map<String, Object> makeHeaders() {
        return new HashMap<>();
    }

    /**
     * Makes the URL to which the HTTP request should be posted. This is primarily used
     * for logging purposes. This particular method returns the base URL appended with the
     * return value from {@link #getPath()}.
     *
     * @return the URL to which from which to get
     */
    public String getUrl() {
        return (getClient().getBaseUrl() + getPath());
    }

    /**
     * Resets the polling count
     *
     * <p/>
     * Note: This should be invoked at the start of each operation (i.e., in
     * {@link #startOperationAsync(int, OperationOutcome)}.
     */
    protected void resetPollCount() {
        pollCount = 0;
    }

    /**
     * Arranges to handle a response.
     *
     * @param outcome outcome to be populate
     * @param url URL to which to request was sent
     * @param requester function to initiate the request and invoke the given callback
     *        when it completes
     * @return a future for the response
     */
    protected CompletableFuture<OperationOutcome> handleResponse(OperationOutcome outcome, String url,
                    Function<InvocationCallback<Response>, Future<Response>> requester) {

        final PipelineControllerFuture<OperationOutcome> controller = new PipelineControllerFuture<>();
        final CompletableFuture<Response> future = new CompletableFuture<>();
        final var executor = params.getExecutor();

        // arrange for the callback to complete "future"
        InvocationCallback<Response> callback = new InvocationCallback<>() {
            @Override
            public void completed(Response response) {
                future.complete(response);
            }

            @Override
            public void failed(Throwable throwable) {
                logger.warn("{}.{}: response failure for {}", params.getActor(), params.getOperation(),
                                params.getRequestId());
                future.completeExceptionally(throwable);
            }
        };

        // start the request and arrange to cancel it if the controller is canceled
        controller.add(requester.apply(callback));

        // once "future" completes, process the response, and then complete the controller
        future.thenComposeAsync(response -> processResponse(outcome, url, response), executor)
                        .whenCompleteAsync(controller.delayedComplete(), executor);

        return controller;
    }

    /**
     * Processes a response. This method decodes the response, sets the outcome based on
     * the response, and then returns a completed future.
     *
     * @param outcome outcome to be populate
     * @param url URL to which to request was sent
     * @param rawResponse raw response to process
     * @return a future to cancel or await the outcome
     */
    protected CompletableFuture<OperationOutcome> processResponse(OperationOutcome outcome, String url,
                    Response rawResponse) {

        logger.info("{}.{}: response received for {}", params.getActor(), params.getOperation(), params.getRequestId());

        String strResponse = rawResponse.readEntity(String.class);

        logMessage(EventType.IN, CommInfrastructure.REST, url, strResponse);

        T response;
        if (responseClass == String.class) {
            response = responseClass.cast(strResponse);
        } else {
            try {
                response = getCoder().decode(strResponse, responseClass);
            } catch (CoderException e) {
                logger.warn("{}.{} cannot decode response for {}", params.getActor(), params.getOperation(),
                                params.getRequestId(), e);
                throw new IllegalArgumentException("cannot decode response");
            }
        }

        if (!isSuccess(rawResponse, response)) {
            logger.info("{}.{} request failed with http error code {} for {}", params.getActor(), params.getOperation(),
                            rawResponse.getStatus(), params.getRequestId());
            return CompletableFuture.completedFuture(
                    setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
        }

        logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(), params.getRequestId());
        setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response);
        return postProcessResponse(outcome, url, rawResponse, response);
    }

    /**
     * Sets an operation's outcome and default message based on the result.
     *
     * @param outcome operation to be updated
     * @param result result of the operation
     * @param rawResponse raw response
     * @param response decoded response
     * @return the updated operation
     */
    public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
                    T response) {

        outcome.setResponse(response);
        return setOutcome(outcome, result);
    }

    /**
     * Processes a successful response. This method simply returns the outcome wrapped in
     * a completed future.
     *
     * @param outcome outcome to be populate
     * @param url URL to which to request was sent
     * @param rawResponse raw response
     * @param response decoded response
     * @return a future to cancel or await the outcome
     */
    protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
                    Response rawResponse, T response) {

        if (!usePolling) {
            // doesn't use polling - just return the completed future
            return CompletableFuture.completedFuture(outcome);
        }

        HttpPollingConfig cfg = (HttpPollingConfig) config;

        switch (detmStatus(rawResponse, response)) {
            case SUCCESS -> {
                logger.info("{}.{} request succeeded for {}", params.getActor(), params.getOperation(),
                    params.getRequestId());
                return CompletableFuture
                    .completedFuture(setOutcome(outcome, OperationResult.SUCCESS, rawResponse, response));
            }
            case FAILURE -> {
                logger.info("{}.{} request failed for {}", params.getActor(), params.getOperation(),
                    params.getRequestId());
                return CompletableFuture
                    .completedFuture(setOutcome(outcome, OperationResult.FAILURE, rawResponse, response));
            }
            default -> logger.info("{}.{} request incomplete for {}", params.getActor(), params.getOperation(),
                params.getRequestId());
        }

        // still incomplete

        // see if the limit for the number of polls has been reached
        if (pollCount++ >= cfg.getMaxPolls()) {
            logger.warn("{}: exceeded 'poll' limit {} for {}", getFullName(), cfg.getMaxPolls(),
                            params.getRequestId());
            setOutcome(outcome, OperationResult.FAILURE_TIMEOUT);
            return CompletableFuture.completedFuture(outcome);
        }

        // sleep and then poll
        Function<Void, CompletableFuture<OperationOutcome>> doPoll = unused -> issuePoll(outcome);
        return sleep(getPollWaitMs(), TimeUnit.MILLISECONDS).thenComposeAsync(doPoll);
    }

    /**
     * Polls to see if the original request is complete. This method polls using an HTTP
     * "get" request whose URL is constructed by appending the extracted "poll ID" to the
     * poll path from the configuration data.
     *
     * @param outcome outcome to be populated with the response
     * @return a future that can be used to cancel the poll or await its response
     */
    protected CompletableFuture<OperationOutcome> issuePoll(OperationOutcome outcome) {
        String path = getPollingPath();
        String url = getClient().getBaseUrl() + path;

        logger.debug("{}: 'poll' count {} for {}", getFullName(), pollCount, params.getRequestId());

        logMessage(EventType.OUT, CommInfrastructure.REST, url, null);

        return handleResponse(outcome, url, callback -> getClient().get(callback, path, null));
    }

    /**
     * Determines the status of the response. This particular method simply throws an
     * exception.
     *
     * @param rawResponse raw response
     * @param response decoded response
     * @return the status of the response
     */
    protected Status detmStatus(Response rawResponse, T response) {
        throw new UnsupportedOperationException("cannot determine response status");
    }

    /**
     * Gets the URL to use when polling. Typically, this is some unique ID appended to the
     * polling path found within the configuration data. This particular method simply
     * returns the polling path from the configuration data.
     *
     * @return the URL to use when polling
     */
    protected String getPollingPath() {
        return ((HttpPollingConfig) config).getPollPath();
    }

    /**
     * Determines if the response indicates success. This method simply checks the HTTP
     * status code.
     *
     * @param rawResponse raw response
     * @param response decoded response
     * @return {@code true} if the response indicates success, {@code false} otherwise
     */
    protected boolean isSuccess(Response rawResponse, T response) {
        return (rawResponse.getStatus() == 200);
    }

    @Override
    public <Q> String logMessage(EventType direction, CommInfrastructure infra, String sink, Q request) {
        String json = super.logMessage(direction, infra, sink, request);
        NetLoggerUtil.log(direction, infra, sink, json);
        return json;
    }

    // these may be overridden by junit tests

    protected long getPollWaitMs() {
        HttpPollingConfig cfg = (HttpPollingConfig) config;

        return TimeUnit.MILLISECONDS.convert(cfg.getPollWaitSec(), TimeUnit.SECONDS);
    }
}