Add an explicit guard to child modifications 92/19692/3
authorRobert Varga <rovarga@cisco.com>
Wed, 6 May 2015 09:45:46 +0000 (11:45 +0200)
committerTony Tkacik <ttkacik@cisco.com>
Wed, 13 May 2015 09:28:22 +0000 (09:28 +0000)
If we attempt to apply a merge operation to a non-existent child, we may
end up throwing an IllegalStateException stemming from unchecked
Optional.get().

Add an explicit argument check for child node presence to provide better
diagnostics.

Change-Id: I9e319aa994f01b97a14d5499fba6b1c33dad9a63
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/OperationWithModification.java

index 08fbcaa9a6f764ae7f46ce6e0f5c1211ce7ccbd0..9a4c97802a6bd1c11f4cea05b6485e448a9888d7 100644 (file)
@@ -7,8 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
@@ -95,9 +95,12 @@ final class OperationWithModification {
     }
 
     private OperationWithModification forChild(final PathArgument childId) {
-        ModificationApplyOperation childOp = applyOperation.getChild(childId).get();
+        final Optional<ModificationApplyOperation> maybeChildOp = applyOperation.getChild(childId);
+        Preconditions.checkArgument(maybeChildOp.isPresent(), "Attempted to apply operation to non-existent child %s", childId);
+
+        ModificationApplyOperation childOp = maybeChildOp.get();
         ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy());
 
-        return from(childOp,childMod);
+        return from(childOp, childMod);
     }
 }