BUG-7222: Fix clustering BGPPeer NPE 67/48767/3
authorClaudio D. Gasparini <cgaspari@cisco.com>
Mon, 28 Nov 2016 10:46:33 +0000 (11:46 +0100)
committerClaudio D. Gasparini <cgaspari@cisco.com>
Tue, 29 Nov 2016 11:16:09 +0000 (12:16 +0100)
Fix NPE: Any call to rib should be done
during singleton service instantiation or after.
Any call before can cause a NPE since RIB might be
on undefined status yet.

Change-Id: Ia1ebbc334f07362f325ac599f39f9d601e717321
Signed-off-by: Claudio D. Gasparini <cgaspari@cisco.com>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPPeer.java
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/config/BgpPeerTest.java

index 42995e6985560a7c55b42ae37a74eecd4532d105..230ef3029ce22a60aa64bb4ba796bccba9e24861 100644 (file)
@@ -130,9 +130,6 @@ public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeer
         this.name = name;
         this.rpcRegistry = rpcRegistry;
         this.peerStats = new BGPPeerStatsImpl(this.name, this.tables);
-
-        // add current peer to "configured BGP peer" stats
-        this.rib.getRenderStats().getConfiguredPeerCounter().increaseCount();
         this.chain = rib.createPeerChain(this);
     }
 
@@ -141,6 +138,8 @@ public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeer
     }
 
     public void instantiateServiceInstance() {
+        // add current peer to "configured BGP peer" stats
+        this.rib.getRenderStats().getConfiguredPeerCounter().increaseCount();
         this.ribWriter = AdjRibInWriter.create(rib.getYangRibId(), this.peerRole, this.simpleRoutingPolicy, this.chain);
     }
 
index 83a31dd26406b774aca4348a584681df2a23dabf..3cde1b5a25c4d29f1296da63a498024b38d34198 100644 (file)
@@ -77,13 +77,13 @@ public class BgpPeerTest extends AbstractConfig {
     }
 
     @Test
+    @SuppressWarnings("unchecked")
     public void testBgpPeer() throws Exception {
         final Neighbor neighbor = new NeighborBuilder().setAfiSafis(createAfiSafi()).setConfig(createConfig()).setNeighborAddress(NEIGHBOR_ADDRESS)
             .setRouteReflector(createRR()).setTimers(createTimers()).setTransport(createTransport()).setAddPaths(createAddPath()).build();
 
         this.bgpPeer.start(this.rib, neighbor, this.mappingService, this.configurationWriter);
         Mockito.verify(this.mappingService).toPeerRole(any());
-        Mockito.verify(this.render).getConfiguredPeerCounter();
         Mockito.verify(this.rib).createPeerChain(any());
         Mockito.verify(this.rib, times(2)).getLocalAs();
         Mockito.verify(this.rib).getLocalTables();
@@ -93,6 +93,7 @@ public class BgpPeerTest extends AbstractConfig {
         Mockito.verify(this.rib).registerClusterSingletonService(any(ClusterSingletonService.class));
 
         this.singletonService.instantiateServiceInstance();
+        Mockito.verify(this.render).getConfiguredPeerCounter();
         Mockito.verify(this.configurationWriter).apply();
         Mockito.verify(this.bgpPeerRegistry).addPeer(any(), any(), any());
         Mockito.verify(this.dispatcher).createReconnectingClient(any(InetSocketAddress.class), any(BGPPeerRegistry.class), anyInt(), any(Optional.class));
@@ -107,7 +108,6 @@ public class BgpPeerTest extends AbstractConfig {
         this.bgpPeer.close();
         this.bgpPeer.restart(this.rib, this.mappingService);
         Mockito.verify(this.mappingService, times(2)).toPeerRole(any());
-        Mockito.verify(this.render, times(2)).getConfiguredPeerCounter();
         Mockito.verify(this.rib, times(2)).createPeerChain(any());
         Mockito.verify(this.rib, times(4)).getLocalAs();
         Mockito.verify(this.rib, times(2)).getLocalTables();
@@ -116,7 +116,7 @@ public class BgpPeerTest extends AbstractConfig {
         Mockito.verify(this.rib, times(2)).getRibIServiceGroupIdentifier();
         Mockito.verify(this.rib, times(2)).registerClusterSingletonService(any(ClusterSingletonService.class));
         this.singletonService.instantiateServiceInstance();
-
+        Mockito.verify(this.render, times(2)).getConfiguredPeerCounter();
         assertNotNull(this.bgpPeer.getBgpPeerState());
         assertNotNull(this.bgpPeer.getBgpSessionState());
         this.bgpPeer.resetStats();