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 / NetconfDeviceDatastoreAdapter.java
index 8590c49..48b41da 100644 (file)
@@ -16,10 +16,14 @@ import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.Set;
+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.ReadWriteTransaction;
 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.util.RemoteDeviceId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
@@ -46,11 +50,22 @@ final class NetconfDeviceDatastoreAdapter implements AutoCloseable {
     private static final Logger logger  = LoggerFactory.getLogger(NetconfDeviceDatastoreAdapter.class);
 
     private final RemoteDeviceId id;
-    private final DataBroker dataService;
+    private final BindingTransactionChain txChain;
 
     NetconfDeviceDatastoreAdapter(final RemoteDeviceId deviceId, final DataBroker dataService) {
         this.id = Preconditions.checkNotNull(deviceId);
-        this.dataService = Preconditions.checkNotNull(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);
+            }
+        });
 
         initDeviceData();
     }
@@ -59,7 +74,7 @@ final class NetconfDeviceDatastoreAdapter implements AutoCloseable {
         final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node data = buildDataForDeviceState(
                 up, capabilities, id);
 
-        final ReadWriteTransaction transaction = dataService.newReadWriteTransaction();
+        final ReadWriteTransaction transaction = txChain.newReadWriteTransaction();
         logger.trace("{}: Update device state transaction {} merging operational data started.", id, transaction.getIdentifier());
         transaction.put(LogicalDatastoreType.OPERATIONAL, id.getBindingPath(), data);
         logger.trace("{}: Update device state transaction {} merging operational data ended.", id, transaction.getIdentifier());
@@ -68,7 +83,7 @@ final class NetconfDeviceDatastoreAdapter implements AutoCloseable {
     }
 
     private void removeDeviceConfigAndState() {
-        final WriteTransaction transaction = dataService.newWriteOnlyTransaction();
+        final WriteTransaction transaction = txChain.newWriteOnlyTransaction();
         logger.trace("{}: Close device state transaction {} removing all data started.", id, transaction.getIdentifier());
         transaction.delete(LogicalDatastoreType.CONFIGURATION, id.getBindingPath());
         transaction.delete(LogicalDatastoreType.OPERATIONAL, id.getBindingPath());
@@ -78,7 +93,7 @@ final class NetconfDeviceDatastoreAdapter implements AutoCloseable {
     }
 
     private void initDeviceData() {
-        final WriteTransaction transaction = dataService.newWriteOnlyTransaction();
+        final WriteTransaction transaction = txChain.newWriteOnlyTransaction();
 
         createNodesListIfNotPresent(transaction);
 
@@ -126,6 +141,7 @@ final class NetconfDeviceDatastoreAdapter implements AutoCloseable {
     @Override
     public void close() throws Exception {
         removeDeviceConfigAndState();
+        txChain.close();
     }
 
     public static org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node buildDataForDeviceState(

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.