Use SingleTransactionDataBroker to read instead of VpnUtil read() - Part 2 18/72818/3
authoreupakir <kiran.n.upadhyaya@ericsson.com>
Sun, 10 Jun 2018 05:27:56 +0000 (10:57 +0530)
committerKiran Upadhyaya <kiran.n.upadhyaya@ericsson.com>
Mon, 11 Jun 2018 10:00:30 +0000 (10:00 +0000)
of 5

Also replaces MDSALUtil.syncWrite() with SingleTransactionDataBroker
writes in the Vpn engine.
Uses ManagedNewTransactionRunner whereever applicable
Restricts VpnUtil.read() accessibility to within the class

Change-Id: Iea1389240d2d56f19c000adba73b34ec2b54ad4d
Signed-off-by: eupakir <kiran.n.upadhyaya@ericsson.com>
vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/SubnetRouteInterfaceStateChangeListener.java
vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/SubnetRoutePacketInHandler.java
vpnmanager/impl/src/main/java/org/opendaylight/netvirt/vpnmanager/SubnetmapChangeListener.java

index 3437c85146501bb3a0a1f58e970d6266e1c76187..cc1adb62f6bf80301e98711cb6d87e0758743784 100644 (file)
@@ -18,7 +18,9 @@ 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.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronUtils;
 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
@@ -76,53 +78,50 @@ public class SubnetRouteInterfaceStateChangeListener extends AsyncDataTreeChange
         return SubnetRouteInterfaceStateChangeListener.this;
     }
 
-    // TODO Clean up the exception handling
-    @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     protected void add(InstanceIdentifier<Interface> identifier, Interface intrf) {
         LOG.trace("{} add: Received interface {} up event", LOGGING_PREFIX, intrf);
-        try {
-            if (L2vlan.class.equals(intrf.getType())) {
-                LOG.trace("SubnetRouteInterfaceListener add: Received interface {} up event", intrf);
-                if (intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
-                    List<Uuid> subnetIdList = getSubnetId(intrf);
-                    if (subnetIdList.isEmpty()) {
-                        LOG.trace("SubnetRouteInterfaceListener add: Port {} doesn't exist in configDS",
-                                intrf.getName());
-                        return;
-                    }
-                    for (Uuid subnetId : subnetIdList) {
-                        jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId,
-                            () -> {
-                                String interfaceName = intrf.getName();
-                                BigInteger dpnId = BigInteger.ZERO;
-                                LOG.info("{} add: Received port UP event for interface {} subnetId {}",
-                                        LOGGING_PREFIX, interfaceName, subnetId);
-                                try {
-                                    dpnId = InterfaceUtils.getDpIdFromInterface(intrf);
-                                } catch (Exception e) {
-                                    LOG.error("{} add: Unable to obtain dpnId for interface {} in subnet {},"
-                                            + " subnetroute inclusion for this interface failed",
-                                            LOGGING_PREFIX, interfaceName, subnetId, e);
-                                }
-                                InstanceIdentifier<VpnInterface> id = VpnUtil
-                                        .getVpnInterfaceIdentifier(interfaceName);
-                                Optional<VpnInterface> cfgVpnInterface = VpnUtil.read(dataBroker,
-                                     LogicalDatastoreType.CONFIGURATION, id);
-                                List<ListenableFuture<Void>> futures = new ArrayList<>();
+        if (L2vlan.class.equals(intrf.getType())) {
+            LOG.trace("SubnetRouteInterfaceListener add: Received interface {} up event", intrf);
+            if (intrf.getOperStatus().equals(Interface.OperStatus.Up)) {
+                List<Uuid> subnetIdList = getSubnetId(intrf);
+                if (subnetIdList.isEmpty()) {
+                    LOG.trace("SubnetRouteInterfaceListener add: Port {} doesn't exist in configDS",
+                            intrf.getName());
+                    return;
+                }
+                for (Uuid subnetId : subnetIdList) {
+                    jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId,
+                        () -> {
+                            String interfaceName = intrf.getName();
+                            BigInteger dpnId = BigInteger.ZERO;
+                            LOG.info("{} add: Received port UP event for interface {} subnetId {}",
+                                    LOGGING_PREFIX, interfaceName, subnetId);
+                            try {
+                                dpnId = InterfaceUtils.getDpIdFromInterface(intrf);
+                            } catch (NullPointerException e) {
+                                LOG.error("{} add: Unable to obtain dpnId for interface {} in subnet {},"
+                                                + " subnetroute inclusion for this interface failed", LOGGING_PREFIX,
+                                        interfaceName, subnetId, e);
+                            }
+                            List<ListenableFuture<Void>> futures = new ArrayList<>();
+                            try {
+                                InstanceIdentifier<VpnInterface> id = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
+                                Optional<VpnInterface> cfgVpnInterface = SingleTransactionDataBroker.syncReadOptional(
+                                        dataBroker, LogicalDatastoreType.CONFIGURATION, id);
                                 if (!cfgVpnInterface.isPresent()) {
                                     return futures;
                                 }
                                 vpnSubnetRouteHandler.onInterfaceUp(dpnId, intrf.getName(), subnetId);
-                                return futures;
-                            });
-                    }
+                                LOG.info("{} add: Processed interface {} up event", LOGGING_PREFIX, intrf.getName());
+                            } catch (ReadFailedException e) {
+                                LOG.error("add: Failed to read data store for interface {} dpn {}", interfaceName,
+                                        dpnId);
+                            }
+                            return futures;
+                        });
                 }
             }
-            LOG.info("{} add: Processed interface {} up event", LOGGING_PREFIX, intrf.getName());
-        } catch (Exception e) {
-            LOG.error("{} add: Exception observed in handling addition for VPN Interface {}.", LOGGING_PREFIX,
-                intrf.getName(), e);
         }
     }
 
@@ -130,18 +129,20 @@ public class SubnetRouteInterfaceStateChangeListener extends AsyncDataTreeChange
     @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     protected void remove(InstanceIdentifier<Interface> identifier, Interface intrf) {
-        try {
-            if (L2vlan.class.equals(intrf.getType())) {
-                LOG.trace("SubnetRouteInterfaceListener remove: Received interface {} down event", intrf);
-                List<Uuid> subnetIdList = getSubnetId(intrf);
-                if (subnetIdList.isEmpty()) {
-                    LOG.trace("SubnetRouteInterfaceListener remove: Port {} doesn't exist in configDS",
-                            intrf.getName());
-                    return;
-                }
-                for (Uuid subnetId : subnetIdList) {
-                    jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId,
-                        () -> {
+        if (L2vlan.class.equals(intrf.getType())) {
+            LOG.trace("SubnetRouteInterfaceListener remove: Received interface {} down event", intrf);
+            List<Uuid> subnetIdList = getSubnetId(intrf);
+            if (subnetIdList.isEmpty()) {
+                LOG.trace("SubnetRouteInterfaceListener remove: Port {} doesn't exist in configDS",
+                        intrf.getName());
+                return;
+            }
+            LOG.trace("{} remove: Processing interface {} down event in ", LOGGING_PREFIX, intrf.getName());
+            for (Uuid subnetId : subnetIdList) {
+                jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId,
+                    () -> {
+                        List<ListenableFuture<Void>> futures = new ArrayList<>();
+                        try {
                             String interfaceName = intrf.getName();
                             BigInteger dpnId = BigInteger.ZERO;
                             LOG.info("{} remove: Received port DOWN event for interface {} in subnet {} ",
@@ -155,20 +156,19 @@ public class SubnetRouteInterfaceStateChangeListener extends AsyncDataTreeChange
                             }
                             InstanceIdentifier<VpnInterface> id = VpnUtil
                                     .getVpnInterfaceIdentifier(interfaceName);
-                            Optional<VpnInterface> cfgVpnInterface = VpnUtil.read(dataBroker,
-                                     LogicalDatastoreType.CONFIGURATION, id);
-                            List<ListenableFuture<Void>> futures = new ArrayList<>();
+                            Optional<VpnInterface> cfgVpnInterface = SingleTransactionDataBroker.syncReadOptional(
+                                    dataBroker, LogicalDatastoreType.CONFIGURATION, id);
                             if (!cfgVpnInterface.isPresent()) {
                                 return futures;
                             }
                             boolean interfaceDownEligible = false;
                             for (VpnInstanceNames vpnInterfaceVpnInstance
-                                 : cfgVpnInterface.get().getVpnInstanceNames()) {
+                                    : cfgVpnInterface.get().getVpnInstanceNames()) {
                                 String vpnName = vpnInterfaceVpnInstance.getVpnName();
                                 InstanceIdentifier<VpnInterfaceOpDataEntry> idOper = VpnUtil
-                                       .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
-                                Optional<VpnInterfaceOpDataEntry> optVpnInterface = VpnUtil.read(dataBroker,
-                                       LogicalDatastoreType.OPERATIONAL, idOper);
+                                        .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
+                                Optional<VpnInterfaceOpDataEntry> optVpnInterface = SingleTransactionDataBroker
+                                        .syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, idOper);
                                 if (optVpnInterface.isPresent()) {
                                     BigInteger dpnIdLocal = dpnId;
                                     if (dpnIdLocal.equals(BigInteger.ZERO)) {
@@ -183,60 +183,58 @@ public class SubnetRouteInterfaceStateChangeListener extends AsyncDataTreeChange
                             if (interfaceDownEligible) {
                                 vpnSubnetRouteHandler.onInterfaceDown(dpnId, intrf.getName(), subnetId);
                             }
-                            return futures;
-                        });
-                }
+                            LOG.info("{} remove: Processed interface {} down event in ", LOGGING_PREFIX,
+                                    intrf.getName());
+                        } catch (ReadFailedException e) {
+                            LOG.error("{} remove: Failed to read data store for {}", LOGGING_PREFIX, intrf.getName());
+                        }
+                        return futures;
+                    });
             }
-            LOG.info("{} remove: Processed interface {} down event in ", LOGGING_PREFIX, intrf.getName());
-        } catch (Exception e) {
-            LOG.error("{} remove: Exception observed in handling deletion of VPN Interface {}.", LOGGING_PREFIX,
-                intrf.getName(), e);
         }
     }
 
-    // TODO Clean up the exception handling
-    @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     protected void update(InstanceIdentifier<Interface> identifier,
         Interface original, Interface update) {
-        try {
-            String interfaceName = update.getName();
-            if (L2vlan.class.equals(update.getType())) {
-                LOG.trace("{} update: Operation Interface update event - Old: {}, New: {}", LOGGING_PREFIX,
+        String interfaceName = update.getName();
+        if (L2vlan.class.equals(update.getType())) {
+            LOG.trace("{} update: Operation Interface update event - Old: {}, New: {}", LOGGING_PREFIX,
                     original, update);
-                List<Uuid> subnetIdList = getSubnetId(update);
-                if (subnetIdList.isEmpty()) {
-                    LOG.error("SubnetRouteInterfaceListener update: Port {} doesn't exist in configDS",
-                            update.getName());
-                    return;
-                }
-                for (Uuid subnetId : subnetIdList) {
-                    jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId,
-                        () -> {
-                            List<ListenableFuture<Void>> futures = new ArrayList<>();
-                            BigInteger dpnId = BigInteger.ZERO;
-                            try {
-                                dpnId = InterfaceUtils.getDpIdFromInterface(update);
-                            } catch (Exception e) {
-                                LOG.error("{} remove: Unable to retrieve dpnId for interface {} in subnet  {}. "
-                                                + "Fetching from vpn interface itself",
-                                        LOGGING_PREFIX, update.getName(), subnetId, e);
-                            }
+            List<Uuid> subnetIdList = getSubnetId(update);
+            if (subnetIdList.isEmpty()) {
+                LOG.error("SubnetRouteInterfaceListener update: Port {} doesn't exist in configDS",
+                        update.getName());
+                return;
+            }
+            for (Uuid subnetId : subnetIdList) {
+                jobCoordinator.enqueueJob("SUBNETROUTE-" + subnetId,
+                    () -> {
+                        BigInteger dpnId = BigInteger.ZERO;
+                        try {
+                            dpnId = InterfaceUtils.getDpIdFromInterface(update);
+                        } catch (NullPointerException e) {
+                            LOG.error("{} remove: Unable to retrieve dpnId for interface {} in subnet  {}. "
+                                            + "Fetching from vpn interface itself", LOGGING_PREFIX, update.getName(),
+                                    subnetId, e);
+                        }
+                        List<ListenableFuture<Void>> futures = new ArrayList<>();
+                        try {
                             InstanceIdentifier<VpnInterface> id = VpnUtil
                                     .getVpnInterfaceIdentifier(interfaceName);
-                            Optional<VpnInterface> cfgVpnInterface = VpnUtil.read(dataBroker,
-                                     LogicalDatastoreType.CONFIGURATION, id);
+                            Optional<VpnInterface> cfgVpnInterface = SingleTransactionDataBroker.syncReadOptional(
+                                    dataBroker, LogicalDatastoreType.CONFIGURATION, id);
                             if (!cfgVpnInterface.isPresent()) {
                                 return futures;
                             }
                             boolean interfaceChangeEligible = false;
                             for (VpnInstanceNames vpnInterfaceVpnInstance
-                                  : cfgVpnInterface.get().getVpnInstanceNames()) {
+                                    : cfgVpnInterface.get().getVpnInstanceNames()) {
                                 String vpnName = vpnInterfaceVpnInstance.getVpnName();
                                 InstanceIdentifier<VpnInterfaceOpDataEntry> idOper = VpnUtil
-                                       .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
-                                Optional<VpnInterfaceOpDataEntry> optVpnInterface = VpnUtil.read(dataBroker,
-                                       LogicalDatastoreType.OPERATIONAL, idOper);
+                                        .getVpnInterfaceOpDataEntryIdentifier(interfaceName, vpnName);
+                                Optional<VpnInterfaceOpDataEntry> optVpnInterface = SingleTransactionDataBroker
+                                        .syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, idOper);
                                 if (optVpnInterface.isPresent()) {
                                     BigInteger dpnIdLocal = dpnId;
                                     if (dpnIdLocal.equals(BigInteger.ZERO)) {
@@ -255,26 +253,26 @@ public class SubnetRouteInterfaceStateChangeListener extends AsyncDataTreeChange
                                     vpnSubnetRouteHandler.onInterfaceUp(dpnId, update.getName(), subnetId);
                                 } else if (update.getOperStatus().equals(Interface.OperStatus.Down)
                                         || update.getOperStatus().equals(Interface.OperStatus.Unknown)) {
-                                    /*
-                                     * If the interface went down voluntarily (or) if the interface is not
-                                     * reachable from control-path involuntarily, trigger subnetRoute election
-                                     */
+                                /*
+                                 * If the interface went down voluntarily (or) if the interface is not
+                                 * reachable from control-path involuntarily, trigger subnetRoute election
+                                 */
                                     LOG.info("{} update: Received port {} event for interface {} in subnet {} ",
                                             LOGGING_PREFIX, update.getOperStatus()
-                                            .equals(Interface.OperStatus.Unknown)
-                                            ? "UNKNOWN" : "DOWN", update.getName(), subnetId);
+                                                    .equals(Interface.OperStatus.Unknown)
+                                                    ? "UNKNOWN" : "DOWN", update.getName(), subnetId);
                                     vpnSubnetRouteHandler.onInterfaceDown(dpnId, update.getName(), subnetId);
                                 }
                             }
-                            return futures;
-                        });
-                }
+                        } catch (ReadFailedException e) {
+                            LOG.error("update: Failed to read data store for interface {} dpn {}", interfaceName,
+                                    dpnId);
+                        }
+                        return futures;
+                    });
             }
-            LOG.info("{} update: Processed Interface {} update event", LOGGING_PREFIX, update.getName());
-        } catch (Exception e) {
-            LOG.error("{} update: Exception observed in handling deletion of VPNInterface {}", LOGGING_PREFIX,
-                    update.getName(), e);
         }
+        LOG.info("{} update: Processed Interface {} update event", LOGGING_PREFIX, update.getName());
     }
 
     @Nonnull
index e81e8db2b34a6d33bd8adc83335c5bab7a345938..222277b6ccc0eeca838f4d2bba8e5a64fbcf69be 100644 (file)
@@ -18,6 +18,8 @@ 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.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
 import org.opendaylight.genius.mdsalutil.NWUtil;
@@ -110,8 +112,8 @@ public class SubnetRoutePacketInHandler implements PacketProcessingListener {
                     LOG.info("{} onPacketReceived: Processing IPv4 Packet received with Source IP {} and Target IP {}"
                             + " and vpnId {}", LOGGING_PREFIX, srcIpStr, dstIpStr, vpnId);
 
-                    Optional<VpnIds> vpnIdsOptional = VpnUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
-                            VpnUtil.getVpnIdToVpnInstanceIdentifier(vpnId));
+                    Optional<VpnIds> vpnIdsOptional = SingleTransactionDataBroker.syncReadOptional(dataBroker,
+                            LogicalDatastoreType.CONFIGURATION, VpnUtil.getVpnIdToVpnInstanceIdentifier(vpnId));
 
                     if (!vpnIdsOptional.isPresent()) {
                         // Donot trigger subnetroute logic for packets from
@@ -161,6 +163,9 @@ public class SubnetRoutePacketInHandler implements PacketProcessingListener {
                 // Failed to handle packet
                 VpnManagerCounters.subnet_route_packet_failed.inc();
                 LOG.error("{} onPacketReceived: Failed to handle subnetroute packet.", LOGGING_PREFIX, ex);
+            } catch (ReadFailedException e) {
+                VpnManagerCounters.subnet_route_packet_failed.inc();
+                LOG.error("{} onPacketReceived: Failed to read data-store.", LOGGING_PREFIX, e);
             }
             return;
         }
@@ -237,39 +242,45 @@ public class SubnetRoutePacketInHandler implements PacketProcessingListener {
 
     private void handlePacketToInternalNetwork(byte[] dstIp, String dstIpStr, int destinationAddress, long elanTag)
             throws UnknownHostException {
+        try {
+            SubnetOpDataEntry targetSubnetForPacketOut =
+                    getTargetSubnetForPacketOut(dataBroker, elanTag, destinationAddress);
 
-        SubnetOpDataEntry targetSubnetForPacketOut =
-                getTargetSubnetForPacketOut(dataBroker, elanTag, destinationAddress);
+            if (targetSubnetForPacketOut == null) {
+                LOG.debug("Couldn't find matching subnet for elan tag {} and destination ip {}", elanTag, dstIpStr);
+                VpnManagerCounters.subnet_route_packet_failed.inc();
+                return;
+            }
 
-        if (targetSubnetForPacketOut == null) {
-            LOG.debug("Couldn't find matching subnet for elan tag {} and destination ip {}", elanTag, dstIpStr);
-            VpnManagerCounters.subnet_route_packet_failed.inc();
-            return;
-        }
+            Optional<Subnetmap> subnetMap = SingleTransactionDataBroker.syncReadOptional(dataBroker,
+                    LogicalDatastoreType.CONFIGURATION,
+                    VpnUtil.buildSubnetmapIdentifier(targetSubnetForPacketOut.getSubnetId()));
+            if (!subnetMap.isPresent()) {
+                LOG.debug("Couldn't find subnet map for subnet {}", targetSubnetForPacketOut.getSubnetId());
+                VpnManagerCounters.subnet_route_packet_failed.inc();
+                return;
+            }
 
-        Optional<Subnetmap> subnetMap = VpnUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
-                VpnUtil.buildSubnetmapIdentifier(targetSubnetForPacketOut.getSubnetId()));
-        if (!subnetMap.isPresent()) {
-            LOG.debug("Couldn't find subnet map for subnet {}", targetSubnetForPacketOut.getSubnetId());
-            VpnManagerCounters.subnet_route_packet_failed.inc();
-            return;
-        }
+            String sourceIp = subnetMap.get().getRouterInterfaceFixedIp();
+            if (sourceIp == null) {
+                LOG.debug("Subnet map {} doesn't have a router interface ip defined", subnetMap.get().getId());
+                VpnManagerCounters.subnet_route_packet_failed.inc();
+                return;
+            }
 
-        String sourceIp = subnetMap.get().getRouterInterfaceFixedIp();
-        if (sourceIp == null) {
-            LOG.debug("Subnet map {} doesn't have a router interface ip defined", subnetMap.get().getId());
-            VpnManagerCounters.subnet_route_packet_failed.inc();
-            return;
-        }
+            String sourceMac = subnetMap.get().getRouterIntfMacAddress();
+            if (sourceMac == null) {
+                LOG.debug("Subnet map {} doesn't have a router interface mac address defined",
+                        subnetMap.get().getId());
+                VpnManagerCounters.subnet_route_packet_failed.inc();
+                return;
+            }
 
-        String sourceMac = subnetMap.get().getRouterIntfMacAddress();
-        if (sourceMac == null) {
-            LOG.debug("Subnet map {} doesn't have a router interface mac address defined", subnetMap.get().getId());
-            VpnManagerCounters.subnet_route_packet_failed.inc();
-            return;
+            transmitArpPacket(targetSubnetForPacketOut.getNhDpnId(), sourceIp, sourceMac, dstIp, elanTag);
+        } catch (ReadFailedException e) {
+            LOG.error("handlePacketToInternalNetwork: Failed to read data store for destIp {} elanTag {}", dstIpStr,
+                    elanTag);
         }
-
-        transmitArpPacket(targetSubnetForPacketOut.getNhDpnId(), sourceIp, sourceMac, dstIp, elanTag);
     }
 
     private void handlePacketFromTunnelToExternalNetwork(String vpnIdVpnInstanceName,
@@ -329,43 +340,47 @@ public class SubnetRoutePacketInHandler implements PacketProcessingListener {
                     elanTag);
             return null;
         }
-
-        Optional<NetworkMap> optionalNetworkMap = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION,
-                VpnUtil.buildNetworkMapIdentifier(new Uuid(elanInfo.getName())));
-        if (!optionalNetworkMap.isPresent()) {
-            LOG.debug("{} getTargetDpnForPacketOut: No network map found for elan info {}", LOGGING_PREFIX,
-                    elanInfo.getName());
-            return null;
-        }
-
-        List<Uuid> subnetList = optionalNetworkMap.get().getSubnetIdList();
-        LOG.debug("{} getTargetDpnForPacketOut: Obtained subnetList as {} for network {}", LOGGING_PREFIX, subnetList,
-                elanInfo.getName());
-        for (Uuid subnetId : subnetList) {
-            String vpnName = null;
-            Subnetmap sn = VpnUtil.getSubnetmapFromItsUuid(broker, subnetId);
-            if (sn != null && sn.getVpnId() != null) {
-                vpnName = sn.getVpnId().getValue();
-            }
-            if (vpnName == null) {
-                continue;
+        try {
+            Optional<NetworkMap> optionalNetworkMap = SingleTransactionDataBroker.syncReadOptional(broker,
+                    LogicalDatastoreType.CONFIGURATION, VpnUtil.buildNetworkMapIdentifier(new Uuid(
+                            elanInfo.getName())));
+            if (!optionalNetworkMap.isPresent()) {
+                LOG.debug("{} getTargetDpnForPacketOut: No network map found for elan info {}", LOGGING_PREFIX,
+                        elanInfo.getName());
+                return null;
             }
-            Optional<SubnetOpDataEntry> optionalSubs;
-            optionalSubs = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL,
-                  VpnUtil.buildSubnetOpDataEntryInstanceIdentifier(subnetId));
-            if (!optionalSubs.isPresent()) {
-                continue;
-            }
-            SubnetOpDataEntry subOpEntry = optionalSubs.get();
-            if (subOpEntry.getNhDpnId() != null) {
-                LOG.trace("{} getTargetDpnForPacketOut: Viewing Subnet {}", LOGGING_PREFIX, subnetId.getValue());
-                boolean match = NWUtil.isIpInSubnet(ipAddress, subOpEntry.getSubnetCidr());
-                LOG.trace("{} getTargetDpnForPacketOut: Viewing Subnet {} matching {}", LOGGING_PREFIX,
-                        subnetId.getValue(), match);
-                if (match) {
-                    return subOpEntry;
+            List<Uuid> subnetList = optionalNetworkMap.get().getSubnetIdList();
+            LOG.debug("{} getTargetDpnForPacketOut: Obtained subnetList as {} for network {}", LOGGING_PREFIX,
+                    subnetList, elanInfo.getName());
+            for (Uuid subnetId : subnetList) {
+                String vpnName = null;
+                Subnetmap sn = VpnUtil.getSubnetmapFromItsUuid(broker, subnetId);
+                if (sn != null && sn.getVpnId() != null) {
+                    vpnName = sn.getVpnId().getValue();
+                }
+                if (vpnName == null) {
+                    continue;
+                }
+                Optional<SubnetOpDataEntry> optionalSubs;
+                optionalSubs = SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.OPERATIONAL,
+                        VpnUtil.buildSubnetOpDataEntryInstanceIdentifier(subnetId));
+                if (!optionalSubs.isPresent()) {
+                    continue;
+                }
+                SubnetOpDataEntry subOpEntry = optionalSubs.get();
+                if (subOpEntry.getNhDpnId() != null) {
+                    LOG.trace("{} getTargetDpnForPacketOut: Viewing Subnet {}", LOGGING_PREFIX, subnetId.getValue());
+                    boolean match = NWUtil.isIpInSubnet(ipAddress, subOpEntry.getSubnetCidr());
+                    LOG.trace("{} getTargetDpnForPacketOut: Viewing Subnet {} matching {}", LOGGING_PREFIX,
+                            subnetId.getValue(), match);
+                    if (match) {
+                        return subOpEntry;
+                    }
                 }
             }
+        } catch (ReadFailedException e) {
+            LOG.error("{} getTargetDpnForPacketOut: Failed to read data store for elan {}", LOGGING_PREFIX,
+                    elanInfo.getName());
         }
         return null;
     }
index 28e25cdfe3f5551b7bb53bfb77683435328ca9f3..c2718ac7a7b1c2875485ee4cafb9ffb8aeefa07c 100644 (file)
@@ -17,7 +17,9 @@ 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.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
@@ -217,15 +219,13 @@ public class SubnetmapChangeListener extends AsyncDataTreeChangeListenerBase<Sub
         return this;
     }
 
-    // TODO Clean up the exception handling
-    @SuppressWarnings("checkstyle:IllegalCatch")
     protected long getElanTag(String elanInstanceName) {
         InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
                 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
         long elanTag = 0L;
         try {
-            Optional<ElanInstance> elanInstance = VpnUtil.read(dataBroker, LogicalDatastoreType
-                    .CONFIGURATION, elanIdentifierId);
+            Optional<ElanInstance> elanInstance = SingleTransactionDataBroker.syncReadOptional(dataBroker,
+                    LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
             if (elanInstance.isPresent()) {
                 if (elanInstance.get().getElanTag() != null) {
                     elanTag = elanInstance.get().getElanTag();
@@ -236,7 +236,7 @@ public class SubnetmapChangeListener extends AsyncDataTreeChangeListenerBase<Sub
             } else {
                 LOG.error("Notification failed because of failure in reading ELANInstance {}", elanInstanceName);
             }
-        } catch (Exception e) {
+        } catch (ReadFailedException e) {
             LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
                 elanInstanceName, e);
         }