BUG-509 transaction future result listening
[controller.git] / opendaylight / md-sal / inventory-manager / src / main / java / org / opendaylight / controller / md / inventory / manager / NodeChangeCommiter.java
index ebb3de7dcae6d8380b1ad465a63e8900689740c1..2a3d8fd37e26c1cafc012c68102f417063c51713 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.controller.md.inventory.manager;
 
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
@@ -20,7 +19,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeCon
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
@@ -38,10 +36,13 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Objects;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.JdkFutureAdapters;
 
-@SuppressWarnings("all")
 public class NodeChangeCommiter implements OpendaylightInventoryListener {
-    private final static Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
+
+    protected final static Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
 
     private final FlowCapableInventoryProvider manager;
 
@@ -58,15 +59,10 @@ public class NodeChangeCommiter implements OpendaylightInventoryListener {
 
         final NodeConnectorRef ref = connector.getNodeConnectorRef();
         final DataModificationTransaction it = this.getManager().startChange();
-        NodeChangeCommiter.LOG.debug("removing node connector {} ", ref.getValue());
+        LOG.debug("removing node connector {} ", ref.getValue());
         it.removeOperationalData(ref.getValue());
         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
-        try {
-            commitResult.get();
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Node Connector {} not removed.", ref.getValue(), e);
-        }
-
+        listenOnTransactionState(it.getIdentifier(), commitResult, "nodeConnector removal", ref.getValue());
     }
 
     @Override
@@ -75,7 +71,7 @@ public class NodeChangeCommiter implements OpendaylightInventoryListener {
         final NodeConnectorRef ref = connector.getNodeConnectorRef();
         final FlowCapableNodeConnectorUpdated flowConnector = connector
                 .getAugmentation(FlowCapableNodeConnectorUpdated.class);
-        final DataModificationTransaction it = this.getManager().startChange();
+        final DataModificationTransaction it = this.manager.startChange();
         final NodeConnectorBuilder data = new NodeConnectorBuilder(connector);
         NodeConnectorId id = connector.getId();
         NodeConnectorKey nodeConnectorKey = new NodeConnectorKey(id);
@@ -86,39 +82,22 @@ public class NodeChangeCommiter implements OpendaylightInventoryListener {
             data.addAugmentation(FlowCapableNodeConnector.class, augment);
         }
         InstanceIdentifier<? extends Object> value = ref.getValue();
-        String string = value.toString();
-        String plus = ("updating node connector : " + string);
-        NodeChangeCommiter.LOG.debug(plus);
-        InstanceIdentifier<? extends Object> value1 = ref.getValue();
+        LOG.debug("updating node connector : {}.", value);
         NodeConnector build = data.build();
-        it.putOperationalData((value1), build);
+        it.putOperationalData((value), build);
         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
-        try {
-            commitResult.get();
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Node Connector {} not updated.", ref.getValue(), e);
-        }
-
+        listenOnTransactionState(it.getIdentifier(), commitResult, "nodeConnector update", ref.getValue());
     }
 
     @Override
     public synchronized void onNodeRemoved(final NodeRemoved node) {
 
         final NodeRef ref = node.getNodeRef();
-        FlowCapableInventoryProvider manager = this.getManager();
-        final DataModificationTransaction it = manager.startChange();
-        InstanceIdentifier<? extends Object> value = ref.getValue();
-        String string = value.toString();
-        String plus = ("removing node : " + string);
-        NodeChangeCommiter.LOG.debug(plus);
-        InstanceIdentifier<? extends Object> value1 = ref.getValue();
-        it.removeOperationalData((value1));
+        final DataModificationTransaction it = this.manager.startChange();
+        LOG.debug("removing node : {}", ref.getValue());
+        it.removeOperationalData((ref.getValue()));
         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
-        try {
-            commitResult.get();
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Node {} not removed.", ref.getValue(), e);
-        }
+        listenOnTransactionState(it.getIdentifier(), commitResult, "node removal", ref.getValue());
     }
 
     @Override
@@ -127,33 +106,49 @@ public class NodeChangeCommiter implements OpendaylightInventoryListener {
         final NodeRef ref = node.getNodeRef();
         final FlowCapableNodeUpdated flowNode = node
                 .<FlowCapableNodeUpdated> getAugmentation(FlowCapableNodeUpdated.class);
-        FlowCapableInventoryProvider manager = this.getManager();
-        final DataModificationTransaction it = manager.startChange();
-        NodeBuilder nodeBuilder = new NodeBuilder(node);
-        final NodeBuilder data = nodeBuilder;
-        NodeId id = node.getId();
-        NodeKey nodeKey = new NodeKey(id);
-        data.setKey(nodeKey);
+        final DataModificationTransaction it = this.manager.startChange();
+        final NodeBuilder nodeBuilder = new NodeBuilder(node);
+        nodeBuilder.setKey(new NodeKey(node.getId()));
         boolean equals = Objects.equal(flowNode, null);
         if (equals) {
             return;
         }
         final FlowCapableNode augment = InventoryMapping.toInventoryAugment(flowNode);
-        data.addAugmentation(FlowCapableNode.class, augment);
+        nodeBuilder.addAugmentation(FlowCapableNode.class, augment);
         InstanceIdentifier<? extends Object> value = ref.getValue();
-        InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.<Node> builder(((InstanceIdentifier<Node>) value));
+        InstanceIdentifierBuilder<Node> builder = ((InstanceIdentifier<Node>) value).builder();
         InstanceIdentifierBuilder<FlowCapableNode> augmentation = builder
                 .<FlowCapableNode> augmentation(FlowCapableNode.class);
         final InstanceIdentifier<FlowCapableNode> path = augmentation.build();
-        NodeChangeCommiter.LOG.debug("updating node :{} ", path);
+        LOG.debug("updating node :{} ", path);
         it.putOperationalData(path, augment);
 
         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
-        try {
-            commitResult.get();
-        } catch (InterruptedException | ExecutionException e) {
-            LOG.error("Node  {} not updated.", ref.getValue(), e);
-        }
-
+        listenOnTransactionState(it.getIdentifier(), commitResult, "node update", ref.getValue());
+    }
+    
+    /**
+     * @param txId transaction identificator
+     * @param future transaction result
+     * @param action performed by transaction
+     * @param nodeConnectorPath target value
+     */
+    private static void listenOnTransactionState(final Object txId, Future<RpcResult<TransactionStatus>> future,
+            final String action, final InstanceIdentifier<?> nodeConnectorPath) {
+        Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future),new FutureCallback<RpcResult<TransactionStatus>>() {
+            
+            @Override
+            public void onFailure(Throwable t) {
+                LOG.error("Action {} [{}] failed for Tx:{}", action, nodeConnectorPath, txId, t);
+                
+            }
+            
+            @Override
+            public void onSuccess(RpcResult<TransactionStatus> result) {
+                if(!result.isSuccessful()) {
+                    LOG.error("Action {} [{}] failed for Tx:{}", action, nodeConnectorPath, txId);
+                }
+            }
+        });
     }
 }