3ee12eedac63af5ac19ab88f7435bcf60a15ff6c
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. 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 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api;
21
22 import com.google.gson.JsonObject;
23 import java.time.Duration;
24 import java.util.UUID;
25 import java.util.function.BiPredicate;
26 import java.util.function.Function;
27 import org.jetbrains.annotations.NotNull;
28 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.ImmutableRequestDiagnosticContext;
29 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
30 import reactor.core.publisher.Flux;
31 import reactor.core.publisher.Mono;
32
33 /**
34  * <p>Main Config Binding Service client interface.</p>
35  *
36  * <p>User should use this interface to subscribe to events published when CBS client fetches configuration.</p>
37  *
38  * @since 1.1.2
39  */
40 public interface CbsClient {
41
42     /**
43      * <p>
44      * Get current application configuration.
45      *
46      * <p>
47      * Returns a {@link Mono} that publishes new configuration after CBS client retrieves one.
48      *
49      * @return reactive stream of configuration
50      * @param diagnosticContext diagnostic context as defined in Logging Guideline
51      * @since 1.1.2
52      */
53     @NotNull Mono<JsonObject> get(RequestDiagnosticContext diagnosticContext);
54
55     /**
56      * <p>
57      * Poll for configuration.
58      *
59      * <p>
60      * Will call {@link #get(RequestDiagnosticContext)} after {@code initialDelay} every {@code period}. Resulting entries may or may not be
61      * changed, ie. items in the stream might be the same until change is made in CBS.
62      *
63      * @param diagnosticContext diagnostic context as defined in Logging Guideline
64      * @param initialDelay delay after first request attempt
65      * @param period frequency of update checks
66      * @return stream of configuration states
67      */
68     default Flux<JsonObject> get(RequestDiagnosticContext diagnosticContext, Duration initialDelay, Duration period) {
69         return Flux.interval(initialDelay, period)
70                 .map(i -> ImmutableRequestDiagnosticContext.copyOf(diagnosticContext).withInvocationId(UUID.randomUUID()))
71                 .flatMap(this::get);
72     }
73
74     /**
75      * <p>
76      * Poll for configuration updates.
77      *
78      * <p>
79      * Will call {@link #get(RequestDiagnosticContext)} after {@code initialDelay} every {@code period}. Will emit an item
80      * only when an update was detected, ie. when new item is different then last emitted item.
81      *
82      * <p>
83      * For more tailored change detection approach you can:
84      * <ul>
85      *     <li>
86      *         Use {@link org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.listener.ListenableCbsConfig}
87      *         (<b>experimental API</b>) if you want to react differently to changes in subsets of the configuration.
88      *     </li>
89      *     <li>
90      *         Use {@link #get(RequestDiagnosticContext, Duration, Duration)} with
91      *         {@link Flux#distinctUntilChanged(Function, BiPredicate)} if you want to specify custom comparison logic.
92      *     </li>
93      * </ul>
94      *
95      * @param diagnosticContext diagnostic context as defined in Logging Guideline
96      * @param initialDelay delay after first request attempt
97      * @param period frequency of update checks
98      * @return stream of configuration updates
99      */
100     default Flux<JsonObject> updates(RequestDiagnosticContext diagnosticContext, Duration initialDelay, Duration period) {
101         return get(diagnosticContext, initialDelay, period).distinctUntilChanged();
102     }
103 }