From 30afcb56b0c6c4529fdaf68d7b061eee44d68d16 Mon Sep 17 00:00:00 2001 From: Jakub Dudycz Date: Wed, 13 Mar 2019 18:44:31 +0100 Subject: Remove environment variables and program arguments - Move all command line program arguments to json file. - Reorganize configuration classes and the way they are passed through application - Implement HV VES configuration stream - Create concrete configuration from partial one - Modify main HV-VES server starting pipeline Change-Id: I6cf874b6904ed768e4820b8132f5f760299c929e Signed-off-by: Jakub Dudycz Issue-ID: DCAEGEN2-1340 --- .../onap/dcae/collectors/veshv/utils/arrow/core.kt | 32 ++++++++++++++++++++++ .../onap/dcae/collectors/veshv/utils/reactive.kt | 25 +++++++++++++++++ .../dcae/collectors/veshv/utils/shutdown_hook.kt | 15 ++++++++-- 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/reactive.kt (limited to 'sources/hv-collector-utils/src/main/kotlin/org/onap/dcae') diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt index bedc2fcd..d5b33b91 100644 --- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/arrow/core.kt @@ -20,10 +20,20 @@ package org.onap.dcae.collectors.veshv.utils.arrow import arrow.core.Either +import arrow.core.ForOption import arrow.core.Option import arrow.core.Try +import arrow.core.fix import arrow.core.identity +import arrow.effects.ForIO +import arrow.effects.IO +import arrow.effects.fix +import arrow.effects.instances.io.monad.monad +import arrow.instances.option.monad.monad import arrow.syntax.collections.firstOption +import arrow.typeclasses.MonadContinuation +import arrow.typeclasses.binding +import reactor.core.publisher.Flux import java.util.concurrent.atomic.AtomicReference /** @@ -31,12 +41,24 @@ import java.util.concurrent.atomic.AtomicReference * @since July 2018 */ +object OptionUtils { + fun binding(c: suspend MonadContinuation.() -> A) + : Option = Option.monad().binding(c).fix() +} + +object IOUtils { + fun binding(c: suspend MonadContinuation.() -> A) + : IO = IO.monad().binding(c).fix() +} + fun Either.flatten() = fold(::identity, ::identity) fun Either.rightOrThrow() = fold({ throw it }, ::identity) fun Either.rightOrThrow(mapper: (A) -> Throwable) = fold({ throw mapper(it) }, ::identity) +fun Flux>.throwOnLeft(f: (A) -> Exception): Flux = map { it.rightOrThrow(f) } + fun AtomicReference.getOption() = Option.fromNullable(get()) fun Option.Companion.fromNullablesChain(firstValue: A?, vararg nextValues: () -> A?): Option = @@ -57,3 +79,13 @@ fun Try.doOnFailure(action: (Throwable) -> Unit): Try = apply { action(exception) } } + +fun A.mapBinding(c: suspend MonadContinuation.(A) -> B) + : Option = let { OptionUtils.binding { c(it) } } + + + + + + + diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/reactive.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/reactive.kt new file mode 100644 index 00000000..aaa598d2 --- /dev/null +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/reactive.kt @@ -0,0 +1,25 @@ +/* + * ============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.utils + +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono + +fun Flux.neverComplete(): Mono = then(Mono.never()).then() \ No newline at end of file diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt index 87aea41e..cc940907 100644 --- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/shutdown_hook.kt @@ -19,10 +19,19 @@ */ package org.onap.dcae.collectors.veshv.utils +import java.util.concurrent.atomic.AtomicReference + /** * @author Piotr Jaszczyk * @since January 2019 */ -fun registerShutdownHook(job: () -> Unit) = - Runtime.getRuntime() - .addShutdownHook(Thread({ job() }, "GracefulShutdownThread")) + +private val currentShutdownHook = AtomicReference() + +fun registerShutdownHook(job: () -> Unit) { + val runtime = Runtime.getRuntime() + val newShutdownHook = Thread({ job() }, "GracefulShutdownThread") + currentShutdownHook.get()?.run(runtime::removeShutdownHook) + currentShutdownHook.set(newShutdownHook) + runtime.addShutdownHook(newShutdownHook) +} -- cgit 1.2.3-korg