Drools support for kafka topics
[policy/drools-pdp.git] / feature-pooling-messages / src / main / java / org / onap / policy / drools / pooling / Serializer.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.pooling;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParseException;
27 import java.util.HashMap;
28 import java.util.Map;
29 import org.onap.policy.drools.pooling.message.Heartbeat;
30 import org.onap.policy.drools.pooling.message.Identification;
31 import org.onap.policy.drools.pooling.message.Leader;
32 import org.onap.policy.drools.pooling.message.Message;
33 import org.onap.policy.drools.pooling.message.Offline;
34 import org.onap.policy.drools.pooling.message.Query;
35
36 /**
37  * Serialization helper functions.
38  */
39 public class Serializer {
40
41     /**
42      * The message type is stored in fields of this name within the JSON.
43      */
44     private static final String TYPE_FIELD = "type";
45
46     /**
47      * Used to encode & decode JSON messages sent & received, respectively, on the
48      * internal topic.
49      */
50     private final Gson gson = new Gson();
51
52     /**
53      * Maps a message subclass to its type.
54      */
55     private static final Map<Class<? extends Message>, String> class2type = new HashMap<>();
56
57     /**
58      * Maps a message type to the appropriate subclass.
59      */
60     private static final Map<String, Class<? extends Message>> type2class = new HashMap<>();
61
62     static {
63         class2type.put(Heartbeat.class, "heartbeat");
64         class2type.put(Identification.class, "identification");
65         class2type.put(Leader.class, "leader");
66         class2type.put(Offline.class, "offline");
67         class2type.put(Query.class, "query");
68
69         class2type.forEach((clazz, type) -> type2class.put(type, clazz));
70     }
71
72     /**
73      * Encodes a filter.
74      *
75      * @param filter filter to be encoded
76      * @return the filter, serialized as a JSON string
77      */
78     public String encodeFilter(Map<String, Object> filter) {
79         return gson.toJson(filter);
80     }
81
82     /**
83      * Encodes a message.
84      *
85      * @param msg message to be encoded
86      * @return the message, serialized as a JSON string
87      */
88     public String encodeMsg(Message msg) {
89         JsonElement jsonEl = gson.toJsonTree(msg);
90
91         String type = class2type.get(msg.getClass());
92         if (type == null) {
93             throw new JsonParseException("cannot serialize " + msg.getClass());
94         }
95
96         jsonEl.getAsJsonObject().addProperty(TYPE_FIELD, type);
97
98         return gson.toJson(jsonEl);
99     }
100
101     /**
102      * Decodes a JSON string into a Message.
103      *
104      * @param msg JSON string representing the message
105      * @return the message
106      */
107     public Message decodeMsg(String msg) {
108         JsonElement jsonEl = gson.fromJson(msg, JsonElement.class);
109
110         JsonElement typeEl = jsonEl.getAsJsonObject().get(TYPE_FIELD);
111         if (typeEl == null) {
112             throw new JsonParseException("cannot deserialize " + Message.class
113                             + " because it does not contain a field named " + TYPE_FIELD);
114
115         }
116
117         Class<? extends Message> clazz = type2class.get(typeEl.getAsString());
118         if (clazz == null) {
119             throw new JsonParseException("cannot deserialize " + typeEl);
120         }
121
122         return gson.fromJson(jsonEl, clazz);
123     }
124 }