summaryrefslogtreecommitdiffstats
path: root/hv-collector-utils
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-06-14 09:48:46 +0200
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-02 09:49:02 +0200
commit67689405071acdad2b26d5112b3662605e474ce9 (patch)
tree3e945129934d5721922fdabf229b0d61b772dfdb /hv-collector-utils
parente7987b7a660060746d5f49e1ec90b1ff90fcf55a (diff)
Various improvements
* Kotlin upgrade * Monad usage on APIs * Idle timeout * Simulator enhancements Closes ONAP-390 Change-Id: I3c00fcfe38c722caf661ddaad428cf089eeefcaa Signed-off-by: Piotr Jaszczyk <piotr.jaszczyk@nokia.com> Issue-ID: DCAEGEN2-601
Diffstat (limited to 'hv-collector-utils')
-rw-r--r--hv-collector-utils/pom.xml4
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt48
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt12
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentException.kt9
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/extensions.kt50
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt4
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())