d31f6585877da32093a94b74c6a7465806449e95
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
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.dcae.collectors.veshv.config.impl
21
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
35
36
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>) {
41
42     private val requestInterval = AtomicReference<Duration>(Duration.ZERO)
43
44     fun configurationUpdates(mdc: MappedDiagnosticContext) = cbsClientMono
45             .doOnNext {
46                 logger.info(mdc) {
47                     "CBS client successfully created, first request will be sent in ${firstRequestDelay.seconds} s"
48                 }
49             }
50             .onErrorLog(logger, mdc) { "Failed to retrieve CBS client" }
51             .retryWhen(retry(mdc))
52             .delayElement(firstRequestDelay)
53             .flatMapMany(::toPeriodicalConfigurations)
54             .distinctUntilChanged()
55
56     fun updateCbsInterval(intervalUpdate: Duration, mdc: MappedDiagnosticContext) {
57         requestInterval.set(intervalUpdate)
58         logger.debug(mdc) { "CBS request interval changed to: ${intervalUpdate.seconds} s" }
59     }
60
61     private fun toPeriodicalConfigurations(cbsClient: CbsClient) =
62             Mono.just(configurationRequest())
63                     .repeat()
64                     .map(CbsRequest::withNewInvocationId)
65                     .flatMap(cbsClient::get)
66                     .transform(delayElements(requestInterval::get))
67
68     private fun configurationRequest() = CbsRequests.getConfiguration(RequestDiagnosticContext.create())
69
70     private fun retry(mdc: MappedDiagnosticContext) = retrySpec.doOnRetry {
71         logger.withWarn(mdc) {
72             log("Exception from HV-VES cbs client, retrying subscription", it.exception())
73         }
74         configurationStateListener.retrying()
75     }
76
77     companion object {
78         private val logger = Logger(CbsClientAdapter::class)
79     }
80
81 }