076501eab99ebb30bdaea320e09b8a94c306dc52
[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 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
31
32 class KafkaBasicAuthMessageConsumerService(
33         private val messageConsumerProperties: KafkaBasicAuthMessageConsumerProperties)
34     : BlueprintMessageConsumerService {
35
36     private val channel = Channel<String>()
37     private var kafkaConsumer: Consumer<String, String>? = null
38     val log = logger(KafkaBasicAuthMessageConsumerService::class)
39
40     @Volatile
41     var keepGoing = true
42
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)
54     }
55
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)
61     }
62
63
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})"
71         }
72
73         kafkaConsumer!!.subscribe(consumerTopic)
74         log.info("Successfully consumed topic($consumerTopic)")
75
76         val listenerThread = thread(start = true, name = "KafkaConsumer") {
77             keepGoing = true
78             kafkaConsumer!!.use { kc ->
79                 while (keepGoing) {
80                     val consumerRecords = kc.poll(Duration.ofMillis(messageConsumerProperties.pollMillSec))
81                     runBlocking {
82                         consumerRecords?.forEach { consumerRecord ->
83                             /** execute the command block */
84                             consumerRecord.value()?.let {
85                                 launch {
86                                     if (!channel.isClosedForSend) {
87                                         channel.send(it)
88                                     } else {
89                                         log.error("Channel is closed to receive message")
90                                     }
91                                 }
92                             }
93                         }
94                     }
95                 }
96             }
97
98         }
99         log.info("Successfully consumed in thread(${listenerThread})")
100         return channel
101     }
102
103     override suspend fun shutDown() {
104         /** Close the Channel */
105         channel.close()
106         /** stop the polling loop */
107         keepGoing = false
108         if (kafkaConsumer != null) {
109             /** sunsubscribe the consumer */
110             kafkaConsumer!!.unsubscribe()
111         }
112     }
113 }