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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api;
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;
34 * <p>Main Config Binding Service client interface.</p>
36 * <p>User should use this interface to subscribe to events published when CBS client fetches configuration.</p>
40 public interface CbsClient {
44 * Get current application configuration.
47 * Returns a {@link Mono} that publishes new configuration after CBS client retrieves one.
49 * @return reactive stream of configuration
50 * @param diagnosticContext diagnostic context as defined in Logging Guideline
53 @NotNull Mono<JsonObject> get(RequestDiagnosticContext diagnosticContext);
57 * Poll for configuration.
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.
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
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()))
76 * Poll for configuration updates.
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.
83 * For more tailored change detection approach you can:
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.
90 * Use {@link #get(RequestDiagnosticContext, Duration, Duration)} with
91 * {@link Flux#distinctUntilChanged(Function, BiPredicate)} if you want to specify custom comparison logic.
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
100 default Flux<JsonObject> updates(RequestDiagnosticContext diagnosticContext, Duration initialDelay, Duration period) {
101 return get(diagnosticContext, initialDelay, period).distinctUntilChanged();