From 2f39dd9ce9922449cb288eb3dfa4535d766e171a Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 29 Nov 2019 10:08:26 +0100 Subject: [PATCH] Move ObjectMapper to JsonRpcEndpoint JSON mapping is an implementation detail of JsonRpcEndpoint, hence move it to that class, allowing us to shed a field and statically bind to it. Change-Id: Ie0a314b3aa6c830d9cc5936eb4ec948823ba351c Signed-off-by: Robert Varga (cherry picked from commit 6ea8a455e8f3e1a2a099be256f97257a82dde409) --- .../lib/impl/OvsdbConnectionService.java | 8 +------ .../ovsdb/lib/jsonrpc/JsonRpcEndpoint.java | 21 +++++++++++-------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbConnectionService.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbConnectionService.java index a79bc74ac..32da87e1d 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbConnectionService.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbConnectionService.java @@ -8,9 +8,6 @@ package org.opendaylight.ovsdb.lib.impl; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; @@ -109,9 +106,6 @@ public class OvsdbConnectionService implements AutoCloseable, OvsdbConnection { private static final Set CONNECTION_LISTENERS = ConcurrentHashMap.newKeySet(); private static final Map CONNECTIONS = new ConcurrentHashMap<>(); - private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper() - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) - .setSerializationInclusion(Include.NON_NULL); private volatile boolean useSSL = false; private final ICertificateManager certManagerSrv; @@ -230,7 +224,7 @@ public class OvsdbConnectionService implements AutoCloseable, OvsdbConnection { private static OvsdbClient getChannelClient(final Channel channel, final ConnectionType type, final SocketConnectionType socketConnType) { - JsonRpcEndpoint endpoint = new JsonRpcEndpoint(OBJECT_MAPPER, channel); + JsonRpcEndpoint endpoint = new JsonRpcEndpoint(channel); channel.pipeline().addLast(endpoint); OvsdbClientImpl client = new OvsdbClientImpl(endpoint, channel, type, socketConnType); diff --git a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java index 44ac28138..8b8cf0428 100644 --- a/library/impl/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java +++ b/library/impl/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java @@ -9,7 +9,9 @@ package org.opendaylight.ovsdb.lib.jsonrpc; import static java.util.Objects.requireNonNull; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -54,6 +56,9 @@ public class JsonRpcEndpoint extends ChannelInboundHandlerAdapter implements Ovs List.class, JsonNode.class); private static final JavaType JT_LIST_STRING = TypeFactory.defaultInstance().constructParametricType( List.class, String.class); + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setSerializationInclusion(Include.NON_NULL); private static int reaperInterval = 1000; @@ -68,13 +73,11 @@ public class JsonRpcEndpoint extends ChannelInboundHandlerAdapter implements Ovs } private final Map methodContext = new ConcurrentHashMap<>(); - private final ObjectMapper objectMapper; private final Channel nettyChannel; private volatile Callback currentCallback = null; - public JsonRpcEndpoint(final ObjectMapper objectMapper, final Channel channel) { - this.objectMapper = requireNonNull(objectMapper); + public JsonRpcEndpoint(final Channel channel) { this.nettyChannel = requireNonNull(channel); } @@ -222,7 +225,7 @@ public class JsonRpcEndpoint extends ChannelInboundHandlerAdapter implements Ovs case "update": { final UpdateNotification arg; try { - arg = objectMapper.convertValue(params, UpdateNotification.class); + arg = OBJECT_MAPPER.convertValue(params, UpdateNotification.class); } catch (IllegalArgumentException e) { return reportedMalformedParameters(requestId, e); } @@ -233,7 +236,7 @@ public class JsonRpcEndpoint extends ChannelInboundHandlerAdapter implements Ovs case "locked": { final List arg; try { - arg = objectMapper.convertValue(params, JT_LIST_STRING); + arg = OBJECT_MAPPER.convertValue(params, JT_LIST_STRING); } catch (IllegalArgumentException e) { return reportedMalformedParameters(requestId, e); } @@ -244,7 +247,7 @@ public class JsonRpcEndpoint extends ChannelInboundHandlerAdapter implements Ovs case "stolen": { final List arg; try { - arg = objectMapper.convertValue(params, JT_LIST_STRING); + arg = OBJECT_MAPPER.convertValue(params, JT_LIST_STRING); } catch (IllegalArgumentException e) { return reportedMalformedParameters(requestId, e); } @@ -273,7 +276,7 @@ public class JsonRpcEndpoint extends ChannelInboundHandlerAdapter implements Ovs final String jsonString; try { - jsonString = objectMapper.writeValueAsString(response); + jsonString = OBJECT_MAPPER.writeValueAsString(response); } catch (JsonProcessingException e) { LOG.error("Exception while processing JSON response {}", response, e); return; @@ -296,7 +299,7 @@ public class JsonRpcEndpoint extends ChannelInboundHandlerAdapter implements Ovs LOG.error("Request {} failed with error {}", requestId, error); } - final Object mappedResult = objectMapper.convertValue(result, returnCtxt.resultType); + final Object mappedResult = OBJECT_MAPPER.convertValue(result, returnCtxt.resultType); if (!returnCtxt.future.set(mappedResult)) { LOG.debug("Request {} did not accept result {}", requestId, mappedResult); } @@ -305,7 +308,7 @@ public class JsonRpcEndpoint extends ChannelInboundHandlerAdapter implements Ovs private ListenableFuture sendRequest(final JsonRpc10Request request, final JavaType resultType) { final String requestString; try { - requestString = objectMapper.writeValueAsString(request); + requestString = OBJECT_MAPPER.writeValueAsString(request); } catch (JsonProcessingException e) { return Futures.immediateFailedFuture(e); } -- 2.36.6