Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / service / KafkaStreamsConsumerService.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.message.service
18
19 import kotlinx.coroutines.channels.Channel
20 import org.apache.kafka.clients.consumer.ConsumerRecord
21 import org.apache.kafka.streams.KafkaStreams
22 import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.logger
25 import java.util.Properties
26
27 open class KafkaStreamsConsumerService(private val messageConsumerProperties: MessageConsumerProperties) :
28     BlueprintMessageConsumerService {
29
30     val log = logger(KafkaStreamsConsumerService::class)
31     lateinit var kafkaStreams: KafkaStreams
32
33     private fun streamsConfig(additionalConfig: Map<String, Any>? = null): Properties {
34         val configProperties = Properties()
35         /** set consumer properties */
36         messageConsumerProperties.getConfig().let { configProperties.putAll(it) }
37         /** add or override already set properties */
38         additionalConfig?.let { configProperties.putAll(it) }
39         /** Create Kafka consumer */
40         return configProperties
41     }
42
43     override suspend fun subscribe(additionalConfig: Map<String, Any>?): Channel<ConsumerRecord<String, ByteArray>> {
44         throw BluePrintProcessorException("not implemented")
45     }
46
47     override suspend fun subscribe(topics: List<String>, additionalConfig: Map<String, Any>?): Channel<ConsumerRecord<String, ByteArray>> {
48         throw BluePrintProcessorException("not implemented")
49     }
50
51     override suspend fun consume(additionalConfig: Map<String, Any>?, consumerFunction: ConsumerFunction) {
52         val streamsConfig = streamsConfig(additionalConfig)
53         val kafkaStreamConsumerFunction = consumerFunction as KafkaStreamConsumerFunction
54         val topology = kafkaStreamConsumerFunction.createTopology(messageConsumerProperties, additionalConfig)
55         log.info("Kafka streams topology : ${topology.describe()}")
56         kafkaStreams = KafkaStreams(topology, streamsConfig)
57         kafkaStreams.cleanUp()
58         kafkaStreams.start()
59         kafkaStreams.localThreadsMetadata().forEach { data -> log.info("Topology : $data") }
60     }
61
62     override suspend fun shutDown() {
63         if (kafkaStreams != null) {
64             kafkaStreams.close()
65         }
66     }
67 }