Upgrade hv-ves, reactor, protobuf and sdk versions
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-configuration / src / main / kotlin / org / onap / dcae / collectors / veshv / config / impl / CbsConfigurationProvider.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-2020 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 arrow.core.toOption
23 import com.google.gson.JsonObject
24 import org.onap.dcae.collectors.veshv.config.api.ConfigurationStateListener
25 import org.onap.dcae.collectors.veshv.utils.logging.Logger
26 import org.onap.dcae.collectors.veshv.utils.logging.MappedDiagnosticContext
27 import org.onap.dcae.collectors.veshv.utils.logging.onErrorLog
28 import org.onap.dcae.collectors.veshv.utils.reader
29 import org.onap.dcaegen2.services.sdk.model.streams.StreamType.KAFKA
30 import org.onap.dcaegen2.services.sdk.model.streams.dmaap.KafkaSink
31 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.DataStreams
32 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParser
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamFromGsonParsers
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.streams.StreamPredicates.streamOfType
35 import reactor.core.publisher.Flux
36 import reactor.retry.Retry
37 import reactor.util.retry.Retry.withThrowable
38
39 /**
40  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
41  * @since May 2018
42  */
43 internal class CbsConfigurationProvider(private val cbsClientAdapter: CbsClientAdapter,
44                                         private val configParser: JsonConfigurationParser,
45                                         private val configurationStateListener: ConfigurationStateListener,
46                                         private val mdc: MappedDiagnosticContext,
47                                         retrySpec: Retry<Any>,
48                                         private val streamParser: StreamFromGsonParser<KafkaSink> =
49                                                 StreamFromGsonParsers.kafkaSinkParser()
50 ) {
51     private val retry = retrySpec.doOnRetry {
52         logger.withWarn(mdc) {
53             log("Exception from configuration provider client, retrying subscription", it.exception())
54         }
55         configurationStateListener.retrying()
56     }
57
58     operator fun invoke(): Flux<PartialConfiguration> =
59             cbsClientAdapter.configurationUpdates()
60                     .doOnNext { logger.info(mdc) { "Received new configuration:\n$it" } }
61                     .map(::parseConfiguration)
62                     .doOnNext { logger.info(mdc) { "Successfully parsed configuration json to:\n$it" } }
63                     .onErrorLog(logger, mdc) { "Error while creating configuration" }
64                     .retryWhen(withThrowable(retry))
65
66     private fun parseConfiguration(json: JsonObject) =
67             configParser
68                     .parse(json.reader())
69                     .apply { streamPublishers = extractStreamDefinitions(json).toOption() }
70
71     private fun extractStreamDefinitions(configuration: JsonObject): List<KafkaSink> =
72             DataStreams.namedSinks(configuration)
73                     .filter(streamOfType(KAFKA))
74                     .map(streamParser::unsafeParse)
75                     .asIterable()
76                     .toList()
77
78     companion object {
79         private val logger = Logger(CbsConfigurationProvider::class)
80     }
81 }