Revert "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 / MessagePrioritizationSerde.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.Deserializer
20 import org.apache.kafka.common.serialization.Serde
21 import org.apache.kafka.common.serialization.Serializer
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.db.MessagePrioritization
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
26 import java.nio.charset.Charset
27
28 open class MessagePrioritizationSerde : Serde<MessagePrioritization> {
29
30     override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
31     }
32
33     override fun close() {
34     }
35
36     override fun deserializer(): Deserializer<MessagePrioritization> {
37         return object : Deserializer<MessagePrioritization> {
38             override fun deserialize(topic: String, data: ByteArray): MessagePrioritization {
39                 return JacksonUtils.readValue(String(data), MessagePrioritization::class.java)
40                     ?: throw BluePrintProcessorException("failed to convert")
41             }
42
43             override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
44             }
45
46             override fun close() {
47             }
48         }
49     }
50
51     override fun serializer(): Serializer<MessagePrioritization> {
52         return object : Serializer<MessagePrioritization> {
53             override fun configure(configs: MutableMap<String, *>?, isKey: Boolean) {
54             }
55
56             override fun serialize(topic: String?, data: MessagePrioritization): ByteArray {
57                 return data.asJsonString().toByteArray(Charset.defaultCharset())
58             }
59
60             override fun close() {
61             }
62         }
63     }
64 }