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
29 import org.onap.ccsdk.cds.controllerblueprints.core.splitCommaAsList
31 open class MessagePrioritizationConsumer(
32 private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
35 private val log = logger(MessagePrioritizationConsumer::class)
37 lateinit var streamingConsumerService: BlueprintMessageConsumerService
39 open fun consumerService(selector: String): BlueprintMessageConsumerService {
40 return bluePrintMessageLibPropertyService
41 .blueprintMessageConsumerService(selector)
44 open fun kafkaStreamConsumerFunction(prioritizationConfiguration: PrioritizationConfiguration):
45 KafkaStreamConsumerFunction {
46 return object : KafkaStreamConsumerFunction {
48 override suspend fun createTopology(
49 messageConsumerProperties: MessageConsumerProperties,
50 additionalConfig: Map<String, Any>?
53 val topology = Topology()
54 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
55 as KafkaStreamsBasicAuthConsumerProperties
57 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.splitCommaAsList()
58 log.info("Consuming prioritization topics($topics)")
60 topology.addSource(MessagePrioritizationConstants.SOURCE_INPUT, *topics.toTypedArray())
62 topology.addProcessor(
63 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
64 bluePrintProcessorSupplier<ByteArray, ByteArray>(
65 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
66 prioritizationConfiguration
68 MessagePrioritizationConstants.SOURCE_INPUT
71 topology.addProcessor(
72 MessagePrioritizationConstants.PROCESSOR_AGGREGATE,
73 bluePrintProcessorSupplier<String, String>(
74 MessagePrioritizationConstants.PROCESSOR_AGGREGATE,
75 prioritizationConfiguration
77 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
80 topology.addProcessor(
81 MessagePrioritizationConstants.PROCESSOR_OUTPUT,
82 bluePrintProcessorSupplier<String, String>(
83 MessagePrioritizationConstants.PROCESSOR_OUTPUT,
84 prioritizationConfiguration
86 MessagePrioritizationConstants.PROCESSOR_AGGREGATE
90 MessagePrioritizationConstants.SINK_EXPIRED,
91 prioritizationConfiguration.expiredTopic,
92 Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
93 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
96 /** To receive completed and error messages */
98 MessagePrioritizationConstants.SINK_OUTPUT,
99 prioritizationConfiguration.outputTopic,
100 Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
101 MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
102 MessagePrioritizationConstants.PROCESSOR_AGGREGATE,
103 MessagePrioritizationConstants.PROCESSOR_OUTPUT
106 // Output will be sent to the group-output topic from Processor API
112 suspend fun startConsuming(prioritizationConfiguration: PrioritizationConfiguration) {
113 streamingConsumerService = consumerService(prioritizationConfiguration.inputTopicSelector)
115 // Dynamic Consumer Function to create Topology
116 val consumerFunction = kafkaStreamConsumerFunction(prioritizationConfiguration)
117 streamingConsumerService.consume(null, consumerFunction)
120 suspend fun shutDown() {
121 if (streamingConsumerService != null) {
122 streamingConsumerService.shutDown()