diff options
author | kjaniak <kornel.janiak@nokia.com> | 2019-03-28 10:43:59 +0100 |
---|---|---|
committer | Kornel Janiak <kornel.janiak@nokia.com> | 2019-03-29 08:17:14 +0000 |
commit | 4281a12d8e892f46f5f2226ee0f8aee8b862b177 (patch) | |
tree | ceccc8eb35d669afdee1d250b0b3c50ee0d9a940 /sources/hv-collector-configuration/src | |
parent | 4f5d73f18a7952379505a4995b6afe22acf31c11 (diff) |
Read HV-VES healthcheck api port from cmd line
Change-Id: I6b4680a62512ef6ba15a0454e109b19619a997a6
Issue-ID: DCAEGEN2-1364
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Diffstat (limited to 'sources/hv-collector-configuration/src')
-rw-r--r-- | sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt | 8 | ||||
-rw-r--r-- | sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt | 36 | ||||
-rw-r--r-- | sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt | 83 | ||||
-rw-r--r-- | sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParserTest.kt (renamed from sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfigurationTest.kt) | 54 |
4 files changed, 132 insertions, 49 deletions
diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt index efe0aa88..dd1b171e 100644 --- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt +++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt @@ -22,7 +22,7 @@ package org.onap.dcae.collectors.veshv.config.api import org.onap.dcae.collectors.veshv.config.api.model.HvVesConfiguration import org.onap.dcae.collectors.veshv.config.api.model.MissingArgumentException import org.onap.dcae.collectors.veshv.config.api.model.ValidationException -import org.onap.dcae.collectors.veshv.config.impl.ArgHvVesConfiguration +import org.onap.dcae.collectors.veshv.config.impl.HvVesCommandLineParser import org.onap.dcae.collectors.veshv.config.impl.ConfigurationValidator import org.onap.dcae.collectors.veshv.config.impl.FileConfigurationReader import org.onap.dcae.collectors.veshv.utils.arrow.throwOnLeft @@ -30,14 +30,16 @@ import reactor.core.publisher.Flux class ConfigurationModule { - private val cmd = ArgHvVesConfiguration() + private val cmd = HvVesCommandLineParser() private val configReader = FileConfigurationReader() private val configValidator = ConfigurationValidator() private lateinit var initialConfig: HvVesConfiguration + fun healthCheckPort(args: Array<String>): Int = cmd.getHealthcheckPort(args) + fun hvVesConfigurationUpdates(args: Array<String>): Flux<HvVesConfiguration> = - Flux.just(cmd.parse(args)) + Flux.just(cmd.getConfigurationFile(args)) .throwOnLeft { MissingArgumentException(it.message, it.cause) } .map { it.reader().use(configReader::loadConfig) } .map { configValidator.validate(it) } diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt deleted file mode 100644 index 9587d5b0..00000000 --- a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * dcaegen2-collectors-veshv - * ================================================================================ - * Copyright (C) 2019 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.config.impl - -import arrow.core.Option -import org.apache.commons.cli.CommandLine -import org.apache.commons.cli.DefaultParser -import org.onap.dcae.collectors.veshv.commandline.ArgBasedConfiguration -import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_FILE -import org.onap.dcae.collectors.veshv.commandline.stringValue -import java.io.File - -internal class ArgHvVesConfiguration : ArgBasedConfiguration<File>(DefaultParser()) { - override val cmdLineOptionsList = listOf(CONFIGURATION_FILE) - - override fun getConfiguration(cmdLine: CommandLine): Option<File> = - cmdLine.stringValue(CONFIGURATION_FILE).map(::File) - -} diff --git a/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt new file mode 100644 index 00000000..3e93a400 --- /dev/null +++ b/sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2019 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.config.impl + +import arrow.core.* +import org.apache.commons.cli.CommandLine +import org.apache.commons.cli.CommandLineParser +import org.apache.commons.cli.DefaultParser +import org.apache.commons.cli.Options +import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.CONFIGURATION_FILE +import org.onap.dcae.collectors.veshv.commandline.CommandLineOption.HEALTH_CHECK_API_PORT +import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError +import org.onap.dcae.collectors.veshv.commandline.intValue +import org.onap.dcae.collectors.veshv.commandline.stringValue +import org.onap.dcae.collectors.veshv.utils.logging.Logger +import java.io.File + +internal class HvVesCommandLineParser(private val parser: CommandLineParser = DefaultParser()) { + private val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT) + + private lateinit var parsedCommandLine: CommandLine + + fun getConfigurationFile(args: Array<out String>): Either<WrongArgumentError, File> = + parse(args) { + it.stringValue(CONFIGURATION_FILE).map(::File) + }.toEither { + WrongArgumentError( + message = "Unexpected error when parsing command line arguments", + cmdLineOptionsList = cmdLineOptionsList) + } + + fun getHealthcheckPort(args: Array<out String>): Int = + parse(args) { + it.intValue(HEALTH_CHECK_API_PORT) + }.getOrElse { + logger.info { "Healthcheck port missing on command line," + + " using default: $DEFAULT_HEALTHCHECK_PORT" } + DEFAULT_HEALTHCHECK_PORT + } + + private fun <T> parse(args: Array<out String>, cmdLineMapper: (CommandLine) -> Option<T>) = + Try { parseIfNotInitialized(args) } + .toOption() + .flatMap(cmdLineMapper) + + private fun parseIfNotInitialized(args: Array<out String>): CommandLine { + if (!this::parsedCommandLine.isInitialized) { + parsedCommandLine = parseArgumentsArray(args) + } + return parsedCommandLine + } + + private fun parseArgumentsArray(args: Array<out String>) = + cmdLineOptionsList + .map { it.option } + .fold(Options(), Options::addOption) + .let { parser.parse(it, args) } + + companion object { + private const val DEFAULT_HEALTHCHECK_PORT: Int = 6060 + private val logger = Logger(HvVesCommandLineParser::class) + } + +} + + diff --git a/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfigurationTest.kt b/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParserTest.kt index dbe757c4..0fdd41c9 100644 --- a/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfigurationTest.kt +++ b/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParserTest.kt @@ -19,6 +19,7 @@ */ package org.onap.dcae.collectors.veshv.config.impl +import arrow.core.identity import org.assertj.core.api.Assertions.assertThat import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.describe @@ -27,20 +28,20 @@ import org.jetbrains.spek.api.dsl.it import org.jetbrains.spek.api.dsl.on import org.onap.dcae.collectors.veshv.commandline.WrongArgumentError import org.onap.dcae.collectors.veshv.tests.utils.absoluteResourcePath -import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingFailure -import org.onap.dcae.collectors.veshv.tests.utils.parseExpectingSuccess import java.io.File /** * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> * @since May 2018 */ -object ArgVesHvConfigurationTest : Spek({ - lateinit var cut: ArgHvVesConfiguration +object HvVesCommandLineParserTest : Spek({ + lateinit var cut: HvVesCommandLineParser + val DEFAULT_HEALTHCHECK_PORT = 6060 + val emptyConfig = "" val configFilePath = javaClass.absoluteResourcePath("sampleConfig.json") beforeEachTest { - cut = ArgHvVesConfiguration() + cut = HvVesCommandLineParser() } describe("parsing arguments") { @@ -48,7 +49,7 @@ object ArgVesHvConfigurationTest : Spek({ lateinit var result: File beforeEachTest { - result = cut.parseExpectingSuccess( + result = cut.parseFileExpectingSuccess( "--configuration-file", configFilePath ) } @@ -58,16 +59,49 @@ object ArgVesHvConfigurationTest : Spek({ } } - describe("required parameter is absent") { + given("required parameter is absent") { on("missing configuration file path") { it("should throw exception") { assertThat( - cut.parseExpectingFailure( - "--non-existing-option", "" + cut.parseFileExpectingFailure( + "--non-existing-option", emptyConfig ) ).isInstanceOf(WrongArgumentError::class.java) } } } + + given("healthcheck port defined via cmd") { + val healthCheckPort = 888 + val configWithHealthcheckPort = "--health-check-api-port $healthCheckPort" + on("parsing command") { + it("should assign proper port") { + assertThat( + cut.getHealthcheckPort(arrayOf(configWithHealthcheckPort)) + ).isEqualTo(healthCheckPort) + } + } + } + + given("no healthcheck port defined via cmd") { + on("parsing command") { + it("should return default port") { + assertThat( + cut.getHealthcheckPort(arrayOf(emptyConfig)) + ).isEqualTo(DEFAULT_HEALTHCHECK_PORT) + } + } + } } -})
\ No newline at end of file +}) + +private fun HvVesCommandLineParser.parseFileExpectingSuccess(vararg cmdLine: String): File = + getConfigurationFile(cmdLine).fold( + { throw AssertionError("Parsing result should be present") }, + ::identity + ) + +private fun HvVesCommandLineParser.parseFileExpectingFailure(vararg cmdLine: String): WrongArgumentError = + getConfigurationFile(cmdLine).fold( + ::identity + ) { throw AssertionError("parsing should have failed") }
\ No newline at end of file |