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.message.service
19 import org.apache.kafka.common.serialization.Deserializer
20 import org.apache.kafka.common.serialization.Serde
21 import org.apache.kafka.common.serialization.Serializer
22 import org.apache.kafka.streams.processor.Processor
23 import org.apache.kafka.streams.processor.ProcessorContext
24 import org.apache.kafka.streams.state.KeyValueStore
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import java.io.Serializable
30 import java.nio.charset.Charset
33 class PriorityMessage : Serializable {
34 lateinit var id: String
35 lateinit var requestMessage: String
38 open class PriorityMessageSerde : Serde<PriorityMessage> {
40 override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
43 override fun close() {
46 override fun deserializer(): Deserializer<PriorityMessage> {
47 return object : Deserializer<PriorityMessage> {
48 override fun deserialize(topic: String, data: ByteArray): PriorityMessage {
49 return JacksonUtils.readValue(String(data), PriorityMessage::class.java)
50 ?: throw BluePrintProcessorException("failed to convert")
53 override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
56 override fun close() {
61 override fun serializer(): Serializer<PriorityMessage> {
62 return object : Serializer<PriorityMessage> {
63 override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
66 override fun serialize(topic: String?, data: PriorityMessage): ByteArray {
67 return data.asJsonString().toByteArray(Charset.defaultCharset())
70 override fun close() {
76 class FirstProcessor : Processor<ByteArray, ByteArray> {
78 private val log = logger(FirstProcessor::class)
80 private lateinit var context: ProcessorContext
81 private lateinit var kvStore: KeyValueStore<String, PriorityMessage>
83 override fun process(key: ByteArray, value: ByteArray) {
84 log.info("First Processor key(${String(key)} : value(${String(value)})")
85 val newMessage = PriorityMessage().apply {
86 id = UUID.randomUUID().toString()
87 requestMessage = String(value)
89 kvStore.put(newMessage.id, newMessage)
90 this.context.forward(newMessage.id, newMessage)
93 override fun init(context: ProcessorContext) {
94 log.info("init... ${context.keySerde()}, ${context.valueSerde()}")
95 this.context = context
96 this.kvStore = context.getStateStore("PriorityMessageState") as KeyValueStore<String, PriorityMessage>
99 override fun close() {