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