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=89f326c5d75fe39cc7ef7f646ccccb1e927c227c;hp=9fa32a68e5741d350747487791649fd59be4ab63;hpb=bcd020ecbeeacc97df1717a238d93bacd87bcbfc;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 9fa32a68e5..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,25 +48,38 @@ 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()) { NormalizedNode data = result.get().getData(); return NormalizedNodeUtils.findNode(modification.getKey(), data, path); } return Optional.absent(); - } private Optional resolveSnapshot( @@ -76,20 +92,21 @@ class MutableDataTree { private Optional resolveSnapshot(final InstanceIdentifier path, final NodeModification modification) { try { + 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 OperationWithModification resolveModificationFor(final InstanceIdentifier path) { @@ -107,15 +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 + "]"; + } }