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.Some
26 import arrow.core.getOrElse
27 import org.onap.dcae.collectors.veshv.config.api.model.CbsConfiguration
28 import org.onap.dcae.collectors.veshv.config.api.model.CollectorConfiguration
29 import org.onap.dcae.collectors.veshv.config.api.model.HvVesConfiguration
30 import org.onap.dcae.collectors.veshv.config.api.model.ServerConfiguration
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.logging.LogLevel
35 import org.onap.dcae.collectors.veshv.utils.logging.Logger
36 import java.net.InetSocketAddress
37 import java.time.Duration
40 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
43 internal class ConfigurationValidator {
45 fun validate(partialConfig: PartialConfiguration)
46 : Either<ValidationError, HvVesConfiguration> = binding {
47 val logLevel = determineLogLevel(partialConfig.logLevel)
49 val serverConfiguration = partialConfig.server.bind()
50 .let { createServerConfiguration(it).bind() }
52 val cbsConfiguration = partialConfig.cbs.bind()
53 .let { createCbsConfiguration(it).bind() }
55 val securityConfiguration = partialConfig.security.bind()
56 .let { createSecurityConfiguration(it) }
58 val collectorConfiguration = partialConfig.collector.bind()
59 .let { createCollectorConfig(it).bind() }
64 securityConfiguration,
65 collectorConfiguration,
68 }.toEither { ValidationError("Some required configuration options are missing") }
70 private fun determineLogLevel(logLevel: Option<LogLevel>) =
73 "Missing or invalid \"logLevel\" field. " +
74 "Using default log level ($DEFAULT_LOG_LEVEL)"
79 private fun createServerConfiguration(partial: PartialServerConfig) =
83 Duration.ofSeconds(it.idleTimeoutSec.bind().toLong()),
84 it.maxPayloadSizeBytes.bind()
88 private fun createCbsConfiguration(partial: PartialCbsConfig) =
91 Duration.ofSeconds(it.firstRequestDelaySec.bind().toLong()),
92 Duration.ofSeconds(it.requestIntervalSec.bind().toLong())
96 private fun createSecurityConfiguration(partial: PartialSecurityConfig) =
99 { SecurityConfiguration(None) },
100 { SecurityConfiguration(Some(it)) }
103 private fun createCollectorConfig(partial: PartialCollectorConfig) =
105 CollectorConfiguration(
106 it.maxRequestSizeBytes.bind(),
107 toKafkaServersString(it.kafkaServers.bind()),
113 private fun toKafkaServersString(kafkaServers: List<InetSocketAddress>): String =
114 kafkaServers.joinToString(",") { "${it.hostName}:${it.port}" }
117 val DEFAULT_LOG_LEVEL = LogLevel.INFO
118 private val logger = Logger(ConfigurationValidator::class)
122 data class ValidationError(val message: String, val cause: Option<Throwable> = None)