InstanceIdentifierContext does not take generics
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / NormalizedNodeJsonBodyWriter.java
index 9445b164d956ea4cedf120c1cc44fe83ebde8df6..a797215ac3f411638b39af4f2b03a8a70253ff1a 100644 (file)
@@ -13,7 +13,6 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
-import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
 import java.util.Map.Entry;
@@ -30,8 +29,7 @@ import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
 import org.opendaylight.netconf.sal.rest.api.RestconfService;
 import org.opendaylight.netconf.util.NetconfUtil;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
@@ -57,8 +55,12 @@ import org.xml.sax.SAXException;
  */
 @Deprecated
 @Provider
-@Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
-        Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
+@Produces({
+    Draft02.MediaTypes.API + RestconfService.JSON,
+    Draft02.MediaTypes.DATA + RestconfService.JSON,
+    Draft02.MediaTypes.OPERATION + RestconfService.JSON,
+    MediaType.APPLICATION_JSON
+})
 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
 
     private static final int DEFAULT_INDENT_SPACES_NUM = 2;
@@ -84,42 +86,39 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
                 httpHeaders.add(entry.getKey(), entry.getValue());
             }
         }
-        NormalizedNode<?, ?> data = context.getData();
+        NormalizedNode data = context.getData();
         if (data == null) {
             return;
         }
 
-        @SuppressWarnings("unchecked")
-        final InstanceIdentifierContext<SchemaNode> identifierCtx =
-                (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
+        final InstanceIdentifierContext identifierCtx = context.getInstanceIdentifierContext();
 
-        final SchemaPath path = identifierCtx.getSchemaNode().getPath();
         try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
             jsonWriter.beginObject();
-            writeNormalizedNode(
-                    jsonWriter, path, identifierCtx, data, context.getWriterParameters().getDepth());
+            writeNormalizedNode(jsonWriter, identifierCtx, data, context.getWriterParameters().getDepth());
             jsonWriter.endObject();
             jsonWriter.flush();
         }
     }
 
-    private static void writeNormalizedNode(final JsonWriter jsonWriter, SchemaPath path,
-            final InstanceIdentifierContext<SchemaNode> context, NormalizedNode<?, ?> data,
-            final @Nullable Integer depth) throws IOException {
+    private static void writeNormalizedNode(final JsonWriter jsonWriter, final InstanceIdentifierContext context,
+            // Note: mutable argument
+            NormalizedNode data, final @Nullable Integer depth) throws IOException {
+        SchemaPath path = context.getSchemaNode().getPath();
         final RestconfNormalizedNodeWriter nnWriter;
         if (SchemaPath.ROOT.equals(path)) {
             /*
              *  Creates writer without initialNs and we write children of root data container
              *  which is not visible in restconf
              */
-            nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
+            nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
             if (data instanceof ContainerNode) {
                 writeChildren(nnWriter,(ContainerNode) data);
             } else if (data instanceof DOMSourceAnyxmlNode) {
                 try {
                     writeChildren(nnWriter,
                             (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(
-                                    context.getSchemaContext(), ((DOMSourceAnyxmlNode)data).getValue()).getResult());
+                                    context.getSchemaContext(), ((DOMSourceAnyxmlNode)data).body()).getResult());
                 } catch (XMLStreamException | URISyntaxException | SAXException e) {
                     throw new IOException("Cannot write anyxml.", e);
                 }
@@ -130,7 +129,7 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
              *  so we need to emit initial output declaratation..
              */
             path = ((RpcDefinition) context.getSchemaNode()).getOutput().getPath();
-            nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
+            nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
             jsonWriter.name("output");
             jsonWriter.beginObject();
             writeChildren(nnWriter, (ContainerNode) data);
@@ -139,9 +138,11 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
             path = path.getParent();
 
             if (data instanceof MapEntryNode) {
-                data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild((MapEntryNode) data).build();
+                data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType())
+                    .withChild((MapEntryNode) data)
+                    .build();
             }
-            nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
+            nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
             nnWriter.write(data);
         }
         nnWriter.flush();
@@ -149,19 +150,19 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
 
     private static void writeChildren(final RestconfNormalizedNodeWriter nnWriter, final ContainerNode data)
             throws IOException {
-        for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
+        for (final DataContainerChild child : data.body()) {
             nnWriter.write(child);
         }
     }
 
     private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(
-            final InstanceIdentifierContext<SchemaNode> context, final SchemaPath path, final JsonWriter jsonWriter,
+            final InstanceIdentifierContext context, final SchemaPath path, final JsonWriter jsonWriter,
             final @Nullable Integer depth) {
 
         final SchemaNode schema = context.getSchemaNode();
         final JSONCodecFactory codecs = getCodecFactory(context);
 
-        final URI initialNs;
+        final XMLNamespace initialNs;
         if (schema instanceof DataSchemaNode && !((DataSchemaNode)schema).isAugmenting()
                 && !(schema instanceof SchemaContext) || schema instanceof RpcDefinition) {
             initialNs = schema.getQName().getNamespace();
@@ -186,7 +187,7 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
         return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
     }
 
-    private static JSONCodecFactory getCodecFactory(final InstanceIdentifierContext<?> context) {
+    private static JSONCodecFactory getCodecFactory(final InstanceIdentifierContext context) {
         // TODO: Performance: Cache JSON Codec factory and schema context
         return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext());
     }