Use Java declarations instead of Google Collections
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepLocalUcastMacListener.java
index af10c65e6a1304de038144abcd85bcde00ccc815..4a2197e4f74118808ae770fbe6fc70335ce09057 100644 (file)
@@ -7,21 +7,18 @@
  */
 package org.opendaylight.netvirt.elan.l2gw.listeners;
 
-import com.google.common.collect.Lists;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataChangeListener;
+import java.util.Collections;
 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.data.AsyncDataBroker.DataChangeScope;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase;
+import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
+import org.opendaylight.netvirt.elan.utils.ElanUtils;
 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -30,121 +27,97 @@ import org.slf4j.LoggerFactory;
  * A listener for Ucast MAC entries that are added/removed to/from an External
  * Device (e.g., TOR).
  *
- * When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must
+ * <p>When a Ucast MAC addr appears in the hwvtep's operational DS, that MAC must
  * be populated in DMAC tables in all Elan participating DPNs. ELAN is selected
  * according to field 'tunnel_key' of the Logical Switch to which the new MAC
  * belongs.
- *
  */
 public class HwvtepLocalUcastMacListener extends
-        AsyncClusteredDataChangeListenerBase<LocalUcastMacs, HwvtepLocalUcastMacListener> implements AutoCloseable {
+        HwvtepClusteredDataTreeChangeListener<LocalUcastMacs, HwvtepLocalUcastMacListener> implements AutoCloseable {
 
-    private DataBroker broker;
-    private ListenerRegistration<DataChangeListener> lstnerRegistration;
+    private static final Logger LOG = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
 
-    private static final Logger logger = LoggerFactory.getLogger(HwvtepLocalUcastMacListener.class);
+    private final DataBroker broker;
+    private final ElanL2GatewayUtils elanL2GatewayUtils;
 
-    public HwvtepLocalUcastMacListener(DataBroker broker) {
+    public HwvtepLocalUcastMacListener(DataBroker broker, ElanUtils elanUtils) {
         super(LocalUcastMacs.class, HwvtepLocalUcastMacListener.class);
 
         this.broker = broker;
-        registerListener();
+        this.elanL2GatewayUtils = elanUtils.getElanL2GatewayUtils();
     }
 
-    protected void registerListener() {
-        try {
-            lstnerRegistration = this.broker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
-                    HwvtepUtils.getWildCardPathForLocalUcastMacs(), this, DataChangeScope.SUBTREE);
-        } catch (final Exception e) {
-            logger.error("Hwvtep LocalUcasMacs DataChange listener registration failed !", e);
-            throw new IllegalStateException("Hwvtep LocalUcasMacs DataChange listener registration failed .", e);
-        }
+    public void init() {
+        registerListener(LogicalDatastoreType.OPERATIONAL, broker);
     }
 
     @Override
-    public void close() throws Exception {
-        if (lstnerRegistration != null) {
-            try {
-                lstnerRegistration.close();
-            } catch (final Exception e) {
-                logger.error("Error when cleaning up DataChangeListener.", e);
-            }
-            lstnerRegistration = null;
-        }
-    }
-
-    @Override
-    protected void remove(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
+    protected void removed(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macRemoved) {
         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
         String macAddress = macRemoved.getMacEntryKey().getValue();
 
-        logger.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
+        LOG.trace("LocalUcastMacs {} removed from {}", macAddress, hwvtepNodeId);
 
-        ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macRemoved);
+        ElanInstance elan = elanL2GatewayUtils.getElanInstanceForUcastLocalMac(macRemoved);
         if (elan == null) {
-            logger.warn("Could not find ELAN for mac {} being deleted", macAddress);
+            LOG.warn("Could not find ELAN for mac {} being deleted", macAddress);
             return;
         }
 
         String elanName = elan.getElanInstanceName();
         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
         if (elanL2GwDevice == null) {
-            logger.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
+            LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
             return;
         }
 
         // Remove MAC from cache
         elanL2GwDevice.removeUcastLocalMac(macRemoved);
 
-        ElanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elan, elanL2GwDevice,
-                Lists.newArrayList(macRemoved.getMacEntryKey()));
+        elanL2GatewayUtils.unInstallL2GwUcastMacFromElan(elan, elanL2GwDevice,
+                Collections.singletonList(macRemoved.getMacEntryKey()));
     }
 
     @Override
-    protected void update(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
+    protected void updated(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs original,
             LocalUcastMacs update) {
         // TODO (eperefr) what can change here?
 
     }
 
     @Override
-    protected void add(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
+    protected void added(InstanceIdentifier<LocalUcastMacs> identifier, LocalUcastMacs macAdded) {
         String hwvtepNodeId = identifier.firstKeyOf(Node.class).getNodeId().getValue();
         String macAddress = macAdded.getMacEntryKey().getValue();
 
-        logger.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
+        LOG.trace("LocalUcastMacs {} added to {}", macAddress, hwvtepNodeId);
 
-        ElanInstance elan = ElanL2GatewayUtils.getElanInstanceForUcastLocalMac(macAdded);
+        ElanInstance elan = elanL2GatewayUtils.getElanInstanceForUcastLocalMac(macAdded);
         if (elan == null) {
-            logger.warn("Could not find ELAN for mac {} being added", macAddress);
+            LOG.warn("Could not find ELAN for mac {} being added", macAddress);
             return;
         }
 
         String elanName = elan.getElanInstanceName();
         L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
         if (elanL2GwDevice == null) {
-            logger.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
+            LOG.warn("Could not find L2GatewayDevice for ELAN: {}, nodeID:{} from cache", elanName, hwvtepNodeId);
             return;
         }
 
         // Cache MAC for furthur processing later
         elanL2GwDevice.addUcastLocalMac(macAdded);
 
-        ElanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, null);
+        elanL2GatewayUtils.installL2GwUcastMacInElan(elan, elanL2GwDevice, macAddress, null);
     }
 
     @Override
     protected InstanceIdentifier<LocalUcastMacs> getWildCardPath() {
-        return InstanceIdentifier.create(LocalUcastMacs.class);
-    }
-
-    @Override
-    protected ClusteredDataChangeListener getDataChangeListener() {
-        return HwvtepLocalUcastMacListener.this;
+        return HwvtepUtils.getWildCardPathForLocalUcastMacs();
     }
 
     @Override
-    protected DataChangeScope getDataChangeScope() {
-        return DataChangeScope.BASE;
+    protected HwvtepLocalUcastMacListener getDataTreeChangeListener() {
+        return this;
     }
 }