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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.config.impl
22 import arrow.core.None
23 import arrow.core.Option
24 import arrow.core.Some
25 import arrow.core.getOrElse
26 import arrow.core.toOption
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.ServerConfiguration
32 import org.onap.dcae.collectors.veshv.config.api.model.ValidationException
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.doOnEmpty
36 import org.onap.dcae.collectors.veshv.utils.arrow.mapBinding
37 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
38 import org.onap.dcae.collectors.veshv.utils.logging.Logger
39 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeys
40 import org.onap.dcaegen2.services.sdk.security.ssl.ImmutableSecurityKeysStore
41 import org.onap.dcaegen2.services.sdk.security.ssl.Passwords
43 import java.nio.file.Path
44 import java.time.Duration
47 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
50 internal class ConfigurationValidator {
52 fun validate(partialConfig: PartialConfiguration) =
53 logger.info { "About to validate configuration: $partialConfig" }.let {
55 val logLevel = determineLogLevel(partialConfig.logLevel)
57 val serverConfiguration = validatedServerConfiguration(partialConfig)
58 .doOnEmpty { logger.debug { "Cannot bind server configuration" } }
61 val cbsConfiguration = validatedCbsConfiguration(partialConfig)
62 .doOnEmpty { logger.debug { "Cannot bind cbs configuration" } }
65 val securityConfiguration = determineSecurityConfiguration(partialConfig)
66 .doOnEmpty { logger.debug { "Cannot bind security configuration" } }
69 val collectorConfiguration = validatedCollectorConfig(partialConfig)
70 .doOnEmpty { logger.debug { "Cannot bind collector configuration" } }
76 securityConfiguration,
77 collectorConfiguration,
80 }.toEither { ValidationException("Some required configuration options are missing") }
84 private fun determineLogLevel(logLevel: Option<LogLevel>) =
87 "Missing or invalid \"logLevel\" field. " +
88 "Using default log level ($DEFAULT_LOG_LEVEL)"
93 private fun validatedServerConfiguration(partial: PartialConfiguration) =
97 it.maxPayloadSizeBytes.bind(),
98 Duration.ofSeconds(it.idleTimeoutSec.bind())
102 internal fun validatedCbsConfiguration(partial: PartialConfiguration) =
105 Duration.ofSeconds(it.firstRequestDelaySec.bind()),
106 Duration.ofSeconds(it.requestIntervalSec.bind())
110 private fun determineSecurityConfiguration(partial: PartialConfiguration) =
111 partial.sslDisable.fold({ createSecurityConfiguration(partial) }, { sslDisabled ->
113 Some(SecurityConfiguration(None))
115 createSecurityConfiguration(partial)
119 private fun createSecurityConfiguration(partial: PartialConfiguration): Option<SecurityConfiguration> =
121 SecurityConfiguration(
123 File(it.keyStoreFile.bind()).toPath(),
124 it.keyStorePassword.bind(),
125 File(it.trustStoreFile.bind()).toPath(),
126 it.trustStorePassword.bind()
131 private fun createSecurityKeys(keyStorePath: Path,
132 keyStorePassword: String,
133 trustStorePath: Path,
134 trustStorePassword: String) =
135 ImmutableSecurityKeys.builder()
136 .keyStore(ImmutableSecurityKeysStore.of(keyStorePath))
137 .keyStorePassword(Passwords.fromString(keyStorePassword))
138 .trustStore(ImmutableSecurityKeysStore.of(trustStorePath))
139 .trustStorePassword(Passwords.fromString(trustStorePassword))
143 private fun validatedCollectorConfig(partial: PartialConfiguration) =
144 partial.mapBinding { config ->
145 CollectorConfiguration(
146 config.streamPublishers.bind().map { Route(it.name(), it) }
151 val DEFAULT_LOG_LEVEL = LogLevel.INFO
152 private val logger = Logger(ConfigurationValidator::class)