Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / grpc-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / grpc / interceptor / GrpcServerLoggingInterceptor.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.grpc.interceptor
18
19 import io.grpc.ForwardingServerCall
20 import io.grpc.ForwardingServerCallListener
21 import io.grpc.Metadata
22 import io.grpc.ServerCall
23 import io.grpc.ServerCallHandler
24 import io.grpc.ServerInterceptor
25 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.GrpcLoggerService
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintDownloadInput
29 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintRemoveInput
30 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput
31 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
32 import org.slf4j.MDC
33
34 class GrpcServerLoggingInterceptor : ServerInterceptor {
35     val log = logger(GrpcServerLoggingInterceptor::class)
36     val loggingService = GrpcLoggerService()
37
38     override fun <ReqT : Any, RespT : Any> interceptCall(
39         call: ServerCall<ReqT, RespT>,
40         requestHeaders: Metadata,
41         next: ServerCallHandler<ReqT, RespT>
42     ):
43             ServerCall.Listener<ReqT> {
44
45         val forwardingServerCall = object : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
46             override fun sendHeaders(responseHeaders: Metadata) {
47                 loggingService.grpResponding(requestHeaders, responseHeaders)
48                 super.sendHeaders(responseHeaders)
49             }
50         }
51
52         return object :
53             ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(
54                 next.startCall(forwardingServerCall, requestHeaders)
55             ) {
56
57             override fun onMessage(message: ReqT) {
58                 /** Get the requestId, SubRequestId and Originator Id and set in MDS context
59                  *  If you are using other GRPC services, Implement own Logging Interceptors to get tracing.
60                  * */
61                 when (message) {
62                     is ExecutionServiceInput -> {
63                         val commonHeader = message.commonHeader
64                             ?: throw BluePrintProcessorException("missing common header in request")
65                         loggingService.grpcRequesting(call, commonHeader, next)
66                     }
67                     is BluePrintUploadInput -> {
68                         val commonHeader = message.commonHeader
69                             ?: throw BluePrintProcessorException("missing common header in request")
70                         loggingService.grpcRequesting(call, commonHeader, next)
71                     }
72                     is BluePrintDownloadInput -> {
73                         val commonHeader = message.commonHeader
74                             ?: throw BluePrintProcessorException("missing common header in request")
75                         loggingService.grpcRequesting(call, commonHeader, next)
76                     }
77                     is BluePrintRemoveInput -> {
78                         val commonHeader = message.commonHeader
79                             ?: throw BluePrintProcessorException("missing common header in request")
80                         loggingService.grpcRequesting(call, commonHeader, next)
81                     }
82                     else -> {
83                         loggingService.grpcRequesting(call, requestHeaders, next)
84                     }
85                 }
86                 super.onMessage(message)
87             }
88
89             override fun onComplete() {
90                 MDC.clear()
91                 super.onComplete()
92             }
93
94             override fun onCancel() {
95                 MDC.clear()
96                 super.onCancel()
97             }
98         }
99     }
100 }