BUG-6858: adapt to ise api, fix NPE in listener when missing masterDB 14/46614/1
authorMichal Rehak <mirehak@cisco.com>
Tue, 4 Oct 2016 14:48:30 +0000 (16:48 +0200)
committerMichal Rehak <mirehak@cisco.com>
Thu, 6 Oct 2016 12:28:05 +0000 (14:28 +0200)
    - added Optional structure for case where topology
      from notification contains null list of nodes

Change-Id: I24af46a0b69410dc472772e51f2bbccde4163688
Signed-off-by: Michal Rehak <mirehak@cisco.com>
(cherry picked from commit ebba85849d74d3438500bebc5be84ad6cb8665ad)

sxp-integration/sxp-ep-provider/src/main/java/org/opendaylight/groupbasedpolicy/sxp/ep/provider/impl/dao/MasterDatabaseBindingDaoImpl.java
sxp-integration/sxp-ep-provider/src/main/java/org/opendaylight/groupbasedpolicy/sxp/ep/provider/impl/listen/EPForwardingTemplateListenerImpl.java
sxp-integration/sxp-ep-provider/src/test/java/org/opendaylight/groupbasedpolicy/sxp/ep/provider/impl/dao/MasterDatabaseBindingDaoImplTest.java

index ebf16d3e01ca2c062fbbbd0e227fadcfd485c60f..01a7320d95c0cae3fec7732f62681917edb0572d 100644 (file)
@@ -14,6 +14,7 @@ import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 import javax.annotation.Nonnull;
@@ -89,11 +90,17 @@ public class MasterDatabaseBindingDaoImpl implements DSAsyncDao<IpPrefix, Master
             @Nullable
             @Override
             public Void apply(@Nullable final Optional<Topology> input) {
-                if (input.isPresent()) {
+                if (input != null) {
                     // clean cache
                     cachedDao.invalidateCache();
 
-                    for (Node node : input.get().getNode()) {
+                    final List<Node> nodeList = java.util.Optional.ofNullable(input.orNull())
+                            .map(Topology::getNode)
+                            .orElseGet(() -> {
+                                LOG.warn("failed to update cache of SxpMasterDB - no data");
+                                return Collections.emptyList();
+                            });
+                    for (Node node : nodeList) {
                         java.util.Optional.ofNullable(node.getAugmentation(SxpNodeIdentity.class))
                                 .map(SxpNodeIdentity::getSxpDomains)
                                 .map(SxpDomains::getSxpDomain)
@@ -119,7 +126,7 @@ public class MasterDatabaseBindingDaoImpl implements DSAsyncDao<IpPrefix, Master
                                 });
                     }
                 } else {
-                    LOG.warn("failed to update cache of SxpMasterDB - no data");
+                    LOG.warn("failed to update cache of SxpMasterDB - null input");
                 }
                 return null;
             }
index 20da733ff2af509157370f7809b6b5ce6f8fb92b..1483c3d6746d4fa72c75fc331f8ae511d29e30a2 100644 (file)
@@ -83,6 +83,7 @@ public class EPForwardingTemplateListenerImpl implements EPTemplateListener<Endp
             final IpPrefix changeKey = changePath.firstKeyOf(EndpointForwardingTemplateBySubnet.class).getIpPrefix();
             SxpListenerUtil.updateCachedDao(templateCachedDao, changeKey, change);
 
+            //TODO: handle removal (now causes NPE)
             final EndpointForwardingTemplateBySubnet epForwardingTemplate = change.getRootNode().getDataAfter();
             processWithEPTemplates(epForwardingTemplate);
         }
index d549367696dfbd8b03651110d9277449e8d36214..cd94eb222ecb79bd8e39dcc7132d92752db8a9a9 100644 (file)
@@ -111,6 +111,21 @@ public class MasterDatabaseBindingDaoImplTest {
         Assert.assertFalse(read.get().isPresent());
     }
 
+    @Test
+    public void testRead_absentNull() throws Exception {
+        Mockito.when(cachedDao.find(Matchers.<IpPrefix>any())).thenReturn(Optional.<MasterDatabaseBinding>absent());
+        Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
+        Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
+                Matchers.<InstanceIdentifier<Topology>>any())).thenReturn(
+                Futures.<Optional<Topology>, ReadFailedException>immediateCheckedFuture(
+                        Optional.of(new TopologyBuilder().build())));
+
+
+        final ListenableFuture<Optional<MasterDatabaseBinding>> read = dao.read(IP_PREFIX);
+        Assert.assertTrue(read.isDone());
+        Assert.assertFalse(read.get().isPresent());
+    }
+
     @Test
     public void testRead_presentCached() throws Exception {
         Mockito.when(cachedDao.find(Matchers.<IpPrefix>any())).thenReturn(Optional.of(MASTER_DB_BINDING_VALUE));