Extract HV-VES configuration module
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-configuration / src / main / kotlin / org / onap / dcae / collectors / veshv / config / impl / ArgVesHvConfiguration.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-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.Option
23 import arrow.core.fix
24 import arrow.core.getOrElse
25 import arrow.instances.option.monad.monad
26 import arrow.typeclasses.binding
27 import org.apache.commons.cli.CommandLine
28 import org.apache.commons.cli.DefaultParser
29 import org.onap.dcae.collectors.veshv.commandline.ArgBasedConfiguration
30 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_REQUEST_INTERVAL
31 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_FIRST_REQUEST_DELAY
32 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.DUMMY_MODE
33 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.HEALTH_CHECK_API_PORT
34 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.IDLE_TIMEOUT_SEC
35 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.KAFKA_SERVERS
36 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.KEY_STORE_FILE
37 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.KEY_STORE_PASSWORD
38 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.LISTEN_PORT
39 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.LOG_LEVEL
40 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.MAXIMUM_PAYLOAD_SIZE_BYTES
41 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.SSL_DISABLE
42 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.TRUST_STORE_FILE
43 import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.TRUST_STORE_PASSWORD
44 import org.onap.dcae.collectors.veshv.commandline.hasOption
45 import org.onap.dcae.collectors.veshv.commandline.intValue
46 import org.onap.dcae.collectors.veshv.commandline.longValue
47 import org.onap.dcae.collectors.veshv.commandline.stringValue
48 import org.onap.dcae.collectors.veshv.config.api.model.ConfigurationProviderParams
49 import org.onap.dcae.collectors.veshv.config.api.model.KafkaConfiguration
50 import org.onap.dcae.collectors.veshv.config.api.model.ServerConfiguration
51 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
52 import org.onap.dcae.collectors.veshv.ssl.boundary.createSecurityConfiguration
53 import org.onap.dcae.collectors.veshv.utils.arrow.doOnFailure
54 import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
55 import org.onap.dcae.collectors.veshv.utils.logging.Logger
56 import java.net.InetSocketAddress
57 import java.time.Duration
58
59
60 internal class ArgVesHvConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) {
61     override val cmdLineOptionsList = listOf(
62             KAFKA_SERVERS,
63             HEALTH_CHECK_API_PORT,
64             LISTEN_PORT,
65             CONFIGURATION_FIRST_REQUEST_DELAY,
66             CONFIGURATION_REQUEST_INTERVAL,
67             SSL_DISABLE,
68             KEY_STORE_FILE,
69             KEY_STORE_PASSWORD,
70             TRUST_STORE_FILE,
71             TRUST_STORE_PASSWORD,
72             IDLE_TIMEOUT_SEC,
73             MAXIMUM_PAYLOAD_SIZE_BYTES,
74             DUMMY_MODE,
75             LOG_LEVEL
76     )
77
78     override fun getConfiguration(cmdLine: CommandLine): Option<ServerConfiguration> =
79             Option.monad().binding {
80                 val healthCheckApiPort = cmdLine.intValue(
81                         HEALTH_CHECK_API_PORT,
82                         DefaultValues.HEALTH_CHECK_API_PORT
83                 )
84                 val kafkaServers = cmdLine.stringValue(KAFKA_SERVERS).bind()
85                 val listenPort = cmdLine.intValue(LISTEN_PORT).bind()
86                 val idleTimeoutSec = cmdLine.longValue(IDLE_TIMEOUT_SEC, DefaultValues.IDLE_TIMEOUT_SEC)
87                 val maxPayloadSizeBytes = cmdLine.intValue(
88                         MAXIMUM_PAYLOAD_SIZE_BYTES,
89                         DefaultValues.MAX_PAYLOAD_SIZE_BYTES
90                 )
91                 val dummyMode = cmdLine.hasOption(DUMMY_MODE)
92                 val security = createSecurityConfiguration(cmdLine)
93                         .doOnFailure { ex ->
94                             logger.withError { log("Could not read security keys", ex) }
95                         }
96                         .toOption()
97                         .bind()
98                 val logLevel = cmdLine.stringValue(LOG_LEVEL, DefaultValues.LOG_LEVEL)
99                 val configurationProviderParams = createConfigurationProviderParams(cmdLine).bind()
100                 ServerConfiguration(
101                         serverListenAddress = InetSocketAddress(listenPort),
102                         kafkaConfiguration = KafkaConfiguration(kafkaServers, maxPayloadSizeBytes),
103                         healthCheckApiListenAddress = InetSocketAddress(healthCheckApiPort),
104                         configurationProviderParams = configurationProviderParams,
105                         securityConfiguration = security,
106                         idleTimeout = Duration.ofSeconds(idleTimeoutSec),
107                         maximumPayloadSizeBytes = maxPayloadSizeBytes,
108                         dummyMode = dummyMode,
109                         logLevel = determineLogLevel(logLevel)
110                 )
111             }.fix()
112
113     private fun createConfigurationProviderParams(cmdLine: CommandLine): Option<ConfigurationProviderParams> =
114             Option.monad().binding {
115                 val firstRequestDelay = cmdLine.longValue(
116                         CONFIGURATION_FIRST_REQUEST_DELAY,
117                         DefaultValues.CONFIGURATION_FIRST_REQUEST_DELAY
118                 )
119                 val requestInterval = cmdLine.longValue(
120                         CONFIGURATION_REQUEST_INTERVAL,
121                         DefaultValues.CONFIGURATION_REQUEST_INTERVAL
122                 )
123                 ConfigurationProviderParams(
124                         Duration.ofSeconds(firstRequestDelay),
125                         Duration.ofSeconds(requestInterval)
126                 )
127             }.fix()
128
129     private fun determineLogLevel(logLevel: String) = LogLevel.optionFromString(logLevel)
130             .getOrElse {
131                 logger.warn {
132                     "Failed to parse $logLevel as $LOG_LEVEL command line. " +
133                             "Using default log level (${DefaultValues.LOG_LEVEL})"
134                 }
135                 LogLevel.valueOf(DefaultValues.LOG_LEVEL)
136             }
137
138
139     internal object DefaultValues {
140         const val HEALTH_CHECK_API_PORT = 6060
141         const val CONFIGURATION_FIRST_REQUEST_DELAY = 10L
142         const val CONFIGURATION_REQUEST_INTERVAL = 5L
143         const val IDLE_TIMEOUT_SEC = 60L
144         const val MAX_PAYLOAD_SIZE_BYTES = WireFrameMessage.DEFAULT_MAX_PAYLOAD_SIZE_BYTES
145         val LOG_LEVEL = LogLevel.INFO.name
146     }
147
148     companion object {
149         private val logger = Logger(ArgVesHvConfiguration::class)
150     }
151 }