Add yang.parser.rfc7950.stmt.meta
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / uses / UsesStatementSupport.java
index 96dac6261b6f842564e78eef6de43e3938379f30..117fcf30dfd521be07afc0baff9326bef4d98a0a 100644 (file)
@@ -7,7 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.uses;
 
-import com.google.common.base.Verify;
+import static com.google.common.base.Verify.verify;
+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;
@@ -17,6 +19,7 @@ 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;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangVersion;
@@ -33,11 +36,14 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Desce
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UsesEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
+import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
+import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
 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.meta.AbstractQNameStatementSupport;
 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;
@@ -59,7 +65,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class UsesStatementSupport
-        extends BaseQNameStatementSupport<UsesStatement, UsesEffectiveStatement> {
+        extends AbstractQNameStatementSupport<UsesStatement, UsesEffectiveStatement> {
     private static final Logger LOG = LoggerFactory.getLogger(UsesStatementSupport.class);
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
         .USES)
@@ -74,7 +80,7 @@ public final class UsesStatementSupport
     private static final UsesStatementSupport INSTANCE = new UsesStatementSupport();
 
     private UsesStatementSupport() {
-        super(YangStmtMapping.USES);
+        super(YangStmtMapping.USES, StatementPolicy.exactReplica());
     }
 
     public static UsesStatementSupport getInstance() {
@@ -94,7 +100,7 @@ public final class UsesStatementSupport
         super.onFullDefinitionDeclared(usesNode);
 
         final ModelActionBuilder usesAction = usesNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
-        final QName groupingName = usesNode.getStatementArgument();
+        final QName groupingName = usesNode.argument();
 
         final Prerequisite<StmtContext<?, ?, ?>> sourceGroupingPre = usesAction.requiresCtx(usesNode,
                 GroupingNamespace.class, groupingName, ModelProcessingPhase.EFFECTIVE_MODEL);
@@ -113,13 +119,14 @@ public final class UsesStatementSupport
                 copyFromSourceToTarget(sourceGrpStmtCtx, targetNodeStmtCtx, usesNode);
                 resolveUsesNode(usesNode, targetNodeStmtCtx);
                 StmtContextUtils.validateIfFeatureAndWhenOnListKeys(usesNode);
+                usesNode.addToNs(SourceGroupingNamespace.class, Empty.getInstance(), sourceGrpStmtCtx);
             }
 
             @Override
             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
-                InferenceException.throwIf(failed.contains(sourceGroupingPre),
-                        usesNode.getStatementSourceReference(), "Grouping '%s' was not resolved.", groupingName);
-                throw new InferenceException("Unknown error occurred.", usesNode.getStatementSourceReference());
+                InferenceException.throwIf(failed.contains(sourceGroupingPre), usesNode,
+                    "Grouping '%s' was not resolved.", groupingName);
+                throw new InferenceException("Unknown error occurred.", usesNode);
             }
         });
     }
@@ -132,19 +139,20 @@ public final class UsesStatementSupport
     @Override
     protected UsesStatement createDeclared(final StmtContext<QName, UsesStatement, ?> ctx,
             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
-        return new RegularUsesStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(), substatements);
+        return DeclaredStatements.createUses(ctx.getRawArgument(), ctx.getArgument(), substatements);
     }
 
     @Override
     protected UsesStatement createEmptyDeclared(final StmtContext<QName, UsesStatement, ?> ctx) {
-        return new EmptyUsesStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
+        return DeclaredStatements.createUses(ctx.getRawArgument(), ctx.getArgument());
     }
 
     @Override
     protected UsesEffectiveStatement createEffective(final Current<QName, UsesStatement> stmt,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
-        final GroupingDefinition sourceGrouping = getSourceGrouping(stmt);
-        final int flags = historyAndStatusFlags(stmt.history(), substatements);
+        final GroupingDefinition sourceGrouping = (GroupingDefinition)
+            verifyNotNull(stmt.getFromNamespace(SourceGroupingNamespace.class, Empty.getInstance())).buildEffective();
+        final int flags = EffectiveStatementMixins.historyAndStatusFlags(stmt.history(), substatements);
         final QName argument = stmt.getArgument();
         final UsesStatement declared = stmt.declared();
 
@@ -177,10 +185,6 @@ public final class UsesStatementSupport
         return ImmutableMap.copyOf(refines);
     }
 
-    private static GroupingDefinition getSourceGrouping(final Current<QName, ?> stmt) {
-        return (GroupingDefinition) stmt.getFromNamespace(GroupingNamespace.class, stmt.getArgument()).buildEffective();
-    }
-
     /**
      * Copy statements from a grouping to a target node.
      *
@@ -264,8 +268,8 @@ public final class UsesStatementSupport
             return StmtContextUtils.getRootModuleQName(targetCtx);
         }
 
-        final Object targetStmtArgument = targetCtx.getStatementArgument();
-        final Object sourceStmtArgument = stmtContext.getStatementArgument();
+        final Object targetStmtArgument = targetCtx.argument();
+        final Object sourceStmtArgument = stmtContext.argument();
         if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
             return ((QName) targetStmtArgument).getModule();
         }
@@ -288,33 +292,40 @@ public final class UsesStatementSupport
         /*
          * In case of Yang 1.1, checks whether features are supported.
          */
-        return !YangVersion.VERSION_1_1.equals(subStmtCtx.getRootVersion()) || subStmtCtx.isSupportedByFeatures();
+        return !YangVersion.VERSION_1_1.equals(subStmtCtx.yangVersion()) || subStmtCtx.isSupportedByFeatures();
     }
 
     private static void performRefine(final Mutable<?, ?, ?> subStmtCtx, final StmtContext<?, ?, ?> usesParentCtx) {
-        final Object refineArgument = subStmtCtx.getStatementArgument();
-        InferenceException.throwIf(!(refineArgument instanceof SchemaNodeIdentifier),
-            subStmtCtx.getStatementSourceReference(),
+        final Object refineArgument = subStmtCtx.argument();
+        InferenceException.throwIf(!(refineArgument instanceof SchemaNodeIdentifier), subStmtCtx,
             "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier.", refineArgument);
 
         final Optional<StmtContext<?, ?, ?>> optRefineTargetCtx = SchemaTreeNamespace.findNode(
             usesParentCtx, (SchemaNodeIdentifier) refineArgument);
-        InferenceException.throwIf(!optRefineTargetCtx.isPresent(), subStmtCtx.getStatementSourceReference(),
-            "Refine target node %s not found.", refineArgument);
+        InferenceException.throwIf(!optRefineTargetCtx.isPresent(), subStmtCtx, "Refine target node %s not found.",
+            refineArgument);
 
+        // FIXME: This communicates the looked-up target node to RefineStatementSupport.buildEffective(). We should do
+        //        this trick through a shared namespace or similar reactor-agnostic meeting place. It really feels like
+        //        an inference action RefineStatementSupport should be doing.
         final StmtContext<?, ?, ?> refineTargetNodeCtx = optRefineTargetCtx.get();
         if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
             LOG.trace("Refine node '{}' in uses '{}' has target node unknown statement '{}'. "
-                + "Refine has been skipped. At line: {}", subStmtCtx.getStatementArgument(),
-                subStmtCtx.coerceParentContext().getStatementArgument(),
-                refineTargetNodeCtx.getStatementArgument(), subStmtCtx.getStatementSourceReference());
-            subStmtCtx.addAsEffectOfStatement(refineTargetNodeCtx);
-            return;
+                + "Refine has been skipped. At line: {}", subStmtCtx.argument(),
+                subStmtCtx.coerceParentContext().argument(), refineTargetNodeCtx.argument(),
+                subStmtCtx.sourceReference());
+        } else {
+            verify(refineTargetNodeCtx instanceof StatementContextBase);
+            addOrReplaceNodes(subStmtCtx, (StatementContextBase<?, ?, ?>) refineTargetNodeCtx);
         }
 
-        Verify.verify(refineTargetNodeCtx instanceof StatementContextBase);
-        addOrReplaceNodes(subStmtCtx, (StatementContextBase<?, ?, ?>) refineTargetNodeCtx);
-        subStmtCtx.addAsEffectOfStatement(refineTargetNodeCtx);
+        // 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.getInstance(), refineTargetNodeCtx);
+        } else {
+            subStmtCtx.setIsSupportedToBuildEffective(false);
+        }
     }
 
     private static void addOrReplaceNodes(final Mutable<?, ?, ?> subStmtCtx,
@@ -332,20 +343,18 @@ public final class UsesStatementSupport
         final StatementDefinition refineSubstatementDef = refineSubstatementCtx.publicDefinition();
 
         SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
-                refineSubstatementCtx.getStatementSourceReference(),
+                refineSubstatementCtx,
                 "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
-                refineSubstatementCtx.getRoot().rawStatementArgument(),
-                refineSubstatementCtx.coerceParentContext().getStatementArgument(),
+                refineSubstatementCtx.getRoot().rawArgument(), refineSubstatementCtx.coerceParentContext().argument(),
                 refineSubstatementCtx.publicDefinition(), refineTargetNodeCtx.publicDefinition());
 
-        if (isAllowedToAddByRefine(refineSubstatementDef)) {
-            refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
-        } else {
+        if (!isAllowedToAddByRefine(refineSubstatementDef)) {
             refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
-            refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
         }
+        refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx.replicaAsChildOf(refineTargetNodeCtx));
     }
 
+    // FIXME: clarify this and inline into single caller
     private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
         return YangStmtMapping.MUST.equals(publicDefinition);
     }