diff options
Diffstat (limited to 'hv-collector-client-simulator')
6 files changed, 168 insertions, 142 deletions
diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgBasedClientConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgBasedClientConfiguration.kt index b946689f..6f53c91d 100644 --- a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgBasedClientConfiguration.kt +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgBasedClientConfiguration.kt @@ -19,126 +19,59 @@ */ package org.onap.dcae.collectors.veshv.simulators.xnf.config -import org.apache.commons.cli.CommandLine import org.apache.commons.cli.DefaultParser -import org.apache.commons.cli.HelpFormatter -import org.apache.commons.cli.Option -import org.apache.commons.cli.Options -import java.io.File -import java.nio.file.Paths +import org.apache.commons.cli.CommandLine +import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.VES_HV_PORT +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.VES_HV_HOST +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.MESSAGES_TO_SEND_AMOUNT +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.PRIVATE_KEY_FILE +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CERT_FILE +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.TRUST_CERT_FILE +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration -internal object DefaultValues { - const val MESSAGES_AMOUNT = -1L - const val PRIVATE_KEY_FILE = "/etc/ves-hv/client.key" - const val CERT_FILE = "/etc/ves-hv/client.crt" - const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt" -} /** * @author Jakub Dudycz <jakub.dudycz@nokia.com> * @since June 2018 */ -internal object ArgBasedClientConfiguration { - - private val OPT_VES_PORT = Option.builder("p") - .longOpt("ves-port") - .required() - .hasArg() - .desc("VesHvCollector port") - .build() - - private val OPT_VES_HOST = Option.builder("h") - .longOpt("ves-host") - .required() - .hasArg() - .desc("VesHvCollector host") - .build() - - private val OPT_MESSAGES_AMOUNT = Option.builder("m") - .longOpt("messages") - .hasArg() - .desc("Amount of messages to send") - .build() - - private val OPT_PK_FILE = Option.builder("k") - .longOpt("private-key-file") - .hasArg() - .desc("File with client private key in PEM format") - .build() - - private val OPT_CERT_FILE = Option.builder("e") - .longOpt("cert-file") - .hasArg() - .desc("File with client certificate bundle") - .build() - - private val OPT_TRUST_CERT_FILE = Option.builder("t") - .longOpt("trust-cert-file") - .hasArg() - .desc("File with trusted certificate bundle for trusting servers") - .build() - - private val options by lazy { - val options = Options() - options.addOption(OPT_VES_PORT) - options.addOption(OPT_VES_HOST) - options.addOption(OPT_MESSAGES_AMOUNT) - options.addOption(OPT_PK_FILE) - options.addOption(OPT_CERT_FILE) - options.addOption(OPT_TRUST_CERT_FILE) - options - } - fun parse(args: Array<out String>): ClientConfiguration { - - - val parser = DefaultParser() +internal object DefaultValues { + const val MESSAGES_AMOUNT = -1L + const val PRIVATE_KEY_FILE = "/etc/ves-hv/client.key" + const val CERT_FILE = "/etc/ves-hv/client.crt" + const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt" +} - try { - val cmdLine = parser.parse(options, args) - val host = cmdLine.stringValue(OPT_VES_HOST) - val port = cmdLine.intValue(OPT_VES_PORT) - val msgsAmount = cmdLine.longValueOrDefault(OPT_MESSAGES_AMOUNT, DefaultValues.MESSAGES_AMOUNT) - return ClientConfiguration( - host, - port, - parseSecurityConfig(cmdLine), - msgsAmount) - } catch (ex: Exception) { - throw WrongArgumentException(ex) - } +internal class ArgBasedClientConfiguration : ArgBasedConfiguration<ClientConfiguration>(DefaultParser()) { + override val cmdLineOptionsList = listOf( + VES_HV_PORT, + VES_HV_HOST, + MESSAGES_TO_SEND_AMOUNT, + PRIVATE_KEY_FILE, + CERT_FILE, + TRUST_CERT_FILE + ) + + override fun getConfiguration(cmdLine: CommandLine): ClientConfiguration { + val host = cmdLine.stringValue(VES_HV_HOST) + val port = cmdLine.intValue(VES_HV_PORT) + val messagesAmount = cmdLine.longValue(MESSAGES_TO_SEND_AMOUNT, DefaultValues.MESSAGES_AMOUNT) + return ClientConfiguration( + host, + port, + parseSecurityConfig(cmdLine), + messagesAmount) } - private fun parseSecurityConfig(cmdLine: CommandLine): ClientSecurityConfiguration { - val pkFile = cmdLine.stringValue(OPT_PK_FILE, DefaultValues.PRIVATE_KEY_FILE) - val certFile = cmdLine.stringValue(OPT_CERT_FILE, DefaultValues.CERT_FILE) - val trustCertFile = cmdLine.stringValue(OPT_TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE) - return ClientSecurityConfiguration( + private fun parseSecurityConfig(cmdLine: CommandLine): SecurityConfiguration { + val pkFile = cmdLine.stringValue(PRIVATE_KEY_FILE, DefaultValues.PRIVATE_KEY_FILE) + val certFile = cmdLine.stringValue(CERT_FILE, DefaultValues.CERT_FILE) + val trustCertFile = cmdLine.stringValue(TRUST_CERT_FILE, DefaultValues.TRUST_CERT_FILE) + return SecurityConfiguration( privateKey = stringPathToPath(pkFile), cert = stringPathToPath(certFile), trustedCert = stringPathToPath(trustCertFile)) } - private fun stringPathToPath(path: String) = Paths.get(File(path).toURI()) - - - private fun CommandLine.longValueOrDefault(option: Option, default: Long) = - getOptionValue(option.opt)?.toLong() ?: default - - private fun CommandLine.intValue(option: Option) = - getOptionValue(option.opt).toInt() - - private fun CommandLine.stringValue(option: Option) = - getOptionValue(option.opt) - - private fun CommandLine.stringValue(option: Option, default: String) = - getOptionValue(option.opt) ?: default - - - class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) { - fun printHelp(programName: String) { - val formatter = HelpFormatter() - formatter.printHelp(programName, options) - } - } } diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientConfiguration.kt index 83d6f7c0..ed96e6c3 100644 --- a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientConfiguration.kt +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientConfiguration.kt @@ -19,6 +19,8 @@ */ package org.onap.dcae.collectors.veshv.simulators.xnf.config +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration + /** * @author Jakub Dudycz <jakub.dudycz@nokia.com> * @since June 2018 @@ -26,5 +28,5 @@ package org.onap.dcae.collectors.veshv.simulators.xnf.config data class ClientConfiguration( val vesHost: String, val vesPort: Int, - val security: ClientSecurityConfiguration, + val security: SecurityConfiguration, val messagesAmount: Long) diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt deleted file mode 100644 index bb4f306a..00000000 --- a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt +++ /dev/null @@ -1,31 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * dcaegen2-collectors-veshv - * ================================================================================ - * Copyright (C) 2018 NOKIA - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.dcae.collectors.veshv.simulators.xnf.config - -import java.nio.file.Path - -/** - * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> - * @since June 2018 - */ -data class ClientSecurityConfiguration( - val privateKey: Path, - val cert: Path, - val trustedCert: Path) diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/VesHvClient.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/VesHvClient.kt index cb56db91..13256c52 100644 --- a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/VesHvClient.kt +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/impl/VesHvClient.kt @@ -25,9 +25,9 @@ import io.netty.handler.ssl.SslContext import io.netty.handler.ssl.SslContextBuilder import io.netty.handler.ssl.SslProvider import org.onap.dcae.collectors.veshv.domain.WireFrame +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration import org.onap.dcae.collectors.veshv.domain.WireFrameEncoder import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientConfiguration -import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientSecurityConfiguration import org.onap.dcae.collectors.veshv.utils.logging.Logger import org.reactivestreams.Publisher import reactor.core.publisher.Flux @@ -76,7 +76,7 @@ class VesHvClient(configuration: ClientConfiguration) { .send(frames) } - private fun createSslContext(config: ClientSecurityConfiguration): SslContext = + private fun createSslContext(config: SecurityConfiguration): SslContext = SslContextBuilder.forClient() .keyManager(config.cert.toFile(), config.privateKey.toFile()) .trustManager(config.trustedCert.toFile()) diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt index 68f999ef..3fa023bf 100644 --- a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt +++ b/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt @@ -22,6 +22,7 @@ package org.onap.dcae.collectors.veshv.simulators.xnf import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgBasedClientConfiguration import org.onap.dcae.collectors.veshv.simulators.xnf.impl.MessageFactory import org.onap.dcae.collectors.veshv.simulators.xnf.impl.VesHvClient +import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentException import org.slf4j.LoggerFactory.getLogger @@ -33,11 +34,11 @@ private val logger = getLogger("Simulator :: main") */ fun main(args: Array<String>) { try { - val clientConfig = ArgBasedClientConfiguration.parse(args) + val clientConfig = ArgBasedClientConfiguration().parse(args) val messageFactory = MessageFactory() val client = VesHvClient(clientConfig) client.send(messageFactory.createMessageFlux(clientConfig.messagesAmount)) - } catch (e: ArgBasedClientConfiguration.WrongArgumentException) { + } catch (e: WrongArgumentException) { e.printHelp("java org.onap.dcae.collectors.veshv.main.MainKt") System.exit(1) } catch (e: Exception) { diff --git a/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgBasedClientConfigurationTest.kt b/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgBasedClientConfigurationTest.kt new file mode 100644 index 00000000..6420d84d --- /dev/null +++ b/hv-collector-client-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgBasedClientConfigurationTest.kt @@ -0,0 +1,121 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.main.config + +import org.assertj.core.api.Assertions.assertThat +import org.jetbrains.spek.api.Spek +import org.jetbrains.spek.api.dsl.describe +import org.jetbrains.spek.api.dsl.given +import org.jetbrains.spek.api.dsl.it +import org.jetbrains.spek.api.dsl.on +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgBasedClientConfiguration +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ClientConfiguration +import org.onap.dcae.collectors.veshv.simulators.xnf.config.DefaultValues +import java.nio.file.Paths + + +object ArgBasedClientConfigurationTest : Spek({ + lateinit var cut: ArgBasedClientConfiguration + val messagesAmount = 3L + val vesHost = "localhosting" + val pk = Paths.get("/", "etc", "ves", "pk.pem") + val cert = Paths.get("/", "etc", "ssl", "certs", "ca-bundle.crt") + val trustCert = Paths.get("/", "etc", "ves", "trusted.crt") + + beforeEachTest { + cut = ArgBasedClientConfiguration() + } + + fun parse(vararg cmdLine: String) = cut.parse(cmdLine) + + describe("parsing arguments") { + lateinit var result: ClientConfiguration + + given("all parameters are present in the long form") { + + beforeEachTest { + result = parse("--ves-port", "6969", + "--ves-host", vesHost, + "--messages", messagesAmount.toString(), + "--private-key-file", pk.toFile().absolutePath, + "--cert-file", cert.toFile().absolutePath, + "--trust-cert-file", trustCert.toFile().absolutePath) + } + + it("should set proper port") { + assertThat(result.vesPort).isEqualTo(6969) + } + + + it("should set proper config url") { + assertThat(result.messagesAmount).isEqualTo(messagesAmount) + } + + it("should set proper security configuration") { + assertThat(result.security).isEqualTo( + SecurityConfiguration(pk, cert, trustCert) + ) + } + } + + given("some parameters are present in the short form") { + + beforeEachTest { + result = parse("-h", "ves-hv", "--ves-port", "666", "-m", messagesAmount.toString()) + } + + it("should set proper port") { + assertThat(result.vesPort).isEqualTo(666) + } + + it("should set proper messages amount") { + assertThat(result.messagesAmount).isEqualTo(messagesAmount) + } + } + + given("all optional parameters are absent") { + + beforeEachTest { + result = parse("-h", "ves-hv", "-p", "666") + } + + it("should set default messages amount") { + assertThat(result.messagesAmount).isEqualTo(DefaultValues.MESSAGES_AMOUNT) + } + + on("security config") { + val securityConfiguration = result.security + + it("should set default trust cert file") { + assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE) + } + + it("should set default server cert file") { + assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE) + } + + it("should set default private key file") { + assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE) + } + } + } + } +}) |