Bulk merge of l2gw changes
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / ElanInstanceEntityOwnershipListener.java
index 296a275a68cbbe6a9a7cdb6b58b27659db323a16..157d5b3dbffe4770dbbc6f4028ae9f72d8221a40 100644 (file)
@@ -7,19 +7,23 @@
  */
 package org.opendaylight.netvirt.elan.l2gw.listeners;
 
-import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
+import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
+import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
 import static org.opendaylight.netvirt.elan.utils.ElanConstants.ELAN_EOS_DELAY;
 
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
 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.eos.binding.api.EntityOwnershipChange;
 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
@@ -29,7 +33,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.Elan
 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.ElanDpnInterfacesListKey;
 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.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfacesKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -44,17 +47,20 @@ public class ElanInstanceEntityOwnershipListener implements EntityOwnershipListe
     private final Scheduler scheduler;
     private final DataBroker dataBroker;
     volatile ScheduledFuture<?> ft;
+    private final ManagedNewTransactionRunner txRunner;
 
     @Inject
     public ElanInstanceEntityOwnershipListener(L2GatewayConnectionListener l2GatewayConnectionListener,
-                                               ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener,
-                                               Scheduler scheduler, DataBroker dataBroker,
-                                               EntityOwnershipService entityOwnershipService) {
+                                        ElanDpnInterfaceClusteredListener elanDpnInterfaceClusteredListener,
+                                        Scheduler scheduler, DataBroker dataBroker,
+                                        EntityOwnershipService entityOwnershipService) {
         this.l2GatewayConnectionListener = l2GatewayConnectionListener;
         this.elanDpnInterfaceClusteredListener = elanDpnInterfaceClusteredListener;
         this.scheduler = scheduler;
         this.dataBroker = dataBroker;
+        this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         entityOwnershipService.registerListener(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, this);
+        ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(this.dataBroker);
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
@@ -79,38 +85,52 @@ public class ElanInstanceEntityOwnershipListener implements EntityOwnershipListe
                     //check if i'm the owner
                     if (ownershipChange.getState().isOwner()) {
                         LOG.info("Elan Entity owner is: {}", ownershipChange);
-                        l2GatewayConnectionListener.loadL2GwConnectionCache();
+
+                        txRunner.callWithNewReadOnlyTransactionAndClose(CONFIGURATION, tx -> {
+                            l2GatewayConnectionListener.loadL2GwConnectionCache(tx);
+                        });
 
                         InstanceIdentifier<ElanDpnInterfaces> elanDpnInterfacesInstanceIdentifier = InstanceIdentifier
                                 .builder(ElanDpnInterfaces.class).build();
 
-                        Optional<ElanDpnInterfaces> optional = MDSALUtil.read(dataBroker, OPERATIONAL,
-                                elanDpnInterfacesInstanceIdentifier);
-                        if (optional.isPresent() && optional.get().getElanDpnInterfacesList() != null) {
-                            LOG.debug("Found elan dpn interfaces list");
-                            optional.get().nonnullElanDpnInterfacesList().values().forEach(elanDpnInterfacesList -> {
-                                Map<DpnInterfacesKey, DpnInterfaces> dpnInterfaces
-                                        = elanDpnInterfacesList.nonnullDpnInterfaces();
-                                InstanceIdentifier<ElanDpnInterfacesList> parentIid = InstanceIdentifier
-                                        .builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class,
-                                                new ElanDpnInterfacesListKey(elanDpnInterfacesList
+                        txRunner.callWithNewReadOnlyTransactionAndClose(OPERATIONAL, tx -> {
+                            Optional<ElanDpnInterfaces> optional = Optional.empty();
+                            try {
+                                optional = tx.read(elanDpnInterfacesInstanceIdentifier).get();
+                            } catch (ExecutionException | InterruptedException e) {
+                                LOG.error("Exception While reading ElanDpnInterfaces", e);
+                            }
+                            if (optional.isPresent()
+                                && optional.get().getElanDpnInterfacesList() != null) {
+                                LOG.debug("Found elan dpn interfaces list");
+                                optional.get().nonnullElanDpnInterfacesList().values()
+                                    .forEach(elanDpnInterfacesList -> {
+                                        List<DpnInterfaces> dpnInterfaces = new ArrayList<>(
+                                            elanDpnInterfacesList.nonnullDpnInterfaces().values());
+                                        InstanceIdentifier<ElanDpnInterfacesList> parentIid = InstanceIdentifier
+                                            .builder(ElanDpnInterfaces.class)
+                                            .child(ElanDpnInterfacesList.class,
+                                                new ElanDpnInterfacesListKey(
+                                                    elanDpnInterfacesList
                                                         .getElanInstanceName())).build();
-                                for (DpnInterfaces dpnInterface : dpnInterfaces.values()) {
-                                    LOG.debug("Found elan dpn interfaces");
-                                    elanDpnInterfaceClusteredListener.add(parentIid
+                                        for (DpnInterfaces dpnInterface : dpnInterfaces) {
+                                            LOG.debug("Found elan dpn interfaces");
+                                            elanDpnInterfaceClusteredListener.add(parentIid
                                                     .child(DpnInterfaces.class, dpnInterface.key()),
-                                            dpnInterface);
-                                }
-                            });
-                        }
+                                                dpnInterface);
+                                        }
+                                    });
+                            }
+                        });
+
                     } else {
                         LOG.info("Not the owner for Elan entity {}", ownershipChange);
                     }
                     ft = null;
-                } catch (ExecutionException | InterruptedException e) {
+                } catch (Exception e) {
                     LOG.error("Failed to read mdsal ", e);
                 }
             }, ELAN_EOS_DELAY, TimeUnit.MINUTES);
         }
     }
-}
\ No newline at end of file
+}