Remove AugmentationSchemaNode.getOriginalDefinition()
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / augment / AugmentInferenceAction.java
index 9877dd692441d9c5cf45a9ecbf5ff497ffc933f0..80f4e7e5db8f1d55fd28e45e1a208b74fa0bcc7e 100644 (file)
@@ -14,6 +14,7 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
 import org.opendaylight.yangtools.yang.common.Empty;
@@ -36,7 +37,6 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,20 +62,25 @@ final class AugmentInferenceAction implements InferenceAction {
 
     @Override
     public void apply(final InferenceContext ctx) {
-        final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.resolve(ctx);
+        if (!augmentNode.isSupportedToBuildEffective()) {
+            // We are not building effective model, hence we should not be performing any effects
+            return;
+        }
+
+        final var augmentTargetCtx = target.resolve(ctx);
         if (!isSupportedAugmentTarget(augmentTargetCtx)
                 || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
-            augmentNode.setIsSupportedToBuildEffective(false);
+            augmentNode.setUnsupported();
             return;
         }
 
         // We are targeting a context which is creating implicit nodes. In order to keep things consistent,
         // we will need to circle back when creating effective statements.
         if (augmentTargetCtx.hasImplicitParentSupport()) {
-            augmentNode.addToNs(AugmentImplicitHandlingNamespace.class, Empty.getInstance(), augmentTargetCtx);
+            augmentNode.addToNs(AugmentImplicitHandlingNamespace.class, Empty.value(), augmentTargetCtx);
         }
 
-        copyFromSourceToTarget((StatementContextBase<?, ?, ?>) augmentNode, augmentTargetCtx);
+        copyFromSourceToTarget(augmentNode, augmentTargetCtx);
         augmentTargetCtx.addEffectiveSubstatement(augmentNode.replicaAsChildOf(augmentTargetCtx));
     }
 
@@ -85,11 +90,16 @@ final class AugmentInferenceAction implements InferenceAction {
          * Do not fail, if it is an uses-augment to an unknown node.
          */
         if (YangStmtMapping.USES == augmentNode.coerceParentContext().publicDefinition()) {
+            if (!augmentNode.isSupportedToBuildEffective()) {
+                // We are not supported, hence the uses is not effective and we should bail
+                return;
+            }
+
             final SchemaNodeIdentifier augmentArg = augmentNode.getArgument();
             final Optional<StmtContext<?, ?, ?>> targetNode = SchemaTreeNamespace.findNode(
                 AbstractAugmentStatementSupport.getSearchRoot(augmentNode), augmentArg);
             if (targetNode.isPresent() && StmtContextUtils.isUnknownStatement(targetNode.get())) {
-                augmentNode.setIsSupportedToBuildEffective(false);
+                augmentNode.setUnsupported();
                 LOG.warn("Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
                     augmentArg, augmentNode.sourceReference());
                 return;
@@ -99,8 +109,16 @@ final class AugmentInferenceAction implements InferenceAction {
         throw new InferenceException(augmentNode, "Augment target '%s' not found", augmentNode.argument());
     }
 
-    private void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
-            final StatementContextBase<?, ?, ?> targetCtx) {
+    @Override
+    public void prerequisiteUnavailable(final Prerequisite<?> unavail) {
+        if (target.equals(unavail)) {
+            augmentNode.setUnsupported();
+        } else {
+            prerequisiteFailed(List.of(unavail));
+        }
+    }
+
+    private void copyFromSourceToTarget(final StmtContext<?, ?, ?> sourceCtx, final Mutable<?, ?, ?> targetCtx) {
         final CopyType typeOfCopy = sourceCtx.coerceParentContext().producesDeclared(UsesStatement.class)
             ? CopyType.ADDED_BY_USES_AUGMENTATION : CopyType.ADDED_BY_AUGMENTATION;
         /*
@@ -110,22 +128,22 @@ final class AugmentInferenceAction implements InferenceAction {
         final boolean skipCheckOfMandatoryNodes = statementSupport.allowsMandatory(sourceCtx);
         final boolean unsupported = !sourceCtx.isSupportedByFeatures();
 
-        final Collection<? extends Mutable<?, ?, ?>> declared = sourceCtx.mutableDeclaredSubstatements();
-        final Collection<? extends Mutable<?, ?, ?>> effective = sourceCtx.mutableEffectiveSubstatements();
-        final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
+        final var declared = sourceCtx.declaredSubstatements();
+        final var effective = sourceCtx.effectiveSubstatements();
+        final var buffer = new ArrayList<Mutable<?, ?, ?>>(declared.size() + effective.size());
 
-        for (final Mutable<?, ?, ?> originalStmtCtx : declared) {
+        for (var originalStmtCtx : declared) {
             copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes,
                 unsupported || !originalStmtCtx.isSupportedByFeatures());
         }
-        for (final Mutable<?, ?, ?> originalStmtCtx : effective) {
+        for (var originalStmtCtx : effective) {
             copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes, unsupported);
         }
 
         targetCtx.addEffectiveSubstatements(buffer);
     }
 
-    private static void copyStatement(final Mutable<?, ?, ?> original, final StatementContextBase<?, ?, ?> target,
+    private static void copyStatement(final StmtContext<?, ?, ?> original, final Mutable<?, ?, ?> target,
             final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer,
             final boolean skipCheckOfMandatoryNodes, final boolean unsupported) {
         // We always copy statements, but if either the source statement or the augmentation which causes it are not
@@ -135,7 +153,7 @@ final class AugmentInferenceAction implements InferenceAction {
 
             final Mutable<?, ?, ?> copy = target.childCopyOf(original, typeOfCopy);
             if (unsupported) {
-                copy.setIsSupportedToBuildEffective(false);
+                copy.setUnsupported();
             }
             buffer.add(copy);
         } else if (!unsupported && original.publicDefinition() == YangStmtMapping.TYPEDEF) {
@@ -148,8 +166,7 @@ final class AugmentInferenceAction implements InferenceAction {
     }
 
     private static void validateNodeCanBeCopiedByAugment(final StmtContext<?, ?, ?> sourceCtx,
-            final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy,
-            final boolean skipCheckOfMandatoryNodes) {
+            final Mutable<?, ?, ?> targetCtx, final CopyType typeOfCopy, final boolean skipCheckOfMandatoryNodes) {
         if (!skipCheckOfMandatoryNodes && typeOfCopy == CopyType.ADDED_BY_AUGMENTATION
                 && requireCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
             checkForMandatoryNodes(sourceCtx);
@@ -157,7 +174,7 @@ final class AugmentInferenceAction implements InferenceAction {
 
         // Data definition statements must not collide on their namespace
         if (sourceCtx.producesDeclared(DataDefinitionStatement.class)) {
-            for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
+            for (StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
                 if (subStatement.producesDeclared(DataDefinitionStatement.class)) {
                     InferenceException.throwIf(Objects.equals(sourceCtx.argument(), subStatement.argument()), sourceCtx,
                         "An augment cannot add node named '%s' because this name is already used in target",