Port dhcpservice to datastore-constrained txes
[netvirt.git] / dhcpservice / impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpExternalTunnelManager.java
index e8edbc088c939efafd02c747dfe86082ca6fddf1..039c0db38f729fbb8632e3579d44ee7ab2cc753e 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.netvirt.dhcpservice;
 
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.math.BigInteger;
@@ -32,11 +34,13 @@ import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.genius.infra.Datastore.Configuration;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.genius.infra.TransactionAdapter;
+import org.opendaylight.genius.infra.TypedWriteTransaction;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.NwConstants;
@@ -110,6 +114,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
     private final JobCoordinator jobCoordinator;
     private final L2GatewayCache l2GatewayCache;
     private IElanService elanService;
+    private final DhcpServiceCounters dhcpServiceCounters;
 
     private final ConcurrentMap<BigInteger, Set<Pair<IpAddress, String>>> designatedDpnsToTunnelIpElanNameCache =
             new ConcurrentHashMap<>();
@@ -143,7 +148,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
             final IMdsalApiManager mdsalUtil, final ItmRpcService itmRpcService,
             final EntityOwnershipService entityOwnershipService, final IInterfaceManager interfaceManager,
             final JobCoordinator jobCoordinator, final L2GatewayCache l2GatewayCache,
-            @Named("elanService") IElanService ielanService) {
+            @Named("elanService") IElanService ielanService, DhcpServiceCounters dhcpServiceCounters) {
         this.broker = broker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
         this.mdsalUtil = mdsalUtil;
@@ -153,6 +158,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         this.jobCoordinator = jobCoordinator;
         this.l2GatewayCache = l2GatewayCache;
         this.elanService = ielanService;
+        this.dhcpServiceCounters = dhcpServiceCounters;
     }
 
     @PostConstruct
@@ -223,13 +229,14 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         jobCoordinator.enqueueJob(getJobKey(tunnelIpDpnKey), () -> {
             if (entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
                     HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
-                return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
-                    dpns.remove(designatedDpnId);
-                    for (BigInteger dpn : dpns) {
-                        installDhcpDropAction(dpn, vmMacAddress, tx);
-                    }
-                    installDhcpEntries(designatedDpnId, vmMacAddress, tx);
-                }));
+                return Collections.singletonList(
+                    txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
+                        dpns.remove(designatedDpnId);
+                        for (BigInteger dpn : dpns) {
+                            installDhcpDropAction(dpn, vmMacAddress, tx);
+                        }
+                        installDhcpEntries(designatedDpnId, vmMacAddress, tx);
+                    }));
             } else {
                 LOG.trace("Exiting installDhcpEntries since this cluster node is not the owner for dpn");
             }
@@ -240,7 +247,8 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         updateLocalCache(tunnelIp, elanInstanceName, vmMacAddress);
     }
 
-    public void installDhcpFlowsForVms(BigInteger designatedDpnId, Set<String> listVmMacAddress, WriteTransaction tx) {
+    public void installDhcpFlowsForVms(BigInteger designatedDpnId, Set<String> listVmMacAddress,
+        TypedWriteTransaction<Configuration> tx) {
         for (String vmMacAddress : listVmMacAddress) {
             installDhcpEntries(designatedDpnId, vmMacAddress, tx);
         }
@@ -291,7 +299,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         DesignatedSwitchForTunnel designatedSwitchForTunnel =
                 new DesignatedSwitchForTunnelBuilder().setDpId(dpnId.longValue())
                         .setElanInstanceName(elanInstanceName).setTunnelRemoteIpAddress(tunnelIp)
-                        .setKey(designatedSwitchForTunnelKey).build();
+                        .withKey(designatedSwitchForTunnelKey).build();
         LOG.trace("Writing into CONFIG DS tunnelIp {}, elanInstanceName {}, dpnId {}", tunnelIp, elanInstanceName,
                 dpnId);
         MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, instanceIdentifier, designatedSwitchForTunnel);
@@ -326,7 +334,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         }
         List<String> vmMacs = getAllVmMacs();
         LOG.trace("Installing drop actions to this new DPN {} VMs {}", dpId, vmMacs);
-        ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
+        ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
             for (String vmMacAddress : vmMacs) {
                 installDhcpDropAction(dpId, vmMacAddress, tx);
             }
@@ -405,7 +413,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         if (setOfTunnelIpElanNamePairs == null || setOfTunnelIpElanNamePairs.isEmpty()) {
             LOG.trace("No tunnelIpElanName to handle for dpn {}. Returning", dpnId);
         } else {
-            ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+            ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
                 if (!dpnId.equals(DhcpMConstants.INVALID_DPID)) {
                     List<String> listOfVms = getAllVmMacs();
                     for (String vmMacAddress : listOfVms) {
@@ -420,7 +428,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
     }
 
     public void updateCacheAndInstallNewFlows(List<BigInteger> listOfDpns, Pair<IpAddress, String> pair,
-            WriteTransaction tx) {
+            TypedWriteTransaction<Configuration> tx) {
         BigInteger newDesignatedDpn = chooseDpn(pair.getLeft(), pair.getRight(), listOfDpns);
         if (newDesignatedDpn.equals(DhcpMConstants.INVALID_DPID)) {
             return;
@@ -438,7 +446,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
     }
 
     private void changeExistingFlowToDrop(Pair<IpAddress, String> tunnelIpElanNamePair, BigInteger dpnId,
-                                          WriteTransaction tx) {
+                                          TypedWriteTransaction<Configuration> tx) {
         Set<String> setOfVmMacAddress = tunnelIpElanNameToVmMacCache.get(tunnelIpElanNamePair);
         if (setOfVmMacAddress == null || setOfVmMacAddress.isEmpty()) {
             return;
@@ -508,9 +516,9 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         writeDesignatedSwitchForExternalTunnel(DhcpMConstants.INVALID_DPID, tunnelIp, elanInstanceName);
     }
 
-    private void installDhcpEntries(BigInteger dpnId, String vmMacAddress, WriteTransaction tx) {
+    private void installDhcpEntries(BigInteger dpnId, String vmMacAddress, TypedWriteTransaction<Configuration> tx) {
         DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL,
-                vmMacAddress, NwConstants.ADD_FLOW, mdsalUtil, tx);
+                vmMacAddress, NwConstants.ADD_FLOW, mdsalUtil, dhcpServiceCounters, tx);
     }
 
     public void addOrRemoveDhcpArpFlowforElan(String elanInstanceName, boolean addFlow, String dhcpIpAddress,
@@ -609,7 +617,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         builder.setDpId(dpnId).setInterfaceName(interfaceName).setSpa(dhcpIpAddress).setSha(dhcpMacAddress)
                 .setLportTag(lportTag);
         builder.setInstructions(ArpResponderUtil.getInterfaceInstructions(interfaceManager, interfaceName,
-                dhcpIpAddress, dhcpMacAddress));
+                dhcpIpAddress, dhcpMacAddress, itmRpcService));
         elanService.addExternalTunnelArpResponderFlow(builder.buildForInstallFlow(), elanInstanceName);
     }
 
@@ -629,14 +637,14 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
     }
 
 
-    public void unInstallDhcpEntries(BigInteger dpnId, String vmMacAddress, WriteTransaction tx) {
+    public void unInstallDhcpEntries(BigInteger dpnId, String vmMacAddress, TypedWriteTransaction<Configuration> tx) {
         DhcpServiceUtils.setupDhcpFlowEntry(dpnId, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL,
-                vmMacAddress, NwConstants.DEL_FLOW, mdsalUtil, tx);
+                vmMacAddress, NwConstants.DEL_FLOW, mdsalUtil, dhcpServiceCounters, tx);
     }
 
-    private void installDhcpDropAction(BigInteger dpn, String vmMacAddress, WriteTransaction tx) {
+    private void installDhcpDropAction(BigInteger dpn, String vmMacAddress, TypedWriteTransaction<Configuration> tx) {
         DhcpServiceUtils.setupDhcpDropAction(dpn, NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL,
-                vmMacAddress, NwConstants.ADD_FLOW, mdsalUtil, tx);
+                vmMacAddress, NwConstants.ADD_FLOW, mdsalUtil, dhcpServiceCounters, tx);
     }
 
     public List<ListenableFuture<Void>> handleTunnelStateDown(IpAddress tunnelIp, BigInteger interfaceDpn) {
@@ -660,7 +668,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
                     }
                 }
             }
-            return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+            return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
                 for (Pair<IpAddress, String> tunnelElanPair : tunnelElanPairSet) {
                     IpAddress tunnelIpInDpn = tunnelElanPair.getLeft();
                     String elanInstanceName = tunnelElanPair.getRight();
@@ -813,7 +821,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
                 .setMacEntryKey(new MacAddress(UNKNOWN_DMAC))
                 .setLogicalSwitchRef(lsRef).build();
         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(
-                dstDevice.getNodeId(), remoteMcastMacs.getKey());
+                dstDevice.getNodeId(), remoteMcastMacs.key());
         ReadOnlyTransaction transaction = broker.newReadOnlyTransaction();
         try {
             //TODO do async mdsal read
@@ -826,17 +834,16 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         return null;
     }
 
-    private WriteTransaction putRemoteMcastMac(WriteTransaction transaction, String elanName,
-                                               L2GatewayDevice device, IpAddress internalTunnelIp) {
+    private void putRemoteMcastMac(TypedWriteTransaction<Configuration> transaction, String elanName,
+                                   L2GatewayDevice device, IpAddress internalTunnelIp) {
         Optional<Node> optionalNode = getNode(broker, device.getHwvtepNodeId());
         Node dstNode = optionalNode.get();
         if (dstNode == null) {
             LOG.trace("could not get device node {} ", device.getHwvtepNodeId());
-            return null;
+            return;
         }
         RemoteMcastMacs macs = createRemoteMcastMac(dstNode, elanName, internalTunnelIp);
-        HwvtepUtils.putRemoteMcastMac(transaction, dstNode.getNodeId(), macs);
-        return transaction;
+        HwvtepUtils.putRemoteMcastMac(TransactionAdapter.toWriteTransaction(transaction), dstNode.getNodeId(), macs);
     }
 
     public void installRemoteMcastMac(final BigInteger designatedDpnId, final IpAddress tunnelIp,
@@ -868,9 +875,9 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
                     LOG.trace("Tunnel Interface is not present {}", tunnelInterfaceName);
                     return Collections.emptyList();
                 }
-                return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(
+                return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
                     tx -> putRemoteMcastMac(tx, elanInstanceName, device,
-                            tunnelInterface.getAugmentation(IfTunnel.class).getTunnelSource())));
+                            tunnelInterface.augmentation(IfTunnel.class).getTunnelSource())));
             }
             return Collections.emptyList();
         });
@@ -913,7 +920,7 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
                 LOG.trace("There are no undesignated DPNs");
                 return Collections.emptyList();
             }
-            return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+            return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
                 for (Pair<IpAddress, String> pair : tunnelIpElanPair) {
                     if (tunnelIp.equals(pair.getLeft())) {
                         String elanInstanceName = pair.getRight();
@@ -959,11 +966,12 @@ public class DhcpExternalTunnelManager implements IDhcpExternalTunnelManager {
         jobCoordinator.enqueueJob(getJobKey(vmMacAddress), () -> {
             if (entityOwnershipUtils.isEntityOwner(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
                     HwvtepSouthboundConstants.ELAN_ENTITY_NAME)) {
-                return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
-                    for (final BigInteger dpn : dpns) {
-                        unInstallDhcpEntries(dpn, vmMacAddress, tx);
-                    }
-                }));
+                return Collections.singletonList(
+                    txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
+                        for (final BigInteger dpn : dpns) {
+                            unInstallDhcpEntries(dpn, vmMacAddress, tx);
+                        }
+                    }));
             } else {
                 LOG.trace("Exiting unInstallDhcpEntries since this cluster node is not the owner for dpn");
             }