From e55809c0219be0898138c436d82ceba212b92df9 Mon Sep 17 00:00:00 2001
From: Filip Krzywka <filip.krzywka@nokia.com>
Date: Fri, 5 Apr 2019 11:10:46 +0200
Subject: Flatten configuration structure

Change-Id: I9a5a53f3c4cf0973d071f2bc6962016b5613972d
Issue-ID: DCAEGEN2-710
Signed-off-by: Filip Krzywka <filip.krzywka@nokia.com>
---
 .../veshv/config/impl/CbsConfigurationProvider.kt  |  2 +-
 .../veshv/config/impl/ConfigurationMerger.kt       | 70 ++++++----------------
 .../veshv/config/impl/ConfigurationValidator.kt    | 57 ++++++++++--------
 .../veshv/config/impl/FileConfigurationReader.kt   |  2 -
 .../config/impl/gsonadapters/SecurityAdapter.kt    | 68 ---------------------
 .../veshv/config/impl/partial_configuration.kt     | 40 +++++++------
 6 files changed, 75 insertions(+), 164 deletions(-)
 delete mode 100644 sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/gsonadapters/SecurityAdapter.kt

(limited to 'sources/hv-collector-configuration/src/main')

diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/CbsConfigurationProvider.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/CbsConfigurationProvider.kt
index 2038c31a..b6462936 100644
--- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/CbsConfigurationProvider.kt
+++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/CbsConfigurationProvider.kt
@@ -95,7 +95,7 @@ internal class CbsConfigurationProvider(private val cbsClientMono: Mono<CbsClien
             .map(::createRoutingDescription)
             .onErrorLog(logger, mdc) { "Error while creating configuration" }
             .retryWhen(retry)
-            .map { PartialConfiguration(collector = Some(PartialCollectorConfig(routing = it))) }
+            .map { PartialConfiguration(routing = it) }
 
     private fun createRoutingDescription(configuration: JsonObject): Option<Routing> = try {
         val routes = DataStreams.namedSinks(configuration)
diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationMerger.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationMerger.kt
index 63d590a2..8e6bafc4 100644
--- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationMerger.kt
+++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationMerger.kt
@@ -20,12 +20,12 @@
 package org.onap.dcae.collectors.veshv.config.impl
 
 
-import arrow.core.None
 import arrow.core.Option
 import arrow.core.Some
 import arrow.core.getOrElse
 import arrow.core.toOption
-import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
+import kotlin.reflect.KProperty0
+import kotlin.reflect.KProperty1
 
 /**
  * @author Pawel Biniek <pawel.biniek@nokia.com>
@@ -34,62 +34,28 @@ import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
 internal class ConfigurationMerger {
     fun merge(base: PartialConfiguration, update: PartialConfiguration): PartialConfiguration =
             PartialConfiguration(
-                    mergeServerConfig(base.server, update.server),
-                    mergeCbsConfig(base.cbs, update.cbs),
-                    mergeSecurityConfig(base.security, update.security),
-                    mergeCollectorConfig(base.collector, update.collector),
-                    mergeLogLevel(base.logLevel, update.logLevel)
-            )
-
-
-    private fun mergeServerConfig(baseOption: Option<PartialServerConfig>,
-                                  updateOption: Option<PartialServerConfig>) =
-            applyUpdate(baseOption, updateOption) { base, update ->
-                PartialServerConfig(
-                        base.listenPort.updateToGivenOrNone(update.listenPort),
-                        base.idleTimeoutSec.updateToGivenOrNone(update.idleTimeoutSec),
-                        base.maxPayloadSizeBytes.updateToGivenOrNone(update.maxPayloadSizeBytes)
-                )
-            }
+                    listenPort = base.listenPort.updateToGivenOrNone(update.listenPort),
+                    idleTimeoutSec = base.idleTimeoutSec.updateToGivenOrNone(update.idleTimeoutSec),
+                    maxPayloadSizeBytes = base.maxPayloadSizeBytes.updateToGivenOrNone(update.maxPayloadSizeBytes),
 
+                    firstRequestDelaySec = base.firstRequestDelaySec.updateToGivenOrNone(update.firstRequestDelaySec),
+                    requestIntervalSec = base.requestIntervalSec.updateToGivenOrNone(update.requestIntervalSec),
 
-    private fun mergeCbsConfig(baseOption: Option<PartialCbsConfig>,
-                               updateOption: Option<PartialCbsConfig>) =
-            applyUpdate(baseOption, updateOption) { base, update ->
-                PartialCbsConfig(
-                        base.firstRequestDelaySec.updateToGivenOrNone(update.firstRequestDelaySec),
-                        base.requestIntervalSec.updateToGivenOrNone(update.requestIntervalSec)
-                )
-            }
+                    sslDisable = base.sslDisable.updateToGivenOrNone(update.sslDisable),
+                    keyStoreFile = base.keyStoreFile.updateToGivenOrNone(update.keyStoreFile),
+                    keyStorePassword = base.keyStorePassword.updateToGivenOrNone(update.keyStorePassword),
+                    trustStoreFile = base.trustStoreFile.updateToGivenOrNone(update.trustStoreFile),
+                    trustStorePassword = base.trustStorePassword.updateToGivenOrNone(update.trustStorePassword),
 
-    private fun mergeSecurityConfig(baseOption: Option<PartialSecurityConfig>,
-                                    updateOption: Option<PartialSecurityConfig>) =
-            applyUpdate(baseOption, updateOption) { base, update ->
-                PartialSecurityConfig(
-                        base.keys.updateToGivenOrNone(update.keys)
-                )
-            }
+                    routing = base.routing.updateToGivenOrNone(update.routing),
 
-    private fun mergeCollectorConfig(baseOption: Option<PartialCollectorConfig>,
-                                     updateOption: Option<PartialCollectorConfig>) =
-            applyUpdate(baseOption, updateOption) { base, update ->
-                PartialCollectorConfig(
-                        base.routing.updateToGivenOrNone(update.routing)
-                )
-            }
+                    logLevel = base.logLevel.updateToGivenOrNone(update.logLevel)
+            )
 
+    private fun <T> Option<T>.updateToGivenOrNone(update: Option<T>) =
+            update.getOrElse(this::orNull).toOption()
 
-    private fun mergeLogLevel(base: Option<LogLevel>, update: Option<LogLevel>) =
-            base.updateToGivenOrNone(update)
 }
 
-private fun <T> applyUpdate(base: Option<T>, update: Option<T>, overrider: (base: T, update: T) -> T) =
-        when {
-            base is Some && update is Some -> overrider(base.t, update.t).toOption()
-            base is Some && update is None -> base
-            base is None && update is Some -> update
-            else -> None
-        }
 
-private fun <T> Option<T>.updateToGivenOrNone(update: Option<T>) =
-        update.getOrElse(this::orNull).toOption()
+
diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationValidator.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationValidator.kt
index 407fd745..cfcc7d76 100644
--- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationValidator.kt
+++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationValidator.kt
@@ -19,9 +19,9 @@
  */
 package org.onap.dcae.collectors.veshv.config.impl
 
-import arrow.core.Either
 import arrow.core.None
 import arrow.core.Option
+import arrow.core.Some
 import arrow.core.getOrElse
 import org.onap.dcae.collectors.veshv.config.api.model.CbsConfiguration
 import org.onap.dcae.collectors.veshv.config.api.model.CollectorConfiguration
@@ -35,6 +35,7 @@ import org.onap.dcae.collectors.veshv.utils.arrow.mapBinding
 import org.onap.dcae.collectors.veshv.utils.arrow.doOnEmpty
 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
 import org.onap.dcae.collectors.veshv.utils.logging.Logger
+import java.io.File
 
 /**
  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
@@ -55,7 +56,7 @@ internal class ConfigurationValidator {
                             .doOnEmpty { logger.debug { "Cannot bind cbs configuration" } }
                             .bind()
 
-                    val securityConfiguration = validatedSecurityConfiguration(partialConfig)
+                    val securityConfiguration = determineSecurityConfiguration(partialConfig)
                             .doOnEmpty { logger.debug { "Cannot bind security configuration" } }
                             .bind()
 
@@ -85,39 +86,47 @@ internal class ConfigurationValidator {
 
     private fun validatedServerConfiguration(partial: PartialConfiguration) =
             partial.mapBinding {
-                partial.server.bind().let {
-                    ServerConfiguration(
-                            it.listenPort.bind(),
-                            it.idleTimeoutSec.bind(),
-                            it.maxPayloadSizeBytes.bind()
-                    )
-                }
+                ServerConfiguration(
+                        it.listenPort.bind(),
+                        it.idleTimeoutSec.bind(),
+                        it.maxPayloadSizeBytes.bind()
+                )
             }
 
     internal fun validatedCbsConfiguration(partial: PartialConfiguration) =
             partial.mapBinding {
-                it.cbs.bind().let {
-                    CbsConfiguration(
-                            it.firstRequestDelaySec.bind(),
-                            it.requestIntervalSec.bind()
-                    )
-                }
+                CbsConfiguration(
+                        it.firstRequestDelaySec.bind(),
+                        it.requestIntervalSec.bind()
+                )
             }
 
-    private fun validatedSecurityConfiguration(partial: PartialConfiguration) =
-            partial.mapBinding {
-                it.security.bind().let {
-                    SecurityConfiguration(it.keys.map(SecurityKeysPaths::asImmutableSecurityKeys))
+    private fun determineSecurityConfiguration(partial: PartialConfiguration) =
+            partial.sslDisable.fold({ createSecurityConfiguration(partial) }, { sslDisabled ->
+                if (sslDisabled) {
+                    Some(SecurityConfiguration(None))
+                } else {
+                    createSecurityConfiguration(partial)
                 }
+            })
+
+    private fun createSecurityConfiguration(partial: PartialConfiguration): Option<SecurityConfiguration> =
+            partial.mapBinding {
+                SecurityConfiguration(
+                        Option.fromNullable(SecurityKeysPaths(
+                                File(it.keyStoreFile.bind()).toPath(),
+                                it.keyStorePassword.bind(),
+                                File(it.trustStoreFile.bind()).toPath(),
+                                it.trustStorePassword.bind()
+                        ).asImmutableSecurityKeys())
+                )
             }
 
     private fun validatedCollectorConfig(partial: PartialConfiguration) =
             partial.mapBinding {
-                partial.collector.bind().let {
-                    CollectorConfiguration(
-                            it.routing.bind()
-                    )
-                }
+                CollectorConfiguration(
+                        it.routing.bind()
+                )
             }
 
     companion object {
diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/FileConfigurationReader.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/FileConfigurationReader.kt
index f6ae5bec..104ca78c 100644
--- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/FileConfigurationReader.kt
+++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/FileConfigurationReader.kt
@@ -23,7 +23,6 @@ import arrow.core.Option
 import com.google.gson.GsonBuilder
 import org.onap.dcae.collectors.veshv.config.impl.gsonadapters.DurationOfSecondsAdapter
 import org.onap.dcae.collectors.veshv.config.impl.gsonadapters.OptionAdapter
-import org.onap.dcae.collectors.veshv.config.impl.gsonadapters.SecurityAdapter
 import org.onap.dcae.collectors.veshv.utils.logging.Logger
 
 import java.io.Reader
@@ -36,7 +35,6 @@ import java.time.Duration
 internal class FileConfigurationReader {
     private val gson = GsonBuilder()
             .registerTypeAdapter(Option::class.java, OptionAdapter())
-            .registerTypeAdapter(PartialSecurityConfig::class.java, SecurityAdapter())
             .registerTypeAdapter(Duration::class.java, DurationOfSecondsAdapter())
             .create()
 
diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/gsonadapters/SecurityAdapter.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/gsonadapters/SecurityAdapter.kt
deleted file mode 100644
index bc8cf51c..00000000
--- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/gsonadapters/SecurityAdapter.kt
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * dcaegen2-collectors-veshv
- * ================================================================================
- * Copyright (C) 2019 NOKIA
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-package org.onap.dcae.collectors.veshv.config.impl.gsonadapters
-
-import arrow.core.Option
-import com.google.gson.JsonDeserializationContext
-import com.google.gson.JsonDeserializer
-import com.google.gson.JsonElement
-import com.google.gson.JsonObject
-import org.onap.dcae.collectors.veshv.config.impl.PartialSecurityConfig
-import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityKeysPaths
-import java.io.File
-import java.lang.reflect.Type
-
-/**
- * @author Pawel Biniek <pawel.biniek@nokia.com>
- * @since March 2019
- */
-internal class SecurityAdapter : JsonDeserializer<PartialSecurityConfig> {
-
-    override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext?) =
-            json.asJsonObject.let { security ->
-                if (security.entrySet().isEmpty() || hasSslDisableSet(security)) {
-                    PartialSecurityConfig(Option.empty())
-                } else {
-                    PartialSecurityConfig(Option.just(security.securityKeys(::asImmutableSecurityKeys)))
-                }
-            }
-
-    private fun hasSslDisableSet(security: JsonObject) =
-            security.has(SSL_DISABLE_KEY) && security[SSL_DISABLE_KEY].asBoolean
-
-    private fun JsonObject.securityKeys(f: (JsonObject) -> SecurityKeysPaths) = f(getAsJsonObject(KEYS_OBJECT_KEY))
-
-    private fun asImmutableSecurityKeys(keys: JsonObject) = SecurityKeysPaths(
-            File(keys[KEY_STORE_FILE_KEY].asString).toPath(),
-            keys[KEY_STORE_PASSWORD_KEY].asString,
-            File(keys[TRUST_STORE_FILE_KEY].asString).toPath(),
-            keys[TRUST_STORE_PASSWORD_KEY].asString
-    )
-
-    companion object {
-        private val SSL_DISABLE_KEY = "sslDisable"
-        private val KEYS_OBJECT_KEY = "keys"
-        private val KEY_STORE_FILE_KEY = "keyStoreFile"
-        private val KEY_STORE_PASSWORD_KEY = "keyStorePassword"
-        private val TRUST_STORE_FILE_KEY = "trustStoreFile"
-        private val TRUST_STORE_PASSWORD_KEY = "trustStorePassword"
-    }
-}
-
diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/partial_configuration.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/partial_configuration.kt
index 82cf533a..0be2572d 100644
--- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/partial_configuration.kt
+++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/partial_configuration.kt
@@ -21,8 +21,8 @@ package org.onap.dcae.collectors.veshv.config.impl
 
 import arrow.core.None
 import arrow.core.Option
+import com.google.gson.annotations.SerializedName
 import org.onap.dcae.collectors.veshv.config.api.model.Routing
-import org.onap.dcae.collectors.veshv.ssl.boundary.SecurityKeysPaths
 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
 import java.time.Duration
 
@@ -31,26 +31,32 @@ import java.time.Duration
  * @since February 2019
  */
 internal data class PartialConfiguration(
-        val server: Option<PartialServerConfig> = None,
-        val cbs: Option<PartialCbsConfig> = None,
-        val security: Option<PartialSecurityConfig> = None,
-        val collector: Option<PartialCollectorConfig> = None,
-        val logLevel: Option<LogLevel> = None
-)
-
-internal data class PartialServerConfig(
+        @SerializedName("server.listenPort")
         val listenPort: Option<Int> = None,
+        @SerializedName("server.idleTimeoutSec")
         val idleTimeoutSec: Option<Duration> = None,
-        val maxPayloadSizeBytes: Option<Int> = None
-)
+        @SerializedName("server.maxPayloadSizeBytes")
+        val maxPayloadSizeBytes: Option<Int> = None,
 
-internal data class PartialCbsConfig(
+        @SerializedName("cbs.firstRequestDelaySec")
         val firstRequestDelaySec: Option<Duration> = None,
-        val requestIntervalSec: Option<Duration> = None
-)
+        @SerializedName("cbs.requestIntervalSec")
+        val requestIntervalSec: Option<Duration> = None,
+
+        @SerializedName("security.sslDisable")
+        val sslDisable: Option<Boolean> = None,
+        @SerializedName("security.keys.keyStoreFile")
+        val keyStoreFile: Option<String> = None,
+        @SerializedName("security.keys.keyStorePassword")
+        val keyStorePassword: Option<String> = None,
+        @SerializedName("security.keys.trustStoreFile")
+        val trustStoreFile: Option<String> = None,
+        @SerializedName("security.keys.trustStorePassword")
+        val trustStorePassword: Option<String> = None,
 
-internal data class PartialSecurityConfig(val keys: Option<SecurityKeysPaths> = None)
+        @SerializedName("collector.routing")
+        val routing: Option<Routing> = None,
 
-internal data class PartialCollectorConfig(
-        val routing: Option<Routing> = None
+        @SerializedName("logLevel")
+        val logLevel: Option<LogLevel> = None
 )
-- 
cgit 1.2.3-korg