5689a3e6b57be13bea2708195106a145484094f1
[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.SecurityConfiguration
24 import org.onap.dcae.collectors.veshv.domain.ServerConfiguration
25 import java.io.File
26 import java.nio.file.Paths
27
28 internal object DefaultValues {
29     const val PORT = 6061
30     const val CONFIG_URL = ""
31     const val PRIVATE_KEY_FILE = "/etc/ves-hv/server.key"
32     const val CERT_FILE = "/etc/ves-hv/server.crt"
33     const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
34 }
35
36 internal class ArgBasedServerConfiguration {
37
38     fun parse(args: Array<out String>): ServerConfiguration {
39         val parser = DefaultParser()
40
41         try {
42             val cmdLine = parser.parse(options, args)
43             val port = cmdLine.intValue(OPT_PORT, DefaultValues.PORT)
44             val configUrl = cmdLine.stringValue(OPT_CONFIG_URL, DefaultValues.CONFIG_URL)
45             val secConf = createSecurityConfiguration(cmdLine)
46             return ServerConfiguration(port, configUrl, secConf)
47         } catch (ex: Exception) {
48             throw WrongArgumentException(ex)
49         }
50     }
51
52     private fun createSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration {
53
54         val pkFile = cmdLine.stringValue(OPT_PK_FILE, DefaultValues.PRIVATE_KEY_FILE)
55         val certFile = cmdLine.stringValue(OPT_CERT_FILE, DefaultValues.CERT_FILE)
56         val trustCertFile = cmdLine.stringValue(OPT_TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
57
58         return SecurityConfiguration(
59                 privateKey = stringPathToPath(pkFile),
60                 cert = stringPathToPath(certFile),
61                 trustedCert = stringPathToPath(trustCertFile)
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     private fun stringPathToPath(path: String) = Paths.get(File(path).toURI())
72
73     class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
74         fun printMessage() {
75             println(message)
76         }
77
78         fun printHelp(programName: String) {
79             val formatter = HelpFormatter()
80             formatter.printHelp(programName, options)
81         }
82     }
83
84     companion object {
85         private val OPT_PORT = Option.builder("p")
86                 .longOpt("listen-port")
87                 .hasArg()
88                 .desc("Listen port")
89                 .build()
90
91         private val OPT_CONFIG_URL = Option.builder("c")
92                 .longOpt("config-url")
93                 .hasArg()
94                 .desc("URL of ves configuration on consul")
95                 .build()
96
97         private val OPT_PK_FILE = Option.builder("k")
98                 .longOpt("private-key-file")
99                 .hasArg()
100                 .desc("File with private key in PEM format")
101                 .build()
102
103         private val OPT_CERT_FILE = Option.builder("e")
104                 .longOpt("cert-file")
105                 .hasArg()
106                 .desc("File with server certificate bundle")
107                 .build()
108
109         private val OPT_TRUST_CERT_FILE = Option.builder("t")
110                 .longOpt("trust-cert-file")
111                 .hasArg()
112                 .desc("File with trusted certificate bundle for authenticating clients")
113                 .build()
114
115         private val options by lazy {
116             val options = Options()
117             options.addOption(OPT_PORT)
118             options.addOption(OPT_CONFIG_URL)
119             options.addOption(OPT_PK_FILE)
120             options.addOption(OPT_CERT_FILE)
121             options.addOption(OPT_TRUST_CERT_FILE)
122             options
123         }
124     }
125 }