import static java.util.Objects.requireNonNull;
import com.google.common.base.Strings;
+import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableList;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import org.opendaylight.yangtools.yang.common.YangVersion;
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.meta.StatementDefinition;
import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
-import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Parent;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<D> declaredType) {
for (final StmtContext<?, ?, ?> ctx : contexts) {
if (ctx.producesDeclared(declaredType)) {
- return (A) ctx.getStatementArgument();
+ return (A) ctx.argument();
}
}
return null;
@SuppressWarnings("unchecked")
public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(final StmtContext<?, ?, ?> ctx,
final Class<D> declaredType) {
- return ctx.producesDeclared(declaredType) ? (A) ctx.getStatementArgument() : null;
+ return ctx.producesDeclared(declaredType) ? (A) ctx.argument() : null;
}
public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
* @param <A> statement argument type
* @param <D> declared statement type
* @return statement context that was searched for or null if was not found
- * @deprecated Use {@link StmtContext#findSubstatementArgument(Class)} instead.
+ * @deprecated Use {@link BoundStmtCtx#findSubstatementArgument(Class)} instead.
*/
@Deprecated(forRemoval = true)
public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstSubstatement(
* if supplied statement context is null
*/
public static boolean isUnknownStatement(final StmtContext<?, ?, ?> stmtCtx) {
- return UnknownStatement.class
- .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass());
+ return UnknownStatement.class.isAssignableFrom(stmtCtx.publicDefinition().getDeclaredRepresentationClass());
}
/**
boolean isSupported = false;
boolean containsIfFeature = false;
for (final StmtContext<?, ?, ?> stmt : stmtContext.declaredSubstatements()) {
- if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) {
+ if (YangStmtMapping.IF_FEATURE.equals(stmt.publicDefinition())) {
containsIfFeature = true;
@SuppressWarnings("unchecked")
- final Predicate<Set<QName>> argument = (Predicate<Set<QName>>) stmt.coerceStatementArgument();
+ final Predicate<Set<QName>> argument = (Predicate<Set<QName>>) stmt.getArgument();
if (argument.test(supportedFeatures)) {
isSupported = true;
} else {
* @return true if it is a presence container
*/
public static boolean isPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
- return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx);
+ return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx);
}
/**
* @return true if it is a non-presence container
*/
public static boolean isNonPresenceContainer(final StmtContext<?, ?, ?> stmtCtx) {
- return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx);
+ return stmtCtx.publicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx);
}
private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
* according to RFC6020.
*/
public static boolean isMandatoryNode(final StmtContext<?, ?, ?> stmtCtx) {
- if (!(stmtCtx.getPublicDefinition() instanceof YangStmtMapping)) {
+ if (!(stmtCtx.publicDefinition() instanceof YangStmtMapping)) {
return false;
}
- switch ((YangStmtMapping) stmtCtx.getPublicDefinition()) {
+ switch ((YangStmtMapping) stmtCtx.publicDefinition()) {
case LEAF:
case CHOICE:
case ANYXML:
*/
public static boolean isNotMandatoryNodeOfType(final StmtContext<?, ?, ?> stmtCtx,
final StatementDefinition stmtDef) {
- return stmtCtx.getPublicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx);
+ return stmtCtx.publicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx);
}
/**
* Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
* definitions.
*
- * @param stmt EffectiveStmtCtx to be checked
+ * @param stmt Statement context to be checked
* @param ancestorTypes collection of statement definitions
* @return true if at least one ancestor of a StatementContext matches one
* from collection of statement definitions, otherwise false.
*/
- public static boolean hasAncestorOfType(final EffectiveStmtCtx stmt,
+ public static boolean hasAncestorOfType(final StmtContext<?, ?, ?> stmt,
final Collection<StatementDefinition> ancestorTypes) {
requireNonNull(ancestorTypes);
- Parent current = stmt.effectiveParent();
+ StmtContext<?, ?, ?> current = stmt.getParentContext();
while (current != null) {
if (ancestorTypes.contains(current.publicDefinition())) {
return true;
}
- current = current.effectiveParent();
+ current = current.getParentContext();
}
return false;
}
/**
- * Checks whether all of StmtContext's ancestors of specified type have a child of specified type.
+ * Check whether all of StmtContext's {@code list} ancestors have a {@code key}.
*
* @param stmt EffectiveStmtCtx to be checked
- * @param ancestorType type of ancestor to search for
- * @param ancestorChildType type of child to search for in the specified ancestor type
- * @return true if all of StmtContext's ancestors of specified type have a child of specified type, otherwise false
+ * @param name Human-friendly statement name
+ * @throws SourceException if there is any keyless list ancestor
*/
- public static <A, D extends DeclaredStatement<A>> boolean hasAncestorOfTypeWithChildOfType(
- final EffectiveStmtCtx.Current<?, ?> stmt, final StatementDefinition ancestorType,
- final StatementDefinition ancestorChildType) {
+ public static void validateNoKeylessListAncestorOf(final Mutable<?, ?, ?> stmt, final String name) {
requireNonNull(stmt);
- requireNonNull(ancestorType);
- final Class<? extends EffectiveStatement<?, ?>> repr = ancestorChildType.getEffectiveRepresentationClass();
- StmtContext<?, ?, ?> current = stmt.caerbannog().getParentContext();
- StmtContext<?, ?, ?> parent = current.getParentContext();
+ // We do not expect this to by typically populated
+ final List<Mutable<?, ?, ?>> incomplete = new ArrayList<>(0);
+
+ Mutable<?, ?, ?> current = stmt.coerceParentContext();
+ Mutable<?, ?, ?> parent = current.getParentContext();
while (parent != null) {
- if (ancestorType.equals(current.publicDefinition()) && !current.hasSubstatement(repr)) {
- return false;
+ if (YangStmtMapping.LIST == current.publicDefinition()
+ && !current.hasSubstatement(KeyEffectiveStatement.class)) {
+ if (ModelProcessingPhase.FULL_DECLARATION.isCompletedBy(current.getCompletedPhase())) {
+ throw new SourceException(stmt, "%s %s is defined within a list that has no key statement", name,
+ stmt.argument());
+ }
+
+ // Ancestor has not completed full declaration yet missing 'key' statement may materialize later
+ incomplete.add(current);
}
current = parent;
parent = current.getParentContext();
}
- return true;
- }
+ // Deal with whatever incomplete ancestors we encountered
+ for (Mutable<?, ?, ?> ancestor : incomplete) {
+ // This check must complete during the ancestor's FULL_DECLARATION phase, i.e. the ancestor must not reach
+ // EFFECTIVE_MODEL until it is done.
+ final ModelActionBuilder action = ancestor.newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
+ action.apply(new InferenceAction() {
+ @Override
+ public void apply(final InferenceContext ctx) {
+ if (!ancestor.hasSubstatement(KeyEffectiveStatement.class)) {
+ throw new SourceException(stmt, "%s %s is defined within a list that has no key statement",
+ name, stmt.argument());
+ }
+ }
- /**
- * Checks whether the parent of EffectiveStmtCtx is of specified type.
- *
- * @param stmt EffectiveStmtCtx to be checked
- * @param parentType type of parent to check
- * @return true if the parent of StmtContext is of specified type, otherwise false
- */
- public static boolean hasParentOfType(final EffectiveStmtCtx.Current<?, ?> stmt,
- final StatementDefinition parentType) {
- return hasParentOfType(stmt.caerbannog(), parentType);
+ @Override
+ public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
+ throw new VerifyException("Should never happen");
+ }
+ });
+ }
}
/**
public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {
requireNonNull(parentType);
final StmtContext<?, ?, ?> parentContext = ctx.getParentContext();
- return parentContext != null && parentType.equals(parentContext.getPublicDefinition());
+ return parentContext != null && parentType.equals(parentContext.publicDefinition());
}
/**
final StmtContext<?, ?, ?> listStmtCtx = ctx.coerceParentContext();
final StmtContext<Set<QName>, ?, ?> keyStmtCtx = findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class);
- if (YangStmtMapping.LEAF.equals(ctx.getPublicDefinition())) {
+ if (YangStmtMapping.LEAF.equals(ctx.publicDefinition())) {
if (isListKey(ctx, keyStmtCtx)) {
disallowIfFeatureAndWhenOnListKeys(ctx);
}
- } else if (YangStmtMapping.USES.equals(ctx.getPublicDefinition())) {
+ } else if (YangStmtMapping.USES.equals(ctx.publicDefinition())) {
findAllEffectiveSubstatements(listStmtCtx, LeafStatement.class).forEach(leafStmtCtx -> {
if (isListKey(leafStmtCtx, keyStmtCtx)) {
disallowIfFeatureAndWhenOnListKeys(leafStmtCtx);
}
private static boolean isRelevantForIfFeatureAndWhenOnListKeysCheck(final StmtContext<?, ?, ?> ctx) {
- return YangVersion.VERSION_1_1.equals(ctx.getRootVersion()) && hasParentOfType(ctx, YangStmtMapping.LIST)
+ return YangVersion.VERSION_1_1.equals(ctx.yangVersion()) && hasParentOfType(ctx, YangStmtMapping.LIST)
&& findFirstDeclaredSubstatement(ctx.coerceParentContext(), KeyStatement.class) != null;
}
private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
final StmtContext<Set<QName>, ?, ?> keyStmtCtx) {
- return keyStmtCtx.coerceStatementArgument().contains(leafStmtCtx.getStatementArgument());
+ return keyStmtCtx.getArgument().contains(leafStmtCtx.argument());
}
private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> leafStmtCtx) {
leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> {
- final StatementDefinition statementDef = leafSubstmtCtx.getPublicDefinition();
+ final StatementDefinition statementDef = leafSubstmtCtx.publicDefinition();
SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef)
- || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.getStatementSourceReference(),
+ || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx,
"%s statement is not allowed in %s leaf statement which is specified as a list key.",
- statementDef.getStatementName(), leafStmtCtx.getStatementArgument());
+ statementDef.getStatementName(), leafStmtCtx.argument());
});
}
public static QName qnameFromArgument(StmtContext<?, ?, ?> ctx, final String value) {
if (Strings.isNullOrEmpty(value)) {
- return ctx.getPublicDefinition().getStatementName();
+ return ctx.publicDefinition().getStatementName();
}
String prefix;
localName = value;
qnameModule = getRootModuleQName(ctx);
}
- if (qnameModule == null && ctx.getCopyHistory().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
+ if (qnameModule == null && ctx.history().getLastOperation() == CopyType.ADDED_BY_AUGMENTATION) {
ctx = ctx.getOriginalCtx().orElse(null);
qnameModule = getModuleQNameByPrefix(ctx, prefix);
}
}
- return internedQName(ctx,
- InferenceException.throwIfNull(qnameModule, ctx.getStatementSourceReference(),
+ return internedQName(ctx, InferenceException.throwIfNull(qnameModule, ctx,
"Cannot resolve QNameModule for '%s'", value), localName);
}
* @throws SourceException if the string is not a valid YANG identifier
*/
public static QName parseIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
- SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
- "Identifier may not be an empty string");
+ SourceException.throwIf(str.isEmpty(), ctx, "Identifier may not be an empty string");
return internedQName(ctx, str);
}
public static QName parseNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String prefix,
final String localName) {
return internedQName(ctx,
- InferenceException.throwIfNull(getModuleQNameByPrefix(ctx, prefix), ctx.getStatementSourceReference(),
+ InferenceException.throwIfNull(getModuleQNameByPrefix(ctx, prefix), ctx,
"Cannot resolve QNameModule for '%s'", prefix),
localName);
}
* @throws SourceException if the string is not a valid YANG node identifier
*/
public static QName parseNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
- SourceException.throwIf(str.isEmpty(), ctx.getStatementSourceReference(),
- "Node identifier may not be an empty string");
+ SourceException.throwIf(str.isEmpty(), ctx, "Node identifier may not be an empty string");
final int colon = str.indexOf(':');
if (colon == -1) {
}
final String prefix = str.substring(0, colon);
- SourceException.throwIf(prefix.isEmpty(), ctx.getStatementSourceReference(),
- "String '%s' has an empty prefix", str);
+ SourceException.throwIf(prefix.isEmpty(), ctx, "String '%s' has an empty prefix", str);
final String localName = str.substring(colon + 1);
- SourceException.throwIf(localName.isEmpty(), ctx.getStatementSourceReference(),
- "String '%s' has an empty identifier", str);
+ SourceException.throwIf(localName.isEmpty(), ctx, "String '%s' has an empty identifier", str);
return parseNodeIdentifier(ctx, prefix, localName);
}
return internedQName(ctx, getRootModuleQName(ctx), localName);
}
- private static QName internedQName(final StmtContext<?, ?, ?> ctx, final QNameModule module,
+ private static QName internedQName(final CommonStmtCtx ctx, final QNameModule module,
final String localName) {
final QName template;
try {
template = QName.create(module, localName);
} catch (IllegalArgumentException e) {
- throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid identifier '%s'", localName);
+ throw new SourceException(ctx, e, "Invalid identifier '%s'", localName);
}
return template.intern();
}
Revision revision = null;
for (final StmtContext<?, ?, ?> subStmt : subStmts) {
if (subStmt.producesDeclared(RevisionStatement.class)) {
- if (revision == null && subStmt.getStatementArgument() != null) {
- revision = (Revision) subStmt.getStatementArgument();
+ if (revision == null && subStmt.argument() != null) {
+ revision = (Revision) subStmt.argument();
} else {
- final Revision subArg = (Revision) subStmt.getStatementArgument();
+ final Revision subArg = (Revision) subStmt.argument();
if (subArg != null && subArg.compareTo(revision) > 0) {
revision = subArg;
}