04b754b130f13420d45c61eb527c30feb740ce88
[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.message.service
18
19 import org.apache.kafka.clients.consumer.ConsumerRecord
20 import org.apache.kafka.common.header.Headers
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
22 import org.onap.ccsdk.cds.blueprintsprocessor.message.addHeader
23 import org.onap.ccsdk.cds.blueprintsprocessor.message.toMap
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToEmpty
26 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToUUID
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.slf4j.MDC
29 import java.net.InetAddress
30 import java.time.Instant
31 import java.time.ZoneOffset
32 import java.time.ZonedDateTime
33 import java.time.format.DateTimeFormatter
34 import java.util.UUID
35
36 class MessageLoggerService {
37
38     private val log = logger(MessageLoggerService::class)
39
40     fun messageConsuming(headers: CommonHeader, consumerRecord: ConsumerRecord<*, *>) {
41         messageConsuming(
42             headers.requestId, headers.subRequestId,
43             headers.originatorId, consumerRecord
44         )
45     }
46
47     fun messageConsuming(consumerRecord: ConsumerRecord<*, *>) {
48         val headers = consumerRecord.headers().toMap()
49         val requestID = headers[BluePrintConstants.ONAP_REQUEST_ID].defaultToUUID()
50         val invocationID = headers[BluePrintConstants.ONAP_INVOCATION_ID].defaultToUUID()
51         val partnerName = headers[BluePrintConstants.ONAP_PARTNER_NAME] ?: "UNKNOWN"
52         messageConsuming(requestID, invocationID, partnerName, consumerRecord)
53     }
54
55     fun messageConsuming(
56         requestID: String,
57         invocationID: String,
58         partnerName: String,
59         consumerRecord: ConsumerRecord<*, *>
60     ) {
61         val headers = consumerRecord.headers().toMap()
62         val localhost = InetAddress.getLocalHost()
63         MDC.put(
64             "InvokeTimestamp", ZonedDateTime
65                 .ofInstant(Instant.ofEpochMilli(consumerRecord.timestamp()), ZoneOffset.UTC)
66                 .format(DateTimeFormatter.ISO_INSTANT)
67         )
68         MDC.put("RequestID", requestID)
69         MDC.put("InvocationID", invocationID)
70         MDC.put("PartnerName", partnerName)
71         MDC.put("ClientIPAddress", headers["ClientIPAddress"].defaultToEmpty())
72         MDC.put("ServerFQDN", localhost.hostName.defaultToEmpty())
73         MDC.put("ServiceName", consumerRecord.topic())
74         // Custom MDC for Message Consumers
75         MDC.put("Offset", consumerRecord.offset().toString())
76         MDC.put("MessageKey", consumerRecord.key()?.toString().defaultToEmpty())
77         log.info("Consuming MDC Properties : ${MDC.getCopyOfContextMap()}")
78     }
79
80     /** Used before producing message request, Inbound Invocation ID is used as request Id
81      * for produced message Request, If invocation Id is missing then default Request Id will be generated.
82      */
83     fun messageProducing(requestHeader: Headers) {
84         val localhost = InetAddress.getLocalHost()
85         requestHeader.addHeader(BluePrintConstants.ONAP_REQUEST_ID, MDC.get("InvocationID").defaultToUUID())
86         requestHeader.addHeader(BluePrintConstants.ONAP_INVOCATION_ID, UUID.randomUUID().toString())
87         requestHeader.addHeader(BluePrintConstants.ONAP_PARTNER_NAME, BluePrintConstants.APP_NAME)
88         requestHeader.addHeader("ClientIPAddress", localhost.hostAddress)
89     }
90
91     fun messageConsumingExisting() {
92         MDC.clear()
93     }
94 }