aboutsummaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-configuration/src/main/kotlin
diff options
context:
space:
mode:
authorkjaniak <kornel.janiak@nokia.com>2019-03-28 10:43:59 +0100
committerKornel Janiak <kornel.janiak@nokia.com>2019-03-29 08:17:14 +0000
commit4281a12d8e892f46f5f2226ee0f8aee8b862b177 (patch)
treececcc8eb35d669afdee1d250b0b3c50ee0d9a940 /sources/hv-collector-configuration/src/main/kotlin
parent4f5d73f18a7952379505a4995b6afe22acf31c11 (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/main/kotlin')
-rw-r--r--sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/api/ConfigurationModule.kt8
-rw-r--r--sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/ArgHvVesConfiguration.kt36
-rw-r--r--sources/hv-collector-configuration/src/main/kotlin/org/onap/dcae/collectors/veshv/config/impl/HvVesCommandLineParser.kt83
3 files changed, 88 insertions, 39 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)
+ }
+
+}
+
+