2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.pooling;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParseException;
26 import java.util.HashMap;
28 import org.onap.policy.drools.pooling.message.Heartbeat;
29 import org.onap.policy.drools.pooling.message.Identification;
30 import org.onap.policy.drools.pooling.message.Leader;
31 import org.onap.policy.drools.pooling.message.Message;
32 import org.onap.policy.drools.pooling.message.Offline;
33 import org.onap.policy.drools.pooling.message.Query;
36 * Serialization helper functions.
38 public class Serializer {
41 * The message type is stored in fields of this name within the JSON.
43 private static final String TYPE_FIELD = "type";
46 * Used to encode & decode JSON messages sent & received, respectively, on the
47 * internal DMaaP topic.
49 private final Gson gson = new Gson();
52 * Maps a message subclass to its type.
54 private static final Map<Class<? extends Message>, String> class2type = new HashMap<>();
57 * Maps a message type to the appropriate subclass.
59 private static final Map<String, Class<? extends Message>> type2class = new HashMap<>();
62 class2type.put(Heartbeat.class, "heartbeat");
63 class2type.put(Identification.class, "identification");
64 class2type.put(Leader.class, "leader");
65 class2type.put(Offline.class, "offline");
66 class2type.put(Query.class, "query");
68 class2type.forEach((clazz, type) -> type2class.put(type, clazz));
74 * @param filter filter to be encoded
75 * @return the filter, serialized as a JSON string
77 public String encodeFilter(Map<String, Object> filter) {
78 return gson.toJson(filter);
84 * @param msg message to be encoded
85 * @return the message, serialized as a JSON string
87 public String encodeMsg(Message msg) {
88 JsonElement jsonEl = gson.toJsonTree(msg);
90 String type = class2type.get(msg.getClass());
92 throw new JsonParseException("cannot serialize " + msg.getClass());
95 jsonEl.getAsJsonObject().addProperty(TYPE_FIELD, type);
97 return gson.toJson(jsonEl);
101 * Decodes a JSON string into a Message.
103 * @param msg JSON string representing the message
104 * @return the message
106 public Message decodeMsg(String msg) {
107 JsonElement jsonEl = gson.fromJson(msg, JsonElement.class);
109 JsonElement typeEl = jsonEl.getAsJsonObject().get(TYPE_FIELD);
110 if (typeEl == null) {
111 throw new JsonParseException("cannot deserialize " + Message.class
112 + " because it does not contain a field named " + TYPE_FIELD);
116 Class<? extends Message> clazz = type2class.get(typeEl.getAsString());
118 throw new JsonParseException("cannot deserialize " + typeEl);
121 return gson.fromJson(jsonEl, clazz);