NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / handlers / NodeConnectedHandler.java
index 7c646c6c45521157b2290fbbf06139742898985a..443e689b53473f4e3ea0486b4c8263daf83091e8 100644 (file)
@@ -7,17 +7,20 @@
  */
 package org.opendaylight.netvirt.elan.l2gw.ha.handlers;
 
-import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
-import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.mdsal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
 
-import com.google.common.base.Optional;
 import java.util.List;
-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.ReadFailedException;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.genius.utils.hwvtep.HwvtepHACache;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import org.opendaylight.genius.infra.Datastore.Configuration;
+import org.opendaylight.genius.infra.Datastore.Operational;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.genius.infra.TypedReadWriteTransaction;
+import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAJobScheduler;
 import org.opendaylight.netvirt.elan.l2gw.ha.merge.GlobalAugmentationMerger;
@@ -40,15 +43,16 @@ public class NodeConnectedHandler {
 
     private static final Logger LOG = LoggerFactory.getLogger(NodeConnectedHandler.class);
 
-    GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
-    PSAugmentationMerger psAugmentationMerger = PSAugmentationMerger.getInstance();
-    GlobalNodeMerger globalNodeMerger = GlobalNodeMerger.getInstance();
-    PSNodeMerger psNodeMerger = PSNodeMerger.getInstance();
-    DataBroker db;
-    HwvtepHACache hwvtepHACache = HwvtepHACache.getInstance();
+    private final GlobalAugmentationMerger globalAugmentationMerger = GlobalAugmentationMerger.getInstance();
+    private final PSAugmentationMerger psAugmentationMerger = PSAugmentationMerger.getInstance();
+    private final GlobalNodeMerger globalNodeMerger = GlobalNodeMerger.getInstance();
+    private final PSNodeMerger psNodeMerger = PSNodeMerger.getInstance();
+    private final ManagedNewTransactionRunner txRunner;
+    private final HwvtepNodeHACache hwvtepNodeHACache;
 
-    public NodeConnectedHandler(DataBroker db) {
-        this.db = db;
+    public NodeConnectedHandler(final DataBroker db, final HwvtepNodeHACache hwvtepNodeHACache) {
+        this.txRunner = new ManagedNewTransactionRunnerImpl(db);
+        this.hwvtepNodeHACache = hwvtepNodeHACache;
     }
 
     /**
@@ -64,19 +68,19 @@ public class NodeConnectedHandler {
      * @param haNodePath  Ha Iid
      * @param haGlobalCfg Ha Global Config Node
      * @param haPSCfg Ha Physical Config Node
-     * @param tx Transaction
-     * @throws ReadFailedException  Exception thrown if read fails
+     * @param operTx Transaction
      */
     public void handleNodeConnected(Node childNode,
                                     InstanceIdentifier<Node> childNodePath,
                                     InstanceIdentifier<Node> haNodePath,
                                     Optional<Node> haGlobalCfg,
                                     Optional<Node> haPSCfg,
-                                    ReadWriteTransaction tx)
-            throws ReadFailedException {
-        HwvtepHAUtil.buildGlobalConfigForHANode(tx, childNode, haNodePath, haGlobalCfg);
-        copyChildOpToHA(childNode, haNodePath, tx);
-        readAndCopyChildPSOpToHAPS(childNode, haNodePath, tx);
+                                    TypedReadWriteTransaction<Configuration> confTx,
+                                    TypedReadWriteTransaction<Operational> operTx)
+            throws ExecutionException, InterruptedException {
+        HwvtepHAUtil.buildGlobalConfigForHANode(confTx, childNode, haNodePath, haGlobalCfg);
+        copyChildOpToHA(childNode, haNodePath, operTx);
+        readAndCopyChildPSOpToHAPS(childNode, haNodePath, operTx);
         if (haGlobalCfg.isPresent()) {
             //copy ha config to newly connected child case of reconnected child
             if (haPSCfg.isPresent()) {
@@ -88,37 +92,35 @@ public class NodeConnectedHandler {
                  (created in the device)
                  */
                 HAJobScheduler.getInstance().submitJob(() -> {
-                    try {
-                        hwvtepHACache.updateConnectedNodeStatus(childNodePath);
-                        LOG.info("HA child reconnected handleNodeReConnected {}",
+                    LoggingFutures.addErrorLogging(
+                        txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, jobTx -> {
+                            hwvtepNodeHACache.updateConnectedNodeStatus(childNodePath);
+                            LOG.info("HA child reconnected handleNodeReConnected {}",
                                 childNode.getNodeId().getValue());
-                        ReadWriteTransaction tx1 = db.newReadWriteTransaction();
-                        copyHAPSConfigToChildPS(haPSCfg.get(), childNodePath, tx1);
-                        tx1.submit().checkedGet();
-                    } catch (TransactionCommitFailedException e) {
-                        LOG.error("Failed to process ", e);
-                    }
+                            copyHAPSConfigToChildPS(haPSCfg.get(), childNodePath, jobTx);
+                        }), LOG, "Failed to process");
                 });
 
             }
-            copyHANodeConfigToChild(haGlobalCfg.get(), childNodePath, tx);
+            copyHANodeConfigToChild(haGlobalCfg.get(), childNodePath, confTx);
         }
-        deleteChildPSConfigIfHAPSConfigIsMissing(haGlobalCfg, childNode, tx);
+        deleteChildPSConfigIfHAPSConfigIsMissing(haGlobalCfg, childNode, confTx);
     }
 
-    private void deleteChildPSConfigIfHAPSConfigIsMissing(Optional<Node> haPSCfg,
-                                                          Node childNode,
-                                                          ReadWriteTransaction tx) throws ReadFailedException {
+    private static void deleteChildPSConfigIfHAPSConfigIsMissing(Optional<Node> haPSCfg,
+                                                                 Node childNode,
+                                                                 TypedReadWriteTransaction<Configuration> tx)
+            throws ExecutionException, InterruptedException {
         if (haPSCfg.isPresent()) {
             return;
         }
         LOG.info("HA ps node not present cleanup child {}" , childNode);
-        HwvtepGlobalAugmentation augmentation = childNode.getAugmentation(HwvtepGlobalAugmentation.class);
+        HwvtepGlobalAugmentation augmentation = childNode.augmentation(HwvtepGlobalAugmentation.class);
         if (augmentation != null) {
             List<Switches> switches = augmentation.getSwitches();
             if (switches != null) {
                 for (Switches ps : switches) {
-                    HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, ps.getSwitchRef().getValue());
+                    HwvtepHAUtil.deleteNodeIfPresent(tx, ps.getSwitchRef().getValue());
                 }
             }
         } else {
@@ -132,23 +134,21 @@ public class NodeConnectedHandler {
      * @param childGlobalNode Ha Global Child node
      * @param haNodePath Ha node path
      * @param tx  Transaction
-     * @throws ReadFailedException  Exception thrown if read fails
      */
     void readAndCopyChildPSOpToHAPS(Node childGlobalNode,
                                     InstanceIdentifier<Node> haNodePath,
-                                    ReadWriteTransaction tx)
-            throws ReadFailedException {
+                                    TypedReadWriteTransaction<Operational> tx)
+            throws ExecutionException, InterruptedException {
 
-        if (childGlobalNode == null || childGlobalNode.getAugmentation(HwvtepGlobalAugmentation.class) == null) {
+        if (childGlobalNode == null || childGlobalNode.augmentation(HwvtepGlobalAugmentation.class) == null) {
             return;
         }
-        List<Switches> switches = childGlobalNode.getAugmentation(HwvtepGlobalAugmentation.class).getSwitches();
+        List<Switches> switches = childGlobalNode.augmentation(HwvtepGlobalAugmentation.class).getSwitches();
         if (switches == null) {
             return;
         }
         for (Switches ps : switches) {
-            Node childPsNode = HwvtepHAUtil.readNode(tx, OPERATIONAL,
-                    (InstanceIdentifier<Node>) ps.getSwitchRef().getValue());
+            Node childPsNode = tx.read((InstanceIdentifier<Node>) ps.getSwitchRef().getValue()).get().orElse(null);
             if (childPsNode != null) {
                 InstanceIdentifier<Node> haPsPath = HwvtepHAUtil.convertPsPath(childPsNode, haNodePath);
                 copyChildPSOpToHAPS(childPsNode, haNodePath, haPsPath, tx);
@@ -165,11 +165,11 @@ public class NodeConnectedHandler {
      */
     private void copyHANodeConfigToChild(Node srcNode,
                                          InstanceIdentifier<Node> childPath,
-                                         ReadWriteTransaction tx) {
+                                         TypedReadWriteTransaction<Configuration> tx) {
         if (srcNode == null) {
             return;
         }
-        HwvtepGlobalAugmentation src = srcNode.getAugmentation(HwvtepGlobalAugmentation.class);
+        HwvtepGlobalAugmentation src = srcNode.augmentation(HwvtepGlobalAugmentation.class);
         if (src == null) {
             return;
         }
@@ -180,7 +180,7 @@ public class NodeConnectedHandler {
         globalNodeMerger.mergeConfigData(nodeBuilder, srcNode, childPath);
         nodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, dstBuilder.build());
         Node dstNode = nodeBuilder.build();
-        tx.put(CONFIGURATION, childPath, dstNode, WriteTransaction.CREATE_MISSING_PARENTS);
+        tx.put(childPath, dstNode, CREATE_MISSING_PARENTS);
     }
 
     /**
@@ -189,23 +189,22 @@ public class NodeConnectedHandler {
      * @param childNode HA Child Node
      * @param haNodePath HA node path
      * @param tx Transaction
-     * @throws ReadFailedException  Exception thrown if read fails
      */
     private void copyChildOpToHA(Node childNode,
                                  InstanceIdentifier<Node> haNodePath,
-                                 ReadWriteTransaction tx)
-            throws ReadFailedException {
+                                 TypedReadWriteTransaction<Operational> tx)
+            throws ExecutionException, InterruptedException {
         if (childNode == null) {
             return;
         }
-        HwvtepGlobalAugmentation childData = childNode.getAugmentation(HwvtepGlobalAugmentation.class);
+        HwvtepGlobalAugmentation childData = childNode.augmentation(HwvtepGlobalAugmentation.class);
         if (childData == null) {
             return;
         }
         NodeBuilder haNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(haNodePath);
         HwvtepGlobalAugmentationBuilder haBuilder = new HwvtepGlobalAugmentationBuilder();
 
-        Optional<Node> existingHANodeOptional = tx.read(OPERATIONAL, haNodePath).checkedGet();
+        Optional<Node> existingHANodeOptional = tx.read(haNodePath).get();
         Node existingHANode = existingHANodeOptional.isPresent() ? existingHANodeOptional.get() : null;
         HwvtepGlobalAugmentation existingHAData = HwvtepHAUtil.getGlobalAugmentationOfNode(existingHANode);
 
@@ -217,7 +216,7 @@ public class NodeConnectedHandler {
         haBuilder.setDbVersion(childData.getDbVersion());
         haNodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, haBuilder.build());
         Node haNode = haNodeBuilder.build();
-        tx.merge(OPERATIONAL, haNodePath, haNode, true);
+        tx.merge(haNodePath, haNode, CREATE_MISSING_PARENTS);
     }
 
     /**
@@ -246,19 +245,19 @@ public class NodeConnectedHandler {
      */
     public void copyHAPSConfigToChildPS(Node haPsNode,
                                         InstanceIdentifier<Node> childPath,
-                                        ReadWriteTransaction tx) {
+                                        TypedReadWriteTransaction<Configuration> tx) {
         InstanceIdentifier<Node> childPsPath = HwvtepHAUtil.convertPsPath(haPsNode, childPath);
 
         NodeBuilder childPsBuilder = HwvtepHAUtil.getNodeBuilderForPath(childPsPath);
         PhysicalSwitchAugmentationBuilder dstBuilder = new PhysicalSwitchAugmentationBuilder();
-        PhysicalSwitchAugmentation src = haPsNode.getAugmentation(PhysicalSwitchAugmentation.class);
+        PhysicalSwitchAugmentation src = haPsNode.augmentation(PhysicalSwitchAugmentation.class);
 
         psAugmentationMerger.mergeConfigData(dstBuilder, src, childPath);
         psNodeMerger.mergeConfigData(childPsBuilder, haPsNode, childPath);
 
         childPsBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstBuilder.build());
         Node childPSNode = childPsBuilder.build();
-        tx.put(CONFIGURATION, childPsPath, childPSNode, WriteTransaction.CREATE_MISSING_PARENTS);
+        tx.put(childPsPath, childPSNode, CREATE_MISSING_PARENTS);
     }
 
     /**
@@ -268,20 +267,19 @@ public class NodeConnectedHandler {
      * @param haPath  HA node path
      * @param haPspath Ha Physical Switch Node path
      * @param tx Transaction
-     * @throws ReadFailedException  Exception thrown if read fails
      */
     public void copyChildPSOpToHAPS(Node childPsNode,
                                     InstanceIdentifier<Node> haPath,
                                     InstanceIdentifier<Node> haPspath,
-                                    ReadWriteTransaction tx)
-            throws ReadFailedException {
+                                    TypedReadWriteTransaction<Operational> tx)
+            throws ExecutionException, InterruptedException {
 
         NodeBuilder haPSNodeBuilder = HwvtepHAUtil.getNodeBuilderForPath(haPspath);
         PhysicalSwitchAugmentationBuilder dstBuilder = new PhysicalSwitchAugmentationBuilder();
 
-        PhysicalSwitchAugmentation src = childPsNode.getAugmentation(PhysicalSwitchAugmentation.class);
+        PhysicalSwitchAugmentation src = childPsNode.augmentation(PhysicalSwitchAugmentation.class);
 
-        Node existingHAPSNode = HwvtepHAUtil.readNode(tx, OPERATIONAL, haPspath);
+        Node existingHAPSNode = tx.read(haPspath).get().orElse(null);
         PhysicalSwitchAugmentation existingHAPSAugumentation =
                 HwvtepHAUtil.getPhysicalSwitchAugmentationOfNode(existingHAPSNode);
 
@@ -291,7 +289,7 @@ public class NodeConnectedHandler {
 
         haPSNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, dstBuilder.build());
         Node haPsNode = haPSNodeBuilder.build();
-        tx.merge(OPERATIONAL, haPspath, haPsNode, true);
+        tx.merge(haPspath, haPsNode, CREATE_MISSING_PARENTS);
     }
 
 }