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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.main
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
31 internal class ArgBasedServerConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) {
32 override val cmdLineOptionsList = listOf(
35 CONSUL_FIRST_REQUEST_DELAY,
36 CONSUL_REQUEST_INTERVAL,
45 override fun getConfiguration(cmdLine: CommandLine): ServerConfiguration {
46 val port = cmdLine.intValue(LISTEN_PORT, DefaultValues.PORT)
47 val idleTimeoutSec = cmdLine.longValue(IDLE_TIMEOUT_SEC, DefaultValues.IDLE_TIMEOUT_SEC)
48 val dummyMode = cmdLine.hasOption(DUMMY_MODE)
49 val security = createSecurityConfiguration(cmdLine)
50 val configurationProviderParams = createConfigurationProviderParams(cmdLine);
51 return ServerConfiguration(
53 configurationProviderParams = configurationProviderParams,
54 securityConfiguration = security,
55 idleTimeout = Duration.ofSeconds(idleTimeoutSec),
56 dummyMode = dummyMode)
59 private fun createConfigurationProviderParams(cmdLine: CommandLine): ConfigurationProviderParams {
60 val configUrl = cmdLine.stringValue(CONSUL_CONFIG_URL, DefaultValues.CONFIG_URL)
61 val firstRequestDelay = cmdLine.longValue(CONSUL_FIRST_REQUEST_DELAY, DefaultValues.CONSUL_FIRST_REQUEST_DELAY)
62 val requestInterval = cmdLine.longValue(CONSUL_REQUEST_INTERVAL, DefaultValues.CONSUL_REQUEST_INTERVAL)
64 return ConfigurationProviderParams(
66 Duration.ofSeconds(firstRequestDelay),
67 Duration.ofSeconds(requestInterval)
71 private fun createSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration {
72 val sslDisable = cmdLine.hasOption(SSL_DISABLE)
73 val pkFile = cmdLine.stringValue(PRIVATE_KEY_FILE, DefaultValues.PRIVATE_KEY_FILE)
74 val certFile = cmdLine.stringValue(CERT_FILE, DefaultValues.CERT_FILE)
75 val trustCertFile = cmdLine.stringValue(TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
77 return SecurityConfiguration(
78 sslDisable = sslDisable,
79 privateKey = stringPathToPath(pkFile),
80 cert = stringPathToPath(certFile),
81 trustedCert = stringPathToPath(trustCertFile)
85 internal object DefaultValues {
87 const val CONSUL_FIRST_REQUEST_DELAY = 10L
88 const val CONSUL_REQUEST_INTERVAL = 5L
89 const val CONFIG_URL = ""
90 const val PRIVATE_KEY_FILE = "/etc/ves-hv/server.key"
91 const val CERT_FILE = "/etc/ves-hv/server.crt"
92 const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
93 const val IDLE_TIMEOUT_SEC = 60L