Add message prioritization module
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / message-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / message / kafka / AbstractKafkaTopologyComponents.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.message.kafka
18
19 import kotlinx.coroutines.Dispatchers
20 import kotlinx.coroutines.runBlocking
21 import org.apache.kafka.streams.processor.Processor
22 import org.apache.kafka.streams.processor.ProcessorContext
23 import org.apache.kafka.streams.processor.Punctuator
24 import org.onap.ccsdk.cds.controllerblueprints.core.logger
25
26 /** CDS Kafka Stream Processor abstract class to implement */
27 abstract class AbstractBluePrintMessageProcessor<K, V> : Processor<K, V> {
28
29     private val log = logger(AbstractBluePrintMessageProcessor::class)
30
31     lateinit var processorContext: ProcessorContext
32
33
34     override fun process(key: K, value: V) = runBlocking(Dispatchers.IO) {
35         try {
36             processNB(key, value)
37         } catch (e: Exception) {
38             log.error("failed in processor(${this.javaClass.simpleName}) message(${this.javaClass.simpleName} :", e)
39         }
40     }
41
42     override fun init(context: ProcessorContext) {
43         log.info("initializing processor (${this.javaClass.simpleName})")
44         this.processorContext = context
45
46     }
47
48     override fun close() {
49         log.info("closing processor (${this.javaClass.simpleName})")
50     }
51
52     abstract suspend fun processNB(key: K, value: V)
53 }
54
55 /** CDS Kafka Stream Punctuator abstract class to implement */
56 abstract class AbstractBluePrintMessagePunctuator : Punctuator {
57     lateinit var processorContext: ProcessorContext
58
59
60     override fun punctuate(timestamp: Long) = runBlocking(Dispatchers.IO) {
61         punctuateNB(timestamp)
62     }
63
64     abstract suspend fun punctuateNB(timestamp: Long)
65 }