Move default value checks from TypeUtils to EffectiveStmtUtils 41/65441/7
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 11 Nov 2017 13:48:27 +0000 (14:48 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 13 Nov 2017 13:25:54 +0000 (14:25 +0100)
These methods are used by effective statement implementations, hence
EffectiveStmtUtils is the best place to host them for now.

Change-Id: I5151fec7959ef2243ef5094f332ca807f10ff42b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/EffectiveStmtUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/leaf/LeafEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/leaf_list/LeafListEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/typedef/TypedefEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/TypeUtils.java

index ad5d8f7dd33cefee948cf51fc4639c98588a6a58..9f8787b32e5e9c4be5912a67408653c310b98c89 100644 (file)
@@ -8,14 +8,28 @@
 
 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";
@@ -55,4 +69,89 @@ public final class EffectiveStmtUtils {
 
         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;
+    }
 }
index 0c4261c66937dc3aa75028fee5acb077a9e96bf9..c8d78b6893fc3ddf069d57d63aebe0135efe0d6d 100644 (file)
@@ -29,9 +29,9 @@ import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
 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>
@@ -71,10 +71,10 @@ public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchem
             }
         }
 
-        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;
index 30128f6f1cb02bea5f84d9ae0dc94bc7e3660015..b44ede861128da7a6c9706be3d8c6a3db4b8fb1a 100644 (file)
@@ -34,7 +34,6 @@ import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveData
 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>
@@ -83,10 +82,10 @@ public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataS
         // 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
 
index 5ebc860312995b4f69ed3a67fdee9b4a82ec79c5..89578a31d8a1f53c1a37a59cf81800b4a715a4dc 100644 (file)
@@ -30,9 +30,9 @@ import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
 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;
 
@@ -50,11 +50,11 @@ final class TypedefEffectiveStatementImpl extends AbstractEffectiveSchemaNode<Ty
         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) {
@@ -74,10 +74,10 @@ final class TypedefEffectiveStatementImpl extends AbstractEffectiveSchemaNode<Ty
         }
 
         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();
     }
index b7c2f21ef1335414d9e2ba561fdb734641c1a930..6159b5b3272b3e1af2672d7ac3befb40db0556a1 100644 (file)
@@ -8,21 +8,8 @@
 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.
@@ -56,87 +43,4 @@ public final class TypeUtils {
         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;
-    }
 }