1587dbcb6dd10b687e10d56f59313c4c03c0908b
[dcaegen2/collectors/hv-ves.git] /
1 /*
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
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
28
29 internal object DefaultValues {
30     const val MESSAGES_AMOUNT = 1
31 }
32
33 /**
34  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
35  * @since June 2018
36  */
37 internal object ArgBasedClientConfiguration {
38
39     private val OPT_VES_PORT = Option.builder("p")
40             .longOpt("port")
41             .required()
42             .hasArg()
43             .desc("VesHvCollector port")
44             .build()
45
46     private val OPT_VES_HOST = Option.builder("h")
47             .longOpt("host")
48             .required()
49             .hasArg()
50             .desc("VesHvCollector host")
51             .build()
52
53     private val OPT_MESSAGES_AMOUNT = Option.builder("m")
54             .longOpt("messages")
55             .hasArg()
56             .desc("Amount of messages to send")
57             .build()
58
59     private val options by lazy {
60         val options = Options()
61         options.addOption(OPT_VES_PORT)
62         options.addOption(OPT_VES_HOST)
63         options.addOption(OPT_MESSAGES_AMOUNT)
64         options
65     }
66
67     fun parse(args: Array<out String>): ClientConfiguration {
68         val parser = DefaultParser()
69
70         try {
71             parser.parse(options, args).run {
72                 return ClientConfiguration(
73                         stringValue(OPT_VES_HOST),
74                         intValue(OPT_VES_PORT),
75                         intValueOrDefault(OPT_MESSAGES_AMOUNT, DefaultValues.MESSAGES_AMOUNT))
76             }
77         } catch (ex: Exception) {
78             throw WrongArgumentException(ex)
79         }
80     }
81
82     private fun CommandLine.intValueOrDefault(option: Option, default: Int) =
83             getOptionValue(option.opt)?.toInt() ?: default
84
85     private fun CommandLine.intValue(option: Option) =
86             getOptionValue(option.opt).toInt()
87
88     private fun CommandLine.stringValue(option: Option) =
89             getOptionValue(option.opt)
90
91
92     class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
93         fun printMessage() {
94             println(message)
95         }
96
97         fun printHelp(programName: String) {
98             val formatter = HelpFormatter()
99             formatter.printHelp(programName, options)
100         }
101     }
102 }