Remove jackson from feature-pooling-dmaap
[policy/drools-pdp.git] / feature-pooling-dmaap / src / main / java / org / onap / policy / drools / pooling / Serializer.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2019 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.drools.pooling;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParseException;
26 import java.util.HashMap;
27 import java.util.Map;
28 import org.onap.policy.drools.pooling.message.Forward;
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 DMaaP 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(Forward.class, "forward");
64         class2type.put(Heartbeat.class, "heartbeat");
65         class2type.put(Identification.class, "identification");
66         class2type.put(Leader.class, "leader");
67         class2type.put(Offline.class, "offline");
68         class2type.put(Query.class, "query");
69
70         class2type.forEach((clazz, type) -> type2class.put(type, clazz));
71     }
72
73     /**
74      * Constructor.
75      */
76     public Serializer() {
77         super();
78     }
79
80     /**
81      * Encodes a filter.
82      * 
83      * @param filter filter to be encoded
84      * @return the filter, serialized as a JSON string
85      * @throws JsonParseException if it cannot be de-serialized
86      */
87     public String encodeFilter(Map<String, Object> filter) throws JsonParseException {
88         return gson.toJson(filter);
89     }
90
91     /**
92      * Encodes a message.
93      * 
94      * @param msg message to be encoded
95      * @return the message, serialized as a JSON string
96      * @throws JsonParseException if it cannot be de-serialized
97      */
98     public String encodeMsg(Message msg) throws JsonParseException {
99         JsonElement jsonEl = gson.toJsonTree(msg);
100
101         String type = class2type.get(msg.getClass());
102         if (type == null) {
103             throw new JsonParseException("cannot serialize " + msg.getClass());
104         }
105
106         jsonEl.getAsJsonObject().addProperty(TYPE_FIELD, type);
107
108         return gson.toJson(jsonEl);
109     }
110
111     /**
112      * Decodes a JSON string into a Message.
113      * 
114      * @param msg JSON string representing the message
115      * @return the message
116      * @throws JsonParseException if it cannot be serialized
117      */
118     public Message decodeMsg(String msg) throws JsonParseException {
119         JsonElement jsonEl = gson.fromJson(msg, JsonElement.class);
120
121         JsonElement typeEl = jsonEl.getAsJsonObject().get(TYPE_FIELD);
122         if (typeEl == null) {
123             throw new JsonParseException("cannot deserialize " + Message.class
124                             + " because it does not contain a field named " + TYPE_FIELD);
125
126         }
127
128         Class<? extends Message> clazz = type2class.get(typeEl.getAsString());
129         if (clazz == null) {
130             throw new JsonParseException("cannot deserialize " + typeEl);
131         }
132
133         return gson.fromJson(jsonEl, clazz);
134     }
135 }