summaryrefslogtreecommitdiffstats
path: root/hv-collector-utils
diff options
context:
space:
mode:
authorPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-07-04 13:16:21 +0200
committerPiotr Jaszczyk <piotr.jaszczyk@nokia.com>2018-08-02 12:01:26 +0200
commite31d59f63ebe5536d2d2d868703eb8896924b63d (patch)
tree0ed280beb287c59fbd7a3494684884f5738e377d /hv-collector-utils
parente5ce5ac06cf1ce95a65f18ad05be9e8432be6d49 (diff)
Use IO monad when starting servers
Change-Id: I3e97161535fc721dda6109c4cb5f23a1db0afde3 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.xml8
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt31
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt49
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/ArgBasedConfiguration.kt15
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentError.kt (renamed from hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/WrongArgumentException.kt)7
-rw-r--r--hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/extensions.kt30
6 files changed, 108 insertions, 32 deletions
diff --git a/hv-collector-utils/pom.xml b/hv-collector-utils/pom.xml
index 3c48280c..ea19ba3f 100644
--- a/hv-collector-utils/pom.xml
+++ b/hv-collector-utils/pom.xml
@@ -72,6 +72,14 @@
<artifactId>arrow-instances-data</artifactId>
</dependency>
<dependency>
+ <groupId>io.arrow-kt</groupId>
+ <artifactId>arrow-effects</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.arrow-kt</groupId>
+ <artifactId>arrow-syntax</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/arrow/core.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt
new file mode 100644
index 00000000..39964c1e
--- /dev/null
+++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt
@@ -0,0 +1,31 @@
+/*
+ * ============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.arrow
+
+import arrow.core.Either
+import arrow.core.identity
+
+/**
+ * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
+ * @since July 2018
+ */
+
+
+fun <A> Either<A, A>.flatten() = fold(::identity, ::identity)
diff --git a/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt
new file mode 100644
index 00000000..e37b0d7d
--- /dev/null
+++ b/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/effects.kt
@@ -0,0 +1,49 @@
+/*
+ * ============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.arrow
+
+import arrow.core.Either
+import arrow.effects.IO
+import kotlin.system.exitProcess
+
+/**
+ * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
+ * @since June 2018
+ */
+
+sealed class ExitCode {
+ abstract val code: Int
+
+ fun io() = IO {
+ exitProcess(code)
+ }
+}
+
+object ExitSuccess : ExitCode() {
+ override val code = 0
+}
+
+data class ExitFailure(override val code: Int) : ExitCode()
+
+fun Either<IO<Unit>, IO<Unit>>.unsafeRunEitherSync(onError: (Throwable) -> ExitCode, onSuccess: () -> Unit) =
+ flatten().attempt().unsafeRunSync().fold({ onError(it).io().unsafeRunSync() }, { onSuccess() })
+
+
+fun IO<Any>.void() = map { Unit }
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 34c0e651..d5855caa 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,6 +19,7 @@
*/
package org.onap.dcae.collectors.veshv.utils.commandline
+import arrow.core.Either
import arrow.core.Option
import arrow.core.Try
import arrow.core.getOrElse
@@ -30,16 +31,18 @@ import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
-abstract class ArgBasedConfiguration<T>(val parser: CommandLineParser) {
+abstract class ArgBasedConfiguration<T>(private val parser: CommandLineParser) {
abstract val cmdLineOptionsList: List<CommandLineOption>
- fun parse(args: Array<out String>): Try<T> {
+ fun parse(args: Array<out String>): Either<WrongArgumentError, T> {
val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption)
- return Try {
+ val parseResult = Try {
parser.parse(commandLineOptions, args)
- }.recoverWith { ex ->
- Try.raise<CommandLine>(WrongArgumentException(ex, commandLineOptions))
- }.map (this::getConfiguration)
+ }
+ return parseResult
+ .toEither()
+ .mapLeft { ex -> WrongArgumentError(ex, commandLineOptions) }
+ .map(this::getConfiguration)
}
protected abstract fun getConfiguration(cmdLine: CommandLine): T
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/WrongArgumentError.kt
index 083d5798..f3bb3146 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/WrongArgumentError.kt
@@ -23,11 +23,10 @@ import org.apache.commons.cli.HelpFormatter
import org.apache.commons.cli.Options
-class WrongArgumentException(
- message: String,
+data class WrongArgumentError(
+ val message: String,
private val options: Options,
- parent: Throwable? = null
-) : Exception(message, parent) {
+ val cause: Throwable? = null) {
constructor(par: Throwable, options: Options) : this(par.message ?: "", options, par)
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
index 23bf1658..718ebf8b 100644
--- 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
@@ -19,32 +19,18 @@
*/
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
+import arrow.effects.IO
+import arrow.syntax.function.curried
+import org.onap.dcae.collectors.veshv.utils.arrow.ExitFailure
/**
* @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)
- }
+fun handleWrongArgumentError(programName: String, err: WrongArgumentError): IO<Unit> = IO {
+ err.printMessage()
+ err.printHelp(programName)
+}.flatMap { ExitFailure(2).io() }
- 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)
-}
+val handleWrongArgumentErrorCurried = ::handleWrongArgumentError.curried()