aboutsummaryrefslogtreecommitdiffstats
path: root/rest-services/cbs-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/cbs/client/api/CbsClient.java
blob: 3ee12eedac63af5ac19ab88f7435bcf60a15ff6c (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
/*
 * ============LICENSE_START=======================================================
 * DCAEGEN2-SERVICES-SDK
 * ================================================================================
 * Copyright (C) 2019 Nokia. All rights reserved.
 * ================================================================================
 * 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.dcaegen2.services.sdk.rest.services.cbs.client.api;

import com.google.gson.JsonObject;
import java.time.Duration;
import java.util.UUID;
import java.util.function.BiPredicate;
import java.util.function.Function;
import org.jetbrains.annotations.NotNull;
import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext;
import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * <p>Main Config Binding Service client interface.</p>
 *
 * <p>User should use this interface to subscribe to events published when CBS client fetches configuration.</p>
 *
 * @since 1.1.2
 */
public interface CbsClient {

    /**
     * <p>
     * Get current application configuration.
     *
     * <p>
     * Returns a {@link Mono} that publishes new configuration after CBS client retrieves one.
     *
     * @return reactive stream of configuration
     * @param diagnosticContext diagnostic context as defined in Logging Guideline
     * @since 1.1.2
     */
    @NotNull Mono<JsonObject> get(RequestDiagnosticContext diagnosticContext);

    /**
     * <p>
     * Poll for configuration.
     *
     * <p>
     * Will call {@link #get(RequestDiagnosticContext)} after {@code initialDelay} every {@code period}. Resulting entries may or may not be
     * changed, ie. items in the stream might be the same until change is made in CBS.
     *
     * @param diagnosticContext diagnostic context as defined in Logging Guideline
     * @param initialDelay delay after first request attempt
     * @param period frequency of update checks
     * @return stream of configuration states
     */
    default Flux<JsonObject> get(RequestDiagnosticContext diagnosticContext, Duration initialDelay, Duration period) {
        return Flux.interval(initialDelay, period)
                .map(i -> ImmutableRequestDiagnosticContext.copyOf(diagnosticContext).withInvocationId(UUID.randomUUID()))
                .flatMap(this::get);
    }

    /**
     * <p>
     * Poll for configuration updates.
     *
     * <p>
     * Will call {@link #get(RequestDiagnosticContext)} after {@code initialDelay} every {@code period}. Will emit an item
     * only when an update was detected, ie. when new item is different then last emitted item.
     *
     * <p>
     * For more tailored change detection approach you can:
     * <ul>
     *     <li>
     *         Use {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.listener.ListenableCbsConfig}
     *         (<b>experimental API</b>) if you want to react differently to changes in subsets of the configuration.
     *     </li>
     *     <li>
     *         Use {@link #get(RequestDiagnosticContext, Duration, Duration)} with
     *         {@link Flux#distinctUntilChanged(Function, BiPredicate)} if you want to specify custom comparison logic.
     *     </li>
     * </ul>
     *
     * @param diagnosticContext diagnostic context as defined in Logging Guideline
     * @param initialDelay delay after first request attempt
     * @param period frequency of update checks
     * @return stream of configuration updates
     */
    default Flux<JsonObject> updates(RequestDiagnosticContext diagnosticContext, Duration initialDelay, Duration period) {
        return get(diagnosticContext, initialDelay, period).distinctUntilChanged();
    }
}