Fix augment/deviate mechanics
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / augment / AugmentInferenceAction.java
index 9877dd692441d9c5cf45a9ecbf5ff497ffc933f0..54ee72d8abbcece084c0afca75b38c864b208bcc 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;
@@ -25,7 +26,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
-import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
@@ -34,9 +35,8 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prereq
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 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.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundles;
+import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundles.ValidationBundleType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,6 +52,8 @@ final class AugmentInferenceAction implements InferenceAction {
     private final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target;
     private final AbstractAugmentStatementSupport statementSupport;
 
+    private boolean targetUnavailable;
+
     AugmentInferenceAction(final AbstractAugmentStatementSupport statementSupport,
             final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode,
             final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target) {
@@ -62,20 +64,26 @@ final class AugmentInferenceAction implements InferenceAction {
 
     @Override
     public void apply(final InferenceContext ctx) {
-        final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.resolve(ctx);
+        if (targetUnavailable) {
+            // Target node is not available, no further processing and the augment should not leak into effective world
+            augmentNode.setUnsupported();
+            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.INSTANCE, Empty.value(), augmentTargetCtx);
         }
 
-        copyFromSourceToTarget((StatementContextBase<?, ?, ?>) augmentNode, augmentTargetCtx);
+        copyFromSourceToTarget(augmentNode, augmentTargetCtx);
         augmentTargetCtx.addEffectiveSubstatement(augmentNode.replicaAsChildOf(augmentTargetCtx));
     }
 
@@ -85,11 +93,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(
+            final Optional<StmtContext<?, ?, ?>> targetNode = ParserNamespaces.findSchemaTreeStatement(
                 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 +112,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)) {
+            targetUnavailable = true;
+        } 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 +131,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 +156,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 +169,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 +177,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",
@@ -191,11 +211,9 @@ final class AugmentInferenceAction implements InferenceAction {
          * statement, therefore return false and skip mandatory nodes validation
          */
         final Object arg = sourceCtx.argument();
-        if (!(arg instanceof QName)) {
+        if (!(arg instanceof QName sourceStmtQName)) {
             return false;
         }
-        final QName sourceStmtQName = (QName) arg;
-
         // RootStatementContext, for example
         final Mutable<?, ?, ?> root = targetCtx.getRoot();
         do {
@@ -265,7 +283,7 @@ final class AugmentInferenceAction implements InferenceAction {
          * ignored as disallowed augment target.
          */
         final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(
-            ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
+            ValidationBundles.NAMESPACE, ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
 
         // if no allowed target is returned we consider all targets allowed
         return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()