Peel DataSchemaNode-only users of InstanceIdentifierContext
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / NormalizedNodeXmlBodyWriter.java
index ca7f3b3451d55430a938b6c555d7ce56b80aeef3..ea559b9b5ec131fdc551aad014ded49f55a48f7a 100644 (file)
@@ -42,9 +42,7 @@ import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStr
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 import org.xml.sax.SAXException;
 
 /**
@@ -90,7 +88,7 @@ public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<Normalized
         for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
             httpHeaders.add(entry.getKey(), entry.getValue());
         }
-        final InstanceIdentifierContext<?> pathContext = context.getInstanceIdentifierContext();
+        final InstanceIdentifierContext pathContext = context.getInstanceIdentifierContext();
         if (context.getData() == null) {
             return;
         }
@@ -105,17 +103,18 @@ public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<Normalized
             throw new IllegalStateException(e);
         }
         final NormalizedNode data = context.getData();
+        final SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
 
-        writeNormalizedNode(xmlWriter, pathContext, data, context.getWriterParameters().getDepth());
+        writeNormalizedNode(xmlWriter, schemaPath, pathContext, data, context.getWriterParameters().getDepth());
     }
 
-    private static void writeNormalizedNode(final XMLStreamWriter xmlWriter,
-            final InstanceIdentifierContext<?> pathContext, NormalizedNode data, final @Nullable Integer depth)
+    private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final SchemaPath schemaPath,
+            final InstanceIdentifierContext pathContext, NormalizedNode data, final @Nullable Integer depth)
             throws IOException {
         final RestconfNormalizedNodeWriter nnWriter;
         final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
-        if (pathContext.getSchemaNode() instanceof SchemaContext) {
-            nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, SchemaPath.ROOT, depth);
+        if (SchemaPath.ROOT.equals(schemaPath)) {
+            nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath, depth);
             if (data instanceof DOMSourceAnyxmlNode) {
                 try {
                     writeElements(xmlWriter, nnWriter,
@@ -128,18 +127,12 @@ public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<Normalized
                 writeElements(xmlWriter, nnWriter, (ContainerNode) data);
             }
         }  else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
-            final RpcDefinition rpc = (RpcDefinition) pathContext.getSchemaNode();
-            final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
-            nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
+            final var rpc = (RpcDefinition) pathContext.getSchemaNode();
+            nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx,
+                    SchemaPath.create(true, rpc.getQName(), rpc.getOutput().getQName()), depth);
             writeElements(xmlWriter, nnWriter, (ContainerNode) data);
         } else {
-            final SchemaPath path;
-            if (pathContext.getSchemaNodeIdentifier() == null) {
-                path = SchemaPath.ROOT;
-            } else {
-                path = SchemaPath.of(pathContext.getSchemaNodeIdentifier()).getParent();
-            }
-            nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
+            nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath.getParent(), depth);
             if (data instanceof MapEntryNode) {
                 // Restconf allows returning one list item. We need to wrap it
                 // in map node in order to serialize it properly