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=4e2d84e13f99dfca366f1a5ae85eb47a0f099bf1;hb=360963842964afa8f27ff04bdaba32cdc47c0e77;hp=4abee0dfb63e53b85bbdb1e064d73959fe390869;hpb=8ff4d271df0d1e2f7d00cffee6958427dc1d4da5;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 4abee0dfb6..4e2d84e13f 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 @@ -9,17 +9,17 @@ package org.opendaylight.yangtools.yang.data.impl.schema.tree; import com.google.common.base.Optional; import com.google.common.base.Preconditions; +import com.google.common.collect.Iterables; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; 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.NormalizedNodeContainer; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; -import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType; +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.data.impl.schema.NormalizedNodeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,7 +38,7 @@ final class InMemoryDataTreeModification implements DataTreeModification { InMemoryDataTreeModification(final InMemoryDataTreeSnapshot snapshot, final RootModificationApplyOperation resolver) { this.snapshot = Preconditions.checkNotNull(snapshot); this.strategyTree = Preconditions.checkNotNull(resolver).snapshot(); - this.rootNode = ModifiedNode.createUnmodified(snapshot.getRootNode(), false); + this.rootNode = ModifiedNode.createUnmodified(snapshot.getRootNode(), strategyTree.getChildPolicy()); /* * We could allocate version beforehand, since Version contract @@ -71,19 +71,7 @@ final class InMemoryDataTreeModification implements DataTreeModification { public void merge(final YangInstanceIdentifier path, final NormalizedNode data) { checkSealed(); - mergeImpl(resolveModificationFor(path),data); - } - - private void mergeImpl(final OperationWithModification op,final NormalizedNode data) { - if (data instanceof NormalizedNodeContainer) { - @SuppressWarnings({ "rawtypes", "unchecked" }) - NormalizedNodeContainer> dataContainer = (NormalizedNodeContainer) data; - for(NormalizedNode child : dataContainer.getValue()) { - PathArgument childId = child.getIdentifier(); - mergeImpl(op.forChild(childId), child); - } - } - op.merge(data); + resolveModificationFor(path).merge(data); } @Override @@ -100,24 +88,23 @@ final class InMemoryDataTreeModification implements DataTreeModification { * the requested path which has been modified. If no such node exists, * we use the node itself. */ - final Entry entry = TreeNodeUtils.findClosestsOrFirstMatch(rootNode, path, ModifiedNode.IS_TERMINAL_PREDICATE); + final Entry entry = StoreTreeNodes.findClosestsOrFirstMatch(rootNode, path, ModifiedNode.IS_TERMINAL_PREDICATE); final YangInstanceIdentifier key = entry.getKey(); final ModifiedNode mod = entry.getValue(); final Optional result = resolveSnapshot(key, mod); if (result.isPresent()) { NormalizedNode data = result.get().getData(); - return NormalizedNodeUtils.findNode(key, data, path); + return NormalizedNodes.findNode(key, data, path); } else { return Optional.absent(); } } - private Optional resolveSnapshot(final YangInstanceIdentifier path, - final ModifiedNode modification) { - final Optional> potentialSnapshot = modification.getSnapshotCache(); - if (potentialSnapshot.isPresent()) { - return potentialSnapshot.get(); + private Optional resolveSnapshot(final YangInstanceIdentifier path, final ModifiedNode modification) { + final Optional potentialSnapshot = modification.getSnapshot(); + if (potentialSnapshot != null) { + return potentialSnapshot; } try { @@ -129,30 +116,48 @@ final class InMemoryDataTreeModification implements DataTreeModification { } } - private ModificationApplyOperation resolveModificationStrategy(final YangInstanceIdentifier path) { - LOG.trace("Resolving modification apply strategy for {}", path); - if (rootNode.getType() == ModificationType.UNMODIFIED) { + private void upgradeIfPossible() { + if (rootNode.getOperation() == LogicalOperation.NONE) { strategyTree.upgradeIfPossible(); } + } - return TreeNodeUtils.findNodeChecked(strategyTree, path); + private ModificationApplyOperation resolveModificationStrategy(final YangInstanceIdentifier path) { + LOG.trace("Resolving modification apply strategy for {}", path); + + upgradeIfPossible(); + return StoreTreeNodes.findNodeChecked(strategyTree, path); } private OperationWithModification resolveModificationFor(final YangInstanceIdentifier path) { - // We ensure strategy is present. - final ModificationApplyOperation operation = resolveModificationStrategy(path); - - final boolean isOrdered; - if (operation instanceof SchemaAwareApplyOperation) { - isOrdered = ((SchemaAwareApplyOperation) operation).isOrdered(); - } else { - isOrdered = true; - } + upgradeIfPossible(); + /* + * Walk the strategy and modification trees in-sync, creating modification nodes as needed. + * + * If the user has provided wrong input, we may end up with a bunch of TOUCH nodes present + * ending with an empty one, as we will throw the exception below. This fact could end up + * being a problem, as we'd have bunch of phantom operations. + * + * That is fine, as we will prune any empty TOUCH nodes in the last phase of the ready + * process. + */ + ModificationApplyOperation operation = strategyTree; ModifiedNode modification = rootNode; - for (PathArgument pathArg : path.getPathArguments()) { - modification = modification.modifyChild(pathArg, isOrdered); + + int i = 1; + for(PathArgument pathArg : path.getPathArguments()) { + Optional potential = operation.getChild(pathArg); + if (!potential.isPresent()) { + throw new IllegalArgumentException(String.format("Child %s is not present in tree.", + Iterables.toString(Iterables.limit(path.getPathArguments(), i)))); + } + operation = potential.get(); + ++i; + + modification = modification.modifyChild(pathArg, operation.getChildPolicy()); } + return OperationWithModification.from(operation, modification); } @@ -177,7 +182,7 @@ final class InMemoryDataTreeModification implements DataTreeModification { public DataTreeModification newModification() { Preconditions.checkState(sealed == 1, "Attempted to chain on an unsealed modification"); - if (rootNode.getType() == ModificationType.UNMODIFIED) { + if (rootNode.getOperation() == LogicalOperation.NONE) { // Simple fast case: just use the underlying modification return snapshot.newModification(); }