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.PrioritizationConfiguration
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.utils.MessageProcessorUtils.bluePrintProcessorSupplier
24 import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsBasicAuthConsumerProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BluePrintMessageLibPropertyService
27 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageConsumerService
28 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.KafkaStreamConsumerFunction
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
30 import org.onap.ccsdk.cds.controllerblueprints.core.logger
31 import org.onap.ccsdk.cds.controllerblueprints.core.splitCommaAsList
33 open class MessagePrioritizationConsumer(
34 private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
37 private val log = logger(MessagePrioritizationConsumer::class)
39 lateinit var streamingConsumerService: BlueprintMessageConsumerService
41 open fun consumerService(selector: String): BlueprintMessageConsumerService {
42 return bluePrintMessageLibPropertyService
43 .blueprintMessageConsumerService(selector)
46 open fun kafkaStreamConsumerFunction(prioritizationConfiguration: PrioritizationConfiguration):
47 KafkaStreamConsumerFunction {
48 return object : KafkaStreamConsumerFunction {
50 val kafkaConsumerConfiguration = prioritizationConfiguration.kafkaConfiguration
51 ?: throw BluePrintProcessorException("failed to get kafka consumer configuration")
53 override suspend fun createTopology(
54 messageConsumerProperties: MessageConsumerProperties,
55 additionalConfig: Map<String, Any>?
58 val topology = Topology()
59 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
60 as KafkaStreamsBasicAuthConsumerProperties
62 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.splitCommaAsList()
63 log.info("Consuming prioritization topics($topics)")
65 topology.addSource(MessagePrioritizationConstants.SOURCE_INPUT, *topics.toTypedArray())
67 topology.addProcessor(
68 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
69 bluePrintProcessorSupplier<ByteArray, ByteArray>(
70 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
71 prioritizationConfiguration
73 MessagePrioritizationConstants.SOURCE_INPUT
76 /** To receive completed and error messages */
78 MessagePrioritizationConstants.SINK_OUTPUT,
79 kafkaConsumerConfiguration.outputTopic,
80 Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
81 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
84 // Output will be sent to the group-output topic from Processor API
90 suspend fun startConsuming(prioritizationConfiguration: PrioritizationConfiguration) {
92 val kafkaConsumerConfiguration = prioritizationConfiguration.kafkaConfiguration
93 ?: throw BluePrintProcessorException("failed to get kafka consumer configuration")
95 streamingConsumerService = consumerService(kafkaConsumerConfiguration.inputTopicSelector)
97 // Dynamic Consumer Function to create Topology
98 val consumerFunction = kafkaStreamConsumerFunction(prioritizationConfiguration)
99 streamingConsumerService.consume(null, consumerFunction)
102 suspend fun shutDown() {
103 if (streamingConsumerService != null) {
104 streamingConsumerService.shutDown()