Migrate elanmanager to use LoggingFutures
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanPacketInHandler.java
index 0a8c18fd66adba9a791c7f847cd355de3bf0d4b2..86df916e18c1cbdcbdd5b2af342f229e0140a195 100755 (executable)
@@ -7,14 +7,20 @@
  */
 package org.opendaylight.netvirt.elan.internal;
 
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
+
 import com.google.common.base.Optional;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.math.BigInteger;
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 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.LogicalDatastoreType;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
@@ -24,12 +30,11 @@ import org.opendaylight.genius.mdsalutil.NWUtil;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.genius.mdsalutil.packet.Ethernet;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
-import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
 import org.opendaylight.netvirt.elan.evpn.utils.EvpnUtils;
 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
 import org.opendaylight.netvirt.elan.utils.ElanUtils;
-import org.opendaylight.openflowplugin.libraries.liblldp.NetUtils;
 import org.opendaylight.openflowplugin.libraries.liblldp.PacketException;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
@@ -60,11 +65,12 @@ public class ElanPacketInHandler implements PacketProcessingListener {
     private final EvpnUtils evpnUtils;
     private final JobCoordinator jobCoordinator;
     private final ElanInstanceCache elanInstanceCache;
+    private final ElanManagerCounters elanManagerCounters;
 
     @Inject
     public ElanPacketInHandler(DataBroker dataBroker, final IInterfaceManager interfaceManager, ElanUtils elanUtils,
             EvpnUtils evpnUtils, ElanL2GatewayUtils elanL2GatewayUtils, JobCoordinator jobCoordinator,
-            ElanInstanceCache elanInstanceCache) {
+            ElanInstanceCache elanInstanceCache, ElanManagerCounters elanManagerCounters) {
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.interfaceManager = interfaceManager;
         this.elanUtils = elanUtils;
@@ -72,6 +78,7 @@ public class ElanPacketInHandler implements PacketProcessingListener {
         this.evpnUtils = evpnUtils;
         this.jobCoordinator = jobCoordinator;
         this.elanInstanceCache = elanInstanceCache;
+        this.elanManagerCounters = elanManagerCounters;
     }
 
     @Override
@@ -79,12 +86,12 @@ public class ElanPacketInHandler implements PacketProcessingListener {
         Class<? extends PacketInReason> pktInReason = notification.getPacketInReason();
         short tableId = notification.getTableId().getValue();
         if (pktInReason == NoMatch.class && tableId == NwConstants.ELAN_SMAC_TABLE) {
-            ElanManagerCounters.unknown_smac_pktin_rcv.inc();
+            elanManagerCounters.unknownSmacPktinRcv();
             try {
                 byte[] data = notification.getPayload();
                 Ethernet res = new Ethernet();
 
-                res.deserialize(data, 0, data.length * NetUtils.NUM_BITS_IN_A_BYTE);
+                res.deserialize(data, 0, data.length * Byte.SIZE);
 
                 byte[] srcMac = res.getSourceMACAddress();
                 final String macAddress = NWUtil.toStringMacAddress(srcMac);
@@ -160,33 +167,32 @@ public class ElanPacketInHandler implements PacketProcessingListener {
                                                MacEntry oldMacEntry, MacEntry newMacEntry,
                                                final boolean isVlanOrFlatProviderIface) {
         jobCoordinator.enqueueJob(ElanUtils.getElanMacKey(elanTag, macAddress),
-            () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
-                if (oldMacEntry != null && oldMacEntry.getInterface().equals(interfaceName)) {
+            () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, tx -> {
+                if (oldMacEntry != null && Objects.equals(oldMacEntry.getInterface(), interfaceName)) {
                     // This should never occur because of ovs temporary mac learning
-                    ElanManagerCounters.unknown_smac_pktin_forwarding_entries_removed.inc();
+                    elanManagerCounters.unknownSmacPktinForwardingEntriesRemoved();
                 } else if (oldMacEntry != null && !isVlanOrFlatProviderIface) {
                     long macTimeStamp = oldMacEntry.getControllerLearnedForwardingEntryTimestamp().longValue();
                     if (System.currentTimeMillis() > macTimeStamp + 1000) {
                         InstanceIdentifier<MacEntry> macEntryId = ElanUtils
                                 .getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName,
                                         physAddress);
-                        tx.delete(LogicalDatastoreType.OPERATIONAL, macEntryId);
+                        tx.delete(macEntryId);
                     } else {
                         // New FEs flood their packets on all interfaces. This can lead
                         // to many contradicting packet_ins. Ignore all packets received
                         // within 1s after the first packet_in
-                        ElanManagerCounters.unknown_smac_pktin_mac_migration_ignored_due_to_protection.inc();
+                        elanManagerCounters.unknownSmacPktinMacMigrationIgnoredDueToProtection();
                     }
                 } else if (oldMacEntry != null) {
-                    ElanManagerCounters.unknown_smac_pktin_removed_for_relearned.inc();
+                    elanManagerCounters.unknownSmacPktinRemovedForRelearned();
                 }
                 // This check is required only to update elan-forwarding-tables when mac is learned
                 // in ports (example: VM interfaces) other than on vlan provider port.
                 if (!isVlanOrFlatProviderIface && oldMacEntry == null) {
                     InstanceIdentifier<MacEntry> elanMacEntryId =
                             ElanUtils.getMacEntryOperationalDataPath(elanName, physAddress);
-                    tx.put(LogicalDatastoreType.OPERATIONAL, elanMacEntryId, newMacEntry,
-                            WriteTransaction.CREATE_MISSING_PARENTS);
+                    tx.put(elanMacEntryId, newMacEntry, WriteTransaction.CREATE_MISSING_PARENTS);
                 }
             })));
     }
@@ -200,24 +206,27 @@ public class ElanPacketInHandler implements PacketProcessingListener {
             BigInteger dpId = interfaceManager.getDpnForInterface(interfaceName);
             elanL2GatewayUtils.scheduleAddDpnMacInExtDevices(elanInstance.getElanInstanceName(), dpId,
                     Collections.singletonList(physAddress));
-            ElanManagerCounters.unknown_smac_pktin_learned.inc();
-            return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
-                elanUtils.setupMacFlows(elanInstance, interfaceInfo, elanInstance.getMacTimeout(),
+            elanManagerCounters.unknownSmacPktinLearned();
+            List<ListenableFuture<Void>> futures = new ArrayList<>();
+            futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
+                futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, operTx -> {
+                    elanUtils.setupMacFlows(elanInstance, interfaceInfo, elanInstance.getMacTimeout(),
                         macAddress, !isVlanOrFlatProviderIface, tx);
-                InstanceIdentifier<MacEntry> macEntryId =
+                    InstanceIdentifier<MacEntry> macEntryId =
                         ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
-                tx.put(LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry,
-                        WriteTransaction.CREATE_MISSING_PARENTS);
+                    operTx.put(macEntryId, newMacEntry, WriteTransaction.CREATE_MISSING_PARENTS);
+                }));
             }));
+            return futures;
         });
     }
 
     private void macMigrationFlowsCleanup(String interfaceName, ElanInstance elanInstance, MacEntry macEntry,
                                           boolean isVlanOrFlatProviderIface) {
-        if (macEntry != null && !macEntry.getInterface().equals(interfaceName)
+        if (macEntry != null && !Objects.equals(macEntry.getInterface(), interfaceName)
                 && !isVlanOrFlatProviderIface) {
             tryAndRemoveInvalidMacEntry(elanInstance.getElanInstanceName(), macEntry);
-            ElanManagerCounters.unknown_smac_pktin_flows_removed_for_relearned.inc();
+            elanManagerCounters.unknownSmacPktinFlowsRemovedForRelearned();
         }
     }
 
@@ -241,7 +250,7 @@ public class ElanPacketInHandler implements PacketProcessingListener {
                     macEntry.getMacAddress(), macEntry.getInterface());
             return;
         }
-        ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(
+        LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
             tx -> elanUtils.deleteMacFlows(elanInfo, oldInterfaceLport, macEntry, tx)), LOG,
             "Error deleting invalid MAC entry");
         elanL2GatewayUtils.removeMacsFromElanExternalDevices(elanInfo,