import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.opendaylight.yangtools.yang.common.Empty;
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.UsesStatement;
-import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
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.InferenceAction;
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.validation.ValidationBundlesNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
+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;
private final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target;
private final AbstractAugmentStatementSupport statementSupport;
+ private boolean targetUnavailable;
+
AugmentInferenceAction(final AbstractAugmentStatementSupport statementSupport,
final Mutable<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> augmentNode,
final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target) {
@Override
public void apply(final InferenceContext ctx) {
- final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.resolve(ctx);
+ if (targetUnavailable) {
+ // Target node is not available, no further processing and the augment should not leak into effective world
+ augmentNode.setUnsupported();
+ return;
+ }
+
+ final var augmentTargetCtx = target.resolve(ctx);
if (!isSupportedAugmentTarget(augmentTargetCtx)
|| StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
- augmentNode.setIsSupportedToBuildEffective(false);
+ augmentNode.setUnsupported();
return;
}
// 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, Empty.getInstance(), augmentTargetCtx);
+ augmentNode.addToNs(AugmentImplicitHandlingNamespace.INSTANCE, Empty.value(), augmentTargetCtx);
}
- copyFromSourceToTarget((StatementContextBase<?, ?, ?>) augmentNode, augmentTargetCtx);
+ copyFromSourceToTarget(augmentNode, augmentTargetCtx);
augmentTargetCtx.addEffectiveSubstatement(augmentNode.replicaAsChildOf(augmentTargetCtx));
}
* Do not fail, if it is an uses-augment to an unknown node.
*/
if (YangStmtMapping.USES == augmentNode.coerceParentContext().publicDefinition()) {
+ if (!augmentNode.isSupportedToBuildEffective()) {
+ // We are not supported, hence the uses is not effective and we should bail
+ return;
+ }
+
final SchemaNodeIdentifier augmentArg = augmentNode.getArgument();
- final Optional<StmtContext<?, ?, ?>> targetNode = SchemaTreeNamespace.findNode(
+ final Optional<StmtContext<?, ?, ?>> targetNode = ParserNamespaces.findSchemaTreeStatement(
AbstractAugmentStatementSupport.getSearchRoot(augmentNode), augmentArg);
if (targetNode.isPresent() && StmtContextUtils.isUnknownStatement(targetNode.get())) {
- augmentNode.setIsSupportedToBuildEffective(false);
+ augmentNode.setUnsupported();
LOG.warn("Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
augmentArg, augmentNode.sourceReference());
return;
throw new InferenceException(augmentNode, "Augment target '%s' not found", augmentNode.argument());
}
- private void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
- final StatementContextBase<?, ?, ?> targetCtx) {
+ @Override
+ public void prerequisiteUnavailable(final Prerequisite<?> unavail) {
+ if (target.equals(unavail)) {
+ targetUnavailable = true;
+ } else {
+ prerequisiteFailed(List.of(unavail));
+ }
+ }
+
+ private void copyFromSourceToTarget(final StmtContext<?, ?, ?> sourceCtx, final Mutable<?, ?, ?> targetCtx) {
final CopyType typeOfCopy = sourceCtx.coerceParentContext().producesDeclared(UsesStatement.class)
? CopyType.ADDED_BY_USES_AUGMENTATION : CopyType.ADDED_BY_AUGMENTATION;
/*
final boolean skipCheckOfMandatoryNodes = statementSupport.allowsMandatory(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());
+ final var declared = sourceCtx.declaredSubstatements();
+ final var effective = sourceCtx.effectiveSubstatements();
+ final var buffer = new ArrayList<Mutable<?, ?, ?>>(declared.size() + effective.size());
- for (final Mutable<?, ?, ?> originalStmtCtx : declared) {
+ for (var originalStmtCtx : declared) {
copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes,
unsupported || !originalStmtCtx.isSupportedByFeatures());
}
- for (final Mutable<?, ?, ?> originalStmtCtx : effective) {
+ for (var originalStmtCtx : effective) {
copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes, unsupported);
}
targetCtx.addEffectiveSubstatements(buffer);
}
- private static void copyStatement(final Mutable<?, ?, ?> original, final StatementContextBase<?, ?, ?> target,
+ private static void copyStatement(final StmtContext<?, ?, ?> original, final Mutable<?, ?, ?> target,
final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer,
final boolean skipCheckOfMandatoryNodes, final boolean unsupported) {
// We always copy statements, but if either the source statement or the augmentation which causes it are not
final Mutable<?, ?, ?> copy = target.childCopyOf(original, typeOfCopy);
if (unsupported) {
- copy.setIsSupportedToBuildEffective(false);
+ copy.setUnsupported();
}
buffer.add(copy);
} else if (!unsupported && original.publicDefinition() == YangStmtMapping.TYPEDEF) {
}
private static void validateNodeCanBeCopiedByAugment(final StmtContext<?, ?, ?> sourceCtx,
- final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy,
- final boolean skipCheckOfMandatoryNodes) {
+ final Mutable<?, ?, ?> targetCtx, final CopyType typeOfCopy, final boolean skipCheckOfMandatoryNodes) {
if (!skipCheckOfMandatoryNodes && typeOfCopy == CopyType.ADDED_BY_AUGMENTATION
&& requireCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
checkForMandatoryNodes(sourceCtx);
// Data definition statements must not collide on their namespace
if (sourceCtx.producesDeclared(DataDefinitionStatement.class)) {
- for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
+ for (StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
if (subStatement.producesDeclared(DataDefinitionStatement.class)) {
InferenceException.throwIf(Objects.equals(sourceCtx.argument(), subStatement.argument()), sourceCtx,
"An augment cannot add node named '%s' because this name is already used in target",
* statement, therefore return false and skip mandatory nodes validation
*/
final Object arg = sourceCtx.argument();
- if (!(arg instanceof QName)) {
+ if (!(arg instanceof QName sourceStmtQName)) {
return false;
}
- final QName sourceStmtQName = (QName) arg;
-
// RootStatementContext, for example
final Mutable<?, ?, ?> root = targetCtx.getRoot();
do {
* ignored as disallowed augment target.
*/
final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(
- ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
+ ValidationBundles.NAMESPACE, ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
// if no allowed target is returned we consider all targets allowed
return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()