From c6da9f5aaa7c29644ead22d5ba5fc8ef3ec5811a Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Sat, 1 Feb 2020 18:47:59 -0500 Subject: Expose rest API with non blocking call. Convert Mono and Flux to coroutines Convert reactor mdc to coroutine mdc Issue-ID: CCSDK-2052 Signed-off-by: Brinda Santh Change-Id: Ic58c0b74866d28fd2d803b96626b08f8e8b2db56 --- .../rest/service/RestLoggerService.kt | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'ms/blueprintsprocessor/modules/commons/rest-lib') diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestLoggerService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestLoggerService.kt index 313710745..846a94a09 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestLoggerService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestLoggerService.kt @@ -21,10 +21,12 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.InternalCoroutinesApi +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.handleCoroutineException import kotlinx.coroutines.newCoroutineContext import kotlinx.coroutines.reactor.ReactorContext import kotlinx.coroutines.reactor.asCoroutineContext +import kotlinx.coroutines.withContext import org.apache.http.message.BasicHeader import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_INVOCATION_ID @@ -94,6 +96,32 @@ class RestLoggerService { } } +/** Used in Rest controller API methods to populate MDC context to nested coroutines from reactor web filter context. */ +suspend fun mdcWebCoroutineScope( + block: suspend CoroutineScope.() -> T +) = coroutineScope { + val reactorContext = this.coroutineContext[ReactorContext] + /** Populate MDC context only if present in Reactor Context */ + val newContext = if (reactorContext != null && + !reactorContext.context.isEmpty && + reactorContext.context.hasKey(MDCContext) + ) { + val mdcContext = reactorContext.context.get(MDCContext) + if (mdcContext != null) + newCoroutineContext(this.coroutineContext + reactorContext + mdcContext) + else + newCoroutineContext(this.coroutineContext + reactorContext) + } else this.coroutineContext + // Execute the block with new and old context + withContext(newContext) { + block() + } +} + +@Deprecated( + message = "Now CDS supports Coruoutin rest controller", + replaceWith = ReplaceWith("mdcWebCoroutineScope") +) /** Used in Rest controller API methods to populate MDC context to nested coroutines from reactor web filter context. */ @UseExperimental(InternalCoroutinesApi::class) fun monoMdc( -- cgit 1.2.3-korg