2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.message.service
19 import kotlinx.coroutines.channels.Channel
20 import kotlinx.coroutines.launch
21 import kotlinx.coroutines.runBlocking
22 import org.apache.kafka.clients.CommonClientConfigs
23 import org.apache.kafka.clients.consumer.Consumer
24 import org.apache.kafka.clients.consumer.ConsumerConfig
25 import org.apache.kafka.clients.consumer.KafkaConsumer
26 import org.apache.kafka.common.serialization.StringDeserializer
27 import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaBasicAuthMessageConsumerProperties
28 import org.onap.ccsdk.cds.controllerblueprints.core.logger
29 import java.time.Duration
30 import kotlin.concurrent.thread
32 class KafkaBasicAuthMessageConsumerService(
33 private val messageConsumerProperties: KafkaBasicAuthMessageConsumerProperties)
34 : BlueprintMessageConsumerService {
36 private val channel = Channel<String>()
37 private var kafkaConsumer: Consumer<String, String>? = null
38 val log = logger(KafkaBasicAuthMessageConsumerService::class)
43 fun kafkaConsumer(additionalConfig: Map<String, Any>? = null): Consumer<String, String> {
44 val configProperties = hashMapOf<String, Any>()
45 configProperties[CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG] = messageConsumerProperties.bootstrapServers
46 configProperties[ConsumerConfig.GROUP_ID_CONFIG] = messageConsumerProperties.groupId
47 configProperties[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "latest"
48 configProperties[ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java
49 configProperties[ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG] = StringDeserializer::class.java
50 /** add or override already set properties */
51 additionalConfig?.let { configProperties.putAll(it) }
52 /** Create Kafka consumer */
53 return KafkaConsumer(configProperties)
56 override suspend fun subscribe(additionalConfig: Map<String, Any>?): Channel<String> {
57 /** get to topic names */
58 val consumerTopic = messageConsumerProperties.consumerTopic?.split(",")?.map { it.trim() }
59 check(!consumerTopic.isNullOrEmpty()) { "couldn't get topic information" }
60 return subscribe(consumerTopic, additionalConfig)
64 override suspend fun subscribe(consumerTopic: List<String>, additionalConfig: Map<String, Any>?): Channel<String> {
65 /** Create Kafka consumer */
66 kafkaConsumer = kafkaConsumer(additionalConfig)
67 checkNotNull(kafkaConsumer) {
68 "failed to create kafka consumer for " +
69 "server(${messageConsumerProperties.bootstrapServers})'s " +
70 "topics(${messageConsumerProperties.bootstrapServers})"
73 kafkaConsumer!!.subscribe(consumerTopic)
74 log.info("Successfully consumed topic($consumerTopic)")
76 val listenerThread = thread(start = true, name = "KafkaConsumer") {
78 kafkaConsumer!!.use { kc ->
80 val consumerRecords = kc.poll(Duration.ofMillis(messageConsumerProperties.pollMillSec))
82 consumerRecords?.forEach { consumerRecord ->
83 /** execute the command block */
84 consumerRecord.value()?.let {
86 if (!channel.isClosedForSend) {
89 log.error("Channel is closed to receive message")
99 log.info("Successfully consumed in thread(${listenerThread})")
103 override suspend fun shutDown() {
104 /** Close the Channel */
106 /** stop the polling loop */
108 if (kafkaConsumer != null) {
109 /** sunsubscribe the consumer */
110 kafkaConsumer!!.unsubscribe()