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.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.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;
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 <AT> statement argument type
+ * @param <DT> declared statement type
+ * @return statement context that was searched for or null if was not found
+ */
public static <AT, DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
- final StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
- return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext,
+ final StmtContext<AT, ?, ?> effectiveSubstatement = findFirstEffectiveSubstatement(stmtContext, declaredType);
+ return effectiveSubstatement != null ? effectiveSubstatement : findFirstDeclaredSubstatement(stmtContext,
declaredType);
}
break;
}
- final Predicate<QName> isFeatureSupported = stmtContext.getFromNamespace(SupportedFeaturesNamespace.class,
+ final Set<QName> 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<QName> isFeatureSupported) {
+ final Set<QName> supportedFeatures) {
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())) {
+ if (((Predicate<Set<QName>>) stmt.getStatementArgument()).test(supportedFeatures)) {
isSupported = true;
} else {
isSupported = false;
}
/**
- * Checks whether statement context is a mandatory node according to RFC6020
- * or not.
+ * 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 node according to RFC6020
+ * @return true if it is a mandatory leaf, choice, anyxml, list or leaf-list
+ * 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;
}
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);
}
}
+ /**
+ * 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.
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<Collection<SchemaNodeIdentifier>, ?, ?> 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<Collection<SchemaNodeIdentifier>, ?, ?> 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());
+ });
+ }
}