Bulk merge of l2gw changes
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / jobs / DpnDmacJob.java
index 4e1ca5e9e19b61ba42ff36670c35a6087de3802a..4d3547f57194e75c1ae4ea60cddc690dfca8af08 100644 (file)
@@ -9,14 +9,14 @@ package org.opendaylight.netvirt.elan.l2gw.jobs;
 
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.List;
 import java.util.Locale;
-import java.util.concurrent.Callable;
+import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
-import org.opendaylight.netvirt.elan.l2gw.utils.ElanRefUtil;
+import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
 import org.opendaylight.netvirt.elan.utils.ElanDmacUtils;
-import org.opendaylight.netvirt.elan.utils.ElanUtils;
+import org.opendaylight.netvirt.elan.utils.Scheduler;
 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
@@ -25,82 +25,97 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class DpnDmacJob implements Callable<List<? extends ListenableFuture<?>>> {
+public class DpnDmacJob extends DataStoreJob {
     private static final Logger LOG = LoggerFactory.getLogger(DpnDmacJob.class);
-
     private String elanName;
     private DpnInterfaces dpnInterfaces;
     private ElanL2GatewayUtils elanL2GatewayUtils;
+    private ElanClusterUtils elanClusterUtils;
+    private ElanInstanceCache elanInstanceCache;
     private ElanDmacUtils elanDmacUtils;
-    private final ElanRefUtil elanRefUtil;
+    private Scheduler scheduler;
+    private JobCoordinator jobCoordinator;
     private String nodeId;
     private boolean added;
-    protected String jobKey;
 
     public DpnDmacJob(String elanName,
                       DpnInterfaces dpnInterfaces,
                       String nodeId,
                       boolean added,
-                      ElanL2GatewayUtils elanL2GatewayUtils, ElanRefUtil elanRefUtil,
-                      ElanDmacUtils elanDmacUtils) {
-        this.jobKey = ElanUtils.getBcGroupUpdateKey(elanName);
+                      ElanL2GatewayUtils elanL2GatewayUtils, ElanClusterUtils elanClusterUtils,
+                      ElanInstanceCache elanInstanceCache, ElanDmacUtils elanDmacUtils,
+                      Scheduler scheduler, JobCoordinator jobCoordinator) {
+        super(elanName + ":l2gwdmac:" + dpnInterfaces.getDpId().toString() + ":" + nodeId,
+                scheduler, jobCoordinator);
         this.elanName = elanName;
         this.dpnInterfaces = dpnInterfaces;
         this.nodeId = nodeId;
         this.elanL2GatewayUtils = elanL2GatewayUtils;
-        this.elanRefUtil = elanRefUtil;
+        this.elanClusterUtils = elanClusterUtils;
+        this.elanInstanceCache = elanInstanceCache;
         this.elanDmacUtils = elanDmacUtils;
+        this.scheduler = scheduler;
+        this.jobCoordinator = jobCoordinator;
         this.added = added;
     }
 
     public void submit() {
-        elanRefUtil.getElanClusterUtils().runOnlyInOwnerNode(this.jobKey,"Dpn Dmac Job", this);
+        elanClusterUtils.runOnlyInOwnerNode(super.jobKey,"Dpn Dmac Job", this);
     }
 
     @Override
-    public List<ListenableFuture<Void>> call() throws Exception {
-        ElanInstance elan = elanRefUtil.getElanInstanceCache().get(elanName).orElse(null);
+    public List<? extends ListenableFuture<?>> call() throws Exception {
+        ElanInstance elan = elanInstanceCache.get(elanName).orElse(null);
         if (elan == null) {
-            LOG.error("failed.elan.not.found.{}", jobKey);
+            LOG.error("failed.elan.not.found. {}", jobKey);
             return null;
         }
-        List<ListenableFuture<Void>> result = new ArrayList<>();
         L2GatewayDevice device = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, nodeId);
+        List<ListenableFuture<Void>> fts = new ArrayList<>();
+        ElanL2GatewayUtils ucastUtils = elanL2GatewayUtils;
         if (added) {
-            result.addAll(elanL2GatewayUtils.installDmacFlowsOnDpn(dpnInterfaces.getDpId(), device, elan,
-                    dpnInterfaces.getInterfaces().get(0)));
+            fts = ucastUtils.installDmacFlowsOnDpn(dpnInterfaces.getDpId(), device, elan,
+                    dpnInterfaces.getInterfaces().get(0));
         } else {
-            Collection<MacAddress> localMacs = elanL2GatewayUtils.getL2GwDeviceLocalMacs(
-                    elan.getElanInstanceName(), device);
+            List<MacAddress> localMacs = ucastUtils.getL2GwDeviceLocalMacs(elan.getElanInstanceName(), device);
             if (localMacs != null && !localMacs.isEmpty()) {
                 for (MacAddress mac : localMacs) {
-                    result.addAll(elanDmacUtils.deleteDmacFlowsToExternalMac(elan.getElanTag().toJava(),
-                            dpnInterfaces.getDpId(), nodeId, mac.getValue().toLowerCase(Locale.getDefault())));
+                    fts.addAll(elanDmacUtils.deleteDmacFlowsToExternalMac(elan.getElanTag().longValue(),
+                        dpnInterfaces.getDpId(), nodeId, mac.getValue().toLowerCase(Locale.getDefault())));
                 }
             }
         }
-        return result;
+        if (!fts.isEmpty()) {
+            processResult(fts.get(0));
+        }
+        return null;
     }
 
     public static void uninstallDmacFromL2gws(String elanName,
                                               DpnInterfaces dpnInterfaces,
                                               ElanL2GatewayUtils elanL2GatewayUtils,
-                                              ElanRefUtil elanRefUtil,
-                                              ElanDmacUtils elanDmacUtils) {
-        ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(l2GatewayDevice -> {
-            new DpnDmacJob(elanName, dpnInterfaces, l2GatewayDevice.getHwvtepNodeId(), false, elanL2GatewayUtils,
-                    elanRefUtil, elanDmacUtils).submit();
+                                              ElanClusterUtils elanClusterUtils,
+                                              ElanInstanceCache elanInstanceCache,
+                                              ElanDmacUtils elanDmacUtils,
+                                              Scheduler scheduler,
+                                              JobCoordinator jobCoordinator) {
+        ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).keySet().forEach(nodeId -> {
+            new DpnDmacJob(elanName, dpnInterfaces, nodeId, false, elanL2GatewayUtils, elanClusterUtils,
+                    elanInstanceCache, elanDmacUtils, scheduler, jobCoordinator).submit();
         });
     }
 
     public static void installDmacFromL2gws(String elanName,
                                             DpnInterfaces dpnInterfaces,
                                             ElanL2GatewayUtils elanL2GatewayUtils,
-                                            ElanRefUtil elanRefUtil,
-                                            ElanDmacUtils elanDmacUtils) {
-        ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).forEach(l2GatewayDevice -> {
-            new DpnDmacJob(elanName, dpnInterfaces, l2GatewayDevice.getHwvtepNodeId(), true, elanL2GatewayUtils,
-                    elanRefUtil, elanDmacUtils).submit();
+                                            ElanClusterUtils elanClusterUtils,
+                                            ElanInstanceCache elanInstanceCache,
+                                            ElanDmacUtils elanDmacUtils,
+                                            Scheduler scheduler,
+                                            JobCoordinator jobCoordinator) {
+        ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).keySet().forEach(nodeId -> {
+            new DpnDmacJob(elanName, dpnInterfaces, nodeId, true, elanL2GatewayUtils, elanClusterUtils,
+                    elanInstanceCache, elanDmacUtils, scheduler, jobCoordinator).submit();
         });
     }
 }