6f16b3d1b4f0475eb9f4c959e2f7a3ffee6fe550
[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-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 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
38 /**
39  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
40  * @since May 2018
41  */
42 internal class CbsConfigurationProvider(private val cbsClientAdapter: CbsClientAdapter,
43                                         private val configParser: JsonConfigurationParser,
44                                         private val configurationStateListener: ConfigurationStateListener,
45                                         private val mdc: MappedDiagnosticContext,
46                                         retrySpec: Retry<Any>,
47                                         private val streamParser: StreamFromGsonParser<KafkaSink> =
48                                                 StreamFromGsonParsers.kafkaSinkParser()
49 ) {
50     private val retry = retrySpec.doOnRetry {
51         logger.withWarn(mdc) {
52             log("Exception from configuration provider client, retrying subscription", it.exception())
53         }
54         configurationStateListener.retrying()
55     }
56
57     operator fun invoke(): Flux<PartialConfiguration> =
58             cbsClientAdapter.configurationUpdates()
59                     .doOnNext { logger.info(mdc) { "Received new configuration:\n$it" } }
60                     .map(::parseConfiguration)
61                     .doOnNext { logger.info(mdc) { "Successfully parsed configuration json to:\n$it" } }
62                     .onErrorLog(logger, mdc) { "Error while creating configuration" }
63                     .retryWhen(retry)
64
65     private fun parseConfiguration(json: JsonObject) =
66             configParser
67                     .parse(json.reader())
68                     .apply { streamPublishers = extractStreamDefinitions(json).toOption() }
69
70     private fun extractStreamDefinitions(configuration: JsonObject): List<KafkaSink> =
71             DataStreams.namedSinks(configuration)
72                     .filter(streamOfType(KAFKA))
73                     .map(streamParser::unsafeParse)
74                     .asIterable()
75                     .toList()
76
77     companion object {
78         private val logger = Logger(CbsConfigurationProvider::class)
79     }
80 }