Remove Augmentation{Identifier,Node}
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / SchemaAwareApplyOperation.java
index 304021a45c26504e01cbebe2334de6565a7a1844..9052f8a1aa1450e4e4e5e61973314ff1e0fa0411 100644 (file)
@@ -13,9 +13,7 @@ import static com.google.common.base.Verify.verifyNotNull;
 import java.util.List;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.AnydataNode;
 import org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode;
@@ -30,11 +28,8 @@ import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
 import org.opendaylight.yangtools.yang.data.tree.impl.node.Version;
 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
@@ -52,42 +47,27 @@ abstract class SchemaAwareApplyOperation<T extends WithStatus> extends Modificat
         if (!belongsToTree(treeConfig.getTreeType(), schemaNode)) {
             throw new ExcludedDataSchemaNodeException(schemaNode + " does not belong to configuration tree");
         }
-        if (schemaNode instanceof ContainerSchemaNode) {
-            return ContainerModificationStrategy.of((ContainerSchemaNode) schemaNode, treeConfig);
-        } else if (schemaNode instanceof ListSchemaNode) {
-            return fromListSchemaNode((ListSchemaNode) schemaNode, treeConfig);
-        } else if (schemaNode instanceof ChoiceSchemaNode) {
-            return new ChoiceModificationStrategy((ChoiceSchemaNode) schemaNode, treeConfig);
-        } else if (schemaNode instanceof LeafListSchemaNode) {
-            return MinMaxElementsValidation.from(new LeafSetModificationStrategy((LeafListSchemaNode) schemaNode,
-                treeConfig));
-        } else if (schemaNode instanceof LeafSchemaNode) {
-            return new ValueNodeModificationStrategy<>(LeafNode.class, (LeafSchemaNode) schemaNode);
-        } else if (schemaNode instanceof AnydataSchemaNode) {
-            return new ValueNodeModificationStrategy<>(AnydataNode.class, (AnydataSchemaNode) schemaNode);
-        } else if (schemaNode instanceof AnyxmlSchemaNode) {
-            return new ValueNodeModificationStrategy<>(AnyxmlNode.class, (AnyxmlSchemaNode) schemaNode);
-        } else if (schemaNode instanceof SchemaContext) {
-            return new StructuralContainerModificationStrategy((SchemaContext) schemaNode, treeConfig);
+        if (schemaNode instanceof ContainerSchemaNode container) {
+            return ContainerModificationStrategy.of(container, treeConfig);
+        } else if (schemaNode instanceof ListSchemaNode list) {
+            return fromListSchemaNode(list, treeConfig);
+        } else if (schemaNode instanceof ChoiceSchemaNode choice) {
+            return new ChoiceModificationStrategy(choice, treeConfig);
+        } else if (schemaNode instanceof LeafListSchemaNode leafList) {
+            return MinMaxElementsValidation.from(new LeafSetModificationStrategy(leafList, treeConfig));
+        } else if (schemaNode instanceof LeafSchemaNode leaf) {
+            return new ValueNodeModificationStrategy<>(LeafNode.class, leaf);
+        } else if (schemaNode instanceof AnydataSchemaNode anydata) {
+            return new ValueNodeModificationStrategy<>(AnydataNode.class, anydata);
+        } else if (schemaNode instanceof AnyxmlSchemaNode anyxml) {
+            return new ValueNodeModificationStrategy<>(AnyxmlNode.class, anyxml);
+        } else if (schemaNode instanceof SchemaContext context) {
+            return new StructuralContainerModificationStrategy(context, treeConfig);
         } else {
             throw new IllegalStateException("Unsupported schema " + schemaNode);
         }
     }
 
-    static @Nullable AugmentationModificationStrategy from(final DataNodeContainer resolvedTree,
-            final AugmentationTarget augSchemas, final AugmentationIdentifier identifier,
-            final DataTreeConfiguration treeConfig) {
-        for (final AugmentationSchemaNode potential : augSchemas.getAvailableAugmentations()) {
-            for (final DataSchemaNode child : potential.getChildNodes()) {
-                if (identifier.getPossibleChildNames().contains(child.getQName())) {
-                    return new AugmentationModificationStrategy(potential, resolvedTree, treeConfig);
-                }
-            }
-        }
-
-        return null;
-    }
-
     static void checkConflicting(final ModificationPath path, final boolean condition, final String message)
             throws ConflictingModificationAppliedException {
         if (!condition) {
@@ -126,23 +106,15 @@ abstract class SchemaAwareApplyOperation<T extends WithStatus> extends Modificat
     final void checkApplicable(final ModificationPath path, final NodeModification modification,
             final Optional<? extends TreeNode> current, final Version version) throws DataValidationFailedException {
         switch (modification.getOperation()) {
-            case DELETE:
-                checkDeleteApplicable(modification, current);
-                break;
-            case TOUCH:
-                checkTouchApplicable(path, modification, current, version);
-                break;
-            case WRITE:
-                checkWriteApplicable(path, modification, current, version);
-                break;
-            case MERGE:
-                checkMergeApplicable(path, modification, current, version);
-                break;
-            case NONE:
-                break;
-            default:
-                throw new UnsupportedOperationException(
-                    "Suplied modification type " + modification.getOperation() + " is not supported.");
+            case DELETE -> checkDeleteApplicable(modification, current);
+            case TOUCH -> checkTouchApplicable(path, modification, current, version);
+            case WRITE -> checkWriteApplicable(path, modification, current, version);
+            case MERGE -> checkMergeApplicable(path, modification, current, version);
+            case NONE -> {
+                // No-op
+            }
+            default -> throw new UnsupportedOperationException(
+                "Suplied modification type " + modification.getOperation() + " is not supported.");
         }
     }
 
@@ -184,8 +156,8 @@ abstract class SchemaAwareApplyOperation<T extends WithStatus> extends Modificat
              * it should not cause transaction to fail, since result of this merge
              * leads to same data.
              */
-            final TreeNode orig = original.get();
-            final TreeNode cur = current.get();
+            final TreeNode orig = original.orElseThrow();
+            final TreeNode cur = current.orElseThrow();
             if (!orig.getData().equals(cur.getData())) {
                 checkNotConflicting(path, orig, cur);
             }
@@ -206,7 +178,7 @@ abstract class SchemaAwareApplyOperation<T extends WithStatus> extends Modificat
             final Optional<? extends TreeNode> current, final Version version) throws DataValidationFailedException {
         final Optional<? extends TreeNode> original = modification.getOriginal();
         if (original.isPresent() && current.isPresent()) {
-            checkNotConflicting(path, original.get(), current.get());
+            checkNotConflicting(path, original.orElseThrow(), current.orElseThrow());
         } else {
             checkConflicting(path, !original.isPresent(), "Node was deleted by other transaction.");
             checkConflicting(path, !current.isPresent(), "Node was created by other transaction.");
@@ -224,17 +196,19 @@ abstract class SchemaAwareApplyOperation<T extends WithStatus> extends Modificat
     @Override
     Optional<? extends TreeNode> apply(final ModifiedNode modification, final Optional<? extends TreeNode> currentMeta,
             final Version version) {
-        switch (modification.getOperation()) {
-            case DELETE:
+        return switch (modification.getOperation()) {
+            case DELETE -> {
                 // Deletion of a non-existing node is a no-op, report it as such
                 modification.resolveModificationType(currentMeta.isPresent() ? ModificationType.DELETE
                         : ModificationType.UNMODIFIED);
-                return modification.setSnapshot(Optional.empty());
-            case TOUCH:
+                yield modification.setSnapshot(Optional.empty());
+            }
+            case TOUCH -> {
                 checkArgument(currentMeta.isPresent(), "Metadata not available for modification %s", modification);
-                return modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.get(),
+                yield modification.setSnapshot(Optional.of(applyTouch(modification, currentMeta.orElseThrow(),
                     version)));
-            case MERGE:
+            }
+            case MERGE -> {
                 final TreeNode result;
 
                 if (!currentMeta.isPresent()) {
@@ -245,20 +219,21 @@ abstract class SchemaAwareApplyOperation<T extends WithStatus> extends Modificat
                     result = applyWrite(modification, modification.getWrittenValue(), currentMeta, version);
                     fullVerifyStructure(result.getData());
                 } else {
-                    result = applyMerge(modification, currentMeta.get(), version);
+                    result = applyMerge(modification, currentMeta.orElseThrow(), version);
                 }
 
-                return modification.setSnapshot(Optional.of(result));
-            case WRITE:
+                yield modification.setSnapshot(Optional.of(result));
+            }
+            case WRITE -> {
                 modification.resolveModificationType(ModificationType.WRITE);
-                return modification.setSnapshot(Optional.of(applyWrite(modification,
+                yield modification.setSnapshot(Optional.of(applyWrite(modification,
                     verifyNotNull(modification.getWrittenValue()), currentMeta, version)));
-            case NONE:
+            }
+            case NONE -> {
                 modification.resolveModificationType(ModificationType.UNMODIFIED);
-                return currentMeta;
-            default:
-                throw new IllegalArgumentException("Provided modification type is not supported.");
-        }
+                yield currentMeta;
+            }
+        };
     }
 
     /**