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.impl
22 import arrow.core.Either
23 import arrow.core.None
24 import arrow.core.Option
25 import arrow.core.getOrElse
26 import org.onap.dcae.collectors.veshv.config.api.model.CbsConfiguration
27 import org.onap.dcae.collectors.veshv.config.api.model.CollectorConfiguration
28 import org.onap.dcae.collectors.veshv.config.api.model.HvVesConfiguration
29 import org.onap.dcae.collectors.veshv.config.api.model.ServerConfiguration
30 import org.onap.dcae.collectors.veshv.config.api.model.ValidationException
31 import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityConfiguration
32 import org.onap.dcae.collectors.veshv.utils.arrow.OptionUtils.binding
33 import org.onap.dcae.collectors.veshv.utils.arrow.mapBinding
34 import org.onap.dcae.collectors.veshv.utils.arrow.doOnEmpty
35 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
36 import org.onap.dcae.collectors.veshv.utils.logging.Logger
39 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
42 internal class ConfigurationValidator {
44 fun validate(partialConfig: PartialConfiguration) =
45 logger.info { "About to validate configuration: $partialConfig" }.let {
47 val logLevel = determineLogLevel(partialConfig.logLevel)
49 val serverConfiguration = validatedServerConfiguration(partialConfig)
50 .doOnEmpty { logger.debug { "Cannot bind server configuration" } }
53 val cbsConfiguration = validatedCbsConfiguration(partialConfig)
54 .doOnEmpty { logger.debug { "Cannot bind cbs configuration" } }
57 val securityConfiguration = SecurityConfiguration(partialConfig.security.bind().keys)
59 val collectorConfiguration = validatedCollectorConfig(partialConfig)
60 .doOnEmpty { logger.debug { "Cannot bind collector configuration" } }
66 securityConfiguration,
67 collectorConfiguration,
70 }.toEither { ValidationException("Some required configuration options are missing") }
74 private fun determineLogLevel(logLevel: Option<LogLevel>) =
77 "Missing or invalid \"logLevel\" field. " +
78 "Using default log level ($DEFAULT_LOG_LEVEL)"
83 private fun validatedServerConfiguration(partial: PartialConfiguration) =
85 partial.server.bind().let {
88 it.idleTimeoutSec.bind(),
89 it.maxPayloadSizeBytes.bind()
94 fun validatedCbsConfiguration(partial: PartialConfiguration) =
98 it.firstRequestDelaySec.bind(),
99 it.requestIntervalSec.bind()
104 private fun validatedCollectorConfig(partial: PartialConfiguration) =
106 partial.collector.bind().let {
107 CollectorConfiguration(
114 val DEFAULT_LOG_LEVEL = LogLevel.INFO
115 private val logger = Logger(ConfigurationValidator::class)