b1d8abd162082b88862395576a073a359172761e
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  * Modifications Copyright © 2020 Bell Canada.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.rest.service
19
20 import kotlinx.coroutines.AbstractCoroutine
21 import kotlinx.coroutines.CoroutineScope
22 import kotlinx.coroutines.CoroutineStart
23 import kotlinx.coroutines.GlobalScope
24 import kotlinx.coroutines.InternalCoroutinesApi
25 import kotlinx.coroutines.coroutineScope
26 import kotlinx.coroutines.handleCoroutineException
27 import kotlinx.coroutines.newCoroutineContext
28 import kotlinx.coroutines.reactor.ReactorContext
29 import kotlinx.coroutines.reactor.asCoroutineContext
30 import kotlinx.coroutines.withContext
31 import org.apache.http.message.BasicHeader
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
33 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
34 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_INVOCATION_ID
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_ORIGINATOR_ID
36 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_PARTNER_NAME
37 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_REQUEST_ID
38 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_SUBREQUEST_ID
39 import org.onap.ccsdk.cds.controllerblueprints.core.MDCContext
40 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToEmpty
41 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToUUID
42 import org.onap.ccsdk.cds.controllerblueprints.core.logger
43 import org.slf4j.MDC
44 import org.springframework.http.server.reactive.ServerHttpRequest
45 import org.springframework.http.server.reactive.ServerHttpResponse
46 import reactor.core.Disposable
47 import reactor.core.publisher.Mono
48 import reactor.core.publisher.MonoSink
49 import java.net.InetAddress
50 import java.time.ZoneOffset
51 import java.time.ZonedDateTime
52 import java.time.format.DateTimeFormatter
53 import java.util.UUID
54 import kotlin.coroutines.CoroutineContext
55 import kotlin.coroutines.EmptyCoroutineContext
56
57 class RestLoggerService {
58
59     private val log = logger(RestLoggerService::class)
60
61     companion object {
62
63         /** Used before invoking any REST outbound request, Inbound Invocation ID is used as request Id
64          * for outbound Request, If invocation Id is missing then default Request Id will be generated.
65          */
66         fun httpInvoking(headers: Array<BasicHeader>) {
67             headers.plusElement(BasicHeader(ONAP_REQUEST_ID, MDC.get("InvocationID").defaultToUUID()))
68             headers.plusElement(BasicHeader(ONAP_INVOCATION_ID, UUID.randomUUID().toString()))
69             headers.plusElement(BasicHeader(ONAP_PARTNER_NAME, BluePrintConstants.APP_NAME))
70         }
71     }
72
73     fun entering(request: ServerHttpRequest) {
74         val localhost = InetAddress.getLocalHost()
75         val headers = request.headers
76         val requestID = headers.getFirst(ONAP_REQUEST_ID).defaultToUUID()
77         val subrequestID = headers.getFirst(ONAP_SUBREQUEST_ID).defaultToEmpty()
78         val originatorID = headers.getFirst(ONAP_ORIGINATOR_ID).defaultToEmpty()
79         val invocationID = headers.getFirst(ONAP_INVOCATION_ID).defaultToUUID()
80         val partnerName = headers.getFirst(ONAP_PARTNER_NAME).defaultToEmpty()
81         MDC.put("InvokeTimestamp", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT))
82         MDC.put("RequestID", requestID)
83         MDC.put("SubRequestID", subrequestID)
84         MDC.put("OriginatorID", originatorID)
85         MDC.put("InvocationID", invocationID)
86         MDC.put("PartnerName", partnerName)
87         MDC.put("ClientIPAddress", request.remoteAddress?.address?.hostAddress.defaultToEmpty())
88         MDC.put("ServerFQDN", localhost.hostName.defaultToEmpty())
89         if (MDC.get("ServiceName") == null || MDC.get("ServiceName").equals("", ignoreCase = true)) {
90             MDC.put("ServiceName", request.uri.path)
91         }
92     }
93
94     fun exiting(request: ServerHttpRequest, response: ServerHttpResponse) {
95         try {
96             val reqHeaders = request.headers
97             val resHeaders = response.headers
98             resHeaders[ONAP_REQUEST_ID] = MDC.get("RequestID")
99             resHeaders[ONAP_SUBREQUEST_ID] = MDC.get("SubRequestID")
100             resHeaders[ONAP_ORIGINATOR_ID] = MDC.get("OriginatorID")
101             resHeaders[ONAP_INVOCATION_ID] = MDC.get("InvocationID")
102             resHeaders[ONAP_PARTNER_NAME] = BluePrintConstants.APP_NAME
103         } catch (e: Exception) {
104             log.warn("couldn't set response headers", e)
105         } finally {
106             MDC.clear()
107         }
108     }
109 }
110
111 /**
112  * Used in Rest controller API methods to populate MDC context to nested coroutines from reactor web filter context.
113  * @param executionServiceInput (Optional) Used to override values populated in the MDC Context
114  */
115 suspend fun <T> mdcWebCoroutineScope(
116     executionServiceInput: ExecutionServiceInput? = null,
117     block: suspend CoroutineScope.() -> T
118 ) = coroutineScope {
119     val reactorContext = this.coroutineContext[ReactorContext]
120
121     /** Populate MDC context only if present in Reactor Context */
122     val newContext = if (reactorContext != null &&
123         !reactorContext.context.isEmpty &&
124         reactorContext.context.hasKey(MDCContext)
125     ) {
126         val mdcContext = if (executionServiceInput != null) {
127             // MDC Context with overridden request ID
128             MDC.put("RequestID", executionServiceInput.commonHeader.requestId)
129             MDCContext(MDC.getCopyOfContextMap())
130         } else {
131             // Default MDC Context
132             reactorContext.context.get(MDCContext)
133         }
134         if (mdcContext != null)
135             newCoroutineContext(this.coroutineContext + reactorContext + mdcContext)
136         else
137             newCoroutineContext(this.coroutineContext + reactorContext)
138     } else this.coroutineContext
139
140     // Execute the block with new and old context
141     withContext(newContext) {
142         block()
143     }
144 }
145
146 @Deprecated(
147     message = "Now CDS supports Coruoutin rest controller",
148     replaceWith = ReplaceWith("mdcWebCoroutineScope")
149 )
150 /** Used in Rest controller API methods to populate MDC context to nested coroutines from reactor web filter context. */
151 @UseExperimental(InternalCoroutinesApi::class)
152 fun <T> monoMdc(
153     context: CoroutineContext = EmptyCoroutineContext,
154     block: suspend CoroutineScope.() -> T?
155 ): Mono<T> = Mono.create { sink ->
156
157     val reactorContext = (
158         context[ReactorContext]?.context?.putAll(sink.currentContext())
159             ?: sink.currentContext()
160         ).asCoroutineContext()
161
162     /** Populate MDC context only if present in Reactor Context */
163     val newContext = if (!reactorContext.context.isEmpty &&
164         reactorContext.context.hasKey(MDCContext)
165     ) {
166         val mdcContext = reactorContext.context.get<MDCContext>(MDCContext)
167         GlobalScope.newCoroutineContext(context + reactorContext + mdcContext)
168     } else GlobalScope.newCoroutineContext(context + reactorContext)
169
170     val coroutine = MonoMDCCoroutine(newContext, sink)
171     sink.onDispose(coroutine)
172     coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
173 }
174
175 @InternalCoroutinesApi
176 class MonoMDCCoroutine<in T>(
177     parentContext: CoroutineContext,
178     private val sink: MonoSink<T>
179 ) : AbstractCoroutine<T>(parentContext, true), Disposable {
180
181     private var disposed = false
182
183     override fun onCompleted(value: T) {
184         if (!disposed) {
185             if (value == null) sink.success() else sink.success(value)
186         }
187     }
188
189     override fun onCancelled(cause: Throwable, handled: Boolean) {
190         if (!disposed) {
191             sink.error(cause)
192         } else if (!handled) {
193             handleCoroutineException(context, cause)
194         }
195     }
196
197     override fun dispose() {
198         disposed = true
199         cancel()
200     }
201
202     override fun isDisposed(): Boolean = disposed
203 }