63d590a20e45a78b64b1180e21e6887f1019ecc0
[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
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
29
30 /**
31  * @author Pawel Biniek <pawel.biniek@nokia.com>
32  * @since March 2019
33  */
34 internal class ConfigurationMerger {
35     fun merge(base: PartialConfiguration, update: PartialConfiguration): PartialConfiguration =
36             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)
42             )
43
44
45     private fun mergeServerConfig(baseOption: Option<PartialServerConfig>,
46                                   updateOption: Option<PartialServerConfig>) =
47             applyUpdate(baseOption, updateOption) { base, update ->
48                 PartialServerConfig(
49                         base.listenPort.updateToGivenOrNone(update.listenPort),
50                         base.idleTimeoutSec.updateToGivenOrNone(update.idleTimeoutSec),
51                         base.maxPayloadSizeBytes.updateToGivenOrNone(update.maxPayloadSizeBytes)
52                 )
53             }
54
55
56     private fun mergeCbsConfig(baseOption: Option<PartialCbsConfig>,
57                                updateOption: Option<PartialCbsConfig>) =
58             applyUpdate(baseOption, updateOption) { base, update ->
59                 PartialCbsConfig(
60                         base.firstRequestDelaySec.updateToGivenOrNone(update.firstRequestDelaySec),
61                         base.requestIntervalSec.updateToGivenOrNone(update.requestIntervalSec)
62                 )
63             }
64
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)
70                 )
71             }
72
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)
78                 )
79             }
80
81
82     private fun mergeLogLevel(base: Option<LogLevel>, update: Option<LogLevel>) =
83             base.updateToGivenOrNone(update)
84 }
85
86 private fun <T> applyUpdate(base: Option<T>, update: Option<T>, overrider: (base: T, update: T) -> T) =
87         when {
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
91             else -> None
92         }
93
94 private fun <T> Option<T>.updateToGivenOrNone(update: Option<T>) =
95         update.getOrElse(this::orNull).toOption()