Renaming Files having BluePrint to have Blueprint
[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
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants.ONAP_INVOCATION_ID
28 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants.ONAP_PARTNER_NAME
29 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants.ONAP_REQUEST_ID
30 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToEmpty
32 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToUUID
33 import org.onap.ccsdk.cds.controllerblueprints.core.logger
34 import org.slf4j.MDC
35 import java.net.InetAddress
36 import java.net.InetSocketAddress
37 import java.time.ZoneOffset
38 import java.time.ZonedDateTime
39 import java.time.format.DateTimeFormatter
40 import java.util.UUID
41
42 class GrpcLoggerService {
43
44     private val log = logger(GrpcLoggerService::class)
45
46     /** Used when server receives request */
47     fun <ReqT : Any, RespT : Any> grpcRequesting(
48         call: ServerCall<ReqT, RespT>,
49         headers: Metadata,
50         next: ServerCallHandler<ReqT, RespT>
51     ) {
52         val requestID = headers.getStringKey(ONAP_REQUEST_ID).defaultToUUID()
53         val invocationID = headers.getStringKey(ONAP_INVOCATION_ID).defaultToUUID()
54         val partnerName = headers.getStringKey(ONAP_PARTNER_NAME) ?: "UNKNOWN"
55         grpcRequesting(requestID, invocationID, partnerName, call)
56     }
57
58     fun <ReqT : Any, RespT : Any> grpcRequesting(
59         call: ServerCall<ReqT, RespT>,
60         headers: CommonHeader,
61         next: ServerCallHandler<ReqT, RespT>
62     ) {
63         val requestID = headers.requestId.defaultToUUID()
64         val invocationID = headers.subRequestId.defaultToUUID()
65         val partnerName = headers.originatorId ?: "UNKNOWN"
66         grpcRequesting(requestID, invocationID, partnerName, call)
67     }
68
69     fun <ReqT : Any, RespT : Any> grpcRequesting(
70         requestID: String,
71         invocationID: String,
72         partnerName: String,
73         call: ServerCall<ReqT, RespT>
74     ) {
75         val localhost = InetAddress.getLocalHost()
76
77         val clientSocketAddress = call.attributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR) as? InetSocketAddress
78             ?: throw BlueprintProcessorException("failed to get client address")
79         val serviceName = call.methodDescriptor.fullMethodName
80
81         MDC.put("InvokeTimestamp", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT))
82         MDC.put("RequestID", requestID)
83         MDC.put("InvocationID", invocationID)
84         MDC.put("PartnerName", partnerName)
85         MDC.put("ClientIPAddress", clientSocketAddress.address.defaultToEmpty())
86         MDC.put("ServerFQDN", localhost.hostName.defaultToEmpty())
87         MDC.put("ServiceName", serviceName)
88         log.trace("MDC Properties : ${MDC.getCopyOfContextMap()}")
89     }
90
91     /** Used before invoking any GRPC outbound request, Inbound Invocation ID is used as request Id
92      * for outbound Request, If invocation Id is missing then default Request Id will be generated.
93      */
94     fun grpcInvoking(requestHeader: Metadata) {
95         requestHeader.putStringKeyValue(ONAP_REQUEST_ID, MDC.get("InvocationID").defaultToUUID())
96         requestHeader.putStringKeyValue(ONAP_INVOCATION_ID, UUID.randomUUID().toString())
97         requestHeader.putStringKeyValue(ONAP_PARTNER_NAME, BlueprintConstants.APP_NAME)
98     }
99
100     /** Used when server returns response */
101     fun grpResponding(requestHeaders: Metadata, responseHeaders: Metadata) {
102         try {
103             responseHeaders.putStringKeyValue(ONAP_REQUEST_ID, MDC.get("RequestID").defaultToEmpty())
104             responseHeaders.putStringKeyValue(ONAP_INVOCATION_ID, MDC.get("InvocationID").defaultToEmpty())
105             responseHeaders.putStringKeyValue(ONAP_PARTNER_NAME, MDC.get("PartnerName").defaultToEmpty())
106         } catch (e: Exception) {
107             log.warn("couldn't set grpc response headers", e)
108         }
109     }
110 }