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