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
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.api
22 import arrow.core.getOrElse
23 import org.onap.dcae.collectors.veshv.config.api.model.HvVesConfiguration
24 import org.onap.dcae.collectors.veshv.config.api.model.MissingArgumentException
25 import org.onap.dcae.collectors.veshv.config.api.model.ValidationException
26 import org.onap.dcae.collectors.veshv.config.impl.CbsConfigurationProvider
27 import org.onap.dcae.collectors.veshv.config.impl.ConfigurationMerger
28 import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator
29 import org.onap.dcae.collectors.veshv.config.impl.HvVesCommandLineParser
30 import org.onap.dcae.collectors.veshv.config.impl.JsonConfigurationParser
31 import org.onap.dcae.collectors.veshv.config.impl.PartialConfiguration
32 import org.onap.dcae.collectors.veshv.utils.arrow.throwOnLeft
33 import org.onap.dcae.collectors.veshv.utils.logging.Logger
34 import org.onap.dcae.collectors.veshv.utils.logging.MappedDiagnosticContext
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties
37 import reactor.core.publisher.Flux
38 import reactor.core.publisher.Mono
40 class ConfigurationModule {
42 private val cmd = HvVesCommandLineParser()
43 private val configParser = JsonConfigurationParser()
44 private val configValidator = ConfigurationValidator()
45 private val merger = ConfigurationMerger()
47 fun healthCheckPort(args: Array<String>): Int = cmd.getHealthcheckPort(args)
49 fun hvVesConfigurationUpdates(args: Array<String>,
50 configStateListener: ConfigurationStateListener,
51 mdc: MappedDiagnosticContext): Flux<HvVesConfiguration> =
52 Mono.just(cmd.getConfigurationFile(args))
53 .throwOnLeft(::MissingArgumentException)
54 .doOnNext { logger.info { "Using base configuration file: ${it.absolutePath}" } }
55 .map { it.reader().use(configParser::parse) }
56 .doOnNext { logger.info { "Successfully parsed json file to configuration: $it" } }
58 .flatMapMany { basePartialConfig ->
59 cbsConfigurationProvider(basePartialConfig, configStateListener, mdc)
61 .map { merger.merge(basePartialConfig, it) }
62 .map(configValidator::validate)
66 private fun cbsConfigurationProvider(basePartialConfig: PartialConfiguration,
67 configStateListener: ConfigurationStateListener,
68 mdc: MappedDiagnosticContext): CbsConfigurationProvider =
69 CbsConfigurationProvider(
70 CbsClientFactory.createCbsClient(EnvProperties.fromEnvironment()),
71 cbsConfigurationFrom(basePartialConfig),
76 private fun cbsConfigurationFrom(basePartialConfig: PartialConfiguration) = configValidator
77 .validatedCbsConfiguration(basePartialConfig)
78 .getOrElse { throw ValidationException("Invalid CBS section defined in configuration file") }
81 private val logger = Logger(ConfigurationModule::class)