package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
+import com.google.common.annotations.Beta;
+import com.google.common.base.Strings;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.Optional;
+import java.util.Set;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.YangVersion;
import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.MaxElementsEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+@Beta
public final class EffectiveStmtUtils {
// FIXME: this should reside somewhere in max_elements
private static final String UNBOUNDED_STR = "unbounded";
return ElementCountConstraint.forNullable(minElements, maxElements);
}
+
+
+ /**
+ * Checks whether supplied type has any of specified default values marked
+ * with an if-feature. This method creates mutable copy of supplied set of
+ * default values.
+ *
+ * @param yangVersion
+ * yang version
+ * @param typeStmt
+ * type statement which should be checked
+ * @param defaultValues
+ * set of default values which should be checked. The method
+ * creates mutable copy of this set
+ *
+ * @return true if any of specified default values is marked with an
+ * if-feature, otherwise false
+ */
+ public static boolean hasDefaultValueMarkedWithIfFeature(final YangVersion yangVersion,
+ final TypeEffectiveStatement<?> typeStmt, final Set<String> defaultValues) {
+ return !defaultValues.isEmpty() && yangVersion == YangVersion.VERSION_1_1
+ && isRelevantForIfFeatureCheck(typeStmt)
+ && isAnyDefaultValueMarkedWithIfFeature(typeStmt, new HashSet<>(defaultValues));
+ }
+
+ /**
+ * Checks whether supplied type has specified default value marked with an
+ * if-feature. This method creates mutable set of supplied default value.
+ *
+ * @param yangVersion
+ * yang version
+ * @param typeStmt
+ * type statement which should be checked
+ * @param defaultValue
+ * default value to be checked
+ *
+ * @return true if specified default value is marked with an if-feature,
+ * otherwise false
+ */
+ public static boolean hasDefaultValueMarkedWithIfFeature(final YangVersion yangVersion,
+ final TypeEffectiveStatement<?> typeStmt, final String defaultValue) {
+ final HashSet<String> defaultValues = new HashSet<>();
+ defaultValues.add(defaultValue);
+ return !Strings.isNullOrEmpty(defaultValue) && yangVersion == YangVersion.VERSION_1_1
+ && isRelevantForIfFeatureCheck(typeStmt)
+ && isAnyDefaultValueMarkedWithIfFeature(typeStmt, defaultValues);
+ }
+
+ private static boolean isRelevantForIfFeatureCheck(final TypeEffectiveStatement<?> typeStmt) {
+ final TypeDefinition<?> typeDefinition = typeStmt.getTypeDefinition();
+ return typeDefinition instanceof EnumTypeDefinition || typeDefinition instanceof BitsTypeDefinition
+ || typeDefinition instanceof UnionTypeDefinition;
+ }
+
+ private static boolean isAnyDefaultValueMarkedWithIfFeature(final TypeEffectiveStatement<?> typeStmt,
+ final Set<String> defaultValues) {
+ final Iterator<? extends EffectiveStatement<?, ?>> iter = typeStmt.effectiveSubstatements().iterator();
+ while (iter.hasNext() && !defaultValues.isEmpty()) {
+ final EffectiveStatement<?, ?> effectiveSubstatement = iter.next();
+ if (YangStmtMapping.BIT.equals(effectiveSubstatement.statementDefinition())) {
+ final QName bitQName = (QName) effectiveSubstatement.argument();
+ if (defaultValues.remove(bitQName.getLocalName()) && containsIfFeature(effectiveSubstatement)) {
+ return true;
+ }
+ } else if (YangStmtMapping.ENUM.equals(effectiveSubstatement.statementDefinition())
+ && defaultValues.remove(effectiveSubstatement.argument())
+ && containsIfFeature(effectiveSubstatement)) {
+ return true;
+ } else if (effectiveSubstatement instanceof TypeEffectiveStatement && isAnyDefaultValueMarkedWithIfFeature(
+ (TypeEffectiveStatement<?>) effectiveSubstatement, defaultValues)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private static boolean containsIfFeature(final EffectiveStatement<?, ?> effectiveStatement) {
+ for (final EffectiveStatement<?, ?> effectiveSubstatement : effectiveStatement.effectiveSubstatements()) {
+ if (YangStmtMapping.IF_FEATURE.equals(effectiveSubstatement.statementDefinition())) {
+ return true;
+ }
+ }
+ return false;
+ }
}
import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDataSchemaNode;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
// FIXME: hide this class
public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement>
}
}
- SourceException.throwIf(TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
- ctx.getStatementSourceReference(),
- "Leaf '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(),
- dflt);
+ SourceException.throwIf(
+ EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
+ ctx.getStatementSourceReference(),
+ "Leaf '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(), dflt);
defaultStr = dflt;
unitsStr = units;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
// FIXME: hide this class
public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement>
// FIXME: We need to interpret the default value in terms of supplied element type
defaultValues = defaultValuesBuilder.build();
SourceException.throwIf(
- TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
- ctx.getStatementSourceReference(),
- "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
- ctx.getStatementArgument(), defaultValues);
+ EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
+ ctx.getStatementSourceReference(),
+ "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
+ ctx.getStatementArgument(), defaultValues);
// FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
import org.opendaylight.yangtools.yang.model.util.type.DerivedTypeBuilder;
import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes;
import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveSchemaNode;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final TypeEffectiveStatement<?> typeEffectiveStmt = firstSubstatementOfType(TypeEffectiveStatement.class);
final DerivedTypeBuilder<?> builder = DerivedTypes.derivedTypeBuilder(typeEffectiveStmt.getTypeDefinition(),
ctx.getSchemaPath().get());
- String defaultValue = null;
+ String dflt = null;
for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
if (stmt instanceof DefaultEffectiveStatement) {
- defaultValue = ((DefaultEffectiveStatement) stmt).argument();
- builder.setDefaultValue(defaultValue);
+ dflt = ((DefaultEffectiveStatement) stmt).argument();
+ builder.setDefaultValue(dflt);
} else if (stmt instanceof DescriptionEffectiveStatement) {
builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
} else if (stmt instanceof ReferenceEffectiveStatement) {
}
SourceException.throwIf(
- TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeEffectiveStmt, defaultValue),
- ctx.getStatementSourceReference(),
- "Typedef '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(),
- defaultValue);
+ EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeEffectiveStmt, dflt),
+ ctx.getStatementSourceReference(),
+ "Typedef '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(),
+ dflt);
typeDefinition = builder.build();
}
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
import com.google.common.base.Splitter;
-import com.google.common.base.Strings;
import java.math.BigDecimal;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.YangVersion;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
/**
* Utility class for manipulating YANG base and extended types implementation.
return new BigDecimal(num1.toString()).compareTo(new BigDecimal(num2.toString()));
}
- /**
- * Checks whether supplied type has any of specified default values marked
- * with an if-feature. This method creates mutable copy of supplied set of
- * default values.
- *
- * @param yangVersion
- * yang version
- * @param typeStmt
- * type statement which should be checked
- * @param defaultValues
- * set of default values which should be checked. The method
- * creates mutable copy of this set
- *
- * @return true if any of specified default values is marked with an
- * if-feature, otherwise false
- */
- public static boolean hasDefaultValueMarkedWithIfFeature(final YangVersion yangVersion,
- final TypeEffectiveStatement<?> typeStmt, final Set<String> defaultValues) {
- return !defaultValues.isEmpty() && yangVersion == YangVersion.VERSION_1_1
- && isRelevantForIfFeatureCheck(typeStmt)
- && isAnyDefaultValueMarkedWithIfFeature(typeStmt, new HashSet<>(defaultValues));
- }
-
- /**
- * Checks whether supplied type has specified default value marked with an
- * if-feature. This method creates mutable set of supplied default value.
- *
- * @param yangVersion
- * yang version
- * @param typeStmt
- * type statement which should be checked
- * @param defaultValue
- * default value to be checked
- *
- * @return true if specified default value is marked with an if-feature,
- * otherwise false
- */
- public static boolean hasDefaultValueMarkedWithIfFeature(final YangVersion yangVersion,
- final TypeEffectiveStatement<?> typeStmt, final String defaultValue) {
- final HashSet<String> defaultValues = new HashSet<>();
- defaultValues.add(defaultValue);
- return !Strings.isNullOrEmpty(defaultValue) && yangVersion == YangVersion.VERSION_1_1
- && isRelevantForIfFeatureCheck(typeStmt)
- && isAnyDefaultValueMarkedWithIfFeature(typeStmt, defaultValues);
- }
-
- private static boolean isRelevantForIfFeatureCheck(final TypeEffectiveStatement<?> typeStmt) {
- final TypeDefinition<?> typeDefinition = typeStmt.getTypeDefinition();
- return typeDefinition instanceof EnumTypeDefinition || typeDefinition instanceof BitsTypeDefinition
- || typeDefinition instanceof UnionTypeDefinition;
- }
-
- private static boolean isAnyDefaultValueMarkedWithIfFeature(final TypeEffectiveStatement<?> typeStmt,
- final Set<String> defaultValues) {
- final Iterator<? extends EffectiveStatement<?, ?>> iter = typeStmt.effectiveSubstatements().iterator();
- while (iter.hasNext() && !defaultValues.isEmpty()) {
- final EffectiveStatement<?, ?> effectiveSubstatement = iter.next();
- if (YangStmtMapping.BIT.equals(effectiveSubstatement.statementDefinition())) {
- final QName bitQName = (QName) effectiveSubstatement.argument();
- if (defaultValues.remove(bitQName.getLocalName()) && containsIfFeature(effectiveSubstatement)) {
- return true;
- }
- } else if (YangStmtMapping.ENUM.equals(effectiveSubstatement.statementDefinition())
- && defaultValues.remove(effectiveSubstatement.argument())
- && containsIfFeature(effectiveSubstatement)) {
- return true;
- } else if (effectiveSubstatement instanceof TypeEffectiveStatement && isAnyDefaultValueMarkedWithIfFeature(
- (TypeEffectiveStatement<?>) effectiveSubstatement, defaultValues)) {
- return true;
- }
- }
-
- return false;
- }
-
- private static boolean containsIfFeature(final EffectiveStatement<?, ?> effectiveStatement) {
- for (final EffectiveStatement<?, ?> effectiveSubstatement : effectiveStatement.effectiveSubstatements()) {
- if (YangStmtMapping.IF_FEATURE.equals(effectiveSubstatement.statementDefinition())) {
- return true;
- }
- }
- return false;
- }
}