Apply SchemaContext to dataTree first
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTree.java
index 17c3cd82c35e73d0e7ed049b27d4eb25fa52d608..edf05f2a25bb9fe5bb108b18dd5fcd10709266e8 100644 (file)
@@ -9,12 +9,13 @@ package org.opendaylight.controller.cluster.datastore;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
 import java.util.AbstractMap.SimpleEntry;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import javax.annotation.concurrent.NotThreadSafe;
+import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
+import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
 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;
@@ -47,7 +48,7 @@ import org.slf4j.LoggerFactory;
 public class ShardDataTree extends ShardDataTreeTransactionParent {
     private static final Logger LOG = LoggerFactory.getLogger(ShardDataTree.class);
 
-    private final Map<String, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
+    private final Map<LocalHistoryIdentifier, ShardDataTreeTransactionChain> transactionChains = new HashMap<>();
     private final ShardDataTreeChangeListenerPublisher treeChangeListenerPublisher;
     private final ShardDataChangeListenerPublisher dataChangeListenerPublisher;
     private final TipProducingDataTree dataTree;
@@ -59,9 +60,10 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
             final ShardDataChangeListenerPublisher dataChangeListenerPublisher, final String logContext) {
         dataTree = InMemoryDataTreeFactory.getInstance().create(treeType);
         updateSchemaContext(schemaContext);
-        this.treeChangeListenerPublisher = treeChangeListenerPublisher;
-        this.dataChangeListenerPublisher = dataChangeListenerPublisher;
-        this.logContext = logContext;
+
+        this.treeChangeListenerPublisher = Preconditions.checkNotNull(treeChangeListenerPublisher);
+        this.dataChangeListenerPublisher = Preconditions.checkNotNull(dataChangeListenerPublisher);
+        this.logContext = Preconditions.checkNotNull(logContext);
     }
 
     public ShardDataTree(final SchemaContext schemaContext, final TreeType treeType) {
@@ -78,36 +80,35 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
     }
 
     void updateSchemaContext(final SchemaContext schemaContext) {
-        Preconditions.checkNotNull(schemaContext);
-        this.schemaContext = schemaContext;
         dataTree.setSchemaContext(schemaContext);
+        this.schemaContext = Preconditions.checkNotNull(schemaContext);
     }
 
-    private ShardDataTreeTransactionChain ensureTransactionChain(final String chainId) {
-        ShardDataTreeTransactionChain chain = transactionChains.get(chainId);
+    private ShardDataTreeTransactionChain ensureTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier) {
+        ShardDataTreeTransactionChain chain = transactionChains.get(localHistoryIdentifier);
         if (chain == null) {
-            chain = new ShardDataTreeTransactionChain(chainId, this);
-            transactionChains.put(chainId, chain);
+            chain = new ShardDataTreeTransactionChain(localHistoryIdentifier, this);
+            transactionChains.put(localHistoryIdentifier, chain);
         }
 
         return chain;
     }
 
-    ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final String txId, final String chainId) {
-        if (Strings.isNullOrEmpty(chainId)) {
+    ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
+        if (txId.getHistoryId().getHistoryId() == 0) {
             return new ReadOnlyShardDataTreeTransaction(txId, dataTree.takeSnapshot());
         }
 
-        return ensureTransactionChain(chainId).newReadOnlyTransaction(txId);
+        return ensureTransactionChain(txId.getHistoryId()).newReadOnlyTransaction(txId);
     }
 
-    ReadWriteShardDataTreeTransaction newReadWriteTransaction(final String txId, final String chainId) {
-        if (Strings.isNullOrEmpty(chainId)) {
+    ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
+        if (txId.getHistoryId().getHistoryId() == 0) {
             return new ReadWriteShardDataTreeTransaction(ShardDataTree.this, txId, dataTree.takeSnapshot()
                     .newModification());
         }
 
-        return ensureTransactionChain(chainId).newReadWriteTransaction(txId);
+        return ensureTransactionChain(txId.getHistoryId()).newReadWriteTransaction(txId);
     }
 
     public void notifyListeners(final DataTreeCandidate candidate) {
@@ -115,10 +116,9 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
         dataChangeListenerPublisher.publishChanges(candidate, logContext);
     }
 
-    void notifyOfInitialData(DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
-            NormalizedNode<?, ?>>> listenerReg, Optional<DataTreeCandidate> currentState) {
-
-        if(currentState.isPresent()) {
+    void notifyOfInitialData(final DataChangeListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier,
+            NormalizedNode<?, ?>>> listenerReg, final Optional<DataTreeCandidate> currentState) {
+        if (currentState.isPresent()) {
             ShardDataChangeListenerPublisher localPublisher = dataChangeListenerPublisher.newInstance();
             localPublisher.registerDataChangeListener(listenerReg.getPath(), listenerReg.getInstance(),
                     listenerReg.getScope());
@@ -128,7 +128,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
 
     void notifyOfInitialData(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
             final Optional<DataTreeCandidate> currentState) {
-        if(currentState.isPresent()) {
+        if (currentState.isPresent()) {
             ShardDataTreeChangeListenerPublisher localPublisher = treeChangeListenerPublisher.newInstance();
             localPublisher.registerTreeChangeListener(path, listener);
             localPublisher.publishChanges(currentState.get(), logContext);
@@ -143,7 +143,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
         transactionChains.clear();
     }
 
-    void closeTransactionChain(final String transactionChainId) {
+    void closeTransactionChain(final LocalHistoryIdentifier transactionChainId) {
         final ShardDataTreeTransactionChain chain = transactionChains.remove(transactionChainId);
         if (chain != null) {
             chain.close();
@@ -202,7 +202,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
         return new SimpleShardDataTreeCohort(this, snapshot, transaction.getId());
     }
 
-    public Optional<NormalizedNode<?, ?>> readNode(YangInstanceIdentifier path) {
+    public Optional<NormalizedNode<?, ?>> readNode(final YangInstanceIdentifier path) {
         return dataTree.takeSnapshot().readNode(path);
     }
 
@@ -215,7 +215,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
     }
 
     // FIXME: This should be removed, it violates encapsulation
-    public DataTreeCandidate commit(DataTreeModification modification) throws DataValidationFailedException {
+    public DataTreeCandidate commit(final DataTreeModification modification) throws DataValidationFailedException {
         modification.ready();
         dataTree.validate(modification);
         DataTreeCandidateTip candidate = dataTree.prepare(modification);