*/
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.augment;
-import com.google.common.base.Verify;
-import com.google.common.collect.ImmutableSet;
-import java.util.ArrayList;
-import java.util.Collection;
+import static com.google.common.base.Verify.verify;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+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.common.Empty;
+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.DeclarationReference;
+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.meta.StatementDefinition;
+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.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.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
+import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserConfiguration;
+import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
+import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
+import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
+import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
+import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
+import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
-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.EffectiveStmtCtx.Current;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
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>> {
- private static final Logger LOG = LoggerFactory.getLogger(AbstractAugmentStatementSupport.class);
+abstract class AbstractAugmentStatementSupport
+ extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, AugmentEffectiveStatement> {
private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
- AbstractAugmentStatementSupport() {
- super(YangStmtMapping.AUGMENT);
+ AbstractAugmentStatementSupport(final YangParserConfiguration config) {
+ super(YangStmtMapping.AUGMENT, StatementPolicy.copyDeclared(
+ (copy, current, substatements) ->
+ copy.getArgument().equals(current.getArgument())
+ && copy.moduleName().getModule().equals(current.moduleName().getModule())
+ && Objects.equals(copy.original(), current.original())
+ ), config);
}
@Override
public final SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
SourceException.throwIf(PATH_REL_PATTERN1.matcher(value).matches()
- || PATH_REL_PATTERN2.matcher(value).matches(), ctx.getStatementSourceReference(),
- "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses",
- value);
-
- return ArgumentUtils.nodeIdentifierFromPath(ctx, value);
- }
-
- @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);
+ || PATH_REL_PATTERN2.matcher(value).matches(), ctx,
+ "Augment argument \'%s\' is not valid, it can be only absolute path; or descendant if used in uses", value);
+
+ // As per:
+ // https://tools.ietf.org/html/rfc6020#section-7.15
+ // https://tools.ietf.org/html/rfc7950#section-7.17
+ //
+ // The argument is either Absolute or Descendant based on whether the statement is declared within a 'uses'
+ // statement. The mechanics differs wildly between the two cases, so let's start by ensuring our argument
+ // is in the correct domain.
+ final SchemaNodeIdentifier result = ArgumentUtils.nodeIdentifierFromPath(ctx, value);
+ final StatementDefinition parent = ctx.coerceParentContext().publicDefinition();
+ if (parent == YangStmtMapping.USES) {
+ SourceException.throwIf(result instanceof Absolute, ctx,
+ "Absolute schema node identifier is not allowed when used within a uses statement");
+ } else {
+ SourceException.throwIf(result instanceof Descendant, ctx,
+ "Descendant schema node identifier is not allowed when used outside of a uses statement");
+ }
+ return result;
}
@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.getStatementArgument().getPathFromRoot());
-
- augmentAction.apply(new ModelActionBuilder.InferenceAction() {
- @Override
- public void apply(final ModelActionBuilder.InferenceContext ctx) {
- final StatementContextBase<?, ?, ?> augmentTargetCtx =
- (StatementContextBase<?, ?, ?>) target.resolve(ctx);
- if (!isSupportedAugmentTarget(augmentTargetCtx)
- || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
- augmentNode.setIsSupportedToBuildEffective(false);
- return;
- }
- /**
- * Marks case short hand in augment
- */
- if (augmentTargetCtx.getPublicDefinition() == YangStmtMapping.CHOICE) {
- augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, Boolean.TRUE);
- }
-
- // 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(),
- augmentSourceCtx.getStatementSourceReference(), e);
- }
- }
-
- 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 ModelActionBuilder.Prerequisite<?>> failed) {
- /*
- * Do not fail, if it is an uses-augment to an unknown node.
- */
- if (YangStmtMapping.USES == augmentNode.getParentContext().getPublicDefinition()) {
- final Optional<StmtContext<?, ?, ?>> targetNode = ChildSchemaNodeNamespace.findNode(
- getSearchRoot(augmentNode), augmentNode.getStatementArgument());
- if (targetNode.isPresent() && StmtContextUtils.isUnknownStatement(targetNode.get())) {
- augmentNode.setIsSupportedToBuildEffective(false);
- LOG.warn(
- "Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
- augmentNode.getStatementArgument(), augmentNode.getStatementSourceReference());
- return;
- }
- }
+ 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.getArgument().getNodeIdentifiers());
- throw new InferenceException(augmentNode.getStatementSourceReference(),
- "Augment target '%s' not found", augmentNode.getStatementArgument());
- }
- });
- }
-
- private static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
- final StmtContext<?, ?, ?> parent = augmentContext.getParentContext();
- // Augment is in uses - we need to augment instantiated nodes in parent.
- if (YangStmtMapping.USES == parent.getPublicDefinition()) {
- return parent.getParentContext();
- }
- return parent;
+ augmentAction.apply(new AugmentInferenceAction(this, augmentNode, target));
}
- static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
- final StatementContextBase<?, ?, ?> targetCtx) {
- final CopyType typeOfCopy = UsesStatement.class.equals(sourceCtx.getParentContext().getPublicDefinition()
- .getDeclaredRepresentationClass()) ? 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 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);
- }
- }
- for (final Mutable<?, ?, ?> originalStmtCtx : effective) {
- copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
- }
-
- targetCtx.addEffectiveSubstatements(buffer);
+ @Override
+ protected final AugmentStatement createDeclared(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx,
+ final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+ return DeclaredStatements.createAugment(ctx.getRawArgument(), ctx.getArgument(), substatements);
}
- /**
- * Checks whether supplied statement context is conditional augment
- * statement.
- *
- * @param ctx
- * statement context to be checked
- *
- * @return true if supplied statement context is conditional augment
- * statement, otherwise false
- */
- private static boolean isConditionalAugmentStmt(final StmtContext<?, ?, ?> ctx) {
- return ctx.getPublicDefinition() == YangStmtMapping.AUGMENT
- && StmtContextUtils.findFirstSubstatement(ctx, WhenStatement.class) != null;
+ @Override
+ protected final AugmentStatement attachDeclarationReference(final AugmentStatement stmt,
+ final DeclarationReference reference) {
+ return DeclaredStatementDecorators.decorateAugment(stmt, reference);
}
- private static void copyStatement(final Mutable<?, ?, ?> original, final StatementContextBase<?, ?, ?> target,
- final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer,
- final boolean skipCheckOfMandatoryNodes) {
- if (needToCopyByAugment(original)) {
- validateNodeCanBeCopiedByAugment(original, target, typeOfCopy, skipCheckOfMandatoryNodes);
-
- buffer.add(target.childCopyOf(original, typeOfCopy));
- } else if (isReusedByAugment(original)) {
- buffer.add(original);
- }
+ @Override
+ protected final List<? extends StmtContext<?, ?, ?>> statementsToBuild(
+ final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
+ final List<? extends StmtContext<?, ?, ?>> substatements) {
+ // Pick up the marker left by onFullDefinitionDeclared() inference action. If it is present we need to pass our
+ // children through target's implicit wrapping.
+ final StatementContextBase<?, ?, ?> implicitDef = stmt.getFromNamespace(AugmentImplicitHandlingNamespace.class,
+ Empty.getInstance());
+ return implicitDef == null ? substatements : Lists.transform(substatements, subCtx -> {
+ verify(subCtx instanceof StatementContextBase);
+ return implicitDef.wrapWithImplicit((StatementContextBase<?, ?, ?>) subCtx);
+ });
}
- 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)) {
- checkForMandatoryNodes(sourceCtx);
- }
-
- // Data definition statements must not collide on their namespace
- if (DataDefinitionStatement.class.isAssignableFrom(
- sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
- for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
- if (DataDefinitionStatement.class.isAssignableFrom(
- subStatement.getPublicDefinition().getDeclaredRepresentationClass())) {
-
- InferenceException.throwIf(
- Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument()),
- sourceCtx.getStatementSourceReference(),
- "An augment cannot add node named '%s' because this name is already used in target",
- sourceCtx.rawStatementArgument());
- }
- }
+ @Override
+ protected final AugmentEffectiveStatement createEffective(
+ final Current<SchemaNodeIdentifier, AugmentStatement> stmt,
+ final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+ final int flags = new FlagsBuilder()
+ .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
+ .toFlags();
+
+ try {
+ return EffectiveStatements.createAugment(stmt.declared(), stmt.getArgument(), flags,
+ stmt.moduleName().getModule(), substatements, stmt.original(AugmentationSchemaNode.class));
+ } catch (SubstatementIndexingException e) {
+ throw new SourceException(e.getMessage(), stmt, e);
}
}
- private static void checkForMandatoryNodes(final StmtContext<?, ?, ?> sourceCtx) {
- if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
- /*
- * We need to iterate over both declared and effective sub-statements,
- * because a mandatory node can be:
- * a) declared in augment body
- * b) added to augment body also via uses of a grouping and
- * such sub-statements are stored in effective sub-statements collection.
- */
- sourceCtx.allSubstatementsStream().forEach(AbstractAugmentStatementSupport::checkForMandatoryNodes);
- }
-
- InferenceException.throwIf(StmtContextUtils.isMandatoryNode(sourceCtx),
- sourceCtx.getStatementSourceReference(),
- "An augment cannot add node '%s' because it is mandatory and in module different than target",
- sourceCtx.rawStatementArgument());
- }
+ abstract boolean allowsMandatory(StmtContext<?, ?, ?> ctx);
- private static boolean reguiredCheckOfMandatoryNodes(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)) {
- return false;
+ static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
+ // Augment is in uses - we need to augment instantiated nodes in parent.
+ final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
+ if (YangStmtMapping.USES == parent.publicDefinition()) {
+ return parent.getParentContext();
}
- final QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
-
- // 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();
- /*
- * If target is from another module, return true and perform mandatory nodes validation
- */
- if (!targetStmtQName.getModule().equals(sourceStmtQName.getModule())) {
- return true;
- }
-
- /*
- * If target or one of the target's ancestors from the same namespace
- * is a presence container
- * or is non-mandatory choice
- * or is non-mandatory list
- * return false and skip mandatory nodes validation, because these nodes
- * are not mandatory node containers according to RFC 6020 section 3.1.
- */
- if (StmtContextUtils.isPresenceContainer(targetCtx)
- || StmtContextUtils.isNotMandatoryNodeOfType(targetCtx, YangStmtMapping.CHOICE)
- || StmtContextUtils.isNotMandatoryNodeOfType(targetCtx, YangStmtMapping.LIST)) {
- return false;
- }
- } while ((targetCtx = targetCtx.getParentContext()) != root);
-
- /*
- * All target node's parents belong to the same module as source node,
- * therefore return false and skip mandatory nodes validation.
- */
- return false;
- }
-
- private static final Set<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 boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
- return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
+ return parent;
}
- static boolean isSupportedAugmentTarget(final StmtContext<?, ?, ?> substatementCtx) {
- /*
- * :TODO Substatement must be allowed augment target type e.g.
- * Container, etc... and must not be for example grouping, identity etc.
- * It is problem in case when more than one substatements have the same
- * QName, for example Grouping and Container are siblings and they have
- * the same QName. We must find the Container and the Grouping must be
- * ignored as disallowed augment target.
- */
- final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(
- ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
-
- // if no allowed target is returned we consider all targets allowed
- return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
- || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
+ static boolean hasWhenSubstatement(final StmtContext<?, ?, ?> ctx) {
+ return ctx.hasSubstatement(WhenEffectiveStatement.class);
}
}