Make feature-pooling-dmaap work without filtering
[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-2020 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.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;
34
35 /**
36  * Serialization helper functions.
37  */
38 public class Serializer {
39
40     /**
41      * The message type is stored in fields of this name within the JSON.
42      */
43     private static final String TYPE_FIELD = "type";
44
45     /**
46      * Used to encode & decode JSON messages sent & received, respectively, on the
47      * internal DMaaP topic.
48      */
49     private final Gson gson = new Gson();
50
51     /**
52      * Maps a message subclass to its type.
53      */
54     private static final Map<Class<? extends Message>, String> class2type = new HashMap<>();
55
56     /**
57      * Maps a message type to the appropriate subclass.
58      */
59     private static final Map<String, Class<? extends Message>> type2class = new HashMap<>();
60
61     static {
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");
67
68         class2type.forEach((clazz, type) -> type2class.put(type, clazz));
69     }
70
71     /**
72      * Constructor.
73      */
74     public Serializer() {
75         super();
76     }
77
78     /**
79      * Encodes a filter.
80      *
81      * @param filter filter to be encoded
82      * @return the filter, serialized as a JSON string
83      */
84     public String encodeFilter(Map<String, Object> filter) {
85         return gson.toJson(filter);
86     }
87
88     /**
89      * Encodes a message.
90      *
91      * @param msg message to be encoded
92      * @return the message, serialized as a JSON string
93      */
94     public String encodeMsg(Message msg) {
95         JsonElement jsonEl = gson.toJsonTree(msg);
96
97         String type = class2type.get(msg.getClass());
98         if (type == null) {
99             throw new JsonParseException("cannot serialize " + msg.getClass());
100         }
101
102         jsonEl.getAsJsonObject().addProperty(TYPE_FIELD, type);
103
104         return gson.toJson(jsonEl);
105     }
106
107     /**
108      * Decodes a JSON string into a Message.
109      *
110      * @param msg JSON string representing the message
111      * @return the message
112      */
113     public Message decodeMsg(String msg) {
114         JsonElement jsonEl = gson.fromJson(msg, JsonElement.class);
115
116         JsonElement typeEl = jsonEl.getAsJsonObject().get(TYPE_FIELD);
117         if (typeEl == null) {
118             throw new JsonParseException("cannot deserialize " + Message.class
119                             + " because it does not contain a field named " + TYPE_FIELD);
120
121         }
122
123         Class<? extends Message> clazz = type2class.get(typeEl.getAsString());
124         if (clazz == null) {
125             throw new JsonParseException("cannot deserialize " + typeEl);
126         }
127
128         return gson.fromJson(jsonEl, clazz);
129     }
130 }