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