*
* @return true if any of specified default values is marked with an
* if-feature, otherwise false
- *
- * @throws IllegalStateException
- * if any of specified default values has not been found
*/
public static boolean hasDefaultValueMarkedWithIfFeature(final YangVersion yangVersion,
final TypeEffectiveStatement<?> typeStmt, final Set<String> defaultValues) {
*
* @return true if specified default value is marked with an if-feature,
* otherwise false
- *
- * @throws IllegalStateException
- * if specified default value has not been found
*/
public static boolean hasDefaultValueMarkedWithIfFeature(final YangVersion yangVersion,
final TypeEffectiveStatement<?> typeStmt, final String defaultValue) {
private static boolean isAnyDefaultValueMarkedWithIfFeature(final TypeEffectiveStatement<?> typeStmt,
final Set<String> defaultValues) {
- for (final EffectiveStatement<?, ?> effectiveSubstatement : typeStmt.effectiveSubstatements()) {
+ 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)) {
&& defaultValues.remove(effectiveSubstatement.argument())
&& containsIfFeature(effectiveSubstatement)) {
return true;
- } else if (effectiveSubstatement instanceof TypeEffectiveStatement) {
- return isAnyDefaultValueMarkedWithIfFeature((TypeEffectiveStatement<?>) effectiveSubstatement,
- defaultValues);
+ } else if (effectiveSubstatement instanceof TypeEffectiveStatement && isAnyDefaultValueMarkedWithIfFeature(
+ (TypeEffectiveStatement<?>) effectiveSubstatement, defaultValues)) {
+ return true;
}
}
- Preconditions.checkState(defaultValues.isEmpty(), "Unable to find following default values %s", defaultValues);
return false;
}
}
}
- try {
- 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);
- } catch (final IllegalStateException e) {
- throw new SourceException(ctx.getStatementSourceReference(), e,
- "Unable to find a default value for leaf '%s'", ctx.getStatementArgument());
- }
+ 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);
defaultStr = dflt;
unitsStr = units;
}
defaultValues = defaultValuesBuilder.build();
- try {
- 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);
- } catch (final IllegalStateException e) {
- throw new SourceException(ctx.getStatementSourceReference(), e,
- "Unable to find a default value for leaf-list '%s'", ctx.getStatementArgument());
- }
+ 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);
type = builder.build();
userOrdered = isUserOrdered;
}
}
- try {
- 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);
- } catch (final IllegalStateException e) {
- throw new SourceException(ctx.getStatementSourceReference(), e,
- "Unable to find a default value for typedef '%s'", ctx.getStatementArgument());
- }
+ 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);
typeDefinition = builder.build();
}
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
@Test
public void unsupportedFeatureTest() throws Exception {
try {
- StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-enum.yang", ImmutableSet.of());
+ StmtTestUtils.parseYangSource("/rfc7950/bug6901/invalid-foo-enum.yang");
fail("Test should fail due to invalid Yang 1.1");
} catch (final SomeModifiersUnresolvedException e) {
- assertTrue(e.getCause().getMessage()
- .startsWith("Unable to find a default value for leaf '(foo?revision=1970-01-01)enum-leaf'"));
- assertTrue(e.getCause().getCause().getMessage()
- .startsWith("Unable to find following default values [two]"));
+ assertTrue(
+ e.getCause().getMessage().contains("has default value 'two' marked with an if-feature statement"));
}
}
--- /dev/null
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.stmt;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
+
+public class Bug8831Test {
+ @Test
+ public void test() throws Exception {
+ final SchemaContext context = TestUtils.parseYangSources("/bugs/bug8831/valid");
+ assertNotNull(context);
+ }
+
+ @Test
+ public void invalidModelsTest() throws Exception {
+ try {
+ TestUtils.parseYangSource("/bugs/bug8831/invalid/inv-model.yang");
+ fail("Test should fails due to invalid yang 1.1 model");
+ } catch (final SomeModifiersUnresolvedException e) {
+ assertTrue(
+ e.getCause().getMessage().contains("has default value 'any' marked with an if-feature statement"));
+ }
+ }
+
+ @Test
+ public void invalidModelsTest2() throws Exception {
+ try {
+ TestUtils.parseYangSource("/bugs/bug8831/invalid/inv-model2.yang");
+ fail("Test should fails due to invalid yang 1.1 model");
+ } catch (final SomeModifiersUnresolvedException e) {
+ assertTrue(
+ e.getCause().getMessage().contains("has default value 'any' marked with an if-feature statement"));
+ }
+ }
+}
--- /dev/null
+module inv-model {
+ yang-version 1.1;
+ namespace "http://www.inv-model.com";
+ prefix ex;
+
+ revision 2017-07-10;
+
+ feature my-feature {
+ description "my feature";
+ }
+
+ typedef enum-last {
+ type union {
+ type uint16;
+ type enumeration {
+ enum "any" {
+ if-feature my-feature;
+ }
+ }
+ }
+ }
+
+ container top {
+ leaf enum-last-leaf {
+ type enum-last;
+ default "any";
+ }
+ }
+}
--- /dev/null
+module inv-model2 {
+ yang-version 1.1;
+ namespace "http://www.inv-model2.com";
+ prefix ex;
+
+ revision 2017-07-10;
+
+ feature my-feature {
+ description "my feature";
+ }
+
+ typedef enum-first {
+ type union {
+ type enumeration {
+ enum "any" {
+ if-feature my-feature;
+ }
+ }
+ type uint16;
+ }
+ }
+
+ container top {
+ leaf enum-first-leaf {
+ type enum-first;
+ default "any";
+ }
+ }
+}
--- /dev/null
+module example-model {
+ yang-version 1.1;
+ namespace "http://www.example.com";
+ prefix ex;
+
+ revision 2017-07-10;
+
+ typedef enum-first {
+ type union {
+ type enumeration {
+ enum "any";
+ }
+ type uint16;
+ }
+ }
+
+ typedef enum-last {
+ type union {
+ type uint16;
+ type enumeration {
+ enum "any";
+ }
+ }
+ }
+
+ container top {
+ leaf enum-first-leaf {
+ type enum-first;
+ default "any";
+ }
+ leaf enum-last-leaf {
+ type enum-last;
+ default "any";
+ }
+ }
+}
--- /dev/null
+module example-model2 {
+ yang-version 1.1;
+ namespace "http://www.example2.com";
+ prefix ex2;
+
+ revision 2017-07-10;
+
+ feature my-feature {
+ description "my feature";
+ }
+
+ typedef enum-last {
+ type union {
+ type uint16;
+ type enumeration {
+ enum "any" {
+ if-feature my-feature;
+ }
+ }
+ }
+ }
+
+ container top {
+ leaf enum-last-leaf {
+ type enum-last;
+ default "16";
+ }
+ }
+}