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.simulators.xnf.config
22 import org.apache.commons.cli.CommandLine
23 import org.apache.commons.cli.DefaultParser
24 import org.apache.commons.cli.HelpFormatter
25 import org.apache.commons.cli.Option
26 import org.apache.commons.cli.Options
28 import java.nio.file.Paths
30 internal object DefaultValues {
31 const val MESSAGES_AMOUNT = 1
32 const val PRIVATE_KEY_FILE = "/etc/ves-hv/client.key"
33 const val CERT_FILE = "/etc/ves-hv/client.crt"
34 const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
38 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
41 internal object ArgBasedClientConfiguration {
43 private val OPT_VES_PORT = Option.builder("p")
47 .desc("VesHvCollector port")
50 private val OPT_VES_HOST = Option.builder("h")
54 .desc("VesHvCollector host")
57 private val OPT_MESSAGES_AMOUNT = Option.builder("m")
60 .desc("Amount of messages to send")
63 private val OPT_PK_FILE = Option.builder("k")
64 .longOpt("private-key-file")
66 .desc("File with client private key in PEM format")
69 private val OPT_CERT_FILE = Option.builder("e")
72 .desc("File with client certificate bundle")
75 private val OPT_TRUST_CERT_FILE = Option.builder("t")
76 .longOpt("trust-cert-file")
78 .desc("File with trusted certificate bundle for trusting servers")
81 private val options by lazy {
82 val options = Options()
83 options.addOption(OPT_VES_PORT)
84 options.addOption(OPT_VES_HOST)
85 options.addOption(OPT_MESSAGES_AMOUNT)
86 options.addOption(OPT_PK_FILE)
87 options.addOption(OPT_CERT_FILE)
88 options.addOption(OPT_TRUST_CERT_FILE)
92 fun parse(args: Array<out String>): ClientConfiguration {
95 val parser = DefaultParser()
98 val cmdLine = parser.parse(options, args)
99 val host = cmdLine.stringValue(OPT_VES_HOST)
100 val port = cmdLine.intValue(OPT_VES_PORT)
101 val msgsAmount = cmdLine.intValueOrDefault(OPT_MESSAGES_AMOUNT, DefaultValues.MESSAGES_AMOUNT)
102 return ClientConfiguration(
105 parseSecurityConfig(cmdLine),
107 } catch (ex: Exception) {
108 throw WrongArgumentException(ex)
112 private fun parseSecurityConfig(cmdLine: CommandLine): ClientSecurityConfiguration {
113 val pkFile = cmdLine.stringValue(OPT_PK_FILE, DefaultValues.PRIVATE_KEY_FILE)
114 val certFile = cmdLine.stringValue(OPT_CERT_FILE, DefaultValues.CERT_FILE)
115 val trustCertFile = cmdLine.stringValue(OPT_TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
116 return ClientSecurityConfiguration(
117 privateKey = stringPathToPath(pkFile),
118 cert = stringPathToPath(certFile),
119 trustedCert = stringPathToPath(trustCertFile))
122 private fun stringPathToPath(path: String) = Paths.get(File(path).toURI())
124 private fun CommandLine.intValueOrDefault(option: Option, default: Int) =
125 getOptionValue(option.opt)?.toInt() ?: default
127 private fun CommandLine.intValue(option: Option) =
128 getOptionValue(option.opt).toInt()
130 private fun CommandLine.stringValue(option: Option) =
131 getOptionValue(option.opt)
133 private fun CommandLine.stringValue(option: Option, default: String) =
134 getOptionValue(option.opt) ?: default
137 class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
138 fun printHelp(programName: String) {
139 val formatter = HelpFormatter()
140 formatter.printHelp(programName, options)