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
 
  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 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
 
  39  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
 
  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()
 
  50     private val retry = retrySpec.doOnRetry {
 
  51         logger.withWarn(mdc) {
 
  52             log("Exception from configuration provider client, retrying subscription", it.exception())
 
  54         configurationStateListener.retrying()
 
  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" }
 
  65     private fun parseConfiguration(json: JsonObject) =
 
  68                     .apply { streamPublishers = extractStreamDefinitions(json).toOption() }
 
  70     private fun extractStreamDefinitions(configuration: JsonObject): List<KafkaSink> =
 
  71             DataStreams.namedSinks(configuration)
 
  72                     .filter(streamOfType(KAFKA))
 
  73                     .map(streamParser::unsafeParse)
 
  78         private val logger = Logger(CbsConfigurationProvider::class)