e21d5d3ce2fcbadfbd982f260b148bd3a675b5fc
[ccsdk/cds.git] /
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.*
20 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.GrpcLoggerService
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
22 import org.onap.ccsdk.cds.controllerblueprints.core.logger
23 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintDownloadInput
24 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintRemoveInput
25 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput
26 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
27 import org.slf4j.MDC
28
29 class GrpcServerLoggingInterceptor : ServerInterceptor {
30     val log = logger(GrpcServerLoggingInterceptor::class)
31     val loggingService = GrpcLoggerService()
32
33     override fun <ReqT : Any, RespT : Any> interceptCall(call: ServerCall<ReqT, RespT>,
34                                                          requestHeaders: Metadata, next: ServerCallHandler<ReqT, RespT>)
35             : ServerCall.Listener<ReqT> {
36
37         val forwardingServerCall = object : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
38             override fun sendHeaders(responseHeaders: Metadata) {
39                 loggingService.grpResponding(requestHeaders, responseHeaders)
40                 super.sendHeaders(responseHeaders)
41             }
42         }
43
44         return object
45             : ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(
46                 next.startCall(forwardingServerCall, requestHeaders)) {
47
48             override fun onMessage(message: ReqT) {
49                 /** Get the requestId, SubRequestId and Originator Id and set in MDS context
50                  *  If you are using other GRPC services, Implement own Logging Interceptors to get tracing.
51                  * */
52                 when (message) {
53                     is ExecutionServiceInput -> {
54                         val commonHeader = message.commonHeader
55                                 ?: throw BluePrintProcessorException("missing common header in request")
56                         loggingService.grpcRequesting(call, commonHeader, next)
57                     }
58                     is BluePrintUploadInput -> {
59                         val commonHeader = message.commonHeader
60                                 ?: throw BluePrintProcessorException("missing common header in request")
61                         loggingService.grpcRequesting(call, commonHeader, next)
62                     }
63                     is BluePrintDownloadInput -> {
64                         val commonHeader = message.commonHeader
65                                 ?: throw BluePrintProcessorException("missing common header in request")
66                         loggingService.grpcRequesting(call, commonHeader, next)
67                     }
68                     is BluePrintRemoveInput -> {
69                         val commonHeader = message.commonHeader
70                                 ?: throw BluePrintProcessorException("missing common header in request")
71                         loggingService.grpcRequesting(call, commonHeader, next)
72                     }
73                     else -> {
74                         loggingService.grpcRequesting(call, requestHeaders, next)
75                     }
76                 }
77                 super.onMessage(message)
78             }
79
80             override fun onComplete() {
81                 MDC.clear()
82                 super.onComplete()
83             }
84
85             override fun onCancel() {
86                 MDC.clear()
87                 super.onCancel()
88             }
89
90         }
91     }
92 }