From 4281a12d8e892f46f5f2226ee0f8aee8b862b177 Mon Sep 17 00:00:00 2001 From: kjaniak Date: Thu, 28 Mar 2019 10:43:59 +0100 Subject: Read HV-VES healthcheck api port from cmd line Change-Id: I6b4680a62512ef6ba15a0454e109b19619a997a6 Issue-ID: DCAEGEN2-1364 Signed-off-by: kjaniak --- .../veshv/config/api/ConfigurationModule.kt | 8 ++- .../veshv/config/impl/ArgHvVesConfiguration.kt | 36 ---------- .../veshv/config/impl/HvVesCommandLineParser.kt | 83 ++++++++++++++++++++++ 3 files changed, 88 insertions(+), 39 deletions(-) delete mode 100644 sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt create mode 100644 sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt (limited to 'sources/hv-collector-configuration/src/main/kotlin/org/onap') 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): Int = cmd.getHealthcheckPort(args) + fun hvVesConfigurationUpdates(args: Array): Flux = - 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(DefaultParser()) { - override val cmdLineOptionsList = listOf(CONFIGURATION_FILE) - - override fun getConfiguration(cmdLine: CommandLine): Option = - 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): Either = + parse(args) { + it.stringValue(CONFIGURATION_FILE).map(::File) + }.toEither { + WrongArgumentError( + message = "Unexpected error when parsing command line arguments", + cmdLineOptionsList = cmdLineOptionsList) + } + + fun getHealthcheckPort(args: Array): 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 parse(args: Array, cmdLineMapper: (CommandLine) -> Option) = + Try { parseIfNotInitialized(args) } + .toOption() + .flatMap(cmdLineMapper) + + private fun parseIfNotInitialized(args: Array): CommandLine { + if (!this::parsedCommandLine.isInitialized) { + parsedCommandLine = parseArgumentsArray(args) + } + return parsedCommandLine + } + + private fun parseArgumentsArray(args: Array) = + 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) + } + +} + + -- cgit 1.2.3-korg