diff options
author | fkrzywka <filip.krzywka@nokia.com> | 2018-06-13 10:02:03 +0200 |
---|---|---|
committer | Piotr Jaszczyk <piotr.jaszczyk@nokia.com> | 2018-08-02 08:13:46 +0200 |
commit | 32aeb329986b3d6b9671c0a9e555cbff43964b3a (patch) | |
tree | 355bb884e579b27719f3b98e7aa61f7cb7131193 | |
parent | 1e77afdda9c9e89a313dec034c51f6cd0e407814 (diff) |
Refactor ArgBasedConfiguration
Extracted duplicate code from Client/Server ArgBasedConfiguration, as it
probably would be used third time in future work
Change-Id: I581abbcd5f1dd4a1a049e1d28e68a7e4d82a84f8
Signed-off-by: fkrzywka <filip.krzywka@nokia.com>
Issue-ID: DCAEGEN2-601
19 files changed, 440 insertions, 298 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/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) + } + } + } + } +}) diff --git a/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/NettyTcpServer.kt b/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/NettyTcpServer.kt index 564aa8df..65b3b29e 100644 --- a/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/NettyTcpServer.kt +++ b/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/NettyTcpServer.kt @@ -19,7 +19,6 @@ */ package org.onap.dcae.collectors.veshv.impl.socket -import io.netty.buffer.ByteBuf import org.onap.dcae.collectors.veshv.boundary.CollectorProvider import org.onap.dcae.collectors.veshv.boundary.Server import org.onap.dcae.collectors.veshv.model.ServerConfiguration diff --git a/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/SslContextFactory.kt b/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/SslContextFactory.kt index 950e2c86..b6fb1cf8 100644 --- a/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/SslContextFactory.kt +++ b/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/impl/socket/SslContextFactory.kt @@ -23,7 +23,7 @@ import io.netty.handler.ssl.ClientAuth import io.netty.handler.ssl.SslContext import io.netty.handler.ssl.SslContextBuilder import io.netty.handler.ssl.SslProvider -import org.onap.dcae.collectors.veshv.model.SecurityConfiguration +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration internal open class SslContextFactory { diff --git a/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/model/ServerConfiguration.kt b/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/model/ServerConfiguration.kt index f612cc75..8d01c075 100644 --- a/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/model/ServerConfiguration.kt +++ b/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/model/ServerConfiguration.kt @@ -19,6 +19,8 @@ */ package org.onap.dcae.collectors.veshv.model +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration + /** * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> * @since May 2018 diff --git a/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/socket/SslContextFactoryTest.kt b/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/socket/SslContextFactoryTest.kt index dcd4f412..26a25d33 100644 --- a/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/socket/SslContextFactoryTest.kt +++ b/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/socket/SslContextFactoryTest.kt @@ -27,7 +27,7 @@ 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.it -import org.onap.dcae.collectors.veshv.model.SecurityConfiguration +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration import java.nio.file.Paths /** diff --git a/hv-collector-ct/pom.xml b/hv-collector-ct/pom.xml index 2a10e8fb..1db0345c 100644 --- a/hv-collector-ct/pom.xml +++ b/hv-collector-ct/pom.xml @@ -73,10 +73,6 @@ <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> - <dependency> - <groupId>commons-cli</groupId> - <artifactId>commons-cli</artifactId> - </dependency> <dependency> <groupId>org.assertj</groupId> diff --git a/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/model/SecurityConfiguration.kt b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/SecurityConfiguration.kt index 9a10801a..6f28b6e9 100644 --- a/hv-collector-core/src/main/kotlin/org/onap/dcae/collectors/veshv/model/SecurityConfiguration.kt +++ b/hv-collector-domain/src/main/kotlin/org/onap/dcae/collectors/veshv/domain/SecurityConfiguration.kt @@ -17,7 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.dcae.collectors.veshv.model +package org.onap.dcae.collectors.veshv.domain import java.nio.file.Path diff --git a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt index 91d29106..59b91d7f 100644 --- a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt +++ b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfiguration.kt @@ -19,11 +19,16 @@ */ package org.onap.dcae.collectors.veshv.main -import org.apache.commons.cli.* +import org.apache.commons.cli.DefaultParser +import org.apache.commons.cli.CommandLine +import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.LISTEN_PORT +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.CONSUL_CONFIG_URL +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.model.ServerConfiguration -import org.onap.dcae.collectors.veshv.model.SecurityConfiguration -import java.io.File -import java.nio.file.Paths +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration internal object DefaultValues { const val PORT = 6061 @@ -33,27 +38,26 @@ internal object DefaultValues { const val TRUST_CERT_FILE = "/etc/ves-hv/trust.crt" } -internal class ArgBasedServerConfiguration { - - fun parse(args: Array<out String>): ServerConfiguration { - val parser = DefaultParser() - - try { - val cmdLine = parser.parse(options, args) - val port = cmdLine.intValue(OPT_PORT, DefaultValues.PORT) - val configUrl = cmdLine.stringValue(OPT_CONFIG_URL, DefaultValues.CONFIG_URL) - val secConf = createSecurityConfiguration(cmdLine) - return ServerConfiguration(port, configUrl, secConf) - } catch (ex: Exception) { - throw WrongArgumentException(ex) - } +internal class ArgBasedServerConfiguration : ArgBasedConfiguration<ServerConfiguration>(DefaultParser()) { + override val cmdLineOptionsList = listOf( + LISTEN_PORT, + CONSUL_CONFIG_URL, + PRIVATE_KEY_FILE, + CERT_FILE, + TRUST_CERT_FILE + ) + + override fun getConfiguration(cmdLine: CommandLine): ServerConfiguration { + val port = cmdLine.intValue(LISTEN_PORT, DefaultValues.PORT) + val configUrl = cmdLine.stringValue(CONSUL_CONFIG_URL, DefaultValues.CONFIG_URL) + val security = createSecurityConfiguration(cmdLine) + return ServerConfiguration(port, configUrl, security) } private fun createSecurityConfiguration(cmdLine: CommandLine): SecurityConfiguration { - - 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) + 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), @@ -61,65 +65,4 @@ internal class ArgBasedServerConfiguration { trustedCert = stringPathToPath(trustCertFile) ) } - - private fun CommandLine.intValue(option: Option, default: Int) = - getOptionValue(option.opt)?.toInt() ?: default - - private fun CommandLine.stringValue(option: Option, default: String) = - getOptionValue(option.opt) ?: default - - private fun stringPathToPath(path: String) = Paths.get(File(path).toURI()) - - class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) { - fun printMessage() { - println(message) - } - - fun printHelp(programName: String) { - val formatter = HelpFormatter() - formatter.printHelp(programName, options) - } - } - - companion object { - private val OPT_PORT = Option.builder("p") - .longOpt("listen-port") - .hasArg() - .desc("Listen port") - .build() - - private val OPT_CONFIG_URL = Option.builder("c") - .longOpt("config-url") - .hasArg() - .desc("URL of ves configuration on consul") - .build() - - private val OPT_PK_FILE = Option.builder("k") - .longOpt("private-key-file") - .hasArg() - .desc("File with private key in PEM format") - .build() - - private val OPT_CERT_FILE = Option.builder("e") - .longOpt("cert-file") - .hasArg() - .desc("File with server certificate bundle") - .build() - - private val OPT_TRUST_CERT_FILE = Option.builder("t") - .longOpt("trust-cert-file") - .hasArg() - .desc("File with trusted certificate bundle for authenticating clients") - .build() - - private val options by lazy { - val options = Options() - options.addOption(OPT_PORT) - options.addOption(OPT_CONFIG_URL) - options.addOption(OPT_PK_FILE) - options.addOption(OPT_CERT_FILE) - options.addOption(OPT_TRUST_CERT_FILE) - options - } - } } diff --git a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt index b2f4633a..b7d97028 100644 --- a/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt +++ b/hv-collector-main/src/main/kotlin/org/onap/dcae/collectors/veshv/main/main.kt @@ -20,13 +20,13 @@ package org.onap.dcae.collectors.veshv.main import org.onap.dcae.collectors.veshv.boundary.ConfigurationProvider -import org.onap.dcae.collectors.veshv.factory.CollectorFactory -import org.onap.dcae.collectors.veshv.factory.ServerFactory -import org.onap.dcae.collectors.veshv.impl.adapters.AdapterFactory -import org.onap.dcae.collectors.veshv.main.ArgBasedServerConfiguration.WrongArgumentException +import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentException import org.onap.dcae.collectors.veshv.model.CollectorConfiguration import org.onap.dcae.collectors.veshv.model.ServerConfiguration import org.onap.dcae.collectors.veshv.model.routing +import org.onap.dcae.collectors.veshv.factory.CollectorFactory +import org.onap.dcae.collectors.veshv.factory.ServerFactory +import org.onap.dcae.collectors.veshv.impl.adapters.AdapterFactory import org.onap.ves.VesEventV5.VesEvent.CommonEventHeader.Domain import org.slf4j.LoggerFactory import kotlin.system.exitProcess diff --git a/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt b/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt index 38845945..923f9d58 100644 --- a/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt +++ b/hv-collector-main/src/test/kotlin/org/onap/dcae/collectors/veshv/main/ArgBasedServerConfigurationTest.kt @@ -25,7 +25,7 @@ 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.model.SecurityConfiguration +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration import org.onap.dcae.collectors.veshv.model.ServerConfiguration import java.nio.file.Paths @@ -62,7 +62,6 @@ object ArgBasedServerConfigurationTest : Spek({ assertThat(result.port).isEqualTo(6969) } - it("should set proper config url") { assertThat(result.configurationUrl).isEqualTo(configurationUrl) } @@ -106,18 +105,18 @@ object ArgBasedServerConfigurationTest : Spek({ } on("security config") { - val secConf = result.securityConfiguration + val securityConfiguration = result.securityConfiguration it("should set default trust cert file") { - assertThat(secConf.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE) + assertThat(securityConfiguration.trustedCert.toString()).isEqualTo(DefaultValues.TRUST_CERT_FILE) } it("should set default server cert file") { - assertThat(secConf.cert.toString()).isEqualTo(DefaultValues.CERT_FILE) + assertThat(securityConfiguration.cert.toString()).isEqualTo(DefaultValues.CERT_FILE) } it("should set default private key file") { - assertThat(secConf.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE) + assertThat(securityConfiguration.privateKey.toString()).isEqualTo(DefaultValues.PRIVATE_KEY_FILE) } } } diff --git a/hv-collector-utils/pom.xml b/hv-collector-utils/pom.xml index 2adf21a2..8a8a1d8c 100644 --- a/hv-collector-utils/pom.xml +++ b/hv-collector-utils/pom.xml @@ -19,88 +19,92 @@ ~ ============LICENSE_END========================================================= --> <project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <licenses> - <license> - <name>The Apache Software License, Version 2.0</name> - <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> - </license> - </licenses> + <licenses> + <license> + <name>The Apache Software License, Version 2.0</name> + <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> + </license> + </licenses> - <properties> - <skipAnalysis>false</skipAnalysis> - </properties> + <properties> + <skipAnalysis>false</skipAnalysis> + </properties> - <parent> - <groupId>org.onap.dcaegen2.collectors.veshv</groupId> - <artifactId>ves-hv-collector</artifactId> - <version>1.0.0-SNAPSHOT</version> - <relativePath>..</relativePath> - </parent> + <parent> + <groupId>org.onap.dcaegen2.collectors.veshv</groupId> + <artifactId>ves-hv-collector</artifactId> + <version>1.0.0-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> - <artifactId>hv-collector-utils</artifactId> - <description>VES HighVolume Collector :: Utilities</description> + <artifactId>hv-collector-utils</artifactId> + <description>VES HighVolume Collector :: Utilities</description> - <build> - <plugins> - <plugin> - <artifactId>kotlin-maven-plugin</artifactId> - <groupId>org.jetbrains.kotlin</groupId> - </plugin> - <plugin> - <artifactId>maven-surefire-plugin</artifactId> - <groupId>org.apache.maven.plugins</groupId> - </plugin> - </plugins> - </build> + <build> + <plugins> + <plugin> + <artifactId>kotlin-maven-plugin</artifactId> + <groupId>org.jetbrains.kotlin</groupId> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <groupId>org.apache.maven.plugins</groupId> + </plugin> + </plugins> + </build> - <dependencies> - <dependency> - <groupId>org.jetbrains.kotlin</groupId> - <artifactId>kotlin-reflect</artifactId> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> - </dependency> - <dependency> - <groupId>com.nhaarman</groupId> - <artifactId>mockito-kotlin</artifactId> - </dependency> - <dependency> - <groupId>org.mockito</groupId> - <artifactId>mockito-core</artifactId> - </dependency> - <dependency> - <groupId>org.assertj</groupId> - <artifactId>assertj-core</artifactId> - </dependency> - <dependency> - <groupId>org.jetbrains.kotlin</groupId> - <artifactId>kotlin-test</artifactId> - </dependency> - <dependency> - <groupId>org.jetbrains.spek</groupId> - <artifactId>spek-api</artifactId> - </dependency> - <dependency> - <groupId>org.jetbrains.spek</groupId> - <artifactId>spek-junit-platform-engine</artifactId> - </dependency> - <dependency> - <groupId>io.projectreactor</groupId> - <artifactId>reactor-test</artifactId> - </dependency> - <dependency> - <groupId>ch.qos.logback</groupId> - <artifactId>logback-classic</artifactId> - <scope>test</scope> - </dependency> - </dependencies> + <dependencies> + <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-reflect</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>com.nhaarman</groupId> + <artifactId>mockito-kotlin</artifactId> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-test</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.spek</groupId> + <artifactId>spek-api</artifactId> + </dependency> + <dependency> + <groupId>org.jetbrains.spek</groupId> + <artifactId>spek-junit-platform-engine</artifactId> + </dependency> + <dependency> + <groupId>io.projectreactor</groupId> + <artifactId>reactor-test</artifactId> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <scope>test</scope> + </dependency> + </dependencies> </project>
\ No newline at end of file diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt new file mode 100644 index 00000000..968c340f --- /dev/null +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt @@ -0,0 +1,62 @@ +/* + * ============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.utils.commandline + +import org.apache.commons.cli.CommandLine +import org.apache.commons.cli.CommandLineParser +import org.apache.commons.cli.Options +import java.io.File +import java.nio.file.Paths + +abstract class ArgBasedConfiguration<T>(val parser: CommandLineParser) { + abstract val cmdLineOptionsList: List<CommandLineOption> + + fun parse(args: Array<out String>): T { + val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption) + try { + val cmdLine = parser.parse(commandLineOptions, args) + return getConfiguration(cmdLine) + } catch (ex: Exception) { + throw WrongArgumentException(ex, commandLineOptions) + } + } + + protected abstract fun getConfiguration(cmdLine: CommandLine): T + + protected fun CommandLine.intValue(cmdLineOpt: CommandLineOption): Int = + getOptionValue(cmdLineOpt.option.opt).toInt() + + protected fun CommandLine.intValue(cmdLineOpt: CommandLineOption, default: Int): Int = + getOptionValue(cmdLineOpt.option.opt)?.toInt() ?: default + + protected fun CommandLine.longValue(cmdLineOpt: CommandLineOption): Long = + getOptionValue(cmdLineOpt.option.opt).toLong() + + protected fun CommandLine.longValue(cmdLineOpt: CommandLineOption, default: Long): Long = + getOptionValue(cmdLineOpt.option.opt)?.toLong() ?: default + + protected fun CommandLine.stringValue(cmdLineOpt: CommandLineOption): String = + getOptionValue(cmdLineOpt.option.opt) + + protected fun CommandLine.stringValue(cmdLineOpt: CommandLineOption, default: String): String = + getOptionValue(cmdLineOpt.option.opt) ?: default + + protected fun stringPathToPath(path: String) = Paths.get(File(path).toURI()) +} diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt new file mode 100644 index 00000000..d36f194d --- /dev/null +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt @@ -0,0 +1,76 @@ +/* + * ============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.utils.commandline + +import org.apache.commons.cli.Option + + +enum class CommandLineOption(val option: Option) { + LISTEN_PORT(Option.builder("p") + .longOpt("listen-port") + .hasArg() + .desc("Listen port") + .build() + ), + CONSUL_CONFIG_URL(Option.builder("c") + .longOpt("config-url") + .hasArg() + .desc("URL of ves configuration on consul") + .build() + ), + VES_HV_PORT(Option.builder("p") + .longOpt("ves-port") + .required() + .hasArg() + .desc("VesHvCollector port") + .build() + ), + VES_HV_HOST(Option.builder("h") + .longOpt("ves-host") + .required() + .hasArg() + .desc("VesHvCollector host") + .build() + ), + MESSAGES_TO_SEND_AMOUNT(Option.builder("m") + .longOpt("messages") + .hasArg() + .desc("Amount of messages to send") + .build() + ), + PRIVATE_KEY_FILE(Option.builder("k") + .longOpt("private-key-file") + .hasArg() + .desc("File with private key in PEM format") + .build() + ), + CERT_FILE(Option.builder("e") + .longOpt("cert-file") + .hasArg() + .desc("File with certificate bundle") + .build() + ), + TRUST_CERT_FILE(Option.builder("t") + .longOpt("trust-cert-file") + .hasArg() + .desc("File with trusted certificate bundle for trusting connections") + .build() + ), +} diff --git a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentException.kt index bb4f306a..5f6a86ad 100644 --- a/hv-collector-client-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ClientSecurityConfiguration.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentException.kt @@ -17,15 +17,19 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.dcae.collectors.veshv.simulators.xnf.config +package org.onap.dcae.collectors.veshv.utils.commandline -import java.nio.file.Path +import org.apache.commons.cli.HelpFormatter +import org.apache.commons.cli.Options -/** - * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> - * @since June 2018 - */ -data class ClientSecurityConfiguration( - val privateKey: Path, - val cert: Path, - val trustedCert: Path) + +class WrongArgumentException(parent: Exception, private val options: Options) : Exception(parent.message, parent) { + fun printMessage() { + println(message) + } + + fun printHelp(programName: String) { + val formatter = HelpFormatter() + formatter.printHelp(programName, options) + } +} diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt index eb52a866..314c9bf9 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt @@ -107,4 +107,4 @@ class Logger(val logger: org.slf4j.Logger) { logger.warn(messageProvider(), ex) } } -}
\ No newline at end of file +} |