X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fimpl%2Fschema%2Ftree%2FInMemoryDataTreeModification.java;h=02e062add73f39af6cf382f873443fb3b9e6aa4c;hb=a6ea70c09b13489918c387d54cde8f1095721acc;hp=0a137ab3b15eec6f97d7a2ea5dee0c22266c9088;hpb=19c31f13e5040750d30cf9f4211913b2be04b403;p=yangtools.git diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/InMemoryDataTreeModification.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/InMemoryDataTreeModification.java index 0a137ab3b1..02e062add7 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/InMemoryDataTreeModification.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/InMemoryDataTreeModification.java @@ -13,18 +13,15 @@ import java.util.Collection; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import javax.annotation.Nonnull; -import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; 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.NormalizedNodes; import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification; -import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor; import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNodes; import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +37,8 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements private volatile int sealed = 0; - InMemoryDataTreeModification(final InMemoryDataTreeSnapshot snapshot, final RootModificationApplyOperation resolver) { + InMemoryDataTreeModification(final InMemoryDataTreeSnapshot snapshot, + final RootModificationApplyOperation resolver) { this.snapshot = Preconditions.checkNotNull(snapshot); this.strategyTree = Preconditions.checkNotNull(resolver).snapshot(); this.rootNode = ModifiedNode.createUnmodified(snapshot.getRootNode(), strategyTree.getChildPolicy()); @@ -102,11 +100,12 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements if (result.isPresent()) { final NormalizedNode data = result.get().getData(); return NormalizedNodes.findNode(key, data, path); - } else { - return Optional.absent(); } + + return Optional.absent(); } + @SuppressWarnings("checkstyle:illegalCatch") private Optional resolveSnapshot(final YangInstanceIdentifier path, final ModifiedNode modification) { final Optional potentialSnapshot = modification.getSnapshot(); if (potentialSnapshot != null) { @@ -131,7 +130,7 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements LOG.trace("Resolving modification apply strategy for {}", path); upgradeIfPossible(); - return StoreTreeNodes.findNodeChecked(strategyTree, path); + return StoreTreeNodes.findNodeChecked(strategyTree, path); } private OperationWithModification resolveModificationFor(final YangInstanceIdentifier path) { @@ -150,15 +149,15 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements ModificationApplyOperation operation = strategyTree; ModifiedNode modification = rootNode; - int i = 1; - for(final PathArgument pathArg : path.getPathArguments()) { + int depth = 1; + for (final PathArgument pathArg : path.getPathArguments()) { final Optional potential = operation.getChild(pathArg); if (!potential.isPresent()) { throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", - path.getAncestor(i))); + path.getAncestor(depth))); } operation = potential.get(); - ++i; + ++depth; modification = modification.modifyChild(pathArg, operation, version); } @@ -176,7 +175,7 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements } @Override - public DataTreeModification newModification() { + public InMemoryDataTreeModification newModification() { Preconditions.checkState(sealed == 1, "Attempted to chain on an unsealed modification"); if (rootNode.getOperation() == LogicalOperation.NONE) { @@ -190,9 +189,11 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements */ final TreeNode originalSnapshotRoot = snapshot.getRootNode(); final Optional tempRoot = strategyTree.apply(rootNode, Optional.of(originalSnapshotRoot), version); - Preconditions.checkState(tempRoot.isPresent(), "Data tree root is not present, possibly removed by previous modification"); + Preconditions.checkState(tempRoot.isPresent(), + "Data tree root is not present, possibly removed by previous modification"); - final InMemoryDataTreeSnapshot tempTree = new InMemoryDataTreeSnapshot(snapshot.getSchemaContext(), tempRoot.get(), strategyTree); + final InMemoryDataTreeSnapshot tempTree = new InMemoryDataTreeSnapshot(snapshot.getSchemaContext(), + tempRoot.get(), strategyTree); return tempTree.newModification(); } @@ -217,28 +218,28 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements private static void applyNode(final DataTreeModificationCursor cursor, final ModifiedNode node) { switch (node.getOperation()) { - case NONE: - break; - case DELETE: - cursor.delete(node.getIdentifier()); - break; - case MERGE: - cursor.merge(node.getIdentifier(), node.getWrittenValue()); - applyChildren(cursor, node); - break; - case TOUCH: - // TODO: we could improve efficiency of cursor use if we could understand - // nested TOUCH operations. One way of achieving that would be a proxy - // cursor, which would keep track of consecutive enter and exit calls - // and coalesce them. - applyChildren(cursor, node); - break; - case WRITE: - cursor.write(node.getIdentifier(), node.getWrittenValue()); - applyChildren(cursor, node); - break; - default: - throw new IllegalArgumentException("Unhandled node operation " + node.getOperation()); + case NONE: + break; + case DELETE: + cursor.delete(node.getIdentifier()); + break; + case MERGE: + cursor.merge(node.getIdentifier(), node.getWrittenValue()); + applyChildren(cursor, node); + break; + case TOUCH: + // TODO: we could improve efficiency of cursor use if we could understand + // nested TOUCH operations. One way of achieving that would be a proxy + // cursor, which would keep track of consecutive enter and exit calls + // and coalesce them. + applyChildren(cursor, node); + break; + case WRITE: + cursor.write(node.getIdentifier(), node.getWrittenValue()); + applyChildren(cursor, node); + break; + default: + throw new IllegalArgumentException("Unhandled node operation " + node.getOperation()); } } @@ -254,15 +255,18 @@ final class InMemoryDataTreeModification extends AbstractCursorAware implements "Instance identifier references %s but data identifier is %s", arg, data.getIdentifier()); } - private static void checkIdentifierReferencesData(final YangInstanceIdentifier path, final NormalizedNode data) { + private void checkIdentifierReferencesData(final YangInstanceIdentifier path, + final NormalizedNode data) { + final PathArgument arg; + if (!path.isEmpty()) { - final PathArgument lastArg = path.getLastPathArgument(); - Preconditions.checkArgument(lastArg != null, "Instance identifier %s has invalid null path argument", path); - checkIdentifierReferencesData(lastArg, data); + arg = path.getLastPathArgument(); + Preconditions.checkArgument(arg != null, "Instance identifier %s has invalid null path argument", path); } else { - final QName type = data.getNodeType(); - Preconditions.checkArgument(SchemaContext.NAME.equals(type), "Incorrect name %s of root node", type); + arg = rootNode.getIdentifier(); } + + checkIdentifierReferencesData(arg, data); } @Override