2 * Copyright © 2018-2019 AT&T Intellectual Property.
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.functions.message.prioritization.kafka
19 import org.apache.kafka.common.serialization.Serdes
20 import org.apache.kafka.streams.Topology
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.MessagePrioritizationConstants
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.MessagePrioritizationService
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.PrioritizationConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.utils.MessageProcessorUtils.bluePrintProcessorSupplier
25 import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsBasicAuthConsumerProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BluePrintMessageLibPropertyService
28 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageConsumerService
29 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.KafkaStreamConsumerFunction
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.logger
32 import org.onap.ccsdk.cds.controllerblueprints.core.splitCommaAsList
34 open class KafkaMessagePrioritizationConsumer(
35 private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService,
36 private val kafkaMessagePrioritizationService: MessagePrioritizationService
39 private val log = logger(KafkaMessagePrioritizationConsumer::class)
41 private lateinit var streamingConsumerService: BlueprintMessageConsumerService
43 open fun consumerService(selector: String): BlueprintMessageConsumerService {
44 return bluePrintMessageLibPropertyService
45 .blueprintMessageConsumerService(selector)
48 open fun kafkaStreamConsumerFunction(prioritizationConfiguration: PrioritizationConfiguration):
49 KafkaStreamConsumerFunction {
50 return object : KafkaStreamConsumerFunction {
52 val kafkaConsumerConfiguration = prioritizationConfiguration.kafkaConfiguration
53 ?: throw BluePrintProcessorException("failed to get kafka consumer configuration")
55 override suspend fun createTopology(
56 messageConsumerProperties: MessageConsumerProperties,
57 additionalConfig: Map<String, Any>?
60 val topology = Topology()
61 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
62 as KafkaStreamsBasicAuthConsumerProperties
64 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.splitCommaAsList()
65 log.info("Consuming prioritization topics($topics)")
67 topology.addSource(MessagePrioritizationConstants.SOURCE_INPUT, *topics.toTypedArray())
69 topology.addProcessor(
70 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
71 bluePrintProcessorSupplier<ByteArray, ByteArray>(
72 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
74 MessagePrioritizationConstants.SOURCE_INPUT
77 /** To receive completed and error messages */
79 MessagePrioritizationConstants.SINK_OUTPUT,
80 kafkaConsumerConfiguration.outputTopic,
81 Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
82 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
85 // Output will be sent to the group-output topic from Processor API
91 suspend fun startConsuming(prioritizationConfiguration: PrioritizationConfiguration) {
93 val kafkaConsumerConfiguration = prioritizationConfiguration.kafkaConfiguration
94 ?: throw BluePrintProcessorException("failed to get kafka consumer configuration")
96 streamingConsumerService = consumerService(kafkaConsumerConfiguration.inputTopicSelector)
98 // Dynamic Consumer Function to create Topology
99 val consumerFunction = kafkaStreamConsumerFunction(prioritizationConfiguration)
100 streamingConsumerService.consume(null, consumerFunction)
103 suspend fun shutDown() {
104 if (::streamingConsumerService.isInitialized) {
105 streamingConsumerService.shutDown()