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
 
  23 import arrow.core.None
 
  24 import arrow.core.Option
 
  25 import arrow.core.Some
 
  26 import arrow.core.getOrElse
 
  27 import arrow.core.toOption
 
  28 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
 
  31  * @author Pawel Biniek <pawel.biniek@nokia.com>
 
  34 internal class ConfigurationMerger {
 
  35     fun merge(base: PartialConfiguration, update: PartialConfiguration): PartialConfiguration =
 
  37                     mergeServerConfig(base.server, update.server),
 
  38                     mergeCbsConfig(base.cbs, update.cbs),
 
  39                     mergeSecurityConfig(base.security, update.security),
 
  40                     mergeCollectorConfig(base.collector, update.collector),
 
  41                     mergeLogLevel(base.logLevel, update.logLevel)
 
  45     private fun mergeServerConfig(baseOption: Option<PartialServerConfig>,
 
  46                                   updateOption: Option<PartialServerConfig>) =
 
  47             applyUpdate(baseOption, updateOption) { base, update ->
 
  49                         base.listenPort.updateToGivenOrNone(update.listenPort),
 
  50                         base.idleTimeoutSec.updateToGivenOrNone(update.idleTimeoutSec),
 
  51                         base.maxPayloadSizeBytes.updateToGivenOrNone(update.maxPayloadSizeBytes)
 
  56     private fun mergeCbsConfig(baseOption: Option<PartialCbsConfig>,
 
  57                                updateOption: Option<PartialCbsConfig>) =
 
  58             applyUpdate(baseOption, updateOption) { base, update ->
 
  60                         base.firstRequestDelaySec.updateToGivenOrNone(update.firstRequestDelaySec),
 
  61                         base.requestIntervalSec.updateToGivenOrNone(update.requestIntervalSec)
 
  65     private fun mergeSecurityConfig(baseOption: Option<PartialSecurityConfig>,
 
  66                                     updateOption: Option<PartialSecurityConfig>) =
 
  67             applyUpdate(baseOption, updateOption) { base, update ->
 
  68                 PartialSecurityConfig(
 
  69                         base.keys.updateToGivenOrNone(update.keys)
 
  73     private fun mergeCollectorConfig(baseOption: Option<PartialCollectorConfig>,
 
  74                                      updateOption: Option<PartialCollectorConfig>) =
 
  75             applyUpdate(baseOption, updateOption) { base, update ->
 
  76                 PartialCollectorConfig(
 
  77                         base.routing.updateToGivenOrNone(update.routing)
 
  82     private fun mergeLogLevel(base: Option<LogLevel>, update: Option<LogLevel>) =
 
  83             base.updateToGivenOrNone(update)
 
  86 private fun <T> applyUpdate(base: Option<T>, update: Option<T>, overrider: (base: T, update: T) -> T) =
 
  88             base is Some && update is Some -> overrider(base.t, update.t).toOption()
 
  89             base is Some && update is None -> base
 
  90             base is None && update is Some -> update
 
  94 private fun <T> Option<T>.updateToGivenOrNone(update: Option<T>) =
 
  95         update.getOrElse(this::orNull).toOption()