From bfa6797931a092bd126ec61ddc8bc899d2332530 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 27 May 2020 14:23:31 +0200 Subject: [PATCH] Switch SchemaAwareApplyOperation error reporting Switch to reporting IllegalStateException when the target schema node is not as opposed to IllegalArgumentException. This makes it clear that we have entered internal inconsistency rather than the user doing something wrong. Furthermore eliminate the possibility of throwing an unchecked exception when the user has made a wrong reference -- opting for a well-controlled checked exception instead. JIRA: YANGTOOLS-1105 Change-Id: Icd3ddb03a721358d07427e1fddeec8a71e73b933 Signed-off-by: Robert Varga --- .../tree/ChoiceModificationStrategy.java | 14 ++++++++++--- ...DataNodeContainerModificationStrategy.java | 2 +- .../tree/ExcludedDataSchemaNodeException.java | 21 +++++++++++++++++++ .../impl/schema/tree/InMemoryDataTree.java | 6 +++++- .../tree/SchemaAwareApplyOperation.java | 14 ++++++------- .../tree/ModificationMetadataTreeTest.java | 2 +- .../impl/schema/tree/StoreTreeNodesTest.java | 2 +- 7 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ExcludedDataSchemaNodeException.java diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ChoiceModificationStrategy.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ChoiceModificationStrategy.java index 8230ecc0b5..b79371f48d 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ChoiceModificationStrategy.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ChoiceModificationStrategy.java @@ -57,9 +57,17 @@ final class ChoiceModificationStrategy extends Visible { for (final CaseSchemaNode caze : schema.getCases()) { final CaseEnforcer enforcer = CaseEnforcer.forTree(caze, treeConfig); if (enforcer != null) { - for (final Entry e : enforcer.getChildEntries()) { - childBuilder.put(e.getKey(), SchemaAwareApplyOperation.from(e.getValue(), treeConfig)); - enforcerBuilder.put(e.getKey(), enforcer); + for (final Entry entry : enforcer.getChildEntries()) { + final ModificationApplyOperation childOper; + try { + childOper = SchemaAwareApplyOperation.from(entry.getValue(), treeConfig); + } catch (ExcludedDataSchemaNodeException e) { + // This should never happen as enforcer performs filtering + throw new IllegalStateException("Enforcer references out-of-tree child " + entry, e); + } + + childBuilder.put(entry.getKey(), childOper); + enforcerBuilder.put(entry.getKey(), enforcer); } for (final Entry e : enforcer.getAugmentationEntries()) { diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/DataNodeContainerModificationStrategy.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/DataNodeContainerModificationStrategy.java index 00d752b194..4fde21bdd0 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/DataNodeContainerModificationStrategy.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/DataNodeContainerModificationStrategy.java @@ -78,7 +78,7 @@ class DataNodeContainerModificationStrategy extends ModificationApplyOperation { private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareApplyOperation.class); - public static ModificationApplyOperation from(final DataSchemaNode schemaNode, - final DataTreeConfiguration treeConfig) { - if (treeConfig.getTreeType() == TreeType.CONFIGURATION) { - checkArgument(schemaNode.isConfiguration(), "Supplied %s does not belongs to configuration tree.", - schemaNode); + static ModificationApplyOperation from(final DataSchemaNode schemaNode, + final DataTreeConfiguration treeConfig) throws ExcludedDataSchemaNodeException { + if (!belongsToTree(treeConfig.getTreeType(), schemaNode)) { + throw new ExcludedDataSchemaNodeException(schemaNode + " does not belong to configuration tree"); } if (schemaNode instanceof ContainerSchemaNode) { return ContainerModificationStrategy.of((ContainerSchemaNode) schemaNode, treeConfig); @@ -66,11 +65,12 @@ abstract class SchemaAwareApplyOperation extends Modificat return new ValueNodeModificationStrategy<>(AnydataNode.class, (AnydataSchemaNode) schemaNode); } else if (schemaNode instanceof AnyxmlSchemaNode) { return new ValueNodeModificationStrategy<>(AnyxmlNode.class, (AnyxmlSchemaNode) schemaNode); + } else { + throw new IllegalStateException("Unsupported schema " + schemaNode); } - throw new IllegalArgumentException("Not supported schema node type for " + schemaNode.getClass()); } - public static AugmentationModificationStrategy from(final DataNodeContainer resolvedTree, + static AugmentationModificationStrategy from(final DataNodeContainer resolvedTree, final AugmentationTarget augSchemas, final AugmentationIdentifier identifier, final DataTreeConfiguration treeConfig) { for (final AugmentationSchemaNode potential : augSchemas.getAvailableAugmentations()) { diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ModificationMetadataTreeTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ModificationMetadataTreeTest.java index 3021952039..c2910921a7 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ModificationMetadataTreeTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/ModificationMetadataTreeTest.java @@ -91,7 +91,7 @@ public class ModificationMetadataTreeTest extends AbstractTestModelTest { private RootApplyStrategy rootOper; @Before - public void prepare() { + public void prepare() throws ExcludedDataSchemaNodeException { rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT, DataTreeConfiguration.DEFAULT_OPERATIONAL)); } diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/StoreTreeNodesTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/StoreTreeNodesTest.java index 2ee3a2965f..40408073ee 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/StoreTreeNodesTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/StoreTreeNodesTest.java @@ -66,7 +66,7 @@ public class StoreTreeNodesTest extends AbstractTestModelTest { private RootApplyStrategy rootOper; @Before - public void prepare() { + public void prepare() throws ExcludedDataSchemaNodeException { rootOper = RootApplyStrategy.from(SchemaAwareApplyOperation.from(SCHEMA_CONTEXT, DataTreeConfiguration.DEFAULT_OPERATIONAL)); } -- 2.36.6