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
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.topology.MessagePrioritizationSerde
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.utils.MessageProcessorUtils.bluePrintProcessorSupplier
23 import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsBasicAuthConsumerProperties
24 import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BluePrintMessageLibPropertyService
26 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageConsumerService
27 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.KafkaStreamConsumerFunction
28 import org.onap.ccsdk.cds.controllerblueprints.core.logger
30 open class MessagePrioritizationConsumer(
31 private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService) {
33 private val log = logger(MessagePrioritizationConsumer::class)
35 lateinit var streamingConsumerService: BlueprintMessageConsumerService
37 open fun consumerService(selector: String): BlueprintMessageConsumerService {
38 return bluePrintMessageLibPropertyService
39 .blueprintMessageConsumerService(selector)
42 open fun kafkaStreamConsumerFunction(prioritizationConfiguration: PrioritizationConfiguration)
43 : KafkaStreamConsumerFunction {
44 return object : KafkaStreamConsumerFunction {
46 override suspend fun createTopology(messageConsumerProperties: MessageConsumerProperties,
47 additionalConfig: Map<String, Any>?): Topology {
49 val topology = Topology()
50 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
51 as KafkaStreamsBasicAuthConsumerProperties
53 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.split(",")
54 log.info("Consuming prioritization topics($topics)")
56 topology.addSource(MessagePrioritizationConstants.SOURCE_INPUT, *topics.toTypedArray())
58 topology.addProcessor(MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
59 bluePrintProcessorSupplier<ByteArray, ByteArray>(MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
60 prioritizationConfiguration),
61 MessagePrioritizationConstants.SOURCE_INPUT)
63 topology.addProcessor(MessagePrioritizationConstants.PROCESSOR_AGGREGATE,
64 bluePrintProcessorSupplier<String, String>(MessagePrioritizationConstants.PROCESSOR_AGGREGATE,
65 prioritizationConfiguration),
66 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE)
68 topology.addProcessor(MessagePrioritizationConstants.PROCESSOR_OUTPUT,
69 bluePrintProcessorSupplier<String, String>(MessagePrioritizationConstants.PROCESSOR_OUTPUT,
70 prioritizationConfiguration),
71 MessagePrioritizationConstants.PROCESSOR_AGGREGATE)
73 topology.addSink(MessagePrioritizationConstants.SINK_EXPIRED,
74 prioritizationConfiguration.expiredTopic,
75 Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
76 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE)
78 /** To receive completed and error messages */
79 topology.addSink(MessagePrioritizationConstants.SINK_OUTPUT,
80 prioritizationConfiguration.outputTopic,
81 Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
82 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
83 MessagePrioritizationConstants.PROCESSOR_AGGREGATE,
84 MessagePrioritizationConstants.PROCESSOR_OUTPUT)
86 // Output will be sent to the group-output topic from Processor API
92 suspend fun startConsuming(prioritizationConfiguration: PrioritizationConfiguration) {
93 streamingConsumerService = consumerService(prioritizationConfiguration.inputTopicSelector)
95 // Dynamic Consumer Function to create Topology
96 val consumerFunction = kafkaStreamConsumerFunction(prioritizationConfiguration)
97 streamingConsumerService.consume(null, consumerFunction)
100 suspend fun shutDown() {
101 if (streamingConsumerService != null) {
102 streamingConsumerService.shutDown()