Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / message-prioritization / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / message / prioritization / kafka / KafkaMessagePrioritizationConsumer.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.MessagePrioritizationService
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.PrioritizationConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.utils.MessageProcessorUtils.bluePrintProcessorSupplier
25 import org.onap.ccsdk.cds.blueprintsprocessor.message.KafkaStreamsBasicAuthConsumerProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.message.MessageConsumerProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageLibPropertyService
28 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.BlueprintMessageConsumerService
29 import org.onap.ccsdk.cds.blueprintsprocessor.message.service.KafkaStreamConsumerFunction
30 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.logger
32 import org.onap.ccsdk.cds.controllerblueprints.core.splitCommaAsList
33
34 open class KafkaMessagePrioritizationConsumer(
35     private val bluePrintMessageLibPropertyService: BlueprintMessageLibPropertyService,
36     private val kafkaMessagePrioritizationService: MessagePrioritizationService
37 ) {
38
39     private val log = logger(KafkaMessagePrioritizationConsumer::class)
40
41     private lateinit var streamingConsumerService: BlueprintMessageConsumerService
42
43     open fun consumerService(selector: String): BlueprintMessageConsumerService {
44         return bluePrintMessageLibPropertyService
45             .blueprintMessageConsumerService(selector)
46     }
47
48     open fun kafkaStreamConsumerFunction(prioritizationConfiguration: PrioritizationConfiguration):
49         KafkaStreamConsumerFunction {
50             return object : KafkaStreamConsumerFunction {
51
52                 val kafkaConsumerConfiguration = prioritizationConfiguration.kafkaConfiguration
53                     ?: throw BlueprintProcessorException("failed to get kafka consumer configuration")
54
55                 override suspend fun createTopology(
56                     messageConsumerProperties: MessageConsumerProperties,
57                     additionalConfig: Map<String, Any>?
58                 ): Topology {
59
60                     val topology = Topology()
61                     val kafkaStreamsBasicAuthConsumerProperties = messageConsumerProperties
62                         as KafkaStreamsBasicAuthConsumerProperties
63
64                     val topics = kafkaStreamsBasicAuthConsumerProperties.topic.splitCommaAsList()
65                     log.info("Consuming prioritization topics($topics)")
66
67                     topology.addSource(MessagePrioritizationConstants.SOURCE_INPUT, *topics.toTypedArray())
68
69                     topology.addProcessor(
70                         MessagePrioritizationConstants.PROCESSOR_PRIORITIZE,
71                         bluePrintProcessorSupplier<ByteArray, ByteArray>(
72                             MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
73                         ),
74                         MessagePrioritizationConstants.SOURCE_INPUT
75                     )
76
77                     /** To receive completed and error messages */
78                     topology.addSink(
79                         MessagePrioritizationConstants.SINK_OUTPUT,
80                         kafkaConsumerConfiguration.outputTopic,
81                         Serdes.String().serializer(), MessagePrioritizationSerde().serializer(),
82                         MessagePrioritizationConstants.PROCESSOR_PRIORITIZE
83                     )
84
85                     // Output will be sent to the group-output topic from Processor API
86                     return topology
87                 }
88             }
89         }
90
91     suspend fun startConsuming(prioritizationConfiguration: PrioritizationConfiguration) {
92
93         val kafkaConsumerConfiguration = prioritizationConfiguration.kafkaConfiguration
94             ?: throw BlueprintProcessorException("failed to get kafka consumer configuration")
95
96         streamingConsumerService = consumerService(kafkaConsumerConfiguration.inputTopicSelector)
97
98         // Dynamic Consumer Function to create Topology
99         val consumerFunction = kafkaStreamConsumerFunction(prioritizationConfiguration)
100         streamingConsumerService.consume(null, consumerFunction)
101     }
102
103     suspend fun shutDown() {
104         if (::streamingConsumerService.isInitialized) {
105             streamingConsumerService.shutDown()
106         }
107     }
108 }