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