X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fimpl%2FNormalizedNodeJsonBodyWriter.java;h=5c17f2a14ab2558c4acd089e224828a9248530eb;hb=cd5b4bd47e1cfc40751ef15f6fcdf33fc131c1f8;hp=4944d96b546933c49637fbb66b4b91ea8add05d1;hpb=302ed51819ef1fd233a9b26073eae71e44a9338a;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java index 4944d96b54..5c17f2a14a 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/NormalizedNodeJsonBodyWriter.java @@ -66,36 +66,54 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter context = (InstanceIdentifierContext) t.getInstanceIdentifierContext(); SchemaPath path = context.getSchemaNode().getPath(); - boolean isDataRoot = false; + final JsonWriter jsonWriter = createJsonWriter(entityStream); + jsonWriter.beginObject(); + writeNormalizedNode(jsonWriter,path,context,data); + jsonWriter.endObject(); + jsonWriter.flush(); + } + + private void writeNormalizedNode(JsonWriter jsonWriter, SchemaPath path, + InstanceIdentifierContext context, NormalizedNode data) throws IOException { + final NormalizedNodeWriter nnWriter; if (SchemaPath.ROOT.equals(path)) { - isDataRoot = true; + /* + * Creates writer without initialNs and we write children of root data container + * which is not visible in restconf + */ + nnWriter = createNormalizedNodeWriter(context,path,jsonWriter); + writeChildren(nnWriter,(ContainerNode) data); } else if (context.getSchemaNode() instanceof RpcDefinition) { - isDataRoot = true; + /* + * RpcDefinition is not supported as initial codec in JSONStreamWriter, + * so we need to emit initial output declaratation.. + */ path = ((RpcDefinition) context.getSchemaNode()).getOutput().getPath(); + nnWriter = createNormalizedNodeWriter(context,path,jsonWriter); + jsonWriter.name("output"); + jsonWriter.beginObject(); + writeChildren(nnWriter, (ContainerNode) data); + jsonWriter.endObject(); } else { path = path.getParent(); - // FIXME: Add proper handling of reading root. - } - final JsonWriter jsonWriter = createJsonWriter(entityStream); - final NormalizedNodeWriter nnWriter = createNormalizedNodeWriter(context,path,jsonWriter); - - jsonWriter.beginObject(); - if(isDataRoot) { - writeDataRoot(nnWriter,(ContainerNode) data); - } else { if(data instanceof MapEntryNode) { data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build(); } + nnWriter = createNormalizedNodeWriter(context,path,jsonWriter); nnWriter.write(data); } - nnWriter.flush(); - jsonWriter.endObject(); - jsonWriter.flush(); + } + + private void writeChildren(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException { + for(final DataContainerChild child : data.getValue()) { + nnWriter.write(child); + } } private NormalizedNodeWriter createNormalizedNodeWriter(final InstanceIdentifierContext context, @@ -104,9 +122,15 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter context) { // TODO: Performance: Cache JSON Codec factory and schema context return JSONCodecFactory.create(context.getSchemaContext()); } - private void writeDataRoot(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException { - for(final DataContainerChild child : data.getValue()) { - nnWriter.write(child); - } - } - }