Upgrade spring boot parent and hazelcast
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / rest-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / rest / service / RestLoggerService.kt
index 8c7ba65..7442366 100644 (file)
@@ -29,12 +29,13 @@ 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
-import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants.ONAP_ORIGINATOR_ID
-import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants.ONAP_PARTNER_NAME
-import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants.ONAP_REQUEST_ID
-import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants.ONAP_SUBREQUEST_ID
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_INVOCATION_ID
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_ORIGINATOR_ID
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_PARTNER_NAME
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_REQUEST_ID
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_SUBREQUEST_ID
 import org.onap.ccsdk.cds.controllerblueprints.core.MDCContext
 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToEmpty
 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToUUID
@@ -65,7 +66,7 @@ class RestLoggerService {
         fun httpInvoking(headers: Array<BasicHeader>) {
             headers.plusElement(BasicHeader(ONAP_REQUEST_ID, MDC.get("InvocationID").defaultToUUID()))
             headers.plusElement(BasicHeader(ONAP_INVOCATION_ID, UUID.randomUUID().toString()))
-            headers.plusElement(BasicHeader(ONAP_PARTNER_NAME, BlueprintConstants.APP_NAME))
+            headers.plusElement(BasicHeader(ONAP_PARTNER_NAME, BluePrintConstants.APP_NAME))
         }
     }
 
@@ -98,7 +99,7 @@ class RestLoggerService {
             resHeaders[ONAP_SUBREQUEST_ID] = MDC.get("SubRequestID")
             resHeaders[ONAP_ORIGINATOR_ID] = MDC.get("OriginatorID")
             resHeaders[ONAP_INVOCATION_ID] = MDC.get("InvocationID")
-            resHeaders[ONAP_PARTNER_NAME] = BlueprintConstants.APP_NAME
+            resHeaders[ONAP_PARTNER_NAME] = BluePrintConstants.APP_NAME
         } catch (e: Exception) {
             log.warn("couldn't set response headers", e)
         } finally {
@@ -107,8 +108,12 @@ class RestLoggerService {
     }
 }
 
-/** Used in Rest controller API methods to populate MDC context to nested coroutines from reactor web filter context. */
+/**
+ * Used in Rest controller API methods to populate MDC context to nested coroutines from reactor web filter context.
+ * @param executionServiceInput (Optional) Used to override values populated in the MDC Context
+ */
 suspend fun <T> mdcWebCoroutineScope(
+    executionServiceInput: ExecutionServiceInput? = null,
     block: suspend CoroutineScope.() -> T
 ) = coroutineScope {
     val reactorContext = this.coroutineContext[ReactorContext]
@@ -118,12 +123,22 @@ suspend fun <T> mdcWebCoroutineScope(
         !reactorContext.context.isEmpty &&
         reactorContext.context.hasKey(MDCContext)
     ) {
-        val mdcContext = reactorContext.context.get<MDCContext>(MDCContext)
+        val mdcContext = if (executionServiceInput != null) {
+            // MDC Context with overridden request ID
+            MDC.put("RequestID", executionServiceInput.commonHeader.requestId)
+            MDC.put("SubRequestID", executionServiceInput.commonHeader.subRequestId)
+            MDC.put("OriginatorID", executionServiceInput.commonHeader.originatorId)
+            MDCContext(MDC.getCopyOfContextMap())
+        } else {
+            // Default MDC Context
+            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()
@@ -163,7 +178,7 @@ fun <T> monoMdc(
 class MonoMDCCoroutine<in T>(
     parentContext: CoroutineContext,
     private val sink: MonoSink<T>
-) : AbstractCoroutine<T>(parentContext, true), Disposable {
+) : AbstractCoroutine<T>(parentContext, true, true), Disposable {
 
     private var disposed = false