Use CBS by means of SDK in place of Consul
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-core / src / main / kotlin / org / onap / dcae / collectors / veshv / impl / adapters / ConfigurationProviderImpl.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-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.impl.adapters
21
22 import com.google.gson.JsonObject
23 import org.onap.dcae.collectors.veshv.boundary.ConfigurationProvider
24 import org.onap.dcae.collectors.veshv.healthcheck.api.HealthDescription
25 import org.onap.dcae.collectors.veshv.healthcheck.api.HealthState
26 import org.onap.dcae.collectors.veshv.model.CollectorConfiguration
27 import org.onap.dcae.collectors.veshv.model.ConfigurationProviderParams
28 import org.onap.dcae.collectors.veshv.model.ServiceContext
29 import org.onap.dcae.collectors.veshv.model.routing
30 import org.onap.dcae.collectors.veshv.utils.logging.Logger
31 import org.onap.dcae.collectors.veshv.utils.logging.onErrorLog
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient
33 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext
34 import reactor.core.publisher.Flux
35 import reactor.core.publisher.Mono
36 import reactor.retry.Jitter
37 import reactor.retry.Retry
38 import java.time.Duration
39
40
41 /**
42  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
43  * @since May 2018
44  */
45 internal class ConfigurationProviderImpl(private val cbsClientMono: Mono<CbsClient>,
46                                          private val firstRequestDelay: Duration,
47                                          private val requestInterval: Duration,
48                                          private val healthState: HealthState,
49                                          retrySpec: Retry<Any>
50
51 ) : ConfigurationProvider {
52     constructor(cbsClientMono: Mono<CbsClient>, params: ConfigurationProviderParams) : this(
53             cbsClientMono,
54             params.firstRequestDelay,
55             params.requestInterval,
56             HealthState.INSTANCE,
57             Retry.any<Any>()
58                     .retryMax(MAX_RETRIES)
59                     .fixedBackoff(params.requestInterval)
60                     .jitter(Jitter.random())
61     )
62
63     private val retry = retrySpec.doOnRetry {
64         logger.withWarn(ServiceContext::mdc) {
65             log("Exception from configuration provider client, retrying subscription", it.exception())
66         }
67         healthState.changeState(HealthDescription.RETRYING_FOR_DYNAMIC_CONFIGURATION)
68     }
69
70     override fun invoke(): Flux<CollectorConfiguration> =
71             cbsClientMono
72                     .doOnNext { logger.info(ServiceContext::mdc) { "CBS client successfully created" } }
73                     .onErrorLog(logger, ServiceContext::mdc) { "Failed to retrieve CBS client" }
74                     .retryWhen(retry)
75                     .doFinally { logger.trace(ServiceContext::mdc) { "CBS client subscription finished" } }
76                     .flatMapMany(::handleUpdates)
77
78     private fun handleUpdates(cbsClient: CbsClient): Flux<CollectorConfiguration> = cbsClient
79             .updates(RequestDiagnosticContext.create(),
80                     firstRequestDelay,
81                     requestInterval)
82             .doOnNext { logger.info(ServiceContext::mdc) { "Received new configuration:\n$it" } }
83             .map(::createCollectorConfiguration)
84             .onErrorLog(logger, ServiceContext::mdc) { "Error while creating configuration" }
85             .retryWhen(retry)
86
87
88     private fun createCollectorConfiguration(configuration: JsonObject): CollectorConfiguration =
89             try {
90                 val routingArray = configuration.getAsJsonArray(ROUTING_CONFIGURATION_KEY)
91                 CollectorConfiguration(
92                         routing {
93                             for (route in routingArray) {
94                                 val routeObj = route.asJsonObject
95                                 defineRoute {
96                                     fromDomain(routeObj.getPrimitiveAsString(DOMAIN_CONFIGURATION_KEY))
97                                     toTopic(routeObj.getPrimitiveAsString(TOPIC_CONFIGURATION_KEY))
98                                     withFixedPartitioning()
99                                 }
100                             }
101                         }.build()
102                 )
103             } catch (e: NullPointerException) {
104                 throw ParsingException("Failed to parse configuration", e)
105             }
106
107     private fun JsonObject.getPrimitiveAsString(memberName: String) = getAsJsonPrimitive(memberName).asString
108
109
110     companion object {
111         private const val ROUTING_CONFIGURATION_KEY = "collector.routing"
112         private const val DOMAIN_CONFIGURATION_KEY = "fromDomain"
113         private const val TOPIC_CONFIGURATION_KEY = "toTopic"
114
115         private const val MAX_RETRIES = 5L
116         private val logger = Logger(ConfigurationProviderImpl::class)
117     }
118 }