X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FShardDataTree.java;h=64eb6c87dc9bc4cf571058994665042da489d8d9;hb=bb1891e328feee08ccd29c96034e967f1eeccece;hp=e852cfe420416bbfa38049f0938bc954472b8584;hpb=14f8732b144cbe12c97a31555e794938cbe96e62;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java index e852cfe420..64eb6c87dc 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java @@ -7,32 +7,31 @@ */ package org.opendaylight.controller.cluster.datastore; -import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Strings; -import java.net.URI; import java.util.AbstractMap.SimpleEntry; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; import javax.annotation.concurrent.NotThreadSafe; -import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener; import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener; -import org.opendaylight.controller.md.sal.dom.store.impl.DOMImmutableDataChangeEvent; +import org.opendaylight.controller.md.sal.dom.store.impl.DataChangeListenerRegistration; import org.opendaylight.controller.md.sal.dom.store.impl.ResolveDataChangeEventsTask; import org.opendaylight.controller.md.sal.dom.store.impl.tree.ListenerTree; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification; +import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException; import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree; +import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType; import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.slf4j.Logger; @@ -48,28 +47,31 @@ import org.slf4j.LoggerFactory; @NotThreadSafe public class ShardDataTree extends ShardDataTreeTransactionParent { private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class); + private static final YangInstanceIdentifier ROOT_PATH = YangInstanceIdentifier.builder().build(); private static final ShardDataTreeNotificationManager MANAGER = new ShardDataTreeNotificationManager(); private final Map transactionChains = new HashMap<>(); private final ShardDataTreeChangePublisher treeChangePublisher = new ShardDataTreeChangePublisher(); private final ListenerTree listenerTree = ListenerTree.create(); private final TipProducingDataTree dataTree; - private Set validNamespaces; - private ShardDataTreeTransactionFactory transactionFactory = new RecoveryShardDataTreeTransactionFactory(); + private SchemaContext schemaContext; - ShardDataTree(final SchemaContext schemaContext) { - dataTree = InMemoryDataTreeFactory.getInstance().create(); + public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType) { + dataTree = InMemoryDataTreeFactory.getInstance().create(treeType); updateSchemaContext(schemaContext); - } - TipProducingDataTree getDataTree() { + public TipProducingDataTree getDataTree() { return dataTree; } + SchemaContext getSchemaContext() { + return schemaContext; + } + void updateSchemaContext(final SchemaContext schemaContext) { Preconditions.checkNotNull(schemaContext); + this.schemaContext = schemaContext; dataTree.setSchemaContext(schemaContext); - validNamespaces = NormalizedNodePruner.namespaces(schemaContext); } private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) { @@ -84,7 +86,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) { if (Strings.isNullOrEmpty(chainId)) { - return transactionFactory.newReadOnlyTransaction(txId, chainId); + return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot()); } return ensureTransactionChain(chainId).newReadOnlyTransaction(txId); @@ -92,13 +94,14 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) { if (Strings.isNullOrEmpty(chainId)) { - return transactionFactory.newReadWriteTransaction(txId, chainId); + return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot() + .newModification()); } return ensureTransactionChain(chainId).newReadWriteTransaction(txId); } - void notifyListeners(final DataTreeCandidate candidate) { + public void notifyListeners(final DataTreeCandidate candidate) { LOG.debug("Notifying listeners on candidate {}", candidate); // DataTreeChanges first, as they are more light-weight @@ -108,6 +111,27 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { ResolveDataChangeEventsTask.create(candidate, listenerTree).resolve(MANAGER); } + void notifyOfInitialData(DataChangeListenerRegistration>> listenerReg, Optional currentState) { + + if(currentState.isPresent()) { + ListenerTree localListenerTree = ListenerTree.create(); + localListenerTree.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(), + listenerReg.getScope()); + + ResolveDataChangeEventsTask.create(currentState.get(), localListenerTree).resolve(MANAGER); + } + } + + void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener, + final Optional currentState) { + if(currentState.isPresent()) { + ShardDataTreeChangePublisher localTreeChangePublisher = new ShardDataTreeChangePublisher(); + localTreeChangePublisher.registerTreeChangeListener(path, listener); + localTreeChangePublisher.publishChanges(currentState.get()); + } + } + void closeAllTransactionChains() { for (ShardDataTreeTransactionChain chain : transactionChains.values()) { chain.close(); @@ -125,36 +149,28 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { } } - Entry>>, DOMImmutableDataChangeEvent> registerChangeListener( - final YangInstanceIdentifier path, - final AsyncDataChangeListener> listener, final DataChangeScope scope) { - final ListenerRegistration>> reg = + Entry>>, + Optional> registerChangeListener(final YangInstanceIdentifier path, + final AsyncDataChangeListener> listener, + final DataChangeScope scope) { + final DataChangeListenerRegistration>> reg = listenerTree.registerDataChangeListener(path, listener, scope); - final Optional> currentState = dataTree.takeSnapshot().readNode(path); - final DOMImmutableDataChangeEvent event; - if (currentState.isPresent()) { - final NormalizedNode data = currentState.get(); - event = DOMImmutableDataChangeEvent.builder(DataChangeScope.BASE).setAfter(data).addCreated(path, data).build(); - } else { - event = null; - } + return new SimpleEntry<>(reg, readCurrentData()); + } - return new SimpleEntry<>(reg, event); + private Optional readCurrentData() { + final Optional> currentState = dataTree.takeSnapshot().readNode(ROOT_PATH); + return currentState.isPresent() ? Optional.of(DataTreeCandidates.fromNormalizedNode( + ROOT_PATH, currentState.get())) : Optional.absent(); } - Entry, DataTreeCandidate> registerTreeChangeListener(final YangInstanceIdentifier path, - final DOMDataTreeChangeListener listener) { - final ListenerRegistration reg = treeChangePublisher.registerTreeChangeListener(path, listener); + public Entry, Optional> registerTreeChangeListener( + final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener) { + final ListenerRegistration reg = treeChangePublisher.registerTreeChangeListener( + path, listener); - final Optional> currentState = dataTree.takeSnapshot().readNode(path); - final DataTreeCandidate event; - if (currentState.isPresent()) { - event = DataTreeCandidates.fromNormalizedNode(path, currentState.get()); - } else { - event = null; - } - return new SimpleEntry<>(reg, event); + return new SimpleEntry<>(reg, readCurrentData()); } void applyForeignCandidate(final String identifier, final DataTreeCandidate foreign) throws DataValidationFailedException { @@ -183,51 +199,23 @@ public class ShardDataTree extends ShardDataTreeTransactionParent { return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId()); } - void recoveryDone(){ - transactionFactory = new RegularShardDataTreeTransactionFactory(); + public Optional> readNode(YangInstanceIdentifier path) { + return dataTree.takeSnapshot().readNode(path); } - @VisibleForTesting - ShardDataTreeTransactionFactory getTransactionFactory(){ - return transactionFactory; + public DataTreeSnapshot takeSnapshot() { + return dataTree.takeSnapshot(); } - @VisibleForTesting - static interface ShardDataTreeTransactionFactory { - ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId); - - ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId); + public DataTreeModification newModification() { + return dataTree.takeSnapshot().newModification(); } - @VisibleForTesting - class RecoveryShardDataTreeTransactionFactory implements ShardDataTreeTransactionFactory { - - @Override - public ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(String txId, String chainId) { - return new ReadOnlyShardDataTreeTransaction(txId, - new PruningShardDataTreeSnapshot(dataTree.takeSnapshot(), validNamespaces)); - } - - @Override - public ReadWriteShardDataTreeTransaction newReadWriteTransaction(String txId, String chainId) { - return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, - new PruningShardDataTreeSnapshot(dataTree.takeSnapshot(), validNamespaces).newModification()); - } - } - - @VisibleForTesting - class RegularShardDataTreeTransactionFactory implements ShardDataTreeTransactionFactory { - - @Override - public ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(String txId, String chainId) { - return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot()); - - } - - @Override - public ReadWriteShardDataTreeTransaction newReadWriteTransaction(String txId, String chainId) { - return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot() - .newModification()); - } + public DataTreeCandidate commit(DataTreeModification modification) throws DataValidationFailedException { + modification.ready(); + dataTree.validate(modification); + DataTreeCandidateTip candidate = dataTree.prepare(modification); + dataTree.commit(candidate); + return candidate; } }