Bump odlparent to 10.0.0
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / deviate / AbstractDeviateStatementSupport.java
index e76d9a1f7cb5004736ec90761928807ed9509e67..11dc737e53faf71429dfb4bc795b894a75fc0a1f 100644 (file)
@@ -13,11 +13,12 @@ import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import com.google.common.collect.SetMultimap;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.List;
 import java.util.Objects;
 import java.util.Set;
+import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
@@ -36,6 +37,7 @@ import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.BoundStmtCtx;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
@@ -50,9 +52,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModulesDeviatedByModules;
-import org.opendaylight.yangtools.yang.parser.spi.source.ModulesDeviatedByModules.SupportedModules;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -143,15 +143,18 @@ abstract class AbstractDeviateStatementSupport
         deviateAction.apply(new InferenceAction() {
             @Override
             public void apply(final InferenceContext ctx) {
-                // FIXME once BUG-7760 gets fixed, there will be no need for these dirty casts
-                final StatementContextBase<?, ?, ?> sourceNodeStmtCtx =
-                        (StatementContextBase<?, ?, ?>) sourceCtxPrerequisite.resolve(ctx);
-                final StatementContextBase<?, ?, ?> targetNodeStmtCtx =
-                        (StatementContextBase<?, ?, ?>) targetCtxPrerequisite.resolve(ctx);
+                if (!deviateStmtCtx.isSupportedToBuildEffective()) {
+                    // We are not building effective model, hence we should not be performing any effects
+                    return;
+                }
+
+                final var sourceNodeStmtCtx = sourceCtxPrerequisite.resolve(ctx);
+                final var targetNodeStmtCtx = targetCtxPrerequisite.resolve(ctx);
 
                 switch (deviateKind) {
                     case NOT_SUPPORTED:
-                        targetNodeStmtCtx.setIsSupportedToBuildEffective(false);
+                        // FIXME: this can be short-circuited without an inference action
+                        targetNodeStmtCtx.setUnsupported();
                         break;
                     case ADD:
                         performDeviateAdd(sourceNodeStmtCtx, targetNodeStmtCtx);
@@ -172,6 +175,15 @@ abstract class AbstractDeviateStatementSupport
                 throw new InferenceException(deviateStmtCtx.coerceParentContext(), "Deviation target '%s' not found.",
                     deviationTarget);
             }
+
+            @Override
+            public void prerequisiteUnavailable(final Prerequisite<?> unavail) {
+                if (targetCtxPrerequisite.equals(unavail)) {
+                    deviateStmtCtx.setUnsupported();
+                } else {
+                    prerequisiteFailed(List.of(unavail));
+                }
+            }
         });
     }
 
@@ -191,8 +203,8 @@ abstract class AbstractDeviateStatementSupport
     }
 
     @Override
-    protected final DeviateStatement createDeclared(final StmtContext<DeviateKind, DeviateStatement, ?> ctx,
-            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+    protected final DeviateStatement createDeclared(final BoundStmtCtx<DeviateKind> ctx,
+            final ImmutableList<DeclaredStatement<?>> substatements) {
         return DeclaredStatements.createDeviate(ctx.getArgument(), substatements);
     }
 
@@ -228,7 +240,7 @@ abstract class AbstractDeviateStatementSupport
             final Mutable<DeviateKind, DeviateStatement, DeviateEffectiveStatement> deviateStmtCtx,
             final SchemaNodeIdentifier deviationTarget) {
         final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules = deviateStmtCtx.getFromNamespace(
-                ModulesDeviatedByModules.class, SupportedModules.SUPPORTED_MODULES);
+                ModulesDeviatedByModules.class, Empty.value());
         if (modulesDeviatedByModules == null) {
             return true;
         }
@@ -245,23 +257,20 @@ abstract class AbstractDeviateStatementSupport
         return false;
     }
 
-    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
-            justification = "https://github.com/spotbugs/spotbugs/issues/811")
-    private static void performDeviateAdd(final StatementContextBase<?, ?, ?> deviateStmtCtx,
-            final StatementContextBase<?, ?, ?> targetCtx) {
-        for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
+    private static void performDeviateAdd(final StmtContext<?, ?, ?> deviateStmtCtx,
+            final Mutable<?, ?, ?> targetCtx) {
+        for (StmtContext<?, ?, ?> originalStmtCtx : deviateStmtCtx.declaredSubstatements()) {
             validateDeviationTarget(originalStmtCtx, targetCtx);
             addStatement(originalStmtCtx, targetCtx);
         }
     }
 
-    private static void addStatement(final Mutable<?, ?, ?> stmtCtxToBeAdded,
-            final StatementContextBase<?, ?, ?> targetCtx) {
+    private static void addStatement(final StmtContext<?, ?, ?> stmtCtxToBeAdded, final Mutable<?, ?, ?> targetCtx) {
         if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeAdded)) {
             final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.publicDefinition();
             if (SINGLETON_STATEMENTS.contains(stmtToBeAdded) || YangStmtMapping.DEFAULT.equals(stmtToBeAdded)
                     && YangStmtMapping.LEAF.equals(targetCtx.publicDefinition())) {
-                for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.allSubstatements()) {
+                for (StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.allSubstatements()) {
                     InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.publicDefinition()),
                         stmtCtxToBeAdded,
                         "Deviation cannot add substatement %s to target node %s because it is already defined "
@@ -274,18 +283,16 @@ abstract class AbstractDeviateStatementSupport
         copyStatement(stmtCtxToBeAdded, targetCtx);
     }
 
-    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
-            justification = "https://github.com/spotbugs/spotbugs/issues/811")
-    private static void performDeviateReplace(final StatementContextBase<?, ?, ?> deviateStmtCtx,
-            final StatementContextBase<?, ?, ?> targetCtx) {
-        for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
+    private static void performDeviateReplace(final StmtContext<?, ?, ?> deviateStmtCtx,
+            final Mutable<?, ?, ?> targetCtx) {
+        for (StmtContext<?, ?, ?> originalStmtCtx : deviateStmtCtx.declaredSubstatements()) {
             validateDeviationTarget(originalStmtCtx, targetCtx);
             replaceStatement(originalStmtCtx, targetCtx);
         }
     }
 
-    private static void replaceStatement(final Mutable<?, ?, ?> stmtCtxToBeReplaced,
-            final StatementContextBase<?, ?, ?> targetCtx) {
+    private static void replaceStatement(final StmtContext<?, ?, ?> stmtCtxToBeReplaced,
+            final Mutable<?, ?, ?> targetCtx) {
         final StatementDefinition stmtToBeReplaced = stmtCtxToBeReplaced.publicDefinition();
 
         if (YangStmtMapping.DEFAULT.equals(stmtToBeReplaced)
@@ -296,7 +303,7 @@ abstract class AbstractDeviateStatementSupport
             return;
         }
 
-        for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
+        for (StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
             if (stmtToBeReplaced.equals(targetCtxSubstatement.publicDefinition())) {
                 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced);
                 copyStatement(stmtCtxToBeReplaced, targetCtx);
@@ -304,9 +311,9 @@ abstract class AbstractDeviateStatementSupport
             }
         }
 
-        for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
+        for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
             if (stmtToBeReplaced.equals(targetCtxSubstatement.publicDefinition())) {
-                targetCtxSubstatement.setIsSupportedToBuildEffective(false);
+                targetCtxSubstatement.setUnsupported();
                 copyStatement(stmtCtxToBeReplaced, targetCtx);
                 return;
             }
@@ -325,22 +332,20 @@ abstract class AbstractDeviateStatementSupport
             stmtToBeReplaced.getStatementName(), targetCtx.argument());
     }
 
-    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
-            justification = "https://github.com/spotbugs/spotbugs/issues/811")
-    private static void performDeviateDelete(final StatementContextBase<?, ?, ?> deviateStmtCtx,
-            final StatementContextBase<?, ?, ?> targetCtx) {
-        for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
+    private static void performDeviateDelete(final StmtContext<?, ?, ?> deviateStmtCtx,
+                final Mutable<?, ?, ?> targetCtx) {
+        for (StmtContext<?, ?, ?> originalStmtCtx : deviateStmtCtx.declaredSubstatements()) {
             validateDeviationTarget(originalStmtCtx, targetCtx);
             deleteStatement(originalStmtCtx, targetCtx);
         }
     }
 
     private static void deleteStatement(final StmtContext<?, ?, ?> stmtCtxToBeDeleted,
-            final StatementContextBase<?, ?, ?> targetCtx) {
+            final Mutable<?, ?, ?> targetCtx) {
         final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.publicDefinition();
         final String stmtArgument = stmtCtxToBeDeleted.rawArgument();
 
-        for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) {
+        for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) {
             if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(),
                     targetCtxSubstatement.rawArgument())) {
                 targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeDeleted, stmtArgument);
@@ -348,10 +353,10 @@ abstract class AbstractDeviateStatementSupport
             }
         }
 
-        for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
+        for (Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
             if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(),
                     targetCtxSubstatement.rawArgument())) {
-                targetCtxSubstatement.setIsSupportedToBuildEffective(false);
+                targetCtxSubstatement.setUnsupported();
                 return;
             }
         }
@@ -361,11 +366,11 @@ abstract class AbstractDeviateStatementSupport
                 targetCtx.argument(), stmtCtxToBeDeleted.sourceReference());
     }
 
-    private static void copyStatement(final Mutable<?, ?, ?> stmtCtxToBeCopied,
-            final StatementContextBase<?, ?, ?> targetCtx) {
+    private static void copyStatement(final StmtContext<?, ?, ?> stmtCtxToBeCopied, final Mutable<?, ?, ?> targetCtx) {
         // we need to make a copy of the statement context only if it is an unknown statement, otherwise
         // we can reuse the original statement context
         if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeCopied)) {
+            // FIXME: I think this should be handled by the corresponding support's copy policy
             targetCtx.addEffectiveSubstatement(stmtCtxToBeCopied.replicaAsChildOf(targetCtx));
         } else {
             targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeCopied, CopyType.ORIGINAL));