de-static-ify ElanUtils field in ElanServiceProvider
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / internal / ElanDpnInterfaceClusteredListener.java
index 322b54450d8eff96e79451e1bb2a54798f86a7e6..8a715022a6fb1bdf054a0f79d3b3c10a14d27a87 100644 (file)
@@ -7,59 +7,53 @@
  */
 package org.opendaylight.netvirt.elan.internal;
 
-import java.util.List;
-import java.util.concurrent.Callable;
-
+import com.google.common.collect.Lists;
 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
-import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
+import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
+import org.opendaylight.netvirt.elan.utils.ElanUtils;
 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
-import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.ListenableFuture;
-
 public class ElanDpnInterfaceClusteredListener
-        extends AsyncClusteredDataChangeListenerBase<DpnInterfaces, ElanDpnInterfaceClusteredListener>
-        implements AutoCloseable {
-    private DataBroker broker;
-    private ElanInterfaceManager elanInterfaceManager;
-    private ListenerRegistration<DataChangeListener> listenerRegistration;
+    extends AsyncClusteredDataChangeListenerBase<DpnInterfaces, ElanDpnInterfaceClusteredListener>
+    implements AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(ElanDpnInterfaceClusteredListener.class);
 
-    public ElanDpnInterfaceClusteredListener(final DataBroker db, final ElanInterfaceManager ifManager) {
+    private final DataBroker broker;
+    private final EntityOwnershipService entityOwnershipService;
+    private final ElanL2GatewayUtils elanL2GatewayUtils;
+    private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
+
+    public ElanDpnInterfaceClusteredListener(DataBroker broker, EntityOwnershipService entityOwnershipService,
+                                             ElanUtils elanUtils) {
         super(DpnInterfaces.class, ElanDpnInterfaceClusteredListener.class);
-        broker = db;
-        elanInterfaceManager = ifManager;
-        registerListener(db);
+        this.broker = broker;
+        this.entityOwnershipService = entityOwnershipService;
+        this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
+        this.elanL2GatewayMulticastUtils = elanUtils.getElanL2GatewayMulticastUtils();
     }
 
-    private void registerListener(final DataBroker db) {
-        try {
-            listenerRegistration = broker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
-                    getWildCardPath(), ElanDpnInterfaceClusteredListener.this, AsyncDataBroker.DataChangeScope.BASE);
-        } catch (final Exception e) {
-            LOG.error("DpnInterfaces DataChange listener registration fail!", e);
-        }
+    public void init() {
+        registerListener(LogicalDatastoreType.OPERATIONAL, this.broker);
     }
 
     @Override
     public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
-                .child(DpnInterfaces.class).build();
+            .child(DpnInterfaces.class).build();
     }
 
     @Override
@@ -76,17 +70,12 @@ public class ElanDpnInterfaceClusteredListener
         final String elanName = getElanName(id);
         if (ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).isEmpty()) {
             LOG.debug("dpnInterface updation, no external l2 devices to update for elan {} with Dp Id:", elanName,
-                    dpnInterfaces.getDpId());
+                dpnInterfaces.getDpId());
             return;
         }
-        ElanClusterUtils.runOnlyInLeaderNode(elanName, "updating mcast mac upon tunnel event",
-                new Callable<List<ListenableFuture<Void>>>() {
-                    @Override
-                    public List<ListenableFuture<Void>> call() throws Exception {
-                        return Lists.newArrayList(
-                                ElanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName));
-                    }
-                });
+        ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, elanName, "updating mcast mac upon tunnel event",
+            () -> Lists.newArrayList(
+                elanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName)));
     }
 
     @Override
@@ -94,30 +83,27 @@ public class ElanDpnInterfaceClusteredListener
         // this is the last dpn interface on this elan
         final String elanName = getElanName(identifier);
         LOG.debug("Received ElanDpnInterface removed for for elan {} with Dp Id ", elanName,
-                dpnInterfaces.getDpId());
+            dpnInterfaces.getDpId());
 
         if (ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).isEmpty()) {
             LOG.debug("dpnInterface removed, no external l2 devices to update for elan {} with Dp Id:", elanName,
-                    dpnInterfaces.getDpId());
+                dpnInterfaces.getDpId());
             return;
         }
-        ElanClusterUtils.runOnlyInLeaderNode(elanName, "handling ElanDpnInterface removed",
-                new Callable<List<ListenableFuture<Void>>>() {
-                    @Override
-                    public List<ListenableFuture<Void>> call() throws Exception {
-                        // deleting Elan L2Gw Devices UcastLocalMacs From Dpn
-                        ElanL2GatewayUtils.deleteElanL2GwDevicesUcastLocalMacsFromDpn(elanName,
-                                dpnInterfaces.getDpId());
-                        // updating remote mcast mac on l2gw devices
-                        return Lists.newArrayList(
-                                ElanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName));
-                    }
-                });
+        ElanClusterUtils.runOnlyInLeaderNode(entityOwnershipService, elanName, "handling ElanDpnInterface removed",
+            () -> {
+                // deleting Elan L2Gw Devices UcastLocalMacs From Dpn
+                elanL2GatewayUtils.deleteElanL2GwDevicesUcastLocalMacsFromDpn(elanName,
+                    dpnInterfaces.getDpId());
+                // updating remote mcast mac on l2gw devices
+                return Lists.newArrayList(
+                    elanL2GatewayMulticastUtils.updateRemoteMcastMacOnElanL2GwDevices(elanName));
+            });
     }
 
     @Override
     protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
-            final DpnInterfaces dpnInterfaces) {
+                          final DpnInterfaces dpnInterfaces) {
         LOG.debug("dpninterfaces update fired new size {}", dpnInterfaces.getInterfaces().size());
         if (dpnInterfaces.getInterfaces().size() == 0) {
             LOG.debug("dpninterfaces last dpn interface on this elan {} ", dpnInterfaces.getKey());
@@ -130,17 +116,14 @@ public class ElanDpnInterfaceClusteredListener
     protected void add(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
         if (dpnInterfaces.getInterfaces().size() == 1) {
             LOG.debug("dpninterfaces first dpn interface on this elan {} {} ", dpnInterfaces.getKey(),
-                    dpnInterfaces.getInterfaces().get(0));
+                dpnInterfaces.getInterfaces().get(0));
             // this is the first dpn interface on this elan
             handleUpdate(identifier, dpnInterfaces);
         }
     }
 
-    /**
-     * @param identifier
-     * @return
-     */
     private String getElanName(InstanceIdentifier<DpnInterfaces> identifier) {
         return identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
     }
+
 }