X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fspi%2Fmeta%2FStmtContextUtils.java;h=7725c743664aa288ae639baa65a624397ef5365a;hb=7e845da39aa0101df4338422fdd30724ec280b9c;hp=35b1d20dee7a1337047b71b64052d7f0d00de780;hpb=8400c0b9bc4d236839a30488f3e7c244f079053b;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java index 35b1d20dee..7725c74366 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java @@ -7,57 +7,41 @@ */ package org.opendaylight.yangtools.yang.parser.spi.meta; -import com.google.common.base.Function; +import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; +import com.google.common.collect.Iterables; import java.util.Collection; +import java.util.Set; import java.util.function.Predicate; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; -import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; +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.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.MinElementsStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.PresenceStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; -import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates; +import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace; import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace.SupportedFeatures; +import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext; import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl; public final class StmtContextUtils { public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults(); - private static final Function, DeclaredStatement> BUILD_DECLARED = new Function, DeclaredStatement>() { - @Override - public DeclaredStatement apply(final StmtContext input) { - return input.buildDeclared(); - } - }; - - private static final Function, EffectiveStatement> BUILD_EFFECTIVE = new Function, EffectiveStatement>() { - @Override - public EffectiveStatement apply(final StmtContext input) { - return input.buildEffective(); - } - }; - private StmtContextUtils() { throw new UnsupportedOperationException("Utility class"); } - @SuppressWarnings("unchecked") - public static > Function, D> buildDeclared() { - return Function.class.cast(BUILD_DECLARED); - } - - @SuppressWarnings("unchecked") - public static > Function, E> buildEffective() { - return Function.class.cast(BUILD_EFFECTIVE); - } - @SuppressWarnings("unchecked") public static > AT firstAttributeOf( final Iterable> contexts, final Class
declaredType) { @@ -135,10 +119,22 @@ public final class StmtContextUtils { return null; } + /** + * Searches for the first substatement of the specified type in the specified statement context. + * First, it tries to find the substatement in the effective substatements of the statement context. + * If it was not found, then it proceeds to search in the declared substatements. If it still was not found, + * the method returns null. + * + * @param stmtContext statement context to search in + * @param declaredType substatement type to search for + * @param statement argument type + * @param
declared statement type + * @return statement context that was searched for or null if was not found + */ public static > StmtContext findFirstSubstatement( final StmtContext stmtContext, final Class
declaredType) { - final StmtContext declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType); - return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, + final StmtContext effectiveSubstatement = findFirstEffectiveSubstatement(stmtContext, declaredType); + return effectiveSubstatement != null ? effectiveSubstatement : findFirstDeclaredSubstatement(stmtContext, declaredType); } @@ -246,28 +242,30 @@ public final class StmtContextUtils { break; } - final Predicate isFeatureSupported = stmtContext.getFromNamespace(SupportedFeaturesNamespace.class, + final Set supportedFeatures = stmtContext.getFromNamespace(SupportedFeaturesNamespace.class, SupportedFeatures.SUPPORTED_FEATURES); - if (IfFeaturePredicates.ALL_FEATURES.equals(isFeatureSupported)) { + /* + * If set of supported features has not been provided, all features are + * supported by default. + */ + if (supportedFeatures == null) { stmtContext.setSupportedByFeatures(true); return true; } - final boolean result = checkFeatureSupport(stmtContext, isFeatureSupported); + final boolean result = checkFeatureSupport(stmtContext, supportedFeatures); stmtContext.setSupportedByFeatures(result); return result; } private static boolean checkFeatureSupport(final StmtContext.Mutable stmtContext, - final Predicate isFeatureSupported) { - final Collection> substatements = stmtContext.declaredSubstatements(); - + final Set supportedFeatures) { boolean isSupported = false; boolean containsIfFeature = false; - for (final StatementContextBase stmt : substatements) { - if (stmt.getPublicDefinition().equals(Rfc6020Mapping.IF_FEATURE)) { + for (final StatementContextBase stmt : stmtContext.declaredSubstatements()) { + if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) { containsIfFeature = true; - if (isFeatureSupported.test((QName) stmt.getStatementArgument())) { + if (((Predicate>) stmt.getStatementArgument()).test(supportedFeatures)) { isSupported = true; } else { isSupported = false; @@ -278,4 +276,209 @@ public final class StmtContextUtils { return !containsIfFeature || isSupported; } + + /** + * Checks whether statement context is a presence container or not. + * + * @param stmtCtx + * statement context + * @return true if it is a presence container + */ + public static boolean isPresenceContainer(final StatementContextBase stmtCtx) { + return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && containsPresenceSubStmt(stmtCtx); + } + + /** + * Checks whether statement context is a non-presence container or not. + * + * @param stmtCtx + * statement context + * @return true if it is a non-presence container + */ + public static boolean isNonPresenceContainer(final StatementContextBase stmtCtx) { + return stmtCtx.getPublicDefinition() == YangStmtMapping.CONTAINER && !containsPresenceSubStmt(stmtCtx); + } + + private static boolean containsPresenceSubStmt(final StatementContextBase stmtCtx) { + return findFirstSubstatement(stmtCtx, PresenceStatement.class) != null; + } + + /** + * Checks whether statement context is a mandatory leaf, choice, anyxml, + * list or leaf-list according to RFC6020 or not. + * + * @param stmtCtx + * statement context + * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list + * according to RFC6020. + */ + public static boolean isMandatoryNode(final StatementContextBase stmtCtx) { + if (!(stmtCtx.getPublicDefinition() instanceof YangStmtMapping)) { + return false; + } + switch ((YangStmtMapping) stmtCtx.getPublicDefinition()) { + case LEAF: + case CHOICE: + case ANYXML: + return Boolean.TRUE.equals(firstSubstatementAttributeOf(stmtCtx, MandatoryStatement.class)); + case LIST: + case LEAF_LIST: + final Integer minElements = firstSubstatementAttributeOf(stmtCtx, MinElementsStatement.class); + return minElements != null && minElements > 0; + default: + return false; + } + } + + /** + * Checks whether a statement context is a statement of supplied statement + * definition and whether it is not mandatory leaf, choice, anyxml, list or + * leaf-list according to RFC6020. + * + * @param stmtCtx + * statement context + * @param stmtDef + * statement definition + * @return true if supplied statement context is a statement of supplied + * statement definition and if it is not mandatory leaf, choice, + * anyxml, list or leaf-list according to RFC6020 + */ + public static boolean isNotMandatoryNodeOfType(final StatementContextBase stmtCtx, + final StatementDefinition stmtDef) { + return stmtCtx.getPublicDefinition().equals(stmtDef) && !isMandatoryNode(stmtCtx); + } + + /** + * Checks whether at least one ancestor of a StatementContext matches one + * from collection of statement definitions. + * + * @param ctx + * StatementContext 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 StmtContext ctx, + final Collection ancestorTypes) { + Preconditions.checkNotNull(ctx); + Preconditions.checkNotNull(ancestorTypes); + StmtContext current = ctx.getParentContext(); + while (current != null) { + if (ancestorTypes.contains(current.getPublicDefinition())) { + return true; + } + current = current.getParentContext(); + } + return false; + } + + /** + * Checks whether all of StmtContext's ancestors of specified type have a child of specified type + * + * @param ctx StmtContext 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 + */ + public static > boolean hasAncestorOfTypeWithChildOfType(final StmtContext ctx, + final StatementDefinition ancestorType, final StatementDefinition ancestorChildType) { + Preconditions.checkNotNull(ctx); + Preconditions.checkNotNull(ancestorType); + Preconditions.checkNotNull(ancestorChildType); + StmtContext current = ctx.getParentContext(); + while (!(current instanceof RootStatementContext)) { + if (ancestorType.equals(current.getPublicDefinition())) { + @SuppressWarnings("unchecked") + final Class
ancestorChildTypeClass = (Class
) ancestorChildType.getDeclaredRepresentationClass(); + if (findFirstSubstatement(current, ancestorChildTypeClass) == null) { + return false; + } + } + current = current.getParentContext(); + } + + return true; + } + + /** + * Checks whether the parent of StmtContext is of specified type + * + * @param ctx + * StmtContext 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 StmtContext ctx, final StatementDefinition parentType) { + Preconditions.checkNotNull(ctx); + Preconditions.checkNotNull(parentType); + final StmtContext parentContext = ctx.getParentContext(); + return parentContext != null ? parentType.equals(parentContext.getPublicDefinition()) : false; + } + + /** + * Validates the specified statement context with regards to if-feature and when statement on list keys. + * The context can either be a leaf which is defined directly in the substatements of a keyed list or a uses + * statement defined in a keyed list (a uses statement may add leaves into the list). + * + * If one of the list keys contains an if-feature or a when statement in YANG 1.1 model, an exception is thrown. + * + * @param ctx statement context to be validated + */ + public static void validateIfFeatureAndWhenOnListKeys(final StmtContext ctx) { + Preconditions.checkNotNull(ctx); + + if (!isRelevantForIfFeatureAndWhenOnListKeysCheck(ctx)) { + return; + } + + final StmtContext listStmtCtx = ctx.getParentContext(); + final StmtContext, ?, ?> keyStmtCtx = + StmtContextUtils.findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class); + + if (YangStmtMapping.LEAF.equals(ctx.getPublicDefinition())) { + if (isListKey(ctx, keyStmtCtx)) { + disallowIfFeatureAndWhenOnListKeys(ctx); + } + } else if (YangStmtMapping.USES.equals(ctx.getPublicDefinition())) { + StmtContextUtils.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()) + && StmtContextUtils.hasParentOfType(ctx, YangStmtMapping.LIST) + && StmtContextUtils.findFirstDeclaredSubstatement(ctx.getParentContext(), KeyStatement.class) != null; + } + + private static boolean isListKey(final StmtContext leafStmtCtx, + final StmtContext, ?, ?> keyStmtCtx) { + for (final SchemaNodeIdentifier keyIdentifier : keyStmtCtx.getStatementArgument()) { + if (leafStmtCtx.getStatementArgument().equals(keyIdentifier.getLastComponent())) { + return true; + } + } + + return false; + } + + private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext leafStmtCtx) { + Iterables.concat(leafStmtCtx.declaredSubstatements(), leafStmtCtx.effectiveSubstatements()).forEach( + leafSubstmtCtx -> { + final StatementDefinition statementDef = leafSubstmtCtx.getPublicDefinition(); + SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef) + || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.getStatementSourceReference(), + "%s statement is not allowed in %s leaf statement which is specified as a list key.", + statementDef.getStatementName(), leafStmtCtx.getStatementArgument()); + }); + } }