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