Graduate ChildSchemaNodeNamespace as SchemaTreeNamespace
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / augment / AbstractAugmentStatementSupport.java
index 4bb11bb9c3fa927a1fac2e141c450310d5b8661b..5c85334c3cdaf196a7fe16bf0e9df12f60de89f7 100644 (file)
@@ -32,11 +32,11 @@ 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.StatusEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
-import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
+import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
+import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
 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;
@@ -48,7 +48,6 @@ 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.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
 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;
@@ -79,7 +78,8 @@ abstract class AbstractAugmentStatementSupport
     public final void onFullDefinitionDeclared(
             final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode) {
         if (!augmentNode.isSupportedByFeatures()) {
-            return;
+            // We need this augment node to be present, but it should not escape to effective world
+            augmentNode.setIsSupportedToBuildEffective(false);
         }
 
         super.onFullDefinitionDeclared(augmentNode);
@@ -88,14 +88,11 @@ abstract class AbstractAugmentStatementSupport
             return;
         }
 
-        final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
-            ModelProcessingPhase.EFFECTIVE_MODEL);
-        final Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement,
-                AugmentEffectiveStatement>> sourceCtxPrereq =
-                    augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
-        final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
-                augmentAction.mutatesEffectiveCtxPath(getSearchRoot(augmentNode),
-                    ChildSchemaNodeNamespace.class, augmentNode.coerceStatementArgument().getPathFromRoot());
+        final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
+        augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
+        final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtxPath(
+            getSearchRoot(augmentNode), SchemaTreeNamespace.class,
+            augmentNode.coerceStatementArgument().getNodeIdentifiers());
 
         augmentAction.apply(new InferenceAction() {
             @Override
@@ -120,7 +117,6 @@ abstract class AbstractAugmentStatementSupport
                 try {
                     copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
                     augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
-                    updateAugmentOrder(augmentSourceCtx);
                 } catch (final SourceException e) {
                     LOG.warn("Failed to add augmentation {} defined at {}",
                         augmentTargetCtx.getStatementSourceReference(),
@@ -128,13 +124,6 @@ abstract class AbstractAugmentStatementSupport
                 }
             }
 
-            private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
-                final Integer prev = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
-                    YangStmtMapping.AUGMENT);
-                final int currentOrder = prev == null ? 1 : prev + 1;
-                augmentSourceCtx.addToNs(StmtOrderingNamespace.class, YangStmtMapping.AUGMENT, currentOrder);
-            }
-
             @Override
             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
                 /*
@@ -142,7 +131,7 @@ abstract class AbstractAugmentStatementSupport
                  */
                 if (YangStmtMapping.USES == augmentNode.coerceParentContext().getPublicDefinition()) {
                     final SchemaNodeIdentifier augmentArg = augmentNode.coerceStatementArgument();
-                    final Optional<StmtContext<?, ?, ?>> targetNode = ChildSchemaNodeNamespace.findNode(
+                    final Optional<StmtContext<?, ?, ?>> targetNode = SchemaTreeNamespace.findNode(
                         getSearchRoot(augmentNode), augmentArg);
                     if (targetNode.isPresent() && StmtContextUtils.isUnknownStatement(targetNode.get())) {
                         augmentNode.setIsSupportedToBuildEffective(false);
@@ -161,13 +150,14 @@ abstract class AbstractAugmentStatementSupport
     @Override
     protected final AugmentStatement createDeclared(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx,
             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
-        return new RegularAugmentStatement(ctx, substatements);
+        return new RegularAugmentStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(),
+            substatements);
     }
 
     @Override
     protected final AugmentStatement createEmptyDeclared(
             final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
-        return new EmptyAugmentStatement(ctx);
+        return new EmptyAugmentStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
     }
 
     @Override
@@ -190,8 +180,8 @@ abstract class AbstractAugmentStatementSupport
                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
                 .toFlags();
 
-        return new AugmentEffectiveStatementImpl(declared, flags, StmtContextUtils.getRootModuleQName(ctx),
-            substatements, ctx.getStatementSourceReference(),
+        return new AugmentEffectiveStatementImpl(declared, ctx.coerceStatementArgument(), flags,
+            StmtContextUtils.getRootModuleQName(ctx), substatements, ctx.getStatementSourceReference(),
             (AugmentationSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null));
     }
 
@@ -213,27 +203,26 @@ abstract class AbstractAugmentStatementSupport
 
     static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
-        final CopyType typeOfCopy = UsesStatement.class.equals(sourceCtx.coerceParentContext().getPublicDefinition()
-                .getDeclaredRepresentationClass()) ? CopyType.ADDED_BY_USES_AUGMENTATION
-                : CopyType.ADDED_BY_AUGMENTATION;
+        final CopyType typeOfCopy = sourceCtx.coerceParentContext().producesDeclared(UsesStatement.class)
+                ? CopyType.ADDED_BY_USES_AUGMENTATION : CopyType.ADDED_BY_AUGMENTATION;
         /*
          * Since Yang 1.1, if an augmentation is made conditional with a
          * "when" statement, it is allowed to add mandatory nodes.
          */
         final boolean skipCheckOfMandatoryNodes = YangVersion.VERSION_1_1.equals(sourceCtx.getRootVersion())
                 && isConditionalAugmentStmt(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());
 
         for (final Mutable<?, ?, ?> originalStmtCtx : declared) {
-            if (originalStmtCtx.isSupportedByFeatures()) {
-                copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
-            }
+            copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes,
+                unsupported || !originalStmtCtx.isSupportedByFeatures());
         }
         for (final Mutable<?, ?, ?> originalStmtCtx : effective) {
-            copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
+            copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes, unsupported);
         }
 
         targetCtx.addEffectiveSubstatements(buffer);
@@ -254,17 +243,23 @@ abstract class AbstractAugmentStatementSupport
     }
 
     private static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
-        return StmtContextUtils.findFirstSubstatement(ctx, WhenStatement.class) != null;
+        return ctx.hasSubstatement(WhenEffectiveStatement.class);
     }
 
     private static void copyStatement(final Mutable<?, ?, ?> original, final StatementContextBase<?, ?, ?> target,
             final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer,
-            final boolean skipCheckOfMandatoryNodes) {
+            final boolean skipCheckOfMandatoryNodes, final boolean unsupported) {
+        // We always copy statements, but if either the source statement or the augmentation which causes it are not
+        // supported to build we also mark the target as such.
         if (needToCopyByAugment(original)) {
             validateNodeCanBeCopiedByAugment(original, target, typeOfCopy, skipCheckOfMandatoryNodes);
 
-            buffer.add(target.childCopyOf(original, typeOfCopy));
-        } else if (isReusedByAugment(original)) {
+            final Mutable<?, ?, ?> copy = target.childCopyOf(original, typeOfCopy);
+            if (unsupported) {
+                copy.setIsSupportedToBuildEffective(false);
+            }
+            buffer.add(copy);
+        } else if (isReusedByAugment(original) && !unsupported) {
             buffer.add(original);
         }
     }
@@ -272,23 +267,15 @@ abstract class AbstractAugmentStatementSupport
     private static void validateNodeCanBeCopiedByAugment(final StmtContext<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy,
             final boolean skipCheckOfMandatoryNodes) {
-
-        if (WhenStatement.class.equals(sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
-            return;
-        }
-
         if (!skipCheckOfMandatoryNodes && typeOfCopy == CopyType.ADDED_BY_AUGMENTATION
                 && requireCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
             checkForMandatoryNodes(sourceCtx);
         }
 
         // Data definition statements must not collide on their namespace
-        if (DataDefinitionStatement.class.isAssignableFrom(
-            sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
+        if (sourceCtx.producesDeclared(DataDefinitionStatement.class)) {
             for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
-                if (DataDefinitionStatement.class.isAssignableFrom(
-                    subStatement.getPublicDefinition().getDeclaredRepresentationClass())) {
-
+                if (subStatement.producesDeclared(DataDefinitionStatement.class)) {
                     InferenceException.throwIf(
                         Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument()),
                         sourceCtx.getStatementSourceReference(),