59b91d7f4214266a778d10cb72891066cec9f5ce
[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
21
22 import org.apache.commons.cli.DefaultParser
23 import org.apache.commons.cli.CommandLine
24 import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration
25 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.LISTEN_PORT
26 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_CONFIG_URL
27 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.PRIVATE_KEY_FILE
28 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CERT_FILE
29 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_CERT_FILE
30 import org.onap.dcae.collectors.veshv.model.ServerConfiguration
31 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
32
33 internal object DefaultValues {
34     const val PORT = 6061
35     const val CONFIG_URL = ""
36     const val PRIVATE_KEY_FILE = "/etc/ves-hv/server.key"
37     const val CERT_FILE = "/etc/ves-hv/server.crt"
38     const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
39 }
40
41 internal class ArgBasedServerConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) {
42     override val cmdLineOptionsList = listOf(
43             LISTEN_PORT,
44             CONSUL_CONFIG_URL,
45             PRIVATE_KEY_FILE,
46             CERT_FILE,
47             TRUST_CERT_FILE
48     )
49
50     override fun getConfiguration(cmdLine: CommandLine): ServerConfiguration {
51         val port = cmdLine.intValue(LISTEN_PORT, DefaultValues.PORT)
52         val configUrl = cmdLine.stringValue(CONSUL_CONFIG_URL, DefaultValues.CONFIG_URL)
53         val security = createSecurityConfiguration(cmdLine)
54         return ServerConfiguration(port, configUrl, security)
55     }
56
57     private fun createSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration {
58         val pkFile = cmdLine.stringValue(PRIVATE_KEY_FILE, DefaultValues.PRIVATE_KEY_FILE)
59         val certFile = cmdLine.stringValue(CERT_FILE, DefaultValues.CERT_FILE)
60         val trustCertFile = cmdLine.stringValue(TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
61
62         return SecurityConfiguration(
63                 privateKey = stringPathToPath(pkFile),
64                 cert = stringPathToPath(certFile),
65                 trustedCert = stringPathToPath(trustCertFile)
66         )
67     }
68 }