Bug 2763: Fixed JSON serialization of root of mount point 20/15720/3
authorTony Tkacik <ttkacik@cisco.com>
Wed, 25 Feb 2015 13:13:20 +0000 (14:13 +0100)
committerTony Tkacik <ttkacik@cisco.com>
Mon, 2 Mar 2015 08:59:35 +0000 (08:59 +0000)
Upgrade of yang-data-codec-gson library to use GSON
for streaming and serialization of JSON broke
one special-case which was reading of mount point
root.

This patch migrates NormalizedNodeJsonCodec to
use proper APIs, which allows more explicit control
of how stream is processed.

Change-Id: I0f57e8ef99114a08b1bceb51c47efbcb0546af73
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java

index 03aa316..589f9cd 100644 (file)
@@ -8,13 +8,13 @@
 package org.opendaylight.controller.sal.rest.impl;
 
 import com.google.common.base.Charsets;
+import com.google.gson.stream.JsonWriter;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.net.URI;
-import java.util.Iterator;
 import javax.ws.rs.Produces;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.MediaType;
@@ -34,7 +34,9 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
+import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
+import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -60,45 +62,66 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
             final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
                     throws IOException, WebApplicationException {
         NormalizedNode<?, ?> data = t.getData();
-        final InstanceIdentifierContext context = t.getInstanceIdentifierContext();
-        final DataSchemaNode schema = context.getSchemaNode();
-        SchemaPath path = context.getSchemaNode().getPath();
-        final OutputStreamWriter outputWriter = new OutputStreamWriter(entityStream, Charsets.UTF_8);
         if (data == null) {
             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
         }
 
+        final InstanceIdentifierContext context = t.getInstanceIdentifierContext();
+
+        SchemaPath path = context.getSchemaNode().getPath();
         boolean isDataRoot = false;
-        URI initialNs = null;
         if (SchemaPath.ROOT.equals(path)) {
             isDataRoot = true;
         } else {
             path = path.getParent();
             // FIXME: Add proper handling of reading root.
         }
-        if(!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
-            initialNs = schema.getQName().getNamespace();
-        }
-        final NormalizedNodeStreamWriter jsonWriter = JSONNormalizedNodeStreamWriter.create(context.getSchemaContext(),path,initialNs,outputWriter);
-        final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
+
+        JsonWriter jsonWriter = createJsonWriter(entityStream);
+        NormalizedNodeWriter nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
+
+        jsonWriter.beginObject();
         if(isDataRoot) {
-            writeDataRoot(outputWriter,nnWriter,(ContainerNode) data);
+            writeDataRoot(nnWriter,(ContainerNode) data);
         } else {
             if(data instanceof MapEntryNode) {
                 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
             }
             nnWriter.write(data);
         }
+
         nnWriter.flush();
-        outputWriter.flush();
+        jsonWriter.endObject();
+        jsonWriter.flush();
+    }
+
+    private NormalizedNodeWriter createNormalizedNodeWriter(InstanceIdentifierContext context, SchemaPath path, JsonWriter jsonWriter) {
+
+        final DataSchemaNode schema = context.getSchemaNode();
+        final JSONCodecFactory codecs = getCodecFactory(context);
+
+        URI initialNs = null;
+        if(!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
+            initialNs = schema.getQName().getNamespace();
+        }
+        final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs,path,initialNs,jsonWriter);
+        return NormalizedNodeWriter.forStreamWriter(streamWriter);
+    }
+
+    private JsonWriter createJsonWriter(OutputStream entityStream) {
+        // FIXME BUG-2153: Add pretty print support
+        return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
+
+    }
+
+    private JSONCodecFactory getCodecFactory(InstanceIdentifierContext context) {
+        // TODO: Performance: Cache JSON Codec factory and schema context
+        return JSONCodecFactory.create(context.getSchemaContext());
     }
 
-    private void writeDataRoot(final OutputStreamWriter outputWriter, final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
-        final Iterator<DataContainerChild<? extends PathArgument, ?>> iterator = data.getValue().iterator();
-        while(iterator.hasNext()) {
-            final DataContainerChild<? extends PathArgument, ?> child = iterator.next();
+    private void writeDataRoot(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
+        for(DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
             nnWriter.write(child);
-            nnWriter.flush();
         }
     }