Changed NetconfDeviceDatastoreAdapter and NetconfDeviceTopologyAdapter to use Transac...
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceTopologyAdapter.java
index 24b6205..055beda 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.controller.sal.connect.netconf.sal;
 
 import com.google.common.base.Function;
+import com.google.common.base.Preconditions;
 import com.google.common.collect.FluentIterable;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
@@ -16,9 +17,13 @@ import com.google.common.util.concurrent.Futures;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map.Entry;
+import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCapabilities;
 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
@@ -68,7 +73,7 @@ final class NetconfDeviceTopologyAdapter implements AutoCloseable {
     };
 
     private final RemoteDeviceId id;
-    private final DataBroker dataService;
+    private final BindingTransactionChain txChain;
 
     private final InstanceIdentifier<NetworkTopology> networkTopologyPath;
     private final KeyedInstanceIdentifier<Topology, TopologyKey> topologyListPath;
@@ -76,7 +81,18 @@ final class NetconfDeviceTopologyAdapter implements AutoCloseable {
 
     NetconfDeviceTopologyAdapter(final RemoteDeviceId id, final DataBroker dataService) {
         this.id = id;
-        this.dataService = dataService;
+        this.txChain = Preconditions.checkNotNull(dataService).createTransactionChain(new TransactionChainListener() {
+            @Override
+            public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
+                logger.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
+                throw new IllegalStateException(id + "  TransactionChain(" + chain + ") not committed correctly", cause);
+            }
+
+            @Override
+            public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
+                logger.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
+            }
+        });
 
         this.networkTopologyPath = InstanceIdentifier.builder(NetworkTopology.class).build();
         this.topologyListPath = networkTopologyPath.child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
@@ -85,7 +101,7 @@ final class NetconfDeviceTopologyAdapter implements AutoCloseable {
     }
 
      private void initDeviceData() {
-        final WriteTransaction writeTx = dataService.newWriteOnlyTransaction();
+        final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
 
         createNetworkTopologyIfNotPresent(writeTx);
 
@@ -112,7 +128,7 @@ final class NetconfDeviceTopologyAdapter implements AutoCloseable {
     public void updateDeviceData(boolean up, NetconfDeviceCapabilities capabilities) {
         final Node data = buildDataForNetconfNode(up, capabilities);
 
-        final WriteTransaction writeTx = dataService.newWriteOnlyTransaction();
+        final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
         logger.trace("{}: Update device state transaction {} merging operational data started.", id, writeTx.getIdentifier());
         writeTx.put(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath(), data);
         logger.trace("{}: Update device state transaction {} merging operational data ended.", id, writeTx.getIdentifier());
@@ -126,7 +142,7 @@ final class NetconfDeviceTopologyAdapter implements AutoCloseable {
         final NetconfNode netconfNode = new NetconfNodeBuilder().setConnectionStatus(ConnectionStatus.UnableToConnect).setConnectedMessage(reason).build();
         final Node data = getNodeIdBuilder(id).addAugmentation(NetconfNode.class, netconfNode).build();
 
-        final WriteTransaction writeTx = dataService.newWriteOnlyTransaction();
+        final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
         logger.trace("{}: Setting device state as failed {} putting operational data started.", id, writeTx.getIdentifier());
         writeTx.put(LogicalDatastoreType.OPERATIONAL, id.getTopologyBindingPath(), data);
         logger.trace("{}: Setting device state as failed {} putting operational data ended.", id, writeTx.getIdentifier());
@@ -158,7 +174,7 @@ final class NetconfDeviceTopologyAdapter implements AutoCloseable {
     }
 
     public void removeDeviceConfiguration() {
-        final WriteTransaction writeTx = dataService.newWriteOnlyTransaction();
+        final WriteTransaction writeTx = txChain.newWriteOnlyTransaction();
 
         logger.trace("{}: Close device state transaction {} removing all data started.", id, writeTx.getIdentifier());
         writeTx.delete(LogicalDatastoreType.CONFIGURATION, id.getTopologyBindingPath());
@@ -214,5 +230,6 @@ final class NetconfDeviceTopologyAdapter implements AutoCloseable {
     @Override
     public void close() throws Exception {
         removeDeviceConfiguration();
+        txChain.close();
     }
 }