Modified code to use gson instead of jackson.
Change-Id: I5a1b2dacdc1801b1110154ed7c3c81e0713ef369
Issue-ID: POLICY-1431
Signed-off-by: Jim Hahn <jrh3@att.com>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-core</artifactId>
- <scope>provided</scope>
- </dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>${jackson.version}</version>
- <scope>provided</scope>
- </dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-annotations</artifactId>
- <version>${jackson.version}</version>
- <scope>provided</scope>
- </dependency>
-
- <dependency>
- <groupId>com.fasterxml.jackson.datatype</groupId>
- <artifactId>jackson-datatype-jsr310</artifactId>
- <version>${jackson.version}</version>
- <scope>provided</scope>
- </dependency>
-
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
package org.onap.policy.drools.pooling;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import java.io.IOException;
+import com.google.gson.JsonParseException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
try {
dmaapMgr.setFilter(serializer.encodeFilter(filter));
- } catch (JsonProcessingException e) {
+ } catch (JsonParseException e) {
logger.error("failed to encode server-side filter for topic {}, {}", topic, filter, e);
} catch (PoolingFeatureException e) {
String txt = serializer.encodeMsg(msg);
dmaapMgr.publish(txt);
- } catch (JsonProcessingException e) {
+ } catch (JsonParseException e) {
logger.error("failed to serialize message for topic {} channel {}", topic, channel, e);
} catch (PoolingFeatureException e) {
Method meth = current.getClass().getMethod("process", msg.getClass());
changeState((State) meth.invoke(current, msg));
- } catch (IOException e) {
+ } catch (JsonParseException e) {
logger.warn("failed to decode message for topic {}", topic, e);
} catch (NoSuchMethodException | SecurityException e) {
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
package org.onap.policy.drools.pooling;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.IOException;
+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.Forward;
+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 {
/**
- * Used to encode & decode JSON messages sent & received, respectively, on
- * the internal DMaaP topic.
+ * The message type is stored in fields of this name within the JSON.
*/
- private final ObjectMapper mapper = new ObjectMapper();
+ 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<Class<? extends Message>, String> class2type = new HashMap<>();
+
+ /**
+ * Maps a message type to the appropriate subclass.
+ */
+ private static final Map<String, Class<? extends Message>> type2class = new HashMap<>();
+
+ static {
+ class2type.put(Forward.class, "forward");
+ 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));
+ }
/**
* Constructor.
*
* @param filter filter to be encoded
* @return the filter, serialized as a JSON string
- * @throws JsonProcessingException if it cannot be serialized
+ * @throws JsonParseException if it cannot be de-serialized
*/
- public String encodeFilter(Map<String, Object> filter) throws JsonProcessingException {
- return mapper.writeValueAsString(filter);
+ public String encodeFilter(Map<String, Object> filter) throws JsonParseException {
+ return gson.toJson(filter);
}
/**
*
* @param msg message to be encoded
* @return the message, serialized as a JSON string
- * @throws JsonProcessingException if it cannot be serialized
+ * @throws JsonParseException if it cannot be de-serialized
*/
- public String encodeMsg(Message msg) throws JsonProcessingException {
- return mapper.writeValueAsString(msg);
+ public String encodeMsg(Message msg) throws JsonParseException {
+ 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);
}
/**
*
* @param msg JSON string representing the message
* @return the message
- * @throws IOException if it cannot be serialized
+ * @throws JsonParseException if it cannot be serialized
*/
- public Message decodeMsg(String msg) throws IOException {
- return mapper.readValue(msg, Message.class);
+ public Message decodeMsg(String msg) throws JsonParseException {
+ 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<? extends Message> clazz = type2class.get(typeEl.getAsString());
+ if (clazz == null) {
+ throw new JsonParseException("cannot deserialize " + typeEl);
+ }
+
+ return gson.fromJson(jsonEl, clazz);
}
}
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
package org.onap.policy.drools.pooling.message;
-import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
*/
public class BucketAssignments {
- @JsonIgnore
private static final Logger logger = LoggerFactory.getLogger(BucketAssignments.class);
/**
*
* @return the assignment leader
*/
- @JsonIgnore
public String getLeader() {
if (hostArray == null) {
return null;
* @param host host to be checked
* @return {@code true} if the host has an assignment, {@code false} otherwise
*/
- @JsonIgnore
public boolean hasAssignment(String host) {
if (hostArray == null) {
return false;
*
* @return all of the hosts that have an assignment
*/
- @JsonIgnore
public Set<String> getAllHosts() {
Set<String> set = new HashSet<>();
if (hostArray == null) {
* @param hashCode hash code of the item whose assignment is desired
* @return the assigned host, or {@code null} if the item has no assigned host
*/
- @JsonIgnore
public String getAssignedHost(int hashCode) {
if (hostArray == null || hostArray.length == 0) {
logger.error("no buckets have been assigned");
*
* @return the number of buckets
*/
- @JsonIgnore
public int size() {
return (hostArray != null ? hostArray.length : 0);
}
*
* @throws PoolingFeatureException if the assignments are invalid
*/
- @JsonIgnore
public void checkValidity() throws PoolingFeatureException {
if (hostArray == null || hostArray.length == 0) {
throw new PoolingFeatureException("missing hosts in message bucket assignments");
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
package org.onap.policy.drools.pooling.message;
-import com.fasterxml.jackson.annotation.JsonIgnore;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.drools.pooling.PoolingFeatureException;
this.requestId = requestId;
}
- @JsonIgnore
public boolean isExpired(long minCreateTimeMs) {
return (createTimeMs < minCreateTimeMs);
}
@Override
- @JsonIgnore
public void checkValidity() throws PoolingFeatureException {
super.checkValidity();
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
package org.onap.policy.drools.pooling.message;
-import com.fasterxml.jackson.annotation.JsonIgnore;
import org.onap.policy.drools.pooling.PoolingFeatureException;
/**
* indeed the leader.
*/
@Override
- @JsonIgnore
public void checkValidity() throws PoolingFeatureException {
super.checkValidity();
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
package org.onap.policy.drools.pooling.message;
-import com.fasterxml.jackson.annotation.JsonIgnore;
-import com.fasterxml.jackson.annotation.JsonSubTypes;
-import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
-import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.onap.policy.drools.pooling.PoolingFeatureException;
/**
* Messages sent on the internal topic.
*/
-@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
-@JsonSubTypes({@Type(value = Forward.class, name = "forward"), @Type(value = Heartbeat.class, name = "heartbeat"),
- @Type(value = Identification.class, name = "identification"),
- @Type(value = Leader.class, name = "leader"), @Type(value = Offline.class, name = "offline"),
- @Type(value = Query.class, name = "query")})
public class Message {
/**
*
* @throws PoolingFeatureException if the message is invalid
*/
- @JsonIgnore
public void checkValidity() throws PoolingFeatureException {
if (source == null || source.isEmpty()) {
throw new PoolingFeatureException("missing message source");
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
package org.onap.policy.drools.pooling.message;
-import com.fasterxml.jackson.annotation.JsonIgnore;
import org.onap.policy.drools.pooling.PoolingFeatureException;
/**
* If there are any assignments, it verifies there validity.
*/
@Override
- @JsonIgnore
public void checkValidity() throws PoolingFeatureException {
super.checkValidity();
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
import static org.mockito.Mockito.when;
import static org.onap.policy.drools.pooling.PoolingProperties.PREFIX;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.IOException;
+import com.google.gson.Gson;
+import com.google.gson.JsonParseException;
import java.util.Arrays;
import java.util.Deque;
import java.util.IdentityHashMap;
private static final long STD_OFFLINE_PUB_WAIT_MS = 2;
private static final long EVENT_WAIT_SEC = 15;
- /**
- * Used to decode events into a Map.
- */
- private static final TypeReference<TreeMap<String, String>> typeRef =
- new TypeReference<TreeMap<String, String>>() {};
-
/**
* Used to decode events from the external topic.
*/
- private static final ThreadLocal<ObjectMapper> mapper = new ThreadLocal<ObjectMapper>() {
- @Override
- protected ObjectMapper initialValue() {
- return new ObjectMapper();
- }
- };
+ private static final Gson mapper = new Gson();
/**
* Used to identify the current host.
*/
private static Object decodeEvent(String event) {
try {
- return mapper.get().readValue(event, typeRef);
+ return mapper.fromJson(event, TreeMap.class);
- } catch (IOException e) {
+ } catch (JsonParseException e) {
logger.warn("cannot decode external event", e);
return null;
}
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
import static org.mockito.Mockito.when;
import static org.onap.policy.drools.pooling.PoolingProperties.PREFIX;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.IOException;
+import com.google.gson.Gson;
+import com.google.gson.JsonParseException;
import java.util.Arrays;
import java.util.Deque;
import java.util.IdentityHashMap;
private static long stdPollMs = 2;
private static long stdInterPollMs = 2;
private static long stdEventWaitSec = 10;
-
- /**
- * Used to decode events into a Map.
- */
- private static final TypeReference<TreeMap<String, String>> typeRef =
- new TypeReference<TreeMap<String, String>>() {};
-
/**
* Used to decode events from the external topic.
*/
- private static final ThreadLocal<ObjectMapper> mapper = new ThreadLocal<ObjectMapper>() {
- @Override
- protected ObjectMapper initialValue() {
- return new ObjectMapper();
- }
- };
+ private static final Gson mapper = new Gson();
/**
* Used to identify the current context.
*/
private static Object decodeEvent(String event) {
try {
- return mapper.get().readValue(event, typeRef);
+ return mapper.fromJson(event, TreeMap.class);
- } catch (IOException e) {
+ } catch (JsonParseException e) {
logger.warn("cannot decode external event", e);
return null;
}
return true;
- } catch (IOException e) {
+ } catch (JsonParseException e) {
logger.warn("could not decode message: {}", message);
context.bumpDecodeErrors();
return false;
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
package org.onap.policy.drools.pooling;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.onap.policy.drools.pooling.state.FilterUtils.makeEquals;
import static org.onap.policy.drools.pooling.state.FilterUtils.makeOr;
+import com.google.gson.JsonParseException;
import java.util.Map;
import java.util.TreeMap;
import org.junit.Test;
assertEquals(msg.getSource(), decoded.getSource());
assertEquals(msg.getChannel(), decoded.getChannel());
+
+ // invalid subclass when encoding
+ assertThatThrownBy(() -> ser.encodeMsg(new Message() {})).isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("cannot serialize");
+
+ // missing type when decoding
+ final String enc2 = encoded.replaceAll("type", "other-field-name");
+
+ assertThatThrownBy(() -> ser.decodeMsg(enc2)).isInstanceOf(JsonParseException.class)
+ .hasMessageContaining("does not contain a field named");
+
+ // invalid type
+ final String enc3 = encoded.replaceAll("query", "invalid-type");
+
+ assertThatThrownBy(() -> ser.decodeMsg(enc3)).isInstanceOf(JsonParseException.class)
+ .hasMessage("cannot deserialize \"invalid-type\"");
}
}
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019 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.
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
-import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.onap.policy.drools.pooling.PoolingFeatureException;
+import org.onap.policy.drools.pooling.Serializer;
/**
* Superclass used to test subclasses of {@link Message}.
/**
* Used to perform JSON serialization and de-serialization.
*/
- public final ObjectMapper mapper = new ObjectMapper();
+ public final Serializer mapper = new Serializer();
/**
* The subclass of the type of Message being tested.
public final void testJsonEncodeDecode() throws Exception {
T originalMsg = makeValidMessage();
- Message msg = mapper.readValue(mapper.writeValueAsString(originalMsg), Message.class);
+ Message msg;
+ if (originalMsg.getClass() == Message.class) {
+ msg = originalMsg;
+ } else {
+ msg = mapper.decodeMsg(mapper.encodeMsg(originalMsg));
+ }
+
assertEquals(subclazz, msg.getClass());
msg.checkValidity();