Rest endpoint for message Prioritization
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / message-prioritizaion / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / message / prioritization / kafka / MessagePrioritizationConsumer.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.kafka
18
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
31
32 open class MessagePrioritizationConsumer(
33     private val bluePrintMessageLibPropertyService: BluePrintMessageLibPropertyService
34 ) {
35
36     private val log = logger(MessagePrioritizationConsumer::class)
37
38     lateinit var streamingConsumerService: BlueprintMessageConsumerService
39
40     open fun consumerService(selector: String): BlueprintMessageConsumerService {
41         return bluePrintMessageLibPropertyService
42             .blueprintMessageConsumerService(selector)
43     }
44
45     open fun kafkaStreamConsumerFunction(prioritizationConfiguration: PrioritizationConfiguration):
46         KafkaStreamConsumerFunction {
47         return object : KafkaStreamConsumerFunction {
48
49             override suspend fun createTopology(
50                 messageConsumerProperties: MessageConsumerProperties,
51                 additionalConfig: Map<String, Any>?
52             ): Topology {
53
54                 val topology = Topology()
55                 val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
56                     as KafkaStreamsBasicAuthConsumerProperties
57
58                 val topics = kafkaStreamsBasicAuthConsumerProperties.topic.splitCommaAsList()
59                 log.info("Consuming prioritization topics($topics)")
60
61                 topology.addSource(MessagePrioritizationConstants.SOURCE_INPUT, *topics.toTypedArray())
62
63                 topology.addProcessor(
64                     MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
65                     bluePrintProcessorSupplier<ByteArray, ByteArray>(
66                         MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
67                         prioritizationConfiguration
68                     ),
69                     MessagePrioritizationConstants.SOURCE_INPUT
70                 )
71
72                 /** To receive completed and error messages */
73                 topology.addSink(
74                     MessagePrioritizationConstants.SINK_OUTPUT,
75                     prioritizationConfiguration.outputTopic,
76                     Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
77                     MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
78                 )
79
80                 // Output will be sent to the group-output topic from Processor API
81                 return topology
82             }
83         }
84     }
85
86     suspend fun startConsuming(prioritizationConfiguration: PrioritizationConfiguration) {
87         streamingConsumerService = consumerService(prioritizationConfiguration.inputTopicSelector)
88
89         // Dynamic Consumer Function to create Topology
90         val consumerFunction = kafkaStreamConsumerFunction(prioritizationConfiguration)
91         streamingConsumerService.consume(null, consumerFunction)
92     }
93
94     suspend fun shutDown() {
95         if (streamingConsumerService != null) {
96             streamingConsumerService.shutDown()
97         }
98     }
99 }