2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
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.*
23 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
24 import org.onap.dcae.collectors.veshv.domain.ServerConfiguration
26 import java.nio.file.Paths
28 internal object DefaultValues {
30 const val CONFIG_URL = ""
31 const val PRIVATE_KEY_FILE = "/etc/ves-hv/server.key"
32 const val CERT_FILE = "/etc/ves-hv/server.crt"
33 const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
36 internal class ArgBasedServerConfiguration {
38 fun parse(args: Array<out String>): ServerConfiguration {
39 val parser = DefaultParser()
42 val cmdLine = parser.parse(options, args)
43 val port = cmdLine.intValue(OPT_PORT, DefaultValues.PORT)
44 val configUrl = cmdLine.stringValue(OPT_CONFIG_URL, DefaultValues.CONFIG_URL)
45 val secConf = createSecurityConfiguration(cmdLine)
46 return ServerConfiguration(port, configUrl, secConf)
47 } catch (ex: Exception) {
48 throw WrongArgumentException(ex)
52 private fun createSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration {
54 val pkFile = cmdLine.stringValue(OPT_PK_FILE, DefaultValues.PRIVATE_KEY_FILE)
55 val certFile = cmdLine.stringValue(OPT_CERT_FILE, DefaultValues.CERT_FILE)
56 val trustCertFile = cmdLine.stringValue(OPT_TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
58 return SecurityConfiguration(
59 privateKey = stringPathToPath(pkFile),
60 cert = stringPathToPath(certFile),
61 trustedCert = stringPathToPath(trustCertFile)
65 private fun CommandLine.intValue(option: Option, default: Int) =
66 getOptionValue(option.opt)?.toInt() ?: default
68 private fun CommandLine.stringValue(option: Option, default: String) =
69 getOptionValue(option.opt) ?: default
71 private fun stringPathToPath(path: String) = Paths.get(File(path).toURI())
73 class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
78 fun printHelp(programName: String) {
79 val formatter = HelpFormatter()
80 formatter.printHelp(programName, options)
85 private val OPT_PORT = Option.builder("p")
86 .longOpt("listen-port")
91 private val OPT_CONFIG_URL = Option.builder("c")
92 .longOpt("config-url")
94 .desc("URL of ves configuration on consul")
97 private val OPT_PK_FILE = Option.builder("k")
98 .longOpt("private-key-file")
100 .desc("File with private key in PEM format")
103 private val OPT_CERT_FILE = Option.builder("e")
104 .longOpt("cert-file")
106 .desc("File with server certificate bundle")
109 private val OPT_TRUST_CERT_FILE = Option.builder("t")
110 .longOpt("trust-cert-file")
112 .desc("File with trusted certificate bundle for authenticating clients")
115 private val options by lazy {
116 val options = Options()
117 options.addOption(OPT_PORT)
118 options.addOption(OPT_CONFIG_URL)
119 options.addOption(OPT_PK_FILE)
120 options.addOption(OPT_CERT_FILE)
121 options.addOption(OPT_TRUST_CERT_FILE)