407fd7452b02a237f37b04859ff30b713da0579e
[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.config.api.model.ValidationException
31 import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityConfiguration
32 import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityKeysPaths
33 import org.onap.dcae.collectors.veshv.utils.arrow.OptionUtils.binding
34 import org.onap.dcae.collectors.veshv.utils.arrow.mapBinding
35 import org.onap.dcae.collectors.veshv.utils.arrow.doOnEmpty
36 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
37 import org.onap.dcae.collectors.veshv.utils.logging.Logger
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             logger.info { "About to validate configuration: $partialConfig" }.let {
47                 binding {
48                     val logLevel = determineLogLevel(partialConfig.logLevel)
49
50                     val serverConfiguration = validatedServerConfiguration(partialConfig)
51                             .doOnEmpty { logger.debug { "Cannot bind server configuration" } }
52                             .bind()
53
54                     val cbsConfiguration = validatedCbsConfiguration(partialConfig)
55                             .doOnEmpty { logger.debug { "Cannot bind cbs configuration" } }
56                             .bind()
57
58                     val securityConfiguration = validatedSecurityConfiguration(partialConfig)
59                             .doOnEmpty { logger.debug { "Cannot bind security configuration" } }
60                             .bind()
61
62                     val collectorConfiguration = validatedCollectorConfig(partialConfig)
63                             .doOnEmpty { logger.debug { "Cannot bind collector configuration" } }
64                             .bind()
65
66                     HvVesConfiguration(
67                             serverConfiguration,
68                             cbsConfiguration,
69                             securityConfiguration,
70                             collectorConfiguration,
71                             logLevel
72                     )
73                 }.toEither { ValidationException("Some required configuration options are missing") }
74             }
75
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 validatedServerConfiguration(partial: PartialConfiguration) =
87             partial.mapBinding {
88                 partial.server.bind().let {
89                     ServerConfiguration(
90                             it.listenPort.bind(),
91                             it.idleTimeoutSec.bind(),
92                             it.maxPayloadSizeBytes.bind()
93                     )
94                 }
95             }
96
97     internal fun validatedCbsConfiguration(partial: PartialConfiguration) =
98             partial.mapBinding {
99                 it.cbs.bind().let {
100                     CbsConfiguration(
101                             it.firstRequestDelaySec.bind(),
102                             it.requestIntervalSec.bind()
103                     )
104                 }
105             }
106
107     private fun validatedSecurityConfiguration(partial: PartialConfiguration) =
108             partial.mapBinding {
109                 it.security.bind().let {
110                     SecurityConfiguration(it.keys.map(SecurityKeysPaths::asImmutableSecurityKeys))
111                 }
112             }
113
114     private fun validatedCollectorConfig(partial: PartialConfiguration) =
115             partial.mapBinding {
116                 partial.collector.bind().let {
117                     CollectorConfiguration(
118                             it.routing.bind()
119                     )
120                 }
121             }
122
123     companion object {
124         val DEFAULT_LOG_LEVEL = LogLevel.INFO
125         private val logger = Logger(ConfigurationValidator::class)
126     }
127 }