diff options
Diffstat (limited to 'hv-collector-utils')
6 files changed, 107 insertions, 20 deletions
diff --git a/hv-collector-utils/pom.xml b/hv-collector-utils/pom.xml index 8a8a1d8c..3c48280c 100644 --- a/hv-collector-utils/pom.xml +++ b/hv-collector-utils/pom.xml @@ -68,6 +68,10 @@ <artifactId>kotlin-reflect</artifactId> </dependency> <dependency> + <groupId>io.arrow-kt</groupId> + <artifactId>arrow-instances-data</artifactId> + </dependency> + <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt index 968c340f..34c0e651 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt @@ -19,44 +19,54 @@ */ package org.onap.dcae.collectors.veshv.utils.commandline +import arrow.core.Option +import arrow.core.Try +import arrow.core.getOrElse +import arrow.core.recoverWith import org.apache.commons.cli.CommandLine import org.apache.commons.cli.CommandLineParser import org.apache.commons.cli.Options import java.io.File +import java.nio.file.Path import java.nio.file.Paths abstract class ArgBasedConfiguration<T>(val parser: CommandLineParser) { abstract val cmdLineOptionsList: List<CommandLineOption> - fun parse(args: Array<out String>): T { + fun parse(args: Array<out String>): Try<T> { val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption) - try { - val cmdLine = parser.parse(commandLineOptions, args) - return getConfiguration(cmdLine) - } catch (ex: Exception) { - throw WrongArgumentException(ex, commandLineOptions) - } + return Try { + parser.parse(commandLineOptions, args) + }.recoverWith { ex -> + Try.raise<CommandLine>(WrongArgumentException(ex, commandLineOptions)) + }.map (this::getConfiguration) } protected abstract fun getConfiguration(cmdLine: CommandLine): T - protected fun CommandLine.intValue(cmdLineOpt: CommandLineOption): Int = - getOptionValue(cmdLineOpt.option.opt).toInt() - protected fun CommandLine.intValue(cmdLineOpt: CommandLineOption, default: Int): Int = - getOptionValue(cmdLineOpt.option.opt)?.toInt() ?: default - - protected fun CommandLine.longValue(cmdLineOpt: CommandLineOption): Long = - getOptionValue(cmdLineOpt.option.opt).toLong() + intValue(cmdLineOpt).getOrElse { default } protected fun CommandLine.longValue(cmdLineOpt: CommandLineOption, default: Long): Long = - getOptionValue(cmdLineOpt.option.opt)?.toLong() ?: default + longValue(cmdLineOpt).getOrElse { default } - protected fun CommandLine.stringValue(cmdLineOpt: CommandLineOption): String = - getOptionValue(cmdLineOpt.option.opt) + protected fun CommandLine.stringValue(cmdLineOpt: CommandLineOption): Option<String> = + optionValue(cmdLineOpt) protected fun CommandLine.stringValue(cmdLineOpt: CommandLineOption, default: String): String = - getOptionValue(cmdLineOpt.option.opt) ?: default + optionValue(cmdLineOpt).getOrElse { default } + + protected fun CommandLine.hasOption(cmdLineOpt: CommandLineOption): Boolean = + this.hasOption(cmdLineOpt.option.opt) + + protected fun stringPathToPath(path: String): Path = Paths.get(File(path).toURI()) + + private fun CommandLine.optionValue(cmdLineOpt: CommandLineOption): Option<String> = + Option.fromNullable(getOptionValue(cmdLineOpt.option.opt)) + + private fun CommandLine.intValue(cmdLineOpt: CommandLineOption): Option<Int> = + optionValue(cmdLineOpt).map(String::toInt) - protected fun stringPathToPath(path: String) = Paths.get(File(path).toURI()) + private fun CommandLine.longValue(cmdLineOpt: CommandLineOption): Option<Long> = + optionValue(cmdLineOpt).map(String::toLong) } diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt index 9d1f7aa8..942ca31f 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt @@ -87,4 +87,16 @@ enum class CommandLineOption(val option: Option) { .desc("File with trusted certificate bundle for trusting connections") .build() ), + IDLE_TIMEOUT_SEC(Option.builder("i") + .longOpt("idle-timeout-sec") + .hasArg() + .desc("""Idle timeout for remote hosts. After given time without any data exchange the + |connection might be closed.""".trimMargin()) + .build() + ), + DUMMY_MODE(Option.builder("d") + .longOpt("dummy") + .desc("If present will start in dummy mode (dummy external services)") + .build() + ), } diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentException.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentException.kt index 5f6a86ad..083d5798 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentException.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentException.kt @@ -23,7 +23,14 @@ import org.apache.commons.cli.HelpFormatter import org.apache.commons.cli.Options -class WrongArgumentException(parent: Exception, private val options: Options) : Exception(parent.message, parent) { +class WrongArgumentException( + message: String, + private val options: Options, + parent: Throwable? = null +) : Exception(message, parent) { + + constructor(par: Throwable, options: Options) : this(par.message ?: "", options, par) + fun printMessage() { println(message) } diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/extensions.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/extensions.kt new file mode 100644 index 00000000..23bf1658 --- /dev/null +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/extensions.kt @@ -0,0 +1,50 @@ +/* + * ============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.utils.commandline + +import arrow.core.Failure +import org.onap.dcae.collectors.veshv.utils.logging.Logger +import kotlin.system.exitProcess + +/** + * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> + * @since June 2018 + */ + +fun handleErrorsInMain(ex: Throwable, programName: String, logger: Logger) { + when (ex) { + is WrongArgumentException -> { + ex.printMessage() + ex.printHelp(programName) + exitProcess(1) + } + + else -> { + logger.error(ex.localizedMessage) + logger.debug("An error occurred when starting VES HV Collector", ex) + System.exit(2) + } + } +} + + +fun <A> Failure<A>.handleErrorsInMain(programName: String, logger: Logger) { + handleErrorsInMain(this.exception, programName, logger) +} diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt index f614d426..536fe93c 100644 --- a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt +++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt @@ -24,11 +24,15 @@ import kotlin.reflect.KClass class Logger(val logger: org.slf4j.Logger) { constructor(clazz: KClass<out Any>) : this(LoggerFactory.getLogger(clazz.java)) + constructor(name: String) : this(LoggerFactory.getLogger(name)) // // TRACE // + val traceEnabled: Boolean + get() = logger.isTraceEnabled + fun trace(messageProvider: () -> String) { if (logger.isTraceEnabled) { logger.trace(messageProvider()) |