*/
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.augment;
+import static com.google.common.base.Verify.verify;
+
import com.google.common.base.Verify;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import java.util.Objects;
import java.util.Optional;
-import java.util.Set;
import java.util.regex.Pattern;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.YangVersion;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
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.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.spi.meta.AbstractStatementSupport;
+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;
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.AugmentToChoiceNamespace;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-abstract class AbstractAugmentStatementSupport extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement,
- EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
+abstract class AbstractAugmentStatementSupport
+ extends BaseStatementSupport<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> {
private static final Logger LOG = LoggerFactory.getLogger(AbstractAugmentStatementSupport.class);
private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
}
@Override
- public final AugmentStatement createDeclared(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
- return new AugmentStatementImpl(ctx);
- }
-
- @Override
- public final EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
- final StmtContext<SchemaNodeIdentifier, AugmentStatement,
- EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
- return new AugmentEffectiveStatementImpl(ctx);
- }
-
- @Override
- public final void onFullDefinitionDeclared(final Mutable<SchemaNodeIdentifier, AugmentStatement,
- EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
+ 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);
return;
}
- final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
- ModelProcessingPhase.EFFECTIVE_MODEL);
- final Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement,
- EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> 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
augmentNode.setIsSupportedToBuildEffective(false);
return;
}
- /**
- * Marks case short hand in augment
- */
- if (augmentTargetCtx.getPublicDefinition() == YangStmtMapping.CHOICE) {
- augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, Boolean.TRUE);
+
+ // 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, augmentNode, augmentTargetCtx);
}
+ final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
// FIXME: this is a workaround for models which augment a node which is added via an extension
// which we do not handle. This needs to be reworked in terms of unknown schema nodes.
- final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
try {
copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
- updateAugmentOrder(augmentSourceCtx);
} catch (final SourceException e) {
LOG.warn("Failed to add augmentation {} defined at {}",
augmentTargetCtx.getStatementSourceReference(),
}
}
- private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
- Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
- YangStmtMapping.AUGMENT);
- if (currentOrder == null) {
- currentOrder = 1;
- } else {
- currentOrder++;
- }
-
- augmentSourceCtx.addToNs(StmtOrderingNamespace.class, YangStmtMapping.AUGMENT, currentOrder);
- }
-
@Override
public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
/*
*/
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);
});
}
+ @Override
+ protected final AugmentStatement createDeclared(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx,
+ final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+ return new RegularAugmentStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(),
+ substatements);
+ }
+
+ @Override
+ protected final AugmentStatement createEmptyDeclared(
+ final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
+ return new EmptyAugmentStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
+ }
+
+ @Override
+ protected final List<? extends StmtContext<?, ?, ?>> statementsToBuild(
+ final StmtContext<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> ctx,
+ final List<? extends StmtContext<?, ?, ?>> substatements) {
+ final StatementContextBase<?, ?, ?> implicitDef = ctx.getFromNamespace(AugmentImplicitHandlingNamespace.class,
+ ctx);
+ return implicitDef == null ? substatements : Lists.transform(substatements, subCtx -> {
+ verify(subCtx instanceof StatementContextBase);
+ return implicitDef.wrapWithImplicit((StatementContextBase<?, ?, ?>) subCtx);
+ });
+ }
+
+ @Override
+ protected final AugmentEffectiveStatement createEffective(
+ final StmtContext<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> ctx,
+ final AugmentStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+ final int flags = new FlagsBuilder()
+ .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
+ .toFlags();
+
+ return new AugmentEffectiveStatementImpl(declared, ctx.coerceStatementArgument(), flags,
+ StmtContextUtils.getRootModuleQName(ctx), substatements, ctx.getStatementSourceReference(),
+ (AugmentationSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null));
+ }
+
+ @Override
+ protected final AugmentEffectiveStatement createEmptyEffective(
+ final StmtContext<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> ctx,
+ final AugmentStatement declared) {
+ return createEffective(ctx, declared, ImmutableList.of());
+ }
+
private static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
// Augment is in uses - we need to augment instantiated nodes in parent.
final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
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);
* statement, otherwise false
*/
private static boolean isConditionalAugmentStmt(final StmtContext<?, ?, ?> ctx) {
- return ctx.getPublicDefinition() == YangStmtMapping.AUGMENT
- && StmtContextUtils.findFirstSubstatement(ctx, WhenStatement.class) != null;
+ return ctx.getPublicDefinition() == YangStmtMapping.AUGMENT && hasWhenSubstatement(ctx);
+ }
+
+ private static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
+ 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);
}
}
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
- && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
+ && 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(),
sourceCtx.rawStatementArgument());
}
- private static boolean reguiredCheckOfMandatoryNodes(final StmtContext<?, ?, ?> sourceCtx,
+ private static boolean requireCheckOfMandatoryNodes(final StmtContext<?, ?, ?> sourceCtx,
Mutable<?, ?, ?> targetCtx) {
/*
* If the statement argument is not QName, it cannot be mandatory
* statement, therefore return false and skip mandatory nodes validation
*/
- if (!(sourceCtx.getStatementArgument() instanceof QName)) {
+ final Object arg = sourceCtx.getStatementArgument();
+ if (!(arg instanceof QName)) {
return false;
}
- final QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
+ final QName sourceStmtQName = (QName) arg;
// RootStatementContext, for example
final Mutable<?, ?, ?> root = targetCtx.getRoot();
do {
- Verify.verify(targetCtx.getStatementArgument() instanceof QName,
- "Argument of augment target statement must be QName.");
- final QName targetStmtQName = (QName) targetCtx.getStatementArgument();
+ final Object targetArg = targetCtx.getStatementArgument();
+ Verify.verify(targetArg instanceof QName, "Argument of augment target statement must be QName, not %s",
+ targetArg);
+ final QName targetStmtQName = (QName) targetArg;
/*
* If target is from another module, return true and perform mandatory nodes validation
*/
|| StmtContextUtils.isNotMandatoryNodeOfType(targetCtx, YangStmtMapping.LIST)) {
return false;
}
+
+ // This could be an augmentation stacked on top of a previous augmentation from the same module, which is
+ // conditional -- in which case we do not run further checks
+ if (targetCtx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
+ final Optional<? extends StmtContext<?, ?, ?>> optPrevCopy = targetCtx.getPreviousCopyCtx();
+ if (optPrevCopy.isPresent()) {
+ final StmtContext<?, ?, ?> original = optPrevCopy.get();
+ final Object origArg = original.coerceStatementArgument();
+ Verify.verify(origArg instanceof QName, "Unexpected statement argument %s", origArg);
+
+ if (sourceStmtQName.getModule().equals(((QName) origArg).getModule())
+ && hasWhenSubstatement(getParentAugmentation(original))) {
+ return false;
+ }
+ }
+ }
} while ((targetCtx = targetCtx.getParentContext()) != root);
/*
return false;
}
- private static final Set<YangStmtMapping> NOCOPY_DEF_SET = ImmutableSet.of(YangStmtMapping.USES,
+ private static StmtContext<?, ?, ?> getParentAugmentation(final StmtContext<?, ?, ?> child) {
+ StmtContext<?, ?, ?> parent = Verify.verifyNotNull(child.getParentContext(), "Child %s has not parent", child);
+ while (parent.getPublicDefinition() != YangStmtMapping.AUGMENT) {
+ parent = Verify.verifyNotNull(parent.getParentContext(), "Failed to find augmentation parent of %s", child);
+ }
+ return parent;
+ }
+
+ private static final ImmutableSet<YangStmtMapping> NOCOPY_DEF_SET = ImmutableSet.of(YangStmtMapping.USES,
YangStmtMapping.WHEN, YangStmtMapping.DESCRIPTION, YangStmtMapping.REFERENCE, YangStmtMapping.STATUS);
private static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
}
- private static final Set<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(YangStmtMapping.TYPEDEF);
+ private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(YangStmtMapping.TYPEDEF);
private static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());