Do not suppress 'uses' node effects
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / uses / UsesStatementSupport.java
index f2d2f24a8f7da699936424a0e256a97817501caa..3110ce8a8074130d9ce91e1fe6ee424e86fe755c 100644 (file)
@@ -12,11 +12,9 @@ import static com.google.common.base.Verify.verifyNotNull;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashMap;
-import java.util.Map;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.Empty;
@@ -44,14 +42,12 @@ import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBund
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.refine.RefineEffectiveStatementImpl;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.refine.RefineTargetNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
 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;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
@@ -60,10 +56,9 @@ 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.meta.SubstatementValidator;
-import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-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.spi.validation.ValidationBundles;
+import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundles.ValidationBundleType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -92,30 +87,33 @@ public final class UsesStatementSupport
 
     @Override
     public void onFullDefinitionDeclared(final Mutable<QName, UsesStatement, UsesEffectiveStatement> usesNode) {
-        if (!usesNode.isSupportedByFeatures()) {
-            return;
-        }
         super.onFullDefinitionDeclared(usesNode);
 
-        final ModelActionBuilder usesAction = usesNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
-        final QName groupingName = usesNode.argument();
+        final var usesAction = usesNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
+        final var groupingName = usesNode.argument();
 
-        final Prerequisite<StmtContext<?, ?, ?>> sourceGroupingPre = usesAction.requiresCtx(usesNode,
-                GroupingNamespace.class, groupingName, ModelProcessingPhase.EFFECTIVE_MODEL);
-        final Prerequisite<? extends Mutable<?, ?, ?>> targetNodePre = usesAction.mutatesEffectiveCtx(
-                usesNode.getParentContext());
+        final var sourceGroupingPre = usesAction.requiresCtx(usesNode, ParserNamespaces.GROUPING, groupingName,
+                ModelProcessingPhase.EFFECTIVE_MODEL);
+        final var targetNodePre = usesAction.mutatesEffectiveCtx(usesNode.getParentContext());
 
         usesAction.apply(new InferenceAction() {
 
             @Override
             public void apply(final InferenceContext ctx) {
-                final Mutable<?, ?, ?> targetNodeStmtCtx = targetNodePre.resolve(ctx);
-                final StmtContext<?, ?, ?> sourceGrpStmtCtx = sourceGroupingPre.resolve(ctx);
+                final var targetNodeStmtCtx = targetNodePre.resolve(ctx);
+                final var sourceGrpStmtCtx = sourceGroupingPre.resolve(ctx);
 
                 copyFromSourceToTarget(sourceGrpStmtCtx, targetNodeStmtCtx, usesNode);
-                resolveUsesNode(usesNode, targetNodeStmtCtx);
+
+                // Apply any refine statements
+                for (var subStmtCtx : usesNode.mutableDeclaredSubstatements()) {
+                    if (subStmtCtx.producesDeclared(RefineStatement.class) && areFeaturesSupported(subStmtCtx)) {
+                        performRefine(subStmtCtx, targetNodeStmtCtx);
+                    }
+                }
+
                 StmtContextUtils.validateIfFeatureAndWhenOnListKeys(usesNode);
-                usesNode.addToNs(SourceGroupingNamespace.class, Empty.value(), sourceGrpStmtCtx);
+                usesNode.addToNs(SourceGroupingNamespace.INSTANCE, Empty.value(), sourceGrpStmtCtx);
             }
 
             @Override
@@ -142,7 +140,7 @@ public final class UsesStatementSupport
     protected UsesEffectiveStatement createEffective(final Current<QName, UsesStatement> stmt,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
         final EffectiveStatement<?, ?> source =
-            verifyNotNull(stmt.getFromNamespace(SourceGroupingNamespace.class, Empty.value())).buildEffective();
+            verifyNotNull(stmt.getFromNamespace(SourceGroupingNamespace.INSTANCE, Empty.value())).buildEffective();
         verify(source instanceof GroupingDefinition, "Unexpected source %s", source);
         final GroupingDefinition sourceGrouping = (GroupingDefinition) source;
 
@@ -167,15 +165,12 @@ public final class UsesStatementSupport
 
     static @NonNull ImmutableMap<Descendant, SchemaNode> indexRefines(
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
-        final Map<Descendant, SchemaNode> refines = new LinkedHashMap<>();
-
-        for (EffectiveStatement<?, ?> effectiveStatement : substatements) {
-            if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
-                final RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
+        final var refines = new LinkedHashMap<Descendant, SchemaNode>();
+        for (var effectiveStatement : substatements) {
+            if (effectiveStatement instanceof RefineEffectiveStatementImpl refineStmt) {
                 refines.put(refineStmt.argument(), refineStmt.getRefineTargetNode());
             }
         }
-
         return ImmutableMap.copyOf(refines);
     }
 
@@ -191,19 +186,18 @@ public final class UsesStatementSupport
      * @throws SourceException
      *             instance of SourceException
      */
-    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
-            justification = "https://github.com/spotbugs/spotbugs/issues/811")
     private static void copyFromSourceToTarget(final StmtContext<?, ?, ?> sourceGrpStmtCtx,
             final Mutable<?, ?, ?> targetCtx, final Mutable<QName, UsesStatement, UsesEffectiveStatement> usesNode) {
         final var declared = sourceGrpStmtCtx.declaredSubstatements();
         final var effective = sourceGrpStmtCtx.effectiveSubstatements();
         final var buffer = new ArrayList<Mutable<?, ?, ?>>(declared.size() + effective.size());
-        final QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
+        final var newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
+        final var unsupported = !usesNode.isSupportedByFeatures();
 
-        for (StmtContext<?, ?, ?> original : declared) {
+        for (var original : declared) {
             if (shouldCopy(original)) {
                 original.copyAsChildOf(targetCtx, CopyType.ADDED_BY_USES, newQNameModule).ifPresent(copy -> {
-                    if (!original.isSupportedByFeatures() || !original.isSupportedToBuildEffective()) {
+                    if (unsupported || !original.isSupportedByFeatures() || !original.isSupportedToBuildEffective()) {
                         copy.setUnsupported();
                     }
                     buffer.add(copy);
@@ -211,7 +205,7 @@ public final class UsesStatementSupport
             }
         }
 
-        for (StmtContext<?, ?, ?> original : effective) {
+        for (var original : effective) {
             if (shouldCopy(original)) {
                 original.copyAsChildOf(targetCtx, CopyType.ADDED_BY_USES, newQNameModule).ifPresent(buffer::add);
             }
@@ -260,32 +254,18 @@ public final class UsesStatementSupport
     private static QNameModule getNewQNameModule(final StmtContext<?, ?, ?> targetCtx,
             final StmtContext<?, ?, ?> stmtContext) {
         if (targetCtx.getParentContext() == null) {
-            return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx);
+            return targetCtx.getFromNamespace(ParserNamespaces.MODULECTX_TO_QNAME, targetCtx);
         }
         if (targetCtx.publicDefinition() == YangStmtMapping.AUGMENT) {
             return StmtContextUtils.getRootModuleQName(targetCtx);
         }
-
-        final Object targetStmtArgument = targetCtx.argument();
-        final Object sourceStmtArgument = stmtContext.argument();
-        if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
-            return ((QName) targetStmtArgument).getModule();
+        if (targetCtx.argument() instanceof QName targetQName && stmtContext.argument() instanceof QName) {
+            return targetQName.getModule();
         }
 
         return null;
     }
 
-    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
-            justification = "https://github.com/spotbugs/spotbugs/issues/811")
-    private static void resolveUsesNode(final Mutable<QName, UsesStatement, UsesEffectiveStatement> usesNode,
-            final StmtContext<?, ?, ?> targetNodeStmtCtx) {
-        for (Mutable<?, ?, ?> subStmtCtx : usesNode.mutableDeclaredSubstatements()) {
-            if (subStmtCtx.producesDeclared(RefineStatement.class) && areFeaturesSupported(subStmtCtx)) {
-                performRefine(subStmtCtx, targetNodeStmtCtx);
-            }
-        }
-    }
-
     private static boolean areFeaturesSupported(final StmtContext<?, ?, ?> subStmtCtx) {
         /*
          * In case of Yang 1.1, checks whether features are supported.
@@ -300,7 +280,7 @@ public final class UsesStatementSupport
 
         // FIXME: this really should be handled via separate inference, i.e. we first instantiate the template and when
         //        it appears, this refine will trigger on it. This reinforces the FIXME below.
-        final Optional<StmtContext<?, ?, ?>> optRefineTargetCtx = SchemaTreeNamespace.findNode(
+        final Optional<StmtContext<?, ?, ?>> optRefineTargetCtx = ParserNamespaces.findSchemaTreeStatement(
             usesParentCtx, (SchemaNodeIdentifier) refineArgument);
         InferenceException.throwIf(!optRefineTargetCtx.isPresent(), subStmtCtx, "Refine target node %s not found.",
             refineArgument);
@@ -322,7 +302,7 @@ public final class UsesStatementSupport
         // Target is a prerequisite for the 'refine', hence if the target is not supported, the refine is not supported
         // as well. Otherwise add a pointer to the target into refine's local namespace.
         if (refineTargetNodeCtx.isSupportedToBuildEffective()) {
-            subStmtCtx.addToNs(RefineTargetNamespace.class, Empty.value(), refineTargetNodeCtx);
+            subStmtCtx.addToNs(RefineTargetNamespace.INSTANCE, Empty.value(), refineTargetNodeCtx);
         } else {
             subStmtCtx.setUnsupported();
         }
@@ -363,7 +343,7 @@ public final class UsesStatementSupport
 
     private static boolean isSupportedRefineSubstatement(final StmtContext<?, ?, ?> refineSubstatementCtx) {
         final Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
-                ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
+                ValidationBundles.NAMESPACE, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
 
         return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
                 || supportedRefineSubstatements.contains(refineSubstatementCtx.publicDefinition())