Move ObjectMapper to JsonRpcEndpoint 19/86119/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 29 Nov 2019 09:08:26 +0000 (10:08 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 3 Dec 2019 09:44:56 +0000 (10:44 +0100)
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 <robert.varga@pantheon.tech>
(cherry picked from commit 6ea8a455e8f3e1a2a099be256f97257a82dde409)

library/impl/src/main/java/org/opendaylight/ovsdb/lib/impl/OvsdbConnectionService.java
library/impl/src/main/java/org/opendaylight/ovsdb/lib/jsonrpc/JsonRpcEndpoint.java

index cc07a54c1324eb3109718a21d0f33ad88f151dd0..7a7ac00992eb293dd9a445e1bbb94e8eca029ecd 100644 (file)
@@ -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<OvsdbConnectionListener> CONNECTION_LISTENERS = ConcurrentHashMap.newKeySet();
     private static final Map<OvsdbClient, Channel> 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);
index 44ac281383fc76868474c42ed87025fe34a9bd58..8b8cf04280180cd80f951eef73b238f89fa6b8b8 100644 (file)
@@ -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<String, CallContext> 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<String> 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<String> 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 <T> ListenableFuture<T> 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);
         }