1b932480a290e5458bbf8ca3c0522b39f3b634a4
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / grpc-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / grpc / service / GrpcLoggerService.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.service
18
19 import io.grpc.Grpc
20 import io.grpc.Metadata
21 import io.grpc.ServerCall
22 import io.grpc.ServerCallHandler
23 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.getStringKey
24 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.putStringKeyValue
25 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_INVOCATION_ID
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_PARTNER_NAME
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants.ONAP_REQUEST_ID
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
30 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToEmpty
31 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToUUID
32 import org.onap.ccsdk.cds.controllerblueprints.core.logger
33 import org.slf4j.MDC
34 import java.net.InetSocketAddress
35 import java.time.ZoneOffset
36 import java.time.ZonedDateTime
37 import java.time.format.DateTimeFormatter
38 import java.util.*
39
40 class GrpcLoggerService {
41
42     private val log = logger(GrpcLoggerService::class)
43
44     /** Used when server receives request */
45     fun <ReqT : Any, RespT : Any> grpcRequesting(call: ServerCall<ReqT, RespT>,
46                                                  headers: Metadata, next: ServerCallHandler<ReqT, RespT>) {
47         val requestID = headers.getStringKey(ONAP_REQUEST_ID).defaultToUUID()
48         val invocationID = headers.getStringKey(ONAP_INVOCATION_ID).defaultToUUID()
49         val partnerName = headers.getStringKey(ONAP_PARTNER_NAME).defaultToUUID()
50         grpcRequesting(requestID, invocationID, partnerName, call)
51     }
52
53     fun <ReqT : Any, RespT : Any> grpcRequesting(call: ServerCall<ReqT, RespT>,
54                                                  headers: CommonHeader, next: ServerCallHandler<ReqT, RespT>) {
55         val requestID = headers.requestId.defaultToUUID()
56         val invocationID = headers.subRequestId.defaultToUUID()
57         val partnerName = headers.originatorId.defaultToUUID()
58         grpcRequesting(requestID, invocationID, partnerName, call)
59     }
60
61     fun <ReqT : Any, RespT : Any> grpcRequesting(requestID: String, invocationID: String, partnerName: String,
62                                                  call: ServerCall<ReqT, RespT>) {
63         val clientSocketAddress = call.attributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR) as? InetSocketAddress
64                 ?: throw BluePrintProcessorException("failed to get client address")
65         val serviceName = call.methodDescriptor.fullMethodName
66
67         MDC.put("InvokeTimestamp", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT))
68         MDC.put("RequestID", requestID)
69         MDC.put("InvocationID", invocationID)
70         MDC.put("PartnerName", partnerName)
71         MDC.put("ClientIPAddress", clientSocketAddress.address.defaultToEmpty())
72         MDC.put("ServerFQDN", clientSocketAddress.address.hostName.defaultToEmpty())
73         MDC.put("ServiceName", serviceName)
74         log.trace("MDC Properties : ${MDC.getCopyOfContextMap()}")
75     }
76
77
78     /** Used before invoking any GRPC outbound request, Inbound Invocation ID is used as request Id
79      * for outbound Request, If invocation Id is missing then default Request Id will be generated.
80      */
81     fun grpcInvoking(requestHeader: Metadata) {
82         requestHeader.putStringKeyValue(ONAP_REQUEST_ID, MDC.get("InvocationID").defaultToUUID())
83         requestHeader.putStringKeyValue(ONAP_INVOCATION_ID, UUID.randomUUID().toString())
84         val partnerName = System.getProperty("APPNAME") ?: "BlueprintsProcessor"
85         requestHeader.putStringKeyValue(ONAP_PARTNER_NAME, partnerName)
86     }
87
88     /** Used when server returns response */
89     fun grpResponding(requestHeaders: Metadata, responseHeaders: Metadata) {
90         try {
91             responseHeaders.putStringKeyValue(ONAP_REQUEST_ID, MDC.get("RequestID").defaultToEmpty())
92             responseHeaders.putStringKeyValue(ONAP_INVOCATION_ID, MDC.get("InvocationID").defaultToEmpty())
93             responseHeaders.putStringKeyValue(ONAP_PARTNER_NAME, MDC.get("PartnerName").defaultToEmpty())
94         } catch (e: Exception) {
95             log.warn("couldn't set grpc response headers", e)
96         }
97     }
98 }