Fix exception in addSwitch 78/74378/2
authorSridhar Gaddam <sgaddam@redhat.com>
Tue, 24 Jul 2018 08:33:36 +0000 (14:03 +0530)
committerSam Hague <shague@redhat.com>
Tue, 24 Jul 2018 18:28:13 +0000 (18:28 +0000)
In a fresh deployment when the first switch is
added to the controller and when there are no
neutron resources (like ExtRouters) are present,
the current implementation in addSwitch was
causing ExpectedDataObjectNotFoundException.
This patch fixes it by changing the syncRead
to syncReadOptional while querying the datastore.

Issue: NETVIRT-1382

Change-Id: I4393a7ad2fa896eda2e1cbb88e3c34c9ffcc0c56
Signed-off-by: Sridhar Gaddam <sgaddam@redhat.com>
(cherry picked from commit acb65d76fce6e6a361fd59a72fe33f067f3d27c1)

natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/ha/WeightedCentralizedSwitchScheduler.java

index aa2db055924a4cbb8413582b31949a55082a7dea..83cdf371d58710feb0689b6d20cab11205798c61 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.netvirt.natservice.ha;
 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
 
+import com.google.common.base.Optional;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -204,22 +205,24 @@ public class WeightedCentralizedSwitchScheduler implements CentralizedSwitchSche
         switchWeightsMap.put(dpnId, INITIAL_SWITCH_WEIGHT);
 
         if (scheduleRouters) {
-            ExtRouters routers;
+            Optional<ExtRouters> optRouters;
             try {
-                routers = SingleTransactionDataBroker.syncRead(dataBroker, LogicalDatastoreType.CONFIGURATION,
-                        InstanceIdentifier.create(ExtRouters.class));
+                optRouters = SingleTransactionDataBroker.syncReadOptional(dataBroker,
+                        LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(ExtRouters.class));
             } catch (ReadFailedException e) {
                 LOG.error("addSwitch: Error reading external routers", e);
                 return false;
             }
 
-            // Get the list of routers and verify if any routers do not have primarySwitch allocated.
-            for (Routers router : routers.getRouters()) {
-                List<ExternalIps> externalIps = router.getExternalIps();
-                if (router.isEnableSnat() && externalIps != null && !externalIps.isEmpty()) {
-                    // Check if the primarySwitch is allocated for the router.
-                    if (!isPrimarySwitchAllocatedForRouter(router.getRouterName())) {
-                        scheduleCentralizedSwitch(router);
+            if (optRouters.isPresent()) {
+                // Get the list of routers and verify if any routers do not have primarySwitch allocated.
+                for (Routers router : optRouters.get().getRouters()) {
+                    List<ExternalIps> externalIps = router.getExternalIps();
+                    if (router.isEnableSnat() && externalIps != null && !externalIps.isEmpty()) {
+                        // Check if the primarySwitch is allocated for the router.
+                        if (!isPrimarySwitchAllocatedForRouter(router.getRouterName())) {
+                            scheduleCentralizedSwitch(router);
+                        }
                     }
                 }
             }