Remove GENIUS UTIL references in Elanmanager Module
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / ElanGroupListener.java
index bfd66da2e5add271776089df2a0ffac12ed5213b..7cc4f936489118a5eb436c7751dd1221dd509008 100644 (file)
@@ -7,20 +7,26 @@
  */
 package org.opendaylight.netvirt.elan.l2gw.listeners;
 
-import java.math.BigInteger;
+import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
+
 import java.util.List;
-import java.util.Set;
-import java.util.concurrent.ConcurrentMap;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.datastoreutils.AsyncClusteredDataTreeChangeListenerBase;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
 import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
 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.serviceutils.tools.listener.AbstractClusteredAsyncDataTreeChangeListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
@@ -29,50 +35,57 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class ElanGroupListener extends AsyncClusteredDataTreeChangeListenerBase<Group, ElanGroupListener> {
+public class ElanGroupListener extends AbstractClusteredAsyncDataTreeChangeListener<Group> {
 
     private static final Logger LOG = LoggerFactory.getLogger(ElanGroupListener.class);
-    private final DataBroker broker;
+    private final ManagedNewTransactionRunner txRunner;
     private final ElanClusterUtils elanClusterUtils;
     private final ElanUtils elanUtils;
     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
+    private final ElanInstanceCache elanInstanceCache;
 
     @Inject
     public ElanGroupListener(DataBroker db, ElanClusterUtils elanClusterUtils, ElanUtils elanUtils,
-            ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils) {
-        super(Group.class, ElanGroupListener.class);
-        broker = db;
+            ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils, ElanInstanceCache elanInstanceCache) {
+        super(db, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Nodes.class).child(Node.class)
+                .augmentation(FlowCapableNode.class).child(Group.class),
+                Executors.newListeningSingleThreadExecutor("ElanGroupListener", LOG));
+        this.txRunner = new ManagedNewTransactionRunnerImpl(db);
         this.elanClusterUtils = elanClusterUtils;
         this.elanUtils = elanUtils;
         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
-        registerListener(LogicalDatastoreType.CONFIGURATION, broker);
+        this.elanInstanceCache = elanInstanceCache;
         LOG.trace("ElanGroupListener registered");
     }
 
+    public void init() {
+        LOG.info("{} init", getClass().getSimpleName());
+    }
+
     @Override
-    protected InstanceIdentifier<Group> getWildCardPath() {
-        return InstanceIdentifier.create(Nodes.class).child(Node.class)
-                .augmentation(FlowCapableNode.class).child(Group.class);
+    @PreDestroy
+    public void close() {
+        super.close();
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Group> identifier, Group del) {
-        LOG.trace("received group removed {}", del.getKey().getGroupId());
+    public void remove(InstanceIdentifier<Group> identifier, Group del) {
+        LOG.trace("received group removed {}", del.key().getGroupId());
     }
 
 
+    @Nullable
     ElanInstance getElanInstanceFromGroupId(Group update) {
-        Set<String> elanNames = ElanUtils.getAllElanNames();
-        for (String elanName : elanNames) {
-            ElanInstance elanInstance = ElanUtils.getElanInstanceByName(broker, elanName);
+        for (ElanInstance elanInstance : elanInstanceCache.getAllPresent()) {
             if (elanInstance.getElanTag() != null) {
-                long elanTag = elanInstance.getElanTag();
+                long elanTag = elanInstance.getElanTag().longValue();
                 long elanBCGroupId = ElanUtils.getElanRemoteBroadCastGroupID(elanTag);
-                if (elanBCGroupId == update.getGroupId().getValue()) {
+                if (elanBCGroupId == update.getGroupId().getValue().longValue()) {
                     return elanInstance;
                 }
             }
@@ -80,40 +93,40 @@ public class ElanGroupListener extends AsyncClusteredDataTreeChangeListenerBase<
         return null;
     }
 
-    private BigInteger getDpnId(String node) {
+    @Nullable
+    private static Uint64 getDpnId(String node) {
         //openflow:1]
         String[] temp = node.split(":");
         if (temp.length == 2) {
-            return new BigInteger(temp[1]);
+            return Uint64.valueOf(temp[1]);
         }
         return null;
     }
 
     @Override
-    protected void update(InstanceIdentifier<Group> identifier, Group original, Group update) {
-        LOG.trace("received group updated {}", update.getKey().getGroupId());
-        final BigInteger dpnId = getDpnId(identifier.firstKeyOf(Node.class).getId().getValue());
+    public void update(InstanceIdentifier<Group> identifier, @Nullable Group original, Group update) {
+        LOG.trace("received group updated {}", update.key().getGroupId());
+        final Uint64 dpnId = getDpnId(identifier.firstKeyOf(Node.class).getId().getValue());
         if (dpnId == null) {
             return;
         }
 
         List<L2GatewayDevice> allDevices = ElanL2GwCacheUtils.getAllElanDevicesFromCache();
-        if (allDevices == null || allDevices.isEmpty()) {
-            LOG.trace("no elan devices present in cache {}", update.getKey().getGroupId());
+        if (allDevices.isEmpty()) {
+            LOG.trace("no elan devices present in cache {}", update.key().getGroupId());
             return;
         }
         int expectedElanFootprint = 0;
         final ElanInstance elanInstance = getElanInstanceFromGroupId(update);
         if (elanInstance == null) {
-            LOG.trace("no elan instance is null {}", update.getKey().getGroupId());
+            LOG.trace("no elan instance is null {}", update.key().getGroupId());
             return;
         }
 
-        ConcurrentMap<String, L2GatewayDevice> devices =
-                ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanInstance.getElanInstanceName());
-        if (devices == null || devices.isEmpty()) {
+        final int devicesSize = ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanInstance.getElanInstanceName()).size();
+        if (devicesSize == 0) {
             LOG.trace("no elan devices in elan cache {} {}", elanInstance.getElanInstanceName(),
-                    update.getKey().getGroupId());
+                    update.key().getGroupId());
             return;
         }
         boolean updateGroup = false;
@@ -123,45 +136,42 @@ public class ElanGroupListener extends AsyncClusteredDataTreeChangeListenerBase<
         } else {
             updateGroup = true;
         }
-        expectedElanFootprint += devices.size();
+        expectedElanFootprint += devicesSize;
         if (update.getBuckets() != null && update.getBuckets().getBucket() != null) {
             if (update.getBuckets().getBucket().size() != expectedElanFootprint) {
                 updateGroup = true;
             } else {
                 LOG.trace("no of buckets matched perfectly {} {}", elanInstance.getElanInstanceName(),
-                        update.getKey().getGroupId());
+                        update.key().getGroupId());
             }
         }
         if (updateGroup) {
             List<Bucket> bucketList = elanL2GatewayMulticastUtils.getRemoteBCGroupBuckets(elanInstance, null, dpnId, 0,
-                    elanInstance.getElanTag());
+                    elanInstance.getElanTag().toJava());
             expectedElanFootprint--;//remove local bcgroup bucket
             if (bucketList.size() != expectedElanFootprint) {
                 //no point in retrying if not able to meet expected foot print
                 return;
             }
             LOG.trace("no of buckets mismatched {} {}", elanInstance.getElanInstanceName(),
-                    update.getKey().getGroupId());
+                    update.key().getGroupId());
             elanClusterUtils.runOnlyInOwnerNode(elanInstance.getElanInstanceName(), "updating broadcast group", () -> {
-                elanL2GatewayMulticastUtils.setupElanBroadcastGroups(elanInstance, dpnId);
+                LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                    confTx -> elanL2GatewayMulticastUtils.setupElanBroadcastGroups(elanInstance, dpnId, confTx)),
+                    LOG, "Error setting up ELAN BGs");
                 return null;
             });
         } else {
             LOG.trace("no buckets in the update {} {}", elanInstance.getElanInstanceName(),
-                    update.getKey().getGroupId());
+                    update.key().getGroupId());
         }
     }
 
     @Override
-    protected void add(InstanceIdentifier<Group> identifier, Group added) {
-        LOG.trace("received group add {}", added.getKey().getGroupId());
+    public void add(InstanceIdentifier<Group> identifier, Group added) {
+        LOG.trace("received group add {}", added.key().getGroupId());
         update(identifier, null/*original*/, added);
     }
-
-    @Override
-    protected ElanGroupListener getDataTreeChangeListener() {
-        return this;
-    }
 }