86c04f6be0b574a3369f33029080e6bf81def223
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
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.commons.lang.builder.ToStringBuilder
20 import org.apache.kafka.clients.producer.Callback
21 import org.apache.kafka.clients.producer.KafkaProducer
22 import org.apache.kafka.clients.producer.ProducerConfig.*
23 import org.apache.kafka.clients.producer.ProducerRecord
24 import org.apache.kafka.common.header.internals.RecordHeader
25 import org.apache.kafka.common.serialization.ByteArraySerializer
26 import org.apache.kafka.common.serialization.StringSerializer
27 import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaBasicAuthMessageProducerProperties
28 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
29 import org.onap.ccsdk.cds.controllerblueprints.core.defaultToUUID
30 import org.slf4j.LoggerFactory
31 import java.nio.charset.Charset
32
33 class KafkaBasicAuthMessageProducerService(
34         private val messageProducerProperties: KafkaBasicAuthMessageProducerProperties)
35     : BlueprintMessageProducerService {
36
37     private val log = LoggerFactory.getLogger(KafkaBasicAuthMessageProducerService::class.java)!!
38
39     private var kafkaProducer: KafkaProducer<String, ByteArray>? = null
40
41     override suspend fun sendMessageNB(message: Any): Boolean {
42         checkNotNull(messageProducerProperties.topic) { "default topic is not configured" }
43         return sendMessageNB(messageProducerProperties.topic!!, message)
44     }
45
46     override suspend fun sendMessageNB(message: Any, headers: MutableMap<String, String>?): Boolean {
47         checkNotNull(messageProducerProperties.topic) { "default topic is not configured" }
48         return sendMessageNB(messageProducerProperties.topic!!, message, headers)
49     }
50
51     override suspend fun sendMessageNB(topic: String, message: Any,
52                                        headers: MutableMap<String, String>?): Boolean {
53         val byteArrayMessage = when (message) {
54             is String -> message.toByteArray(Charset.defaultCharset())
55             else -> message.asJsonString().toByteArray(Charset.defaultCharset())
56         }
57
58         val record = ProducerRecord<String, ByteArray>(topic, defaultToUUID(), byteArrayMessage)
59         headers?.let {
60             headers.forEach { (key, value) -> record.headers().add(RecordHeader(key, value.toByteArray())) }
61         }
62         val callback = Callback { metadata, exception ->
63             log.info("message published offset(${metadata.offset()}, headers :$headers )")
64         }
65         messageTemplate().send(record, callback).get()
66         return true
67     }
68
69     fun messageTemplate(additionalConfig: Map<String, ByteArray>? = null): KafkaProducer<String, ByteArray> {
70         log.trace("Client Properties : ${ToStringBuilder.reflectionToString(messageProducerProperties)}")
71         val configProps = hashMapOf<String, Any>()
72         configProps[BOOTSTRAP_SERVERS_CONFIG] = messageProducerProperties.bootstrapServers
73         configProps[KEY_SERIALIZER_CLASS_CONFIG] = StringSerializer::class.java
74         configProps[VALUE_SERIALIZER_CLASS_CONFIG] = ByteArraySerializer::class.java
75         if (messageProducerProperties.clientId != null) {
76             configProps[CLIENT_ID_CONFIG] = messageProducerProperties.clientId!!
77         }
78         // TODO("Security Implementation based on type")
79
80         // Add additional Properties
81         if (additionalConfig != null) {
82             configProps.putAll(additionalConfig)
83         }
84
85         if (kafkaProducer == null) {
86             kafkaProducer = KafkaProducer(configProps)
87         }
88         return kafkaProducer!!
89     }
90 }
91