Bump upstream versions
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / JsonPatchBodyReader.java
index 4e34e1d57bddaf84645701eac85198136fa3d092..95190882a7c97fa0f3c520dd3663bdc4c58165c8 100644 (file)
@@ -32,14 +32,13 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.common.patch.PatchEditOperation;
 import org.opendaylight.restconf.common.patch.PatchEntity;
 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
-import org.opendaylight.restconf.nb.rfc8040.codecs.StringModuleInstanceIdentifierCodec;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -50,9 +49,11 @@ import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
+import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -68,7 +69,7 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
 
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
-    protected PatchContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
+    protected PatchContext readBody(final InstanceIdentifierContext path, final InputStream entityStream)
             throws WebApplicationException {
         try {
             return readFrom(path, entityStream);
@@ -77,7 +78,7 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
         }
     }
 
-    private PatchContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream)
+    private PatchContext readFrom(final InstanceIdentifierContext path, final InputStream entityStream)
             throws IOException {
         final JsonReader jsonReader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
         AtomicReference<String> patchId = new AtomicReference<>();
@@ -88,19 +89,19 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public PatchContext readFrom(final String uriPath, final InputStream entityStream) throws
-            RestconfDocumentedException {
+    public PatchContext readFrom(final String uriPath, final InputStream entityStream)
+            throws RestconfDocumentedException {
         try {
             return readFrom(
                     ParserIdentifier.toInstanceIdentifier(uriPath, getSchemaContext(),
                             Optional.ofNullable(getMountPointService())), entityStream);
         } catch (final Exception e) {
-            propagateExceptionAs(e);
-            return null; // no-op
+            throw propagateExceptionAs(e);
         }
     }
 
-    private static RuntimeException propagateExceptionAs(final Exception exception) throws RestconfDocumentedException {
+    private static RestconfDocumentedException propagateExceptionAs(final Exception exception)
+            throws RestconfDocumentedException {
         Throwables.throwIfInstanceOf(exception, RestconfDocumentedException.class);
         LOG.debug("Error parsing json input", exception);
 
@@ -110,14 +111,13 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
 
         RestconfDocumentedException.throwIfYangError(exception);
         throw new RestconfDocumentedException("Error parsing json input: " + exception.getMessage(), ErrorType.PROTOCOL,
-                ErrorTag.MALFORMED_MESSAGE, exception);
+            ErrorTag.MALFORMED_MESSAGE, exception);
     }
 
-    private List<PatchEntity> read(final JsonReader in, final InstanceIdentifierContext<?> path,
+    private List<PatchEntity> read(final JsonReader in, final InstanceIdentifierContext path,
             final AtomicReference<String> patchId) throws IOException {
+        final DataSchemaContextTree schemaTree = DataSchemaContextTree.from(path.getSchemaContext());
         final List<PatchEntity> resultCollection = new ArrayList<>();
-        final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
-                path.getSchemaContext());
         final JsonPatchBodyReader.PatchEdit edit = new JsonPatchBodyReader.PatchEdit();
 
         while (in.hasNext()) {
@@ -141,7 +141,7 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
                 case END_DOCUMENT:
                     break;
                 case NAME:
-                    parseByName(in.nextName(), edit, in, path, codec, resultCollection, patchId);
+                    parseByName(in.nextName(), edit, in, path, schemaTree, resultCollection, patchId);
                     break;
                 case END_OBJECT:
                     in.endObject();
@@ -170,8 +170,8 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
      * @throws IOException if operation fails
      */
     private void parseByName(final @NonNull String name, final @NonNull PatchEdit edit,
-                             final @NonNull JsonReader in, final @NonNull InstanceIdentifierContext<?> path,
-                             final @NonNull StringModuleInstanceIdentifierCodec codec,
+                             final @NonNull JsonReader in, final @NonNull InstanceIdentifierContext path,
+                             final @NonNull DataSchemaContextTree schemaTree,
                              final @NonNull List<PatchEntity> resultCollection,
                              final @NonNull AtomicReference<String> patchId) throws IOException {
         switch (name) {
@@ -180,14 +180,14 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
                     in.beginArray();
 
                     while (in.hasNext()) {
-                        readEditDefinition(edit, in, path, codec);
+                        readEditDefinition(edit, in, path, schemaTree);
                         resultCollection.add(prepareEditOperation(edit));
                         edit.clear();
                     }
 
                     in.endArray();
                 } else {
-                    readEditDefinition(edit, in, path, codec);
+                    readEditDefinition(edit, in, path, schemaTree);
                     resultCollection.add(prepareEditOperation(edit));
                     edit.clear();
                 }
@@ -211,8 +211,8 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
      * @throws IOException if operation fails
      */
     private void readEditDefinition(final @NonNull PatchEdit edit, final @NonNull JsonReader in,
-                                    final @NonNull InstanceIdentifierContext<?> path,
-                                    final @NonNull StringModuleInstanceIdentifierCodec codec) throws IOException {
+                                    final @NonNull InstanceIdentifierContext path,
+                                    final @NonNull DataSchemaContextTree schemaTree) throws IOException {
         String deferredValue = null;
         in.beginObject();
 
@@ -230,17 +230,20 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
                     final String target = in.nextString();
                     if (target.equals("/")) {
                         edit.setTarget(path.getInstanceIdentifier());
-                        edit.setTargetSchemaNode(path.getSchemaContext());
+                        edit.setTargetSchemaNode(SchemaInferenceStack.of(path.getSchemaContext()).toInference());
                     } else {
-                        edit.setTarget(codec.deserialize(codec.serialize(path.getInstanceIdentifier()).concat(target)));
-
-                        final EffectiveStatement<?, ?> parentStmt = SchemaInferenceStack.ofInstantiatedPath(
-                            path.getSchemaContext(),
-                            codec.getDataContextTree().findChild(edit.getTarget()).orElseThrow().getDataSchemaNode()
-                                .getPath().getParent())
-                            .currentStatement();
-                        verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt);
-                        edit.setTargetSchemaNode((SchemaNode) parentStmt);
+                        edit.setTarget(ParserIdentifier.parserPatchTarget(path, target));
+
+                        final var stack = schemaTree.enterPath(edit.getTarget()).orElseThrow().stack();
+                        if (!stack.isEmpty()) {
+                            stack.exit();
+                        }
+
+                        if (!stack.isEmpty()) {
+                            final EffectiveStatement<?, ?> parentStmt = stack.currentStatement();
+                            verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt);
+                        }
+                        edit.setTargetSchemaNode(stack.toInference());
                     }
 
                     break;
@@ -370,12 +373,11 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
      * @return NormalizedNode representing data
      */
     private static NormalizedNode readEditData(final @NonNull JsonReader in,
-             final @NonNull SchemaNode targetSchemaNode, final @NonNull InstanceIdentifierContext<?> path) {
+             final @NonNull Inference targetSchemaNode, final @NonNull InstanceIdentifierContext path) {
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
         JsonParserStream.create(writer, JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()),
-            SchemaInferenceStack.ofInstantiatedPath(path.getSchemaContext(), targetSchemaNode.getPath()).toInference())
-            .parse(in);
+            targetSchemaNode).parse(in);
 
         return resultHolder.getResult();
     }
@@ -424,7 +426,7 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
         private String id;
         private PatchEditOperation operation;
         private YangInstanceIdentifier target;
-        private SchemaNode targetSchemaNode;
+        private Inference targetSchemaNode;
         private NormalizedNode data;
 
         String getId() {
@@ -451,11 +453,11 @@ public class JsonPatchBodyReader extends AbstractPatchBodyReader {
             this.target = requireNonNull(target);
         }
 
-        SchemaNode getTargetSchemaNode() {
+        Inference getTargetSchemaNode() {
             return targetSchemaNode;
         }
 
-        void setTargetSchemaNode(final SchemaNode targetSchemaNode) {
+        void setTargetSchemaNode(final Inference targetSchemaNode) {
             this.targetSchemaNode = requireNonNull(targetSchemaNode);
         }