From c49971ebea9f449efb529ae54cd9b8f8a996d111 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 6 May 2015 11:37:58 +0200 Subject: [PATCH] Clarify the correctness of Optional.get() The check of Optional.isPresent() exists in all codepaths, but it is not immediately obvious. Make it more explicit. Change-Id: I73b6931299ee8cd8e40d0331acc6d6e753db8612 Signed-off-by: Robert Varga --- ...ractNodeContainerModificationStrategy.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java index b323e6b397..8a8afb046a 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/AbstractNodeContainerModificationStrategy.java @@ -14,6 +14,7 @@ import java.util.Collection; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer; +import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType; import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException; @@ -181,15 +182,24 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl throw new ModifiedNodeDoesNotExistException(path, String.format("Node %s does not exist. Cannot apply modification to its children.", path)); } - SchemaAwareApplyOperation.checkConflicting(path, current.isPresent(), "Node was deleted by other transaction."); - checkChildPreconditions(path, modification, current); + if (!current.isPresent()) { + throw new ConflictingModificationAppliedException(path, "Node was deleted by other transaction."); + } + + checkChildPreconditions(path, modification, current.get()); } - private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final Optional current) throws DataValidationFailedException { - final TreeNode currentMeta = current.get(); + /** + * Recursively check child preconditions. + * + * @param path current node path + * @param modification current modification + * @param current Current data tree node. + */ + private void checkChildPreconditions(final YangInstanceIdentifier path, final NodeModification modification, final TreeNode current) throws DataValidationFailedException { for (NodeModification childMod : modification.getChildren()) { final YangInstanceIdentifier.PathArgument childId = childMod.getIdentifier(); - final Optional childMeta = currentMeta.getChild(childId); + final Optional childMeta = current.getChild(childId); YangInstanceIdentifier childPath = path.node(childId); resolveChildOperation(childId).checkApplicable(childPath, childMod, childMeta); @@ -199,8 +209,8 @@ abstract class AbstractNodeContainerModificationStrategy extends SchemaAwareAppl @Override protected void checkMergeApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional current) throws DataValidationFailedException { - if(current.isPresent()) { - checkChildPreconditions(path, modification,current); + if (current.isPresent()) { + checkChildPreconditions(path, modification, current.get()); } } -- 2.36.6