4e614cdbf8dfed66747b3fdfb6290d0f85a3bb62
[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
21
22 import org.apache.commons.cli.*
23 import org.onap.dcae.collectors.veshv.domain.ServerConfiguration
24
25 internal object DefaultValues {
26     const val PORT = 8600
27     const val CONFIG_URL = ""
28 }
29
30 internal object ArgBasedServerConfiguration {
31     private val OPT_PORT = Option.builder("p")
32             .longOpt("listen-port")
33             .hasArg()
34             .desc("Listen port")
35             .build()
36
37     private val OPT_CONFIG_URL = Option.builder("c")
38             .longOpt("config-url")
39             .optionalArg(true)
40             .hasArg()
41             .desc("Url of ves configuration on consul")
42             .build()
43
44     private val options by lazy {
45         val options = Options()
46         options.addOption(OPT_PORT)
47         options.addOption(OPT_CONFIG_URL)
48         options
49     }
50
51     fun parse(args: Array<out String>): ServerConfiguration {
52         val parser = DefaultParser()
53
54         try {
55             parser.parse(options, args).run {
56                 return ServerConfiguration(
57                         stringValue(OPT_CONFIG_URL, DefaultValues.CONFIG_URL),
58                         intValue(OPT_PORT, DefaultValues.PORT))
59             }
60         } catch (ex: Exception) {
61             throw WrongArgumentException(ex)
62         }
63     }
64
65     private fun CommandLine.intValue(option: Option, default: Int) =
66             getOptionValue(option.opt)?.toInt() ?: default
67
68     private fun CommandLine.stringValue(option: Option, default: String) =
69             getOptionValue(option.opt) ?: default
70
71
72     class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
73         fun printMessage() {
74             println(message)
75         }
76
77         fun printHelp(programName: String) {
78             val formatter = HelpFormatter()
79             formatter.printHelp(programName, options)
80         }
81     }
82 }