a11fe3d49489c803cc0809dc7ac0569689f803cd
[dcaegen2/collectors/hv-ves.git] /
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 org.apache.commons.cli.CommandLine
23 import org.apache.commons.cli.DefaultParser
24 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
25 import org.onap.dcae.collectors.veshv.model.ConfigurationProviderParams
26 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
27 import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration
28 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.*
29 import java.time.Duration
30
31 internal object DefaultValues {
32     const val PORT = 6061
33     const val CONSUL_FIRST_REQUEST_DELAY = 10L
34     const val CONSUL_REQUEST_INTERVAL = 5L
35     const val CONFIG_URL = ""
36     const val PRIVATE_KEY_FILE = "/etc/ves-hv/server.key"
37     const val CERT_FILE = "/etc/ves-hv/server.crt"
38     const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
39     const val IDLE_TIMEOUT_SEC = 60L
40 }
41
42 internal class ArgBasedServerConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) {
43     override val cmdLineOptionsList = listOf(
44             LISTEN_PORT,
45             CONSUL_CONFIG_URL,
46             CONSUL_FIRST_REQUEST_DELAY,
47             CONSUL_REQUEST_INTERVAL,
48             SSL_DISABLE,
49             PRIVATE_KEY_FILE,
50             CERT_FILE,
51             TRUST_CERT_FILE,
52             IDLE_TIMEOUT_SEC,
53             DUMMY_MODE
54     )
55
56     override fun getConfiguration(cmdLine: CommandLine): ServerConfiguration {
57         val port = cmdLine.intValue(LISTEN_PORT, DefaultValues.PORT)
58         val idleTimeoutSec = cmdLine.longValue(IDLE_TIMEOUT_SEC, DefaultValues.IDLE_TIMEOUT_SEC)
59         val dummyMode = cmdLine.hasOption(DUMMY_MODE)
60         val security = createSecurityConfiguration(cmdLine)
61         val configurationProviderParams = createConfigurationProviderParams(cmdLine);
62         return ServerConfiguration(
63                 port = port,
64                 configurationProviderParams = configurationProviderParams,
65                 securityConfiguration = security,
66                 idleTimeout = Duration.ofSeconds(idleTimeoutSec),
67                 dummyMode = dummyMode)
68     }
69
70     private fun createConfigurationProviderParams(cmdLine: CommandLine): ConfigurationProviderParams {
71         val configUrl = cmdLine.stringValue(CONSUL_CONFIG_URL, DefaultValues.CONFIG_URL)
72         val firstRequestDelay = cmdLine.longValue(CONSUL_FIRST_REQUEST_DELAY, DefaultValues.CONSUL_FIRST_REQUEST_DELAY)
73         val requestInterval = cmdLine.longValue(CONSUL_REQUEST_INTERVAL, DefaultValues.CONSUL_REQUEST_INTERVAL)
74
75         return ConfigurationProviderParams(
76                 configUrl,
77                 Duration.ofSeconds(firstRequestDelay),
78                 Duration.ofSeconds(requestInterval)
79         )
80     }
81
82     private fun createSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration {
83         val sslDisable = cmdLine.hasOption(SSL_DISABLE)
84         val pkFile = cmdLine.stringValue(PRIVATE_KEY_FILE, DefaultValues.PRIVATE_KEY_FILE)
85         val certFile = cmdLine.stringValue(CERT_FILE, DefaultValues.CERT_FILE)
86         val trustCertFile = cmdLine.stringValue(TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
87
88         return SecurityConfiguration(
89                 sslDisable = sslDisable,
90                 privateKey = stringPathToPath(pkFile),
91                 cert = stringPathToPath(certFile),
92                 trustedCert = stringPathToPath(trustCertFile)
93         )
94     }
95 }