b8a4b888674f1323d3c412fdbc24a5468c8e25d9
[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.simulators.xnf.config
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.VES_HV_PORT
26 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.VES_HV_HOST
27 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.MESSAGES_TO_SEND_AMOUNT
28 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.PRIVATE_KEY_FILE
29 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CERT_FILE
30 import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_CERT_FILE
31 import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration
32
33
34 /**
35  * @author Jakub Dudycz <jakub.dudycz@nokia.com>
36  * @since June 2018
37  */
38 internal object DefaultValues {
39     const val MESSAGES_AMOUNT = -1L
40     const val PRIVATE_KEY_FILE = "/etc/ves-hv/client.key"
41     const val CERT_FILE = "/etc/ves-hv/client.crt"
42     const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt"
43 }
44
45 internal class ArgBasedClientConfiguration : ArgBasedConfiguration<ClientConfiguration>(DefaultParser()) {
46     override val cmdLineOptionsList = listOf(
47             VES_HV_PORT,
48             VES_HV_HOST,
49             MESSAGES_TO_SEND_AMOUNT,
50             PRIVATE_KEY_FILE,
51             CERT_FILE,
52             TRUST_CERT_FILE
53     )
54
55     override fun getConfiguration(cmdLine: CommandLine): ClientConfiguration {
56         val host = cmdLine.stringValue(VES_HV_HOST)
57         val port = cmdLine.intValue(VES_HV_PORT)
58         val messagesAmount = cmdLine.longValue(MESSAGES_TO_SEND_AMOUNT, DefaultValues.MESSAGES_AMOUNT)
59         return ClientConfiguration(
60                 host,
61                 port,
62                 parseSecurityConfig(cmdLine),
63                 messagesAmount)
64     }
65
66     private fun parseSecurityConfig(cmdLine: CommandLine): SecurityConfiguration {
67         val pkFile = cmdLine.stringValue(PRIVATE_KEY_FILE, DefaultValues.PRIVATE_KEY_FILE)
68         val certFile = cmdLine.stringValue(CERT_FILE, DefaultValues.CERT_FILE)
69         val trustCertFile = cmdLine.stringValue(TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE)
70         return SecurityConfiguration(
71                 privateKey = stringPathToPath(pkFile),
72                 cert = stringPathToPath(certFile),
73                 trustedCert = stringPathToPath(trustCertFile))
74     }
75
76 }