aboutsummaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt
blob: 933815722908ca71b2a7b47b6f5e4005065952d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * ============LICENSE_START=======================================================
 * dcaegen2-collectors-veshv
 * ================================================================================
 * Copyright (C) 2019 NOKIA
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dcae.collectors.veshv.config.api

import arrow.core.getOrElse
import org.onap.dcae.collectors.veshv.config.api.model.HvVesConfiguration
import org.onap.dcae.collectors.veshv.config.api.model.MissingArgumentException
import org.onap.dcae.collectors.veshv.config.api.model.ValidationException
import org.onap.dcae.collectors.veshv.config.impl.CbsConfigurationProvider
import org.onap.dcae.collectors.veshv.config.impl.ConfigurationMerger
import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator
import org.onap.dcae.collectors.veshv.config.impl.HvVesCommandLineParser
import org.onap.dcae.collectors.veshv.config.impl.JsonConfigurationParser
import org.onap.dcae.collectors.veshv.config.impl.PartialConfiguration
import org.onap.dcae.collectors.veshv.utils.arrow.throwOnLeft
import org.onap.dcae.collectors.veshv.utils.logging.Logger
import org.onap.dcae.collectors.veshv.utils.logging.MappedDiagnosticContext
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono

class ConfigurationModule {

    private val cmd = HvVesCommandLineParser()
    private val configParser = JsonConfigurationParser()
    private val configValidator = ConfigurationValidator()
    private val merger = ConfigurationMerger()

    fun healthCheckPort(args: Array<String>): Int = cmd.getHealthcheckPort(args)

    fun hvVesConfigurationUpdates(args: Array<String>,
                                  configStateListener: ConfigurationStateListener,
                                  mdc: MappedDiagnosticContext): Flux<HvVesConfiguration> =
            Mono.just(cmd.getConfigurationFile(args))
                    .throwOnLeft(::MissingArgumentException)
                    .doOnNext { logger.info { "Using base configuration file: ${it.absolutePath}" } }
                    .map { it.reader().use(configParser::parse) }
                    .doOnNext { logger.info { "Successfully parsed json file to configuration: $it" } }
                    .cache()
                    .flatMapMany { basePartialConfig ->
                        cbsConfigurationProvider(basePartialConfig, configStateListener, mdc)
                                .invoke()
                                .map { merger.merge(basePartialConfig, it) }
                                .map(configValidator::validate)
                                .throwOnLeft()
                    }

    private fun cbsConfigurationProvider(basePartialConfig: PartialConfiguration,
                                         configStateListener: ConfigurationStateListener,
                                         mdc: MappedDiagnosticContext): CbsConfigurationProvider =
            CbsConfigurationProvider(
                    CbsClientFactory.createCbsClient(EnvProperties.fromEnvironment()),
                    cbsConfigurationFrom(basePartialConfig),
                    configParser,
                    configStateListener,
                    mdc)

    private fun cbsConfigurationFrom(basePartialConfig: PartialConfiguration) = configValidator
            .validatedCbsConfiguration(basePartialConfig)
            .getOrElse { throw ValidationException("Invalid CBS section defined in configuration file") }

    companion object {
        private val logger = Logger(ConfigurationModule::class)
    }
}