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