90be3dbd47f5e55ed5b38cf1aeff7bf26b197b24
[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 = SecurityConfiguration(partialConfig.security.bind().keys)
56
57         val collectorConfiguration = partialConfig.collector.bind()
58                 .let { createCollectorConfig(it).bind() }
59
60         HvVesConfiguration(
61                 serverConfiguration,
62                 cbsConfiguration,
63                 securityConfiguration,
64                 collectorConfiguration,
65                 logLevel
66         )
67     }.toEither { ValidationError("Some required configuration options are missing") }
68
69     private fun determineLogLevel(logLevel: Option<LogLevel>) =
70             logLevel.getOrElse {
71                 logger.warn {
72                     "Missing or invalid \"logLevel\" field. " +
73                             "Using default log level ($DEFAULT_LOG_LEVEL)"
74                 }
75                 DEFAULT_LOG_LEVEL
76             }
77
78     private fun createServerConfiguration(partial: PartialServerConfig) =
79             partial.mapBinding {
80                 ServerConfiguration(
81                         it.listenPort.bind(),
82                         Duration.ofSeconds(it.idleTimeoutSec.bind().toLong()),
83                         it.maxPayloadSizeBytes.bind()
84                 )
85             }
86
87     private fun createCbsConfiguration(partial: PartialCbsConfig) =
88             partial.mapBinding {
89                 CbsConfiguration(
90                         Duration.ofSeconds(it.firstRequestDelaySec.bind().toLong()),
91                         Duration.ofSeconds(it.requestIntervalSec.bind().toLong())
92                 )
93             }
94
95     private fun createCollectorConfig(partial: PartialCollectorConfig) =
96             partial.mapBinding {
97                 CollectorConfiguration(
98                         it.maxRequestSizeBytes.bind(),
99                         toKafkaServersString(it.kafkaServers.bind()),
100                         it.routing.bind(),
101                         it.dummyMode.bind()
102                 )
103             }
104
105     private fun toKafkaServersString(kafkaServers: List<InetSocketAddress>): String =
106             kafkaServers.joinToString(",") { "${it.hostName}:${it.port}" }
107
108     companion object {
109         val DEFAULT_LOG_LEVEL = LogLevel.INFO
110         private val logger = Logger(ConfigurationValidator::class)
111     }
112 }
113
114 data class ValidationError(val message: String, val cause: Option<Throwable> = None)