X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=feature-pooling-dmaap%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fdrools%2Fpooling%2FSerializer.java;fp=feature-pooling-dmaap%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fdrools%2Fpooling%2FSerializer.java;h=0000000000000000000000000000000000000000;hb=cc1d3d352771d1fa35d297e90663539e34b022f6;hp=15c98e0de64b1efed607cfa7e3541caff525b1b1;hpb=cce79248655f484e007769ba81b092148f940d27;p=policy%2Fdrools-pdp.git diff --git a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/Serializer.java b/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/Serializer.java deleted file mode 100644 index 15c98e0d..00000000 --- a/feature-pooling-dmaap/src/main/java/org/onap/policy/drools/pooling/Serializer.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP - * ================================================================================ - * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.drools.pooling; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import java.util.HashMap; -import java.util.Map; -import org.onap.policy.drools.pooling.message.Heartbeat; -import org.onap.policy.drools.pooling.message.Identification; -import org.onap.policy.drools.pooling.message.Leader; -import org.onap.policy.drools.pooling.message.Message; -import org.onap.policy.drools.pooling.message.Offline; -import org.onap.policy.drools.pooling.message.Query; - -/** - * Serialization helper functions. - */ -public class Serializer { - - /** - * The message type is stored in fields of this name within the JSON. - */ - private static final String TYPE_FIELD = "type"; - - /** - * Used to encode & decode JSON messages sent & received, respectively, on the - * internal DMaaP topic. - */ - private final Gson gson = new Gson(); - - /** - * Maps a message subclass to its type. - */ - private static final Map, String> class2type = new HashMap<>(); - - /** - * Maps a message type to the appropriate subclass. - */ - private static final Map> type2class = new HashMap<>(); - - static { - class2type.put(Heartbeat.class, "heartbeat"); - class2type.put(Identification.class, "identification"); - class2type.put(Leader.class, "leader"); - class2type.put(Offline.class, "offline"); - class2type.put(Query.class, "query"); - - class2type.forEach((clazz, type) -> type2class.put(type, clazz)); - } - - /** - * Encodes a filter. - * - * @param filter filter to be encoded - * @return the filter, serialized as a JSON string - */ - public String encodeFilter(Map filter) { - return gson.toJson(filter); - } - - /** - * Encodes a message. - * - * @param msg message to be encoded - * @return the message, serialized as a JSON string - */ - public String encodeMsg(Message msg) { - JsonElement jsonEl = gson.toJsonTree(msg); - - String type = class2type.get(msg.getClass()); - if (type == null) { - throw new JsonParseException("cannot serialize " + msg.getClass()); - } - - jsonEl.getAsJsonObject().addProperty(TYPE_FIELD, type); - - return gson.toJson(jsonEl); - } - - /** - * Decodes a JSON string into a Message. - * - * @param msg JSON string representing the message - * @return the message - */ - public Message decodeMsg(String msg) { - JsonElement jsonEl = gson.fromJson(msg, JsonElement.class); - - JsonElement typeEl = jsonEl.getAsJsonObject().get(TYPE_FIELD); - if (typeEl == null) { - throw new JsonParseException("cannot deserialize " + Message.class - + " because it does not contain a field named " + TYPE_FIELD); - - } - - Class clazz = type2class.get(typeEl.getAsString()); - if (clazz == null) { - throw new JsonParseException("cannot deserialize " + typeEl); - } - - return gson.fromJson(jsonEl, clazz); - } -}