From fc6ab3e5fee2bc3e607848caa665b166d6f38dd6 Mon Sep 17 00:00:00 2001 From: Jakub Dudycz Date: Tue, 24 Jul 2018 11:48:12 +0200 Subject: Rework argument configuration - Unify names of argument configuration classes in DCAE APP simulator, XNF simualtor and VES HV Collector - Make some of the arguments required - Adjust docker-compose and Dockerfiles - Adjust test cases and error handling Closes ONAP-683 Change-Id: I4a9d43791cced9dcb52eb83e2f7956462e8712d9 Signed-off-by: Jakub Dudycz Issue-ID: DCAEGEN2-601 --- hv-collector-xnf-simulator/Dockerfile | 3 - .../xnf/config/ArgConfigurationProvider.kt | 77 -------- .../xnf/config/ArgXnfSimulatorConfiguration.kt | 82 +++++++++ .../dcae/collectors/veshv/simulators/xnf/main.kt | 4 +- .../main/config/ArgConfigurationProviderTest.kt | 142 --------------- .../main/config/ArgXnfSimulatorConfiurationTest.kt | 193 +++++++++++++++++++++ 6 files changed, 277 insertions(+), 224 deletions(-) delete mode 100644 hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgConfigurationProvider.kt create mode 100644 hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgXnfSimulatorConfiguration.kt delete mode 100644 hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgConfigurationProviderTest.kt create mode 100644 hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgXnfSimulatorConfiurationTest.kt (limited to 'hv-collector-xnf-simulator') diff --git a/hv-collector-xnf-simulator/Dockerfile b/hv-collector-xnf-simulator/Dockerfile index b713bafc..48f9dc56 100644 --- a/hv-collector-xnf-simulator/Dockerfile +++ b/hv-collector-xnf-simulator/Dockerfile @@ -5,11 +5,8 @@ LABEL license.name="The Apache Software License, Version 2.0" LABEL license.url="http://www.apache.org/licenses/LICENSE-2.0" LABEL maintainer="Nokia Wroclaw ONAP Team" -EXPOSE 5000 - WORKDIR /opt/ves-hv-client-simulator ENTRYPOINT ["java", "-cp", "*:", "org.onap.dcae.collectors.veshv.simulators.xnf.MainKt"] -CMD ["--ves-host", "ves-hv-collector", "--ves-port", "6061"] COPY target/libs/external/* ./ COPY target/libs/internal/* ./ COPY target/hv-collector-xnf-simulator-*.jar ./ diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgConfigurationProvider.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgConfigurationProvider.kt deleted file mode 100644 index 8954fd22..00000000 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgConfigurationProvider.kt +++ /dev/null @@ -1,77 +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 org.apache.commons.cli.CommandLine -import org.apache.commons.cli.DefaultParser -import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration -import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration -import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.* - - -/** - * @author Jakub Dudycz - * @since June 2018 - */ -internal class ArgConfigurationProvider : ArgBasedConfiguration(DefaultParser()) { - override val cmdLineOptionsList = listOf( - VES_HV_PORT, - VES_HV_HOST, - LISTEN_PORT, - SSL_DISABLE, - PRIVATE_KEY_FILE, - CERT_FILE, - TRUST_CERT_FILE - ) - - override fun getConfiguration(cmdLine: CommandLine): SimulatorConfiguration { - val vesHost = cmdLine.stringValue(VES_HV_HOST, DefaultValues.VES_HV_HOST) - val vesPort = cmdLine.intValue(VES_HV_PORT, DefaultValues.VES_HV_PORT) - val listenPort = cmdLine.intValue(LISTEN_PORT, DefaultValues.LISTEN_PORT) - - return SimulatorConfiguration( - listenPort, - vesHost, - vesPort, - parseSecurityConfig(cmdLine)) - } - - private fun parseSecurityConfig(cmdLine: CommandLine): SecurityConfiguration { - val sslDisable = cmdLine.hasOption(SSL_DISABLE) - 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( - sslDisable = sslDisable, - privateKey = stringPathToPath(pkFile), - cert = stringPathToPath(certFile), - trustedCert = stringPathToPath(trustCertFile)) - } - - internal object DefaultValues { - 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" - const val VES_HV_PORT = 6061 - const val VES_HV_HOST = "veshvcollector" - const val LISTEN_PORT = 6062 - } -} diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgXnfSimulatorConfiguration.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgXnfSimulatorConfiguration.kt new file mode 100644 index 00000000..999d0327 --- /dev/null +++ b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/config/ArgXnfSimulatorConfiguration.kt @@ -0,0 +1,82 @@ +/* + * ============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 arrow.core.ForOption +import arrow.core.Option +import arrow.core.fix +import arrow.instances.extensions +import arrow.typeclasses.binding +import org.apache.commons.cli.CommandLine +import org.apache.commons.cli.DefaultParser +import org.onap.dcae.collectors.veshv.domain.SecurityConfiguration +import org.onap.dcae.collectors.veshv.utils.commandline.ArgBasedConfiguration +import org.onap.dcae.collectors.veshv.utils.commandline.CommandLineOption.* + + +/** + * @author Jakub Dudycz + * @since June 2018 + */ +internal class ArgXnfSimulatorConfiguration : ArgBasedConfiguration(DefaultParser()) { + override val cmdLineOptionsList = listOf( + VES_HV_PORT, + VES_HV_HOST, + LISTEN_PORT, + SSL_DISABLE, + PRIVATE_KEY_FILE, + CERT_FILE, + TRUST_CERT_FILE + ) + + override fun getConfiguration(cmdLine: CommandLine): Option = + ForOption extensions { + binding { + val listenPort = cmdLine.intValue(LISTEN_PORT).bind() + val vesHost = cmdLine.stringValue(VES_HV_HOST).bind() + val vesPort = cmdLine.intValue(VES_HV_PORT).bind() + + SimulatorConfiguration( + listenPort, + vesHost, + vesPort, + parseSecurityConfig(cmdLine)) + }.fix() + } + + private fun parseSecurityConfig(cmdLine: CommandLine): SecurityConfiguration { + val sslDisable = cmdLine.hasOption(SSL_DISABLE) + 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( + sslDisable = sslDisable, + privateKey = stringPathToPath(pkFile), + cert = stringPathToPath(certFile), + trustedCert = stringPathToPath(trustCertFile)) + } + + internal object DefaultValues { + 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" + } +} diff --git a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt index a47cef58..19c52efa 100644 --- a/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt +++ b/hv-collector-xnf-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/xnf/main.kt @@ -19,7 +19,7 @@ */ package org.onap.dcae.collectors.veshv.simulators.xnf -import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgConfigurationProvider +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgXnfSimulatorConfiguration import org.onap.dcae.collectors.veshv.simulators.xnf.impl.HttpServer import org.onap.dcae.collectors.veshv.simulators.xnf.impl.XnfSimulator import org.onap.dcae.collectors.veshv.utils.arrow.ExitFailure @@ -36,7 +36,7 @@ const val PROGRAM_NAME = "java $PACKAGE_NAME.MainKt" * @author Jakub Dudycz * @since June 2018 */ -fun main(args: Array) = ArgConfigurationProvider().parse(args) +fun main(args: Array) = ArgXnfSimulatorConfiguration().parse(args) .mapLeft(handleWrongArgumentErrorCurried(PROGRAM_NAME)) .map { XnfSimulator(it) diff --git a/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgConfigurationProviderTest.kt b/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgConfigurationProviderTest.kt deleted file mode 100644 index 04d81c74..00000000 --- a/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgConfigurationProviderTest.kt +++ /dev/null @@ -1,142 +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.main.config - -import arrow.core.identity -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.ArgConfigurationProvider -import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgConfigurationProvider.DefaultValues -import org.onap.dcae.collectors.veshv.simulators.xnf.config.SimulatorConfiguration -import java.nio.file.Paths -import kotlin.test.assertTrue - - -object ArgConfigurationProviderTest : Spek({ - lateinit var cut: ArgConfigurationProvider - 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 = ArgConfigurationProvider() - } - - fun parse(vararg cmdLine: String): SimulatorConfiguration = - cut.parse(cmdLine).fold( - { throw AssertionError("Parsing result should be present") }, - ::identity - ) - - describe("parsing arguments") { - lateinit var result: SimulatorConfiguration - - given("all parameters are present in the long form") { - - beforeEachTest { - result = parse("--ssl-disable", - "--ves-port", "6969", - "--ves-host", vesHost, - "--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 security configuration") { - assertThat(result.security).isEqualTo( - SecurityConfiguration(sslDisable = true, privateKey = pk, cert = cert, trustedCert = trustCert) - ) - } - } - - given("some parameters are present in the short form") { - - beforeEachTest { - result = parse("-h", "ves-hv", "--ves-port", "666") - } - - it("should set proper port") { - assertThat(result.vesPort).isEqualTo(666) - } - } - - given("all optional parameters are absent") { - - beforeEachTest { - result = parse("-h", "ves-hv", "-v", "666") - } - - 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) - } - } - } - - given("disabled ssl certs together with all other parameters") { - beforeEachTest { - result = parse("--ssl-disable", - "--ves-port", "888", - "--ves-host", vesHost, - "--private-key-file", pk.toFile().absolutePath, - "--cert-file", cert.toFile().absolutePath, - "--trust-cert-file", trustCert.toFile().absolutePath) - } - - on("security config") { - val securityConfiguration = result.security - - it("should set ssl disable to true") { - assertTrue(securityConfiguration.sslDisable) - } - - it("should set proper security configuration") { - assertThat(securityConfiguration).isEqualTo( - SecurityConfiguration( - sslDisable = true, - privateKey = pk, - cert = cert, - trustedCert = trustCert) - ) - } - } - } - } -}) diff --git a/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgXnfSimulatorConfiurationTest.kt b/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgXnfSimulatorConfiurationTest.kt new file mode 100644 index 00000000..e3a20c70 --- /dev/null +++ b/hv-collector-xnf-simulator/src/test/kotlin/org/onap/dcae/collectors/veshv/main/config/ArgXnfSimulatorConfiurationTest.kt @@ -0,0 +1,193 @@ +/* + * ============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 arrow.core.identity +import org.assertj.core.api.Assertions +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.ArgXnfSimulatorConfiguration +import org.onap.dcae.collectors.veshv.simulators.xnf.config.ArgXnfSimulatorConfiguration.DefaultValues +import org.onap.dcae.collectors.veshv.simulators.xnf.config.SimulatorConfiguration +import org.onap.dcae.collectors.veshv.utils.commandline.WrongArgumentError +import java.nio.file.Paths +import kotlin.test.assertTrue + + +object ArgXnfSimulatorConfiurationTest : Spek({ + lateinit var cut: ArgXnfSimulatorConfiguration + val listenPort = "4321" + val vesHost = "localhost" + val vesPort = "1234" + 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 = ArgXnfSimulatorConfiguration() + } + + fun parse(vararg cmdLine: String): SimulatorConfiguration = + cut.parse(cmdLine).fold( + { throw AssertionError("Parsing result should be present") }, + ::identity + ) + + fun parseExpectingFailure(vararg cmdLine: String) = + cut.parse(cmdLine).fold( + ::identity, + { throw AssertionError("parsing should have failed") } + ) + + describe("parsing arguments") { + lateinit var result: SimulatorConfiguration + + given("all parameters are present in the long form") { + + beforeEachTest { + result = parse("--ssl-disable", + "--listen-port", listenPort, + "--ves-host", vesHost, + "--ves-port", vesPort, + "--private-key-file", pk.toFile().absolutePath, + "--cert-file", cert.toFile().absolutePath, + "--trust-cert-file", trustCert.toFile().absolutePath) + } + + it("should set proper listen port") { + assertThat(result.listenPort).isEqualTo(listenPort.toInt()) + } + + it("should set proper ves host") { + assertThat(result.vesHost).isEqualTo(vesHost) + } + + it("should set proper ves port") { + assertThat(result.vesPort).isEqualTo(vesPort.toInt()) + } + + it("should set proper security configuration") { + assertThat(result.security).isEqualTo( + SecurityConfiguration(sslDisable = true, privateKey = pk, cert = cert, trustedCert = trustCert) + ) + } + } + + given("some parameters are present in the short form") { + + beforeEachTest { + result = parse("-p", listenPort, "-h", vesHost, "--ves-port", vesPort) + } + + it("should set proper listen port") { + assertThat(result.listenPort).isEqualTo(listenPort.toInt()) + } + + it("should set proper ves host") { + assertThat(result.vesHost).isEqualTo(vesHost) + } + + it("should set proper ves port") { + assertThat(result.vesPort).isEqualTo(vesPort.toInt()) + } + } + + given("all optional parameters are absent") { + + beforeEachTest { + result = parse("-p", listenPort, "-h", vesHost, "-v", vesPort) + } + + 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) + } + } + } + + given("disabled ssl certs together with all other parameters") { + beforeEachTest { + result = parse("--ssl-disable", + "--listen-port", listenPort, + "--ves-port", "888", + "--ves-host", vesHost, + "--private-key-file", pk.toFile().absolutePath, + "--cert-file", cert.toFile().absolutePath, + "--trust-cert-file", trustCert.toFile().absolutePath) + } + + on("security config") { + val securityConfiguration = result.security + + it("should set ssl disable to true") { + assertTrue(securityConfiguration.sslDisable) + } + + it("should set proper security configuration") { + assertThat(securityConfiguration).isEqualTo( + SecurityConfiguration( + sslDisable = true, + privateKey = pk, + cert = cert, + trustedCert = trustCert) + ) + } + } + } + + describe("required parameter is absent") { + given("ves port is missing") { + it("should throw exception") { + assertThat(parseExpectingFailure("-p", listenPort, "-h", vesHost)) + .isInstanceOf(WrongArgumentError::class.java) + } + } + + given("ves host is missing") { + it("should throw exception") { + assertThat(parseExpectingFailure("-p", listenPort, "-v", vesPort)) + .isInstanceOf(WrongArgumentError::class.java) + } + } + + given("listen port is missing") { + it("should throw exception") { + assertThat(parseExpectingFailure("-h", vesHost, "-v", vesPort)) + .isInstanceOf(WrongArgumentError::class.java) + } + } + } + } +}) -- cgit 1.2.3-korg