ae87f1c25cfbd7f202f8fecd6f0e10ff7703d1ad
[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.instances.option.monad.monad
25 import arrow.typeclasses.binding
26 import org.apache.commons.cli.CommandLine
27 import org.apache.commons.cli.DefaultParser
28 import org.onap.dcae.collectors.veshv.domain.WireFrameMessage
29 import org.onap.dcae.collectors.veshv.model.ConfigurationProviderParams
30 import org.onap.dcae.collectors.veshv.model.KafkaConfiguration
31 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
32 import org.onap.dcae.collectors.veshv.ssl.boundary.createSecurityConfiguration
33 import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration
34 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_CONFIG_URL
35 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.KAFKA_SERVERS
36 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_FIRST_REQUEST_DELAY
37 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_REQUEST_INTERVAL
38 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.DUMMY_MODE
39 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.HEALTH_CHECK_API_PORT
40 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.IDLE_TIMEOUT_SEC
41 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.KEY_STORE_FILE
42 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.KEY_STORE_PASSWORD
43 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.LISTEN_PORT
44 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.MAXIMUM_PAYLOAD_SIZE_BYTES
45 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.SSL_DISABLE
46 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_STORE_FILE
47 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_STORE_PASSWORD
48 import org.onap.dcae.collectors.veshv.utils.commandline.hasOption
49 import org.onap.dcae.collectors.veshv.utils.commandline.intValue
50 import org.onap.dcae.collectors.veshv.utils.commandline.longValue
51 import org.onap.dcae.collectors.veshv.utils.commandline.stringValue
52 import java.net.InetSocketAddress
53 import java.time.Duration
54
55 internal class ArgVesHvConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) {
56     override val cmdLineOptionsList = listOf(
57             KAFKA_SERVERS,
58             HEALTH_CHECK_API_PORT,
59             LISTEN_PORT,
60             CONSUL_CONFIG_URL,
61             CONSUL_FIRST_REQUEST_DELAY,
62             CONSUL_REQUEST_INTERVAL,
63             SSL_DISABLE,
64             KEY_STORE_FILE,
65             KEY_STORE_PASSWORD,
66             TRUST_STORE_FILE,
67             TRUST_STORE_PASSWORD,
68             IDLE_TIMEOUT_SEC,
69             MAXIMUM_PAYLOAD_SIZE_BYTES,
70             DUMMY_MODE
71     )
72
73     override fun getConfiguration(cmdLine: CommandLine): Option<ServerConfiguration> =
74             Option.monad().binding {
75                 val healthCheckApiPort = cmdLine.intValue(
76                         HEALTH_CHECK_API_PORT,
77                         DefaultValues.HEALTH_CHECK_API_PORT
78                 )
79                 val kafkaServers = cmdLine.stringValue(KAFKA_SERVERS).bind()
80                 val listenPort = cmdLine.intValue(LISTEN_PORT).bind()
81                 val idleTimeoutSec = cmdLine.longValue(IDLE_TIMEOUT_SEC, DefaultValues.IDLE_TIMEOUT_SEC)
82                 val maxPayloadSizeBytes = cmdLine.intValue(MAXIMUM_PAYLOAD_SIZE_BYTES,
83                         DefaultValues.MAX_PAYLOAD_SIZE_BYTES)
84                 val dummyMode = cmdLine.hasOption(DUMMY_MODE)
85                 val security = createSecurityConfiguration(cmdLine).bind()
86                 val configurationProviderParams = createConfigurationProviderParams(cmdLine).bind()
87                 ServerConfiguration(
88                         serverListenAddress = InetSocketAddress(listenPort),
89                         kafkaConfiguration = KafkaConfiguration(kafkaServers),
90                         healthCheckApiListenAddress = InetSocketAddress(healthCheckApiPort),
91                         configurationProviderParams = configurationProviderParams,
92                         securityConfiguration = security,
93                         idleTimeout = Duration.ofSeconds(idleTimeoutSec),
94                         maximumPayloadSizeBytes = maxPayloadSizeBytes,
95                         dummyMode = dummyMode)
96             }.fix()
97
98     private fun createConfigurationProviderParams(cmdLine: CommandLine): Option<ConfigurationProviderParams> =
99             Option.monad().binding {
100                 val configUrl = cmdLine.stringValue(CONSUL_CONFIG_URL).bind()
101                 val firstRequestDelay = cmdLine.longValue(
102                         CONSUL_FIRST_REQUEST_DELAY,
103                         DefaultValues.CONSUL_FIRST_REQUEST_DELAY
104                 )
105                 val requestInterval = cmdLine.longValue(
106                         CONSUL_REQUEST_INTERVAL,
107                         DefaultValues.CONSUL_REQUEST_INTERVAL
108                 )
109                 ConfigurationProviderParams(
110                         configUrl,
111                         Duration.ofSeconds(firstRequestDelay),
112                         Duration.ofSeconds(requestInterval)
113                 )
114             }.fix()
115
116     internal object DefaultValues {
117         const val HEALTH_CHECK_API_PORT = 6060
118         const val CONSUL_FIRST_REQUEST_DELAY = 10L
119         const val CONSUL_REQUEST_INTERVAL = 5L
120         const val IDLE_TIMEOUT_SEC = 60L
121         const val MAX_PAYLOAD_SIZE_BYTES = WireFrameMessage.DEFAULT_MAX_PAYLOAD_SIZE_BYTES
122     }
123 }