cfcc7d760e727894c8153d64d3e75a7cd0900146
[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.None
23 import arrow.core.Option
24 import arrow.core.Some
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 import java.io.File
39
40 /**
41  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
42  * @since March 2019
43  */
44 internal class ConfigurationValidator {
45
46     fun validate(partialConfig: PartialConfiguration) =
47             logger.info { "About to validate configuration: $partialConfig" }.let {
48                 binding {
49                     val logLevel = determineLogLevel(partialConfig.logLevel)
50
51                     val serverConfiguration = validatedServerConfiguration(partialConfig)
52                             .doOnEmpty { logger.debug { "Cannot bind server configuration" } }
53                             .bind()
54
55                     val cbsConfiguration = validatedCbsConfiguration(partialConfig)
56                             .doOnEmpty { logger.debug { "Cannot bind cbs configuration" } }
57                             .bind()
58
59                     val securityConfiguration = determineSecurityConfiguration(partialConfig)
60                             .doOnEmpty { logger.debug { "Cannot bind security configuration" } }
61                             .bind()
62
63                     val collectorConfiguration = validatedCollectorConfig(partialConfig)
64                             .doOnEmpty { logger.debug { "Cannot bind collector configuration" } }
65                             .bind()
66
67                     HvVesConfiguration(
68                             serverConfiguration,
69                             cbsConfiguration,
70                             securityConfiguration,
71                             collectorConfiguration,
72                             logLevel
73                     )
74                 }.toEither { ValidationException("Some required configuration options are missing") }
75             }
76
77
78     private fun determineLogLevel(logLevel: Option<LogLevel>) =
79             logLevel.getOrElse {
80                 logger.warn {
81                     "Missing or invalid \"logLevel\" field. " +
82                             "Using default log level ($DEFAULT_LOG_LEVEL)"
83                 }
84                 DEFAULT_LOG_LEVEL
85             }
86
87     private fun validatedServerConfiguration(partial: PartialConfiguration) =
88             partial.mapBinding {
89                 ServerConfiguration(
90                         it.listenPort.bind(),
91                         it.idleTimeoutSec.bind(),
92                         it.maxPayloadSizeBytes.bind()
93                 )
94             }
95
96     internal fun validatedCbsConfiguration(partial: PartialConfiguration) =
97             partial.mapBinding {
98                 CbsConfiguration(
99                         it.firstRequestDelaySec.bind(),
100                         it.requestIntervalSec.bind()
101                 )
102             }
103
104     private fun determineSecurityConfiguration(partial: PartialConfiguration) =
105             partial.sslDisable.fold({ createSecurityConfiguration(partial) }, { sslDisabled ->
106                 if (sslDisabled) {
107                     Some(SecurityConfiguration(None))
108                 } else {
109                     createSecurityConfiguration(partial)
110                 }
111             })
112
113     private fun createSecurityConfiguration(partial: PartialConfiguration): Option<SecurityConfiguration> =
114             partial.mapBinding {
115                 SecurityConfiguration(
116                         Option.fromNullable(SecurityKeysPaths(
117                                 File(it.keyStoreFile.bind()).toPath(),
118                                 it.keyStorePassword.bind(),
119                                 File(it.trustStoreFile.bind()).toPath(),
120                                 it.trustStorePassword.bind()
121                         ).asImmutableSecurityKeys())
122                 )
123             }
124
125     private fun validatedCollectorConfig(partial: PartialConfiguration) =
126             partial.mapBinding {
127                 CollectorConfiguration(
128                         it.routing.bind()
129                 )
130             }
131
132     companion object {
133         val DEFAULT_LOG_LEVEL = LogLevel.INFO
134         private val logger = Logger(ConfigurationValidator::class)
135     }
136 }