Bump versions to 4.0.0-SNAPSHOT
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / rest / impl / RestconfDocumentedExceptionMapper.java
index 7005968be883e0eb39845baa1130593c42798490..ff0be39a807e4a41f232d143e1a62959d4e4f75a 100644 (file)
@@ -11,12 +11,10 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.collect.Iterables;
 import com.google.gson.stream.JsonWriter;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
-import java.net.URI;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
@@ -24,6 +22,7 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.ext.ExceptionMapper;
 import javax.ws.rs.ext.Provider;
 import javax.xml.XMLConstants;
@@ -33,19 +32,21 @@ import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import org.opendaylight.netconf.sal.rest.api.Draft02;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
+import org.opendaylight.restconf.common.ErrorTags;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder;
+import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
@@ -53,17 +54,12 @@ import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
-import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -119,24 +115,21 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
             return Response.status(exception.getStatus()).type(MediaType.TEXT_PLAIN_TYPE).entity(" ").build();
         }
 
-        final int status = errors.iterator().next().getErrorTag().getStatusCode();
-
-        final DataNodeContainer errorsSchemaNode =
-                (DataNodeContainer) controllerContext.getRestconfModuleErrorsSchemaNode();
-
-        if (errorsSchemaNode == null) {
+        final Status status = ErrorTags.statusOf(errors.iterator().next().getErrorTag());
+        final var errorsEntry = controllerContext.getRestconfModuleErrorsSchemaNode();
+        if (errorsEntry == null) {
             return Response.status(status).type(MediaType.TEXT_PLAIN_TYPE).entity(exception.getMessage()).build();
         }
 
-        checkState(errorsSchemaNode instanceof ContainerSchemaNode, "Found Errors SchemaNode isn't ContainerNode");
+        final var errorsSchemaNode = errorsEntry.getValue();
         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> errContBuild =
-                Builders.containerBuilder((ContainerSchemaNode) errorsSchemaNode);
+                SchemaAwareBuilders.containerBuilder(errorsSchemaNode);
 
-        final List<DataSchemaNode> schemaList = ControllerContext.findInstanceDataChildrenByName(errorsSchemaNode,
+        final var schemaList = ControllerContext.findInstanceDataChildrenByName(errorsSchemaNode,
                 Draft02.RestConfModule.ERROR_LIST_SCHEMA_NODE);
-        final DataSchemaNode errListSchemaNode = Iterables.getFirst(schemaList, null);
+        final DataSchemaNode errListSchemaNode = ControllerContext.getFirst(schemaList);
         checkState(errListSchemaNode instanceof ListSchemaNode, "Found Error SchemaNode isn't ListSchemaNode");
-        final CollectionNodeBuilder<MapEntryNode, MapNode> listErorsBuilder = Builders
+        final CollectionNodeBuilder<MapEntryNode, SystemMapNode> listErorsBuilder = SchemaAwareBuilders
                 .mapBuilder((ListSchemaNode) errListSchemaNode);
 
 
@@ -145,14 +138,14 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
         }
         errContBuild.withChild(listErorsBuilder.build());
 
-        final NormalizedNodeContext errContext =  new NormalizedNodeContext(new InstanceIdentifierContext<>(null,
-                (DataSchemaNode) errorsSchemaNode, null, controllerContext.getGlobalSchema()), errContBuild.build());
+        final NormalizedNodeContext errContext = new NormalizedNodeContext(
+            InstanceIdentifierContext.ofStack(errorsEntry.getKey(), null), errContBuild.build());
 
-        Object responseBody;
+        final String responseBody;
         if (mediaType.getSubtype().endsWith("json")) {
-            responseBody = toJsonResponseBody(errContext, errorsSchemaNode);
+            responseBody = toJsonResponseBody(errContext);
         } else {
-            responseBody = toXMLResponseBody(errContext, errorsSchemaNode);
+            responseBody = toXMLResponseBody(errContext);
         }
 
         return Response.status(status).type(mediaType).entity(responseBody).build();
@@ -162,37 +155,37 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
         checkArgument(errListSchemaNode instanceof ListSchemaNode,
                 "errListSchemaNode has to be of type ListSchemaNode");
         final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) errListSchemaNode;
-        final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> errNodeValues = Builders
+        final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> errNodeValues = SchemaAwareBuilders
                 .mapEntryBuilder(listStreamSchemaNode);
 
-        List<DataSchemaNode> lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
+        var lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
                 listStreamSchemaNode, "error-type");
-        final DataSchemaNode errTypSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null);
+        final DataSchemaNode errTypSchemaNode = ControllerContext.getFirst(lsChildDataSchemaNode);
         checkState(errTypSchemaNode instanceof LeafSchemaNode);
-        errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errTypSchemaNode)
-                .withValue(error.getErrorType().getErrorTypeTag()).build());
+        errNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) errTypSchemaNode)
+                .withValue(error.getErrorType().elementBody()).build());
 
         lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
                 listStreamSchemaNode, "error-tag");
-        final DataSchemaNode errTagSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null);
+        final DataSchemaNode errTagSchemaNode = ControllerContext.getFirst(lsChildDataSchemaNode);
         checkState(errTagSchemaNode instanceof LeafSchemaNode);
-        errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errTagSchemaNode)
-                .withValue(error.getErrorTag().getTagValue()).build());
+        errNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) errTagSchemaNode)
+                .withValue(error.getErrorTag().elementBody()).build());
 
         if (error.getErrorAppTag() != null) {
             lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
                     listStreamSchemaNode, "error-app-tag");
-            final DataSchemaNode errAppTagSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null);
+            final DataSchemaNode errAppTagSchemaNode = ControllerContext.getFirst(lsChildDataSchemaNode);
             checkState(errAppTagSchemaNode instanceof LeafSchemaNode);
-            errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errAppTagSchemaNode)
+            errNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) errAppTagSchemaNode)
                     .withValue(error.getErrorAppTag()).build());
         }
 
         lsChildDataSchemaNode = ControllerContext.findInstanceDataChildrenByName(
                 listStreamSchemaNode, "error-message");
-        final DataSchemaNode errMsgSchemaNode = Iterables.getFirst(lsChildDataSchemaNode, null);
+        final DataSchemaNode errMsgSchemaNode = ControllerContext.getFirst(lsChildDataSchemaNode);
         checkState(errMsgSchemaNode instanceof LeafSchemaNode);
-        errNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) errMsgSchemaNode)
+        errNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) errMsgSchemaNode)
                 .withValue(error.getErrorMessage()).build());
 
         if (error.getErrorInfo() != null) {
@@ -208,35 +201,36 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
         return errNodeValues.build();
     }
 
-    private static Object toJsonResponseBody(final NormalizedNodeContext errorsNode,
-                                             final DataNodeContainer errorsSchemaNode) {
+    private static String toJsonResponseBody(final NormalizedNodeContext errorsNode) {
         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-        NormalizedNode<?, ?> data = errorsNode.getData();
-        final InstanceIdentifierContext<?> context = errorsNode.getInstanceIdentifierContext();
+        NormalizedNode data = errorsNode.getData();
+        final InstanceIdentifierContext context = errorsNode.getInstanceIdentifierContext();
         final DataSchemaNode schema = (DataSchemaNode) context.getSchemaNode();
 
-        SchemaPath path = context.getSchemaNode().getPath();
         final OutputStreamWriter outputWriter = new OutputStreamWriter(outStream, StandardCharsets.UTF_8);
         if (data == null) {
             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
         }
 
-        boolean isDataRoot = false;
-        URI initialNs = null;
-        if (SchemaPath.ROOT.equals(path)) {
+        final boolean isDataRoot;
+        final var stack = context.inference().toSchemaInferenceStack();
+        if (stack.isEmpty()) {
             isDataRoot = true;
         } else {
-            path = path.getParent();
+            isDataRoot = false;
+            stack.exit();
             // FIXME: Add proper handling of reading root.
         }
+
+        XMLNamespace initialNs = null;
         if (!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
             initialNs = schema.getQName().getNamespace();
         }
 
         final JsonWriter jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter);
         final NormalizedNodeStreamWriter jsonStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
-            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext()), path,
-            initialNs, jsonWriter);
+            JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext()),
+            stack.toInference(), initialNs, jsonWriter);
 
         // We create a delegating writer to special-case error-info as error-info is defined as an empty
         // container in the restconf yang schema but we create a leaf node so we can output it. The delegate
@@ -286,7 +280,9 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
                 writeDataRoot(outputWriter,nnWriter,(ContainerNode) data);
             } else {
                 if (data instanceof MapEntryNode) {
-                    data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild((MapEntryNode) data).build();
+                    data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType())
+                        .withChild((MapEntryNode) data)
+                        .build();
                 }
                 nnWriter.write(data);
             }
@@ -305,9 +301,8 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
         return outStream.toString(StandardCharsets.UTF_8);
     }
 
-    private static Object toXMLResponseBody(final NormalizedNodeContext errorsNode,
-                                            final DataNodeContainer errorsSchemaNode) {
-        final InstanceIdentifierContext<?> pathContext = errorsNode.getInstanceIdentifierContext();
+    private static String toXMLResponseBody(final NormalizedNodeContext errorsNode) {
+        final InstanceIdentifierContext pathContext = errorsNode.getInstanceIdentifierContext();
         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 
         final XMLStreamWriter xmlWriter;
@@ -316,18 +311,19 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
         } catch (final XMLStreamException | FactoryConfigurationError e) {
             throw new IllegalStateException(e);
         }
-        NormalizedNode<?, ?> data = errorsNode.getData();
-        SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
+        NormalizedNode data = errorsNode.getData();
 
-        boolean isDataRoot = false;
-        if (SchemaPath.ROOT.equals(schemaPath)) {
+        final boolean isDataRoot;
+        final var stack = pathContext.inference().toSchemaInferenceStack();
+        if (stack.isEmpty()) {
             isDataRoot = true;
         } else {
-            schemaPath = schemaPath.getParent();
+            isDataRoot = false;
+            stack.exit();
         }
 
         final NormalizedNodeStreamWriter xmlStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
-                pathContext.getSchemaContext(), schemaPath);
+                stack.toInference());
 
         // We create a delegating writer to special-case error-info as error-info is defined as an empty
         // container in the restconf yang schema but we create a leaf node so we can output it. The delegate
@@ -394,7 +390,9 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
                 if (data instanceof MapEntryNode) {
                     // Restconf allows returning one list item. We need to wrap it
                     // in map node in order to serialize it properly
-                    data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
+                    data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType())
+                        .addChild((MapEntryNode) data)
+                        .build();
                 }
                 nnWriter.write(data);
                 nnWriter.flush();
@@ -411,7 +409,7 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
         final QName name = SchemaContext.NAME;
         try {
             xmlWriter.writeStartElement(name.getNamespace().toString(), name.getLocalName());
-            for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
+            for (final DataContainerChild child : data.body()) {
                 nnWriter.write(child);
             }
             nnWriter.flush();
@@ -424,7 +422,7 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper<Restco
 
     private static void writeDataRoot(final OutputStreamWriter outputWriter, final NormalizedNodeWriter nnWriter,
                                       final ContainerNode data) throws IOException {
-        for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
+        for (final DataContainerChild child : data.body()) {
             nnWriter.write(child);
             nnWriter.flush();
         }