49653b57b68020845cfbe109b4609498abefc3c3
[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.simulators.xnf.config
21
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
27 import java.io.File
28 import java.nio.file.Paths
29
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"
35 }
36
37 /**
38  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
39  * @since June 2018
40  */
41 internal object ArgBasedClientConfiguration {
42
43     private val OPT_VES_PORT = Option.builder("p")
44             .longOpt("ves-port")
45             .required()
46             .hasArg()
47             .desc("VesHvCollector port")
48             .build()
49
50     private val OPT_VES_HOST = Option.builder("h")
51             .longOpt("ves-host")
52             .required()
53             .hasArg()
54             .desc("VesHvCollector host")
55             .build()
56
57     private val OPT_MESSAGES_AMOUNT = Option.builder("m")
58             .longOpt("messages")
59             .hasArg()
60             .desc("Amount of messages to send")
61             .build()
62
63     private val OPT_PK_FILE = Option.builder("k")
64             .longOpt("private-key-file")
65             .hasArg()
66             .desc("File with client private key in PEM format")
67             .build()
68
69     private val OPT_CERT_FILE = Option.builder("e")
70             .longOpt("cert-file")
71             .hasArg()
72             .desc("File with client certificate bundle")
73             .build()
74
75     private val OPT_TRUST_CERT_FILE = Option.builder("t")
76             .longOpt("trust-cert-file")
77             .hasArg()
78             .desc("File with trusted certificate bundle for trusting servers")
79             .build()
80
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)
89         options
90     }
91
92     fun parse(args: Array<out String>): ClientConfiguration {
93
94
95         val parser = DefaultParser()
96
97         try {
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(
103                     host,
104                     port,
105                     parseSecurityConfig(cmdLine),
106                     msgsAmount)
107         } catch (ex: Exception) {
108             throw WrongArgumentException(ex)
109         }
110     }
111
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))
120     }
121
122     private fun stringPathToPath(path: String) = Paths.get(File(path).toURI())
123
124     private fun CommandLine.intValueOrDefault(option: Option, default: Int) =
125             getOptionValue(option.opt)?.toInt() ?: default
126
127     private fun CommandLine.intValue(option: Option) =
128             getOptionValue(option.opt).toInt()
129
130     private fun CommandLine.stringValue(option: Option) =
131             getOptionValue(option.opt)
132
133     private fun CommandLine.stringValue(option: Option, default: String) =
134             getOptionValue(option.opt) ?: default
135
136
137     class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
138         fun printHelp(programName: String) {
139             val formatter = HelpFormatter()
140             formatter.printHelp(programName, options)
141         }
142     }
143 }