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=3e5589264ffc19b79c1414eb9f7e30c166196b7c;hb=79a740220a9b90d06446eb094c4bf6795cd314bc;hp=9e3cd702d16f1d45c8343bc95ae79ba02cc4e644;hpb=f5b6a89fbc3ea9021b90d1a603bcb73d1688731f;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 9e3cd702d1..3e5589264f 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,59 +7,392 @@ */ 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 java.util.Collection; +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.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.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.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>() { + private StmtContextUtils() { + throw new UnsupportedOperationException("Utility class"); + } - @Override - public DeclaredStatement apply(StmtContext input) { - return input.buildDeclared(); + @SuppressWarnings("unchecked") + public static > AT firstAttributeOf( + final Iterable> contexts, final Class
declaredType) { + for (final StmtContext ctx : contexts) { + if (producesDeclared(ctx, declaredType)) { + return (AT) ctx.getStatementArgument(); + } } + return null; + } - }; + @SuppressWarnings("unchecked") + public static > AT firstAttributeOf(final StmtContext ctx, + final Class
declaredType) { + return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null; + } - private static final Function, EffectiveStatement> BUILD_EFFECTIVE = new Function, EffectiveStatement>() { + public static > AT firstSubstatementAttributeOf( + final StmtContext ctx, final Class
declaredType) { + final AT firstAttribute = firstAttributeOf(ctx.effectiveSubstatements(), declaredType); + return firstAttribute != null ? firstAttribute : firstAttributeOf(ctx.declaredSubstatements(), declaredType); + } - @Override - public EffectiveStatement apply(StmtContext input) { - return input.buildEffective(); + @SuppressWarnings("unchecked") + public static > StmtContext findFirstDeclaredSubstatement( + final StmtContext stmtContext, final Class
declaredType) { + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { + return (StmtContext) subStmtContext; + } } + return null; + } - }; + @SuppressWarnings("unchecked") + public static > Collection> findAllDeclaredSubstatements( + final StmtContext stmtContext, final Class
declaredType) { + final ImmutableList.Builder> listBuilder = ImmutableList.builder(); + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { + listBuilder.add((StmtContext) subStmtContext); + } + } + return listBuilder.build(); + } - private StmtContextUtils() { - throw new UnsupportedOperationException("Utility class"); + @SuppressWarnings("unchecked") + public static > Collection> findAllEffectiveSubstatements( + final StmtContext stmtContext, final Class
type) { + final ImmutableList.Builder> listBuilder = ImmutableList.builder(); + for (final StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { + if (producesDeclared(subStmtContext, type)) { + listBuilder.add((StmtContext) subStmtContext); + } + } + return listBuilder.build(); } + public static > Collection> findAllSubstatements( + final StmtContext stmtContext, final Class
type) { + final ImmutableList.Builder> listBuilder = ImmutableList.builder(); + listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type)); + listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type)); + return listBuilder.build(); + } @SuppressWarnings("unchecked") - public static final > Function, D> buildDeclared() { - return Function.class.cast(BUILD_DECLARED); + public static > StmtContext findFirstEffectiveSubstatement( + final StmtContext stmtContext, final Class
declaredType) { + for (final StmtContext subStmtContext : stmtContext.effectiveSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { + return (StmtContext) subStmtContext; + } + } + return null; } - @SuppressWarnings("unchecked") - public static final > Function, E> buildEffective() { - return Function.class.cast(BUILD_EFFECTIVE); + public static > StmtContext findFirstSubstatement( + final StmtContext stmtContext, final Class
declaredType) { + final StmtContext declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType); + return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, + declaredType); } - @SuppressWarnings("unchecked") - public static final > AT firstAttributeOf(Iterable> contexts, Class
declaredType) { - for(StmtContext ctx : contexts) { - if(producesDeclared(ctx,declaredType)) { - return (AT) ctx.getStatementArgument(); + @SafeVarargs + public static StmtContext findFirstDeclaredSubstatement(final StmtContext stmtContext, + int startIndex, final Class>... types) { + if (startIndex >= types.length) { + return null; + } + + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, types[startIndex])) { + return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext, + ++startIndex, types); + } + } + return null; + } + + public static
> StmtContext findFirstDeclaredSubstatementOnSublevel( + final StmtContext stmtContext, final Class
declaredType, int sublevel) { + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) { + return subStmtContext; + } + if (sublevel > 1) { + final StmtContext result = findFirstDeclaredSubstatementOnSublevel(subStmtContext, + declaredType, --sublevel); + if (result != null) { + return result; + } + } + } + + return null; + } + + public static
> StmtContext findDeepFirstDeclaredSubstatement( + final StmtContext stmtContext, final Class
declaredType) { + for (final StmtContext subStmtContext : stmtContext.declaredSubstatements()) { + if (producesDeclared(subStmtContext, declaredType)) { + return subStmtContext; + } + + final StmtContext result = findDeepFirstDeclaredSubstatement(subStmtContext, declaredType); + if (result != null) { + return result; } } + return null; } - public static final boolean producesDeclared(StmtContext ctx, Class> type) { + public static boolean producesDeclared(final StmtContext ctx, + final Class> type) { return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass()); } + public static boolean isInExtensionBody(final StmtContext stmtCtx) { + StmtContext current = stmtCtx; + while (!current.getParentContext().isRootContext()) { + current = current.getParentContext(); + if (producesDeclared(current, UnknownStatementImpl.class)) { + return true; + } + } + + return false; + } + + public static boolean isUnknownStatement(final StmtContext stmtCtx) { + return producesDeclared(stmtCtx, UnknownStatementImpl.class); + } + + public static Collection replaceModuleQNameForKey( + final StmtContext, KeyStatement, ?> keyStmtCtx, + final QNameModule newQNameModule) { + + final Builder builder = ImmutableSet.builder(); + boolean replaced = false; + for (final SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) { + final QName qname = arg.getLastComponent(); + if (!newQNameModule.equals(qname)) { + final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class, + QName.create(newQNameModule, qname.getLocalName())); + builder.add(SchemaNodeIdentifier.create(false, newQname)); + replaced = true; + } else { + builder.add(arg); + } + } + + // This makes sure we reuse the collection when a grouping is + // instantiated in the same module + return replaced ? builder.build() : keyStmtCtx.getStatementArgument(); + } + + public static boolean areFeaturesSupported(final StmtContext.Mutable stmtContext) { + switch (stmtContext.getSupportedByFeatures()) { + case SUPPORTED: + return true; + case NOT_SUPPORTED: + return false; + default: + break; + } + + final Predicate isFeatureSupported = stmtContext.getFromNamespace(SupportedFeaturesNamespace.class, + SupportedFeatures.SUPPORTED_FEATURES); + if (IfFeaturePredicates.ALL_FEATURES.equals(isFeatureSupported)) { + stmtContext.setSupportedByFeatures(true); + return true; + } + + final boolean result = checkFeatureSupport(stmtContext, isFeatureSupported); + stmtContext.setSupportedByFeatures(result); + return result; + } + + private static boolean checkFeatureSupport(final StmtContext.Mutable stmtContext, + final Predicate isFeatureSupported) { + boolean isSupported = false; + boolean containsIfFeature = false; + for (final StatementContextBase stmt : stmtContext.declaredSubstatements()) { + if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) { + containsIfFeature = true; + if (isFeatureSupported.test((QName) stmt.getStatementArgument())) { + isSupported = true; + } else { + isSupported = false; + break; + } + } + } + + 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 node according to RFC6020 + * or not. + * + * @param stmtCtx + * statement context + * @return true if it is a mandatory node according to RFC6020 + */ + public static boolean isMandatoryNode(final StatementContextBase stmtCtx) { + return isMandatoryListOrLeafList(stmtCtx) || isMandatoryLeafChoiceOrAnyXML(stmtCtx); + } + + private static boolean isMandatoryLeafChoiceOrAnyXML(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)); + default: + return false; + } + } + + private static boolean isMandatoryListOrLeafList(final StatementContextBase stmtCtx) { + if (!(stmtCtx.getPublicDefinition() instanceof YangStmtMapping)) { + return false; + } + switch ((YangStmtMapping) stmtCtx.getPublicDefinition()) { + case LIST: + case LEAF_LIST: + final Integer minElements = firstSubstatementAttributeOf(stmtCtx, MinElementsStatement.class); + return minElements != null && minElements > 0; + default: + return false; + } + } + + /** + * 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; + } }