X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fstore%2Fimpl%2FMutableDataTree.java;h=b711163b465d3ca07914da3d3472347ab854533e;hb=84248dac9ed8aa37e996e39429c8aa8ece473eaf;hp=ddf65b6d275389d98ac86e6a5971685ea6e683e1;hpb=8b3405d23e3018afdc4cb5e99bab442ced26e304;p=controller.git diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/MutableDataTree.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/MutableDataTree.java index ddf65b6d27..b711163b46 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/MutableDataTree.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/MutableDataTree.java @@ -7,10 +7,10 @@ */ package org.opendaylight.controller.md.sal.dom.store.impl; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import java.util.Map.Entry; +import java.util.concurrent.atomic.AtomicBoolean; import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification; import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode; @@ -18,21 +18,24 @@ import org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.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.impl.schema.NormalizedNodeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +/* + * FIXME: the thread safety of concurrent write/delete/read/seal operations + * needs to be evaluated. + */ class MutableDataTree { - - private static final Logger log = LoggerFactory.getLogger(MutableDataTree.class); - - final DataAndMetadataSnapshot snapshot; - final NodeModification rootModification; - final ModificationApplyOperation strategyTree; - - private boolean sealed = false; + private static final Logger LOG = LoggerFactory.getLogger(MutableDataTree.class); + private final AtomicBoolean sealed = new AtomicBoolean(); + private final ModificationApplyOperation strategyTree; + private final DataAndMetadataSnapshot snapshot; + private final NodeModification rootModification; private MutableDataTree(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation strategyTree) { this.snapshot = snapshot; @@ -45,56 +48,75 @@ class MutableDataTree { resolveModificationFor(path).write(value); } + public void merge(final InstanceIdentifier 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); + } + public void delete(final InstanceIdentifier path) { checkSealed(); resolveModificationFor(path).delete(); } public Optional> read(final InstanceIdentifier path) { - Entry modification = TreeNodeUtils.findClosest(rootModification, path); - return getModifiedVersion(path, modification); - } + Entry modification = TreeNodeUtils.findClosestsOrFirstMatch(rootModification, path, NodeModification.IS_TERMINAL_PREDICATE); - private Optional> getModifiedVersion(final InstanceIdentifier path, final Entry modification) { Optional result = resolveSnapshot(modification); - if(result.isPresent()) { + if (result.isPresent()) { NormalizedNode data = result.get().getData(); - return NormalizedNodeUtils.findNode(modification.getKey(),data, path); + return NormalizedNodeUtils.findNode(modification.getKey(), data, path); } return Optional.absent(); - } - private Optional resolveSnapshot(final Entry keyModification) { + private Optional resolveSnapshot( + final Entry keyModification) { InstanceIdentifier path = keyModification.getKey(); NodeModification modification = keyModification.getValue(); - return resolveSnapshot(path,modification); + return resolveSnapshot(path, modification); } - private Optional resolveSnapshot(final InstanceIdentifier path, final NodeModification modification) { + private Optional resolveSnapshot(final InstanceIdentifier path, + final NodeModification modification) { try { - return resolveModificationStrategy(path).apply(modification,modification.getOriginal()); + Optional> potentialSnapshot = modification.getSnapshotCache(); + if(potentialSnapshot.isPresent()) { + return potentialSnapshot.get(); + } + return resolveModificationStrategy(path).apply(modification, modification.getOriginal(), + StoreUtils.increase(snapshot.getMetadataTree().getSubtreeVersion())); } catch (Exception e) { - log.error("Could not create snapshot for {},",e); + LOG.error("Could not create snapshot for {}:{}", path,modification,e); throw e; } } private ModificationApplyOperation resolveModificationStrategy(final InstanceIdentifier path) { - log.trace("Resolving modification apply strategy for {}",path); - Optional strategy = TreeNodeUtils.findNode(strategyTree, path); - checkArgument(strategy.isPresent(),"Provided path %s is not supported by data store. No schema available for it.",path); - return strategy.get(); + LOG.trace("Resolving modification apply strategy for {}", path); + return TreeNodeUtils.findNodeChecked(strategyTree, path); } - private NodeModification resolveModificationFor(final InstanceIdentifier path) { + private OperationWithModification resolveModificationFor(final InstanceIdentifier path) { NodeModification modification = rootModification; // We ensure strategy is present. - resolveModificationStrategy(path); + ModificationApplyOperation operation = resolveModificationStrategy(path); for (PathArgument pathArg : path.getPath()) { modification = modification.modifyChild(pathArg); } - return modification; + return OperationWithModification.from(operation, modification); } public static MutableDataTree from(final DataAndMetadataSnapshot snapshot, final ModificationApplyOperation resolver) { @@ -102,11 +124,21 @@ class MutableDataTree { } public void seal() { - sealed = true; + final boolean success = sealed.compareAndSet(false, true); + Preconditions.checkState(success, "Attempted to seal an already-sealed Data Tree."); rootModification.seal(); } private void checkSealed() { - checkState(!sealed , "Data Tree is sealed. No further modifications allowed."); + checkState(!sealed.get(), "Data Tree is sealed. No further modifications allowed."); + } + + protected NodeModification getRootModification() { + return rootModification; + } + + @Override + public String toString() { + return "MutableDataTree [modification=" + rootModification + "]"; } }