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.config
22 import org.apache.commons.cli.Option
23 import org.apache.commons.cli.Options
24 import org.apache.commons.cli.DefaultParser
25 import org.apache.commons.cli.CommandLine
26 import org.apache.commons.cli.HelpFormatter
28 import java.nio.file.Paths
31 internal object DefaultValues {
32 const val MESSAGES_AMOUNT = 1
33 const val PRIVATE_KEY_FILE = "/etc/ves-hv/client.key"
34 const val CERT_FILE = "/etc/ves-hv/client.crt"
35 const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
39 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
42 internal object ArgBasedClientConfiguration {
44 private val OPT_VES_PORT = Option.builder("p")
48 .desc("VesHvCollector port")
51 private val OPT_VES_HOST = Option.builder("h")
55 .desc("VesHvCollector host")
58 private val OPT_MESSAGES_AMOUNT = Option.builder("m")
61 .desc("Amount of messages to send")
64 private val OPT_PK_FILE = Option.builder("k")
65 .longOpt("private-key-file")
67 .desc("File with client private key in PEM format")
70 private val OPT_CERT_FILE = Option.builder("e")
73 .desc("File with client certificate bundle")
76 private val OPT_TRUST_CERT_FILE = Option.builder("t")
77 .longOpt("trust-cert-file")
79 .desc("File with trusted certificate bundle for trusting servers")
82 private val options by lazy {
83 val options = Options()
84 options.addOption(OPT_VES_PORT)
85 options.addOption(OPT_VES_HOST)
86 options.addOption(OPT_MESSAGES_AMOUNT)
87 options.addOption(OPT_PK_FILE)
88 options.addOption(OPT_CERT_FILE)
89 options.addOption(OPT_TRUST_CERT_FILE)
93 fun parse(args: Array<out String>): ClientConfiguration {
96 val parser = DefaultParser()
99 val cmdLine = parser.parse(options, args)
100 val host = cmdLine.stringValue(OPT_VES_HOST)
101 val port = cmdLine.intValue(OPT_VES_PORT)
102 val msgsAmount = cmdLine.intValueOrDefault(OPT_MESSAGES_AMOUNT, DefaultValues.MESSAGES_AMOUNT)
103 return ClientConfiguration(
106 parseSecurityConfig(cmdLine),
108 } catch (ex: Exception) {
109 throw WrongArgumentException(ex)
113 private fun parseSecurityConfig(cmdLine: CommandLine): ClientSecurityConfiguration {
114 val pkFile = cmdLine.stringValue(OPT_PK_FILE, DefaultValues.PRIVATE_KEY_FILE)
115 val certFile = cmdLine.stringValue(OPT_CERT_FILE, DefaultValues.CERT_FILE)
116 val trustCertFile = cmdLine.stringValue(OPT_TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
117 return ClientSecurityConfiguration(
118 privateKey = stringPathToPath(pkFile),
119 cert = stringPathToPath(certFile),
120 trustedCert = stringPathToPath(trustCertFile))
123 private fun stringPathToPath(path: String) = Paths.get(File(path).toURI())
125 private fun CommandLine.intValueOrDefault(option: Option, default: Int) =
126 getOptionValue(option.opt)?.toInt() ?: default
128 private fun CommandLine.intValue(option: Option) =
129 getOptionValue(option.opt).toInt()
131 private fun CommandLine.stringValue(option: Option) =
132 getOptionValue(option.opt)
134 private fun CommandLine.stringValue(option: Option, default: String) =
135 getOptionValue(option.opt) ?: default
138 class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
139 fun printHelp(programName: String) {
140 val formatter = HelpFormatter()
141 formatter.printHelp(programName, options)