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.logger
30 import org.onap.ccsdk.cds.controllerblueprints.core.splitCommaAsList
32 open class MessagePrioritizationConsumer(
33 private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
36 private val log = logger(MessagePrioritizationConsumer::class)
38 lateinit var streamingConsumerService: BlueprintMessageConsumerService
40 open fun consumerService(selector: String): BlueprintMessageConsumerService {
41 return bluePrintMessageLibPropertyService
42 .blueprintMessageConsumerService(selector)
45 open fun kafkaStreamConsumerFunction(prioritizationConfiguration: PrioritizationConfiguration):
46 KafkaStreamConsumerFunction {
47 return object : KafkaStreamConsumerFunction {
49 override suspend fun createTopology(
50 messageConsumerProperties: MessageConsumerProperties,
51 additionalConfig: Map<String, Any>?
54 val topology = Topology()
55 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
56 as KafkaStreamsBasicAuthConsumerProperties
58 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.splitCommaAsList()
59 log.info("Consuming prioritization topics($topics)")
61 topology.addSource(MessagePrioritizationConstants.SOURCE_INPUT, *topics.toTypedArray())
63 topology.addProcessor(
64 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
65 bluePrintProcessorSupplier<ByteArray, ByteArray>(
66 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
67 prioritizationConfiguration
69 MessagePrioritizationConstants.SOURCE_INPUT
72 /** To receive completed and error messages */
74 MessagePrioritizationConstants.SINK_OUTPUT,
75 prioritizationConfiguration.outputTopic,
76 Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
77 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
80 // Output will be sent to the group-output topic from Processor API
86 suspend fun startConsuming(prioritizationConfiguration: PrioritizationConfiguration) {
87 streamingConsumerService = consumerService(prioritizationConfiguration.inputTopicSelector)
89 // Dynamic Consumer Function to create Topology
90 val consumerFunction = kafkaStreamConsumerFunction(prioritizationConfiguration)
91 streamingConsumerService.consume(null, consumerFunction)
94 suspend fun shutDown() {
95 if (streamingConsumerService != null) {
96 streamingConsumerService.shutDown()