04bba7e20ec3f8c60076330518412c8e3546b7ea
[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.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.ssl.boundary.SecurityConfiguration
31 import org.onap.dcae.collectors.veshv.utils.arrow.OptionUtils.binding
32 import org.onap.dcae.collectors.veshv.utils.arrow.mapBinding
33 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
34 import org.onap.dcae.collectors.veshv.utils.logging.Logger
35 import java.net.InetSocketAddress
36 import java.time.Duration
37
38 /**
39  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
40  * @since March 2019
41  */
42 internal class ConfigurationValidator {
43
44     fun validate(partialConfig: PartialConfiguration)
45             : Either<ValidationError, HvVesConfiguration> = binding {
46         val logLevel = determineLogLevel(partialConfig.logLevel)
47
48         val serverConfiguration = partialConfig.server.bind()
49                 .let { createServerConfiguration(it).bind() }
50
51         val cbsConfiguration = partialConfig.cbs.bind()
52                 .let { createCbsConfiguration(it).bind() }
53
54         val securityConfiguration = SecurityConfiguration(partialConfig.security.bind().keys)
55
56 // TOD0: retrieve when ConfigurationMerger is implemented
57 //        val collectorConfiguration = partialConfig.collector.bind()
58 //                .let { createCollectorConfig(it).bind() }
59
60         HvVesConfiguration(
61                 serverConfiguration,
62                 cbsConfiguration,
63                 securityConfiguration,
64 // TOD0: swap when ConfigurationMerger is implemented
65 //                    collectorConfiguration
66                 CollectorConfiguration(-1,
67                         "I do not exist. I'm not even a URL :o",
68                         emptyList()),
69 // end TOD0
70                 logLevel
71         )
72     }.toEither { ValidationError("Some required configuration options are missing") }
73
74     private fun determineLogLevel(logLevel: Option<LogLevel>) =
75             logLevel.getOrElse {
76                 logger.warn {
77                     "Missing or invalid \"logLevel\" field. " +
78                             "Using default log level ($DEFAULT_LOG_LEVEL)"
79                 }
80                 DEFAULT_LOG_LEVEL
81             }
82
83     private fun createServerConfiguration(partial: PartialServerConfig) =
84             partial.mapBinding {
85                 ServerConfiguration(
86                         it.listenPort.bind(),
87                         it.idleTimeoutSec.bind(),
88                         it.maxPayloadSizeBytes.bind()
89                 )
90             }
91
92     private fun createCbsConfiguration(partial: PartialCbsConfig) =
93             partial.mapBinding {
94                 CbsConfiguration(
95                         it.firstRequestDelaySec.bind(),
96                         it.requestIntervalSec.bind()
97                 )
98             }
99
100 // TOD0: retrieve when ConfigurationMerger is implemented
101 //    private fun createCollectorConfig(partial: PartialCollectorConfig) =
102 //            partial.mapBinding {
103 //                CollectorConfiguration(
104 //                        it.maxRequestSizeBytes.bind(),
105 //                        toKafkaServersString(it.kafkaServers.bind()),
106 //                        it.routing.bind()
107 //                )
108 //            }
109
110     private fun toKafkaServersString(kafkaServers: List<InetSocketAddress>): String =
111             kafkaServers.joinToString(",") { "${it.hostName}:${it.port}" }
112
113     companion object {
114         val DEFAULT_LOG_LEVEL = LogLevel.INFO
115         private val logger = Logger(ConfigurationValidator::class)
116     }
117 }
118
119 data class ValidationError(val message: String, val cause: Option<Throwable> = None)