6c74c33fca0c0fcf1b3107414c9ed0d1c0dc5ea6
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.config.impl
21
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
38
39 /**
40  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
41  * @since March 2019
42  */
43 internal class ConfigurationValidator {
44
45     fun validate(partialConfig: PartialConfiguration)
46             : Either<ValidationError, HvVesConfiguration> = binding {
47         val logLevel = determineLogLevel(partialConfig.logLevel)
48
49         val serverConfiguration = partialConfig.server.bind()
50                 .let { createServerConfiguration(it).bind() }
51
52         val cbsConfiguration = partialConfig.cbs.bind()
53                 .let { createCbsConfiguration(it).bind() }
54
55         val securityConfiguration = partialConfig.security.bind()
56                 .let { createSecurityConfiguration(it).bind() }
57
58         val collectorConfiguration = partialConfig.collector.bind()
59                 .let { createCollectorConfig(it).bind() }
60
61         HvVesConfiguration(
62                 serverConfiguration,
63                 cbsConfiguration,
64                 securityConfiguration,
65                 collectorConfiguration,
66                 logLevel
67         )
68     }.toEither { ValidationError("Some required configuration options are missing") }
69
70     private fun determineLogLevel(logLevel: Option<LogLevel>) =
71             logLevel.getOrElse {
72                 logger.warn {
73                     "Missing or invalid \"logLevel\" field. " +
74                             "Using default log level ($DEFAULT_LOG_LEVEL)"
75                 }
76                 DEFAULT_LOG_LEVEL
77             }
78
79     private fun createServerConfiguration(partial: PartialServerConfig) =
80             partial.mapBinding {
81                 ServerConfiguration(
82                         it.listenPort.bind(),
83                         Duration.ofSeconds(it.idleTimeoutSec.bind().toLong()),
84                         it.maxPayloadSizeBytes.bind()
85                 )
86             }
87
88     private fun createCbsConfiguration(partial: PartialCbsConfig) =
89             partial.mapBinding {
90                 CbsConfiguration(
91                         Duration.ofSeconds(it.firstRequestDelaySec.bind().toLong()),
92                         Duration.ofSeconds(it.requestIntervalSec.bind().toLong())
93                 )
94             }
95
96     private fun createSecurityConfiguration(partial: PartialSecurityConfig) =
97             partial.keys.map { SecurityConfiguration(Some(it)) }
98
99     private fun createCollectorConfig(partial: PartialCollectorConfig) =
100             partial.mapBinding {
101                 CollectorConfiguration(
102                         it.maxRequestSizeBytes.bind(),
103                         toKafkaServersString(it.kafkaServers.bind()),
104                         it.routing.bind(),
105                         it.dummyMode.bind()
106                 )
107             }
108
109     private fun toKafkaServersString(kafkaServers: List<InetSocketAddress>): String =
110             kafkaServers.joinToString(",") { "${it.hostName}:${it.port}" }
111
112     companion object {
113         val DEFAULT_LOG_LEVEL = LogLevel.INFO
114         private val logger = Logger(ConfigurationValidator::class)
115     }
116 }
117
118 data class ValidationError(val message: String, val cause: Option<Throwable> = None)