From f3d3e6745328b9ec8bb68dfbfb3d3d57aa47b36f Mon Sep 17 00:00:00 2001 From: kjaniak Date: Fri, 22 Mar 2019 12:42:11 +0100 Subject: Read HV-VES healtcheck api port from cmd line Issue-ID: DCAEGEN2-1364 Change-Id: I17c38d7397174fadc7d382cfa3dd0b3a7f4d97ff Signed-off-by: kjaniak --- .../veshv/config/api/ConfigurationModule.kt | 6 ++- .../veshv/config/impl/ArgHvVesConfiguration.kt | 62 ++++++++++++++++++++-- .../veshv/config/impl/ArgHvVesConfigurationTest.kt | 16 ++++-- 3 files changed, 76 insertions(+), 8 deletions(-) (limited to 'sources/hv-collector-configuration') 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 9f8c552b..1b9248b5 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 @@ -19,6 +19,7 @@ */ package org.onap.dcae.collectors.veshv.config.api +import arrow.core.getOrElse 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 @@ -29,13 +30,16 @@ import org.onap.dcae.collectors.veshv.utils.arrow.throwOnLeft import reactor.core.publisher.Flux class ConfigurationModule { + private val DEFAULT_HEALTHCHECK_PORT: Int = 6060 private val cmd = ArgHvVesConfiguration() private val configReader = FileConfigurationReader() private val configValidator = ConfigurationValidator() + fun healthCheckPort(args: Array): Int = cmd.parseToInt(args).getOrElse { DEFAULT_HEALTHCHECK_PORT } + fun hvVesConfigurationUpdates(args: Array): Flux = - Flux.just(cmd.parse(args)) + Flux.just(cmd.parseToFile(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 index 9587d5b0..71ed509b 100644 --- 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 @@ -19,18 +19,72 @@ */ package org.onap.dcae.collectors.veshv.config.impl +import arrow.core.Either import arrow.core.Option +import arrow.core.Try +import arrow.core.flatMap import org.apache.commons.cli.CommandLine +import org.apache.commons.cli.CommandLineParser import org.apache.commons.cli.DefaultParser -import org.onap.dcae.collectors.veshv.commandline.ArgBasedConfiguration +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 java.io.File -internal class ArgHvVesConfiguration : ArgBasedConfiguration(DefaultParser()) { - override val cmdLineOptionsList = listOf(CONFIGURATION_FILE) +class ArgHvVesConfiguration(private val parser: CommandLineParser = DefaultParser()) { + val cmdLineOptionsList = listOf(CONFIGURATION_FILE, HEALTH_CHECK_API_PORT) - override fun getConfiguration(cmdLine: CommandLine): Option = + fun getConfiguration(cmdLine: CommandLine): Option = cmdLine.stringValue(CONFIGURATION_FILE).map(::File) + fun getHealthcheckPort(cmdLine: CommandLine): Option = + cmdLine.intValue(HEALTH_CHECK_API_PORT) + + lateinit var parsingResult: CommandLine + + fun parseToFile(args: Array): Either { + parseIfEmpty(args) + return Try { parsingResult }.toEither() + .mapLeft { WrongArgumentError(it, cmdLineOptionsList) } + .map(this::getConfiguration) + .flatMap { + it.toEither { + WrongArgumentError( + message = "Unexpected error when parsing command line arguments", + cmdLineOptionsList = cmdLineOptionsList) + } + } + } + + fun parseToInt(args: Array): Either { + parseIfEmpty(args) + return Try { parsingResult }.toEither() + .mapLeft { WrongArgumentError(it, cmdLineOptionsList) } + .map(this::getHealthcheckPort) + .flatMap { + it.toEither { + WrongArgumentError( + message = "Unexpected error when parsing command line arguments", + cmdLineOptionsList = cmdLineOptionsList) + } + } + } + + private fun parseIfEmpty(args: Array) { + if (!this::parsingResult.isInitialized) { + parsingResult = parseArgumentsArray(args) + } + } + + private fun parseArgumentsArray(args: Array) = + cmdLineOptionsList + .map { it.option } + .fold(Options(), Options::addOption) + .let { parser.parse(it, args) } + } + + 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/ArgHvVesConfigurationTest.kt index dbe757c4..6fe76958 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/ArgHvVesConfigurationTest.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,8 +28,6 @@ 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 /** @@ -70,4 +69,15 @@ object ArgVesHvConfigurationTest : Spek({ } } } -}) \ No newline at end of file +}) + +fun ArgHvVesConfiguration.parseExpectingSuccess(vararg cmdLine: String): File = + parseToFile(cmdLine).fold( + { throw AssertionError("Parsing result should be present") }, + ::identity + ) + +fun ArgHvVesConfiguration.parseExpectingFailure(vararg cmdLine: String): WrongArgumentError = + parseToFile(cmdLine).fold( + ::identity + ) { throw AssertionError("parsing should have failed") } -- cgit 1.2.3-korg