Revert "Renaming Files having BluePrint to have Blueprint"
[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     override fun process(key: K, value: V) = runBlocking(Dispatchers.IO) {
34         try {
35             processNB(key, value)
36         } catch (e: Exception) {
37             log.error("failed in processor(${this.javaClass.simpleName}) message(${this.javaClass.simpleName} :", e)
38         }
39     }
40
41     override fun init(context: ProcessorContext) {
42         log.info("initializing processor (${this.javaClass.simpleName})")
43         this.processorContext = context
44     }
45
46     override fun close() {
47         log.info("closing processor (${this.javaClass.simpleName})")
48     }
49
50     abstract suspend fun processNB(key: K, value: V)
51 }
52
53 /** CDS Kafka Stream Punctuator abstract class to implement */
54 abstract class AbstractBluePrintMessagePunctuator : Punctuator {
55
56     lateinit var processorContext: ProcessorContext
57
58     override fun punctuate(timestamp: Long) = runBlocking(Dispatchers.IO) {
59         punctuateNB(timestamp)
60     }
61
62     abstract suspend fun punctuateNB(timestamp: Long)
63 }