2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2019 NOKIA
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.dcae.collectors.veshv.config.impl
22 import org.onap.dcae.collectors.veshv.config.api.ConfigurationStateListener
23 import org.onap.dcae.collectors.veshv.utils.logging.Logger
24 import org.onap.dcae.collectors.veshv.utils.logging.MappedDiagnosticContext
25 import org.onap.dcae.collectors.veshv.utils.logging.onErrorLog
26 import org.onap.dcae.collectors.veshv.utils.rx.delayElements
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests
29 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest
30 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext
31 import reactor.core.publisher.Mono
32 import reactor.retry.Retry
33 import java.time.Duration
34 import java.util.concurrent.atomic.AtomicReference
37 internal class CbsClientAdapter(private val cbsClientMono: Mono<CbsClient>,
38 private val configurationStateListener: ConfigurationStateListener,
39 private val firstRequestDelay: Duration,
40 private val retrySpec: Retry<Any>) {
42 private val requestInterval = AtomicReference<Duration>(Duration.ZERO)
44 fun configurationUpdates(mdc: MappedDiagnosticContext) = cbsClientMono
47 "CBS client successfully created, first request will be sent in ${firstRequestDelay.seconds} s"
50 .onErrorLog(logger, mdc) { "Failed to retrieve CBS client" }
51 .retryWhen(retry(mdc))
52 .delayElement(firstRequestDelay)
53 .flatMapMany(::toPeriodicalConfigurations)
54 .distinctUntilChanged()
56 fun updateCbsInterval(intervalUpdate: Duration, mdc: MappedDiagnosticContext) {
57 requestInterval.set(intervalUpdate)
58 logger.debug(mdc) { "CBS request interval changed to: ${intervalUpdate.seconds} s" }
61 private fun toPeriodicalConfigurations(cbsClient: CbsClient) =
62 Mono.just(configurationRequest())
64 .map(CbsRequest::withNewInvocationId)
65 .flatMap(cbsClient::get)
66 .transform(delayElements(requestInterval::get))
68 private fun configurationRequest() = CbsRequests.getConfiguration(RequestDiagnosticContext.create())
70 private fun retry(mdc: MappedDiagnosticContext) = retrySpec.doOnRetry {
71 logger.withWarn(mdc) {
72 log("Exception from HV-VES cbs client, retrying subscription", it.exception())
74 configurationStateListener.retrying()
78 private val logger = Logger(CbsClientAdapter::class)