Bug 9205: NPE received while receiving BGP peers 44/64244/1
authorAjay Lele <ajayslele@gmail.com>
Fri, 22 Sep 2017 21:53:10 +0000 (14:53 -0700)
committerClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Fri, 13 Oct 2017 12:08:14 +0000 (12:08 +0000)
Change-Id: I05383a406b03e5dc460e5ae013da82280920981e
Signed-off-by: Ajay Lele <ajayslele@gmail.com>
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
bgp/openconfig-state/src/main/java/org/opendaylight/protocol/bgp/state/StateProviderImpl.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/AppPeer.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/config/BgpPeer.java

index b02daa2a84fab5ac0ccd9711445fe811a5f4a248..80f9a4ae16cb67a29030af6bf103c2cf0d3c1e35 100644 (file)
@@ -174,8 +174,9 @@ public final class StateProviderImpl implements TransactionChainListener, Cluste
     public synchronized ListenableFuture<Void> closeServiceInstance() {
         this.scheduleTask.cancel(true);
         final WriteTransaction wTx = this.transactionChain.newWriteOnlyTransaction();
-        this.instanceIdentifiersCache.keySet().forEach(ribId -> removeStoredOperationalState(ribId, wTx));
-        final CheckedFuture<Void, TransactionCommitFailedException> futureDelete = wTx.submit();
+        this.instanceIdentifiersCache.keySet().iterator()
+            .forEachRemaining(ribId -> removeStoredOperationalState(ribId, wTx));
+        final ListenableFuture<Void> futureDelete = wTx.submit();
         this.transactionChain.close();
         return futureDelete;
     }
index 4891504c9d80596850f386b1076515f254031f64..29ee2e1719f104ea80dbd7528aaa68b4738d4541 100644 (file)
@@ -41,12 +41,15 @@ import org.slf4j.LoggerFactory;
 public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
     private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
     private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
+    @GuardedBy("this")
     private Neighbor currentConfiguration;
+    @GuardedBy("this")
     private BgpAppPeerSingletonService bgpAppPeerSingletonService;
+    @GuardedBy("this")
     private ServiceRegistration<?> serviceRegistration;
 
     @Override
-    public void start(final RIB rib, final Neighbor neighbor, final BGPTableTypeRegistryConsumer tableTypeRegistry,
+    public synchronized void start(final RIB rib, final Neighbor neighbor, final BGPTableTypeRegistryConsumer tableTypeRegistry,
         final WriteConfiguration configurationWriter) {
         Preconditions.checkState(this.bgpAppPeerSingletonService == null, "Previous peer instance was not closed.");
         this.currentConfiguration = neighbor;
@@ -55,18 +58,20 @@ public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
     }
 
     @Override
-    public void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
+    public synchronized void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
         Preconditions.checkState(this.currentConfiguration != null);
         start(rib, this.currentConfiguration, tableTypeRegistry, null);
     }
 
     @Override
-    public void close() {
-        try {
-            this.bgpAppPeerSingletonService.close();
-            this.bgpAppPeerSingletonService = null;
-        } catch (final Exception e) {
-            LOG.warn("Failed to close application peer instance", e);
+    public synchronized void close() {
+        if (this.bgpAppPeerSingletonService != null) {
+            try {
+                this.bgpAppPeerSingletonService.close();
+                this.bgpAppPeerSingletonService = null;
+            } catch (final Exception e) {
+                LOG.warn("Failed to close application peer instance", e);
+            }
         }
         if (this.serviceRegistration != null) {
             this.serviceRegistration.unregister();
@@ -75,7 +80,7 @@ public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
     }
 
     @Override
-    public ListenableFuture<Void> closeServiceInstance() {
+    public synchronized ListenableFuture<Void> closeServiceInstance() {
         if (this.bgpAppPeerSingletonService != null) {
             return this.bgpAppPeerSingletonService.closeServiceInstance();
         }
@@ -102,7 +107,7 @@ public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
         return this.bgpAppPeerSingletonService.getPeerState();
     }
 
-    void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
+    synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
         this.serviceRegistration = serviceRegistration;
     }
 
@@ -175,4 +180,4 @@ public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
             return this.applicationPeer.getPeerState();
         }
     }
-}
\ No newline at end of file
+}
index 369bc13b886f010242ac4a30d5fecea16b131d77..68f0a97d40909eccf94e3f0a3af49d891f91d8fc 100644 (file)
@@ -71,8 +71,11 @@ public final class BgpPeer implements PeerBean, BGPPeerStateConsumer, BGPPeerRun
     private static final Logger LOG = LoggerFactory.getLogger(BgpPeer.class);
 
     private final RpcProviderRegistry rpcRegistry;
+    @GuardedBy("this")
     private ServiceRegistration<?> serviceRegistration;
+    @GuardedBy("this")
     private Neighbor currentConfiguration;
+    @GuardedBy("this")
     private BgpPeerSingletonService bgpPeerSingletonService;
 
     public BgpPeer(final RpcProviderRegistry rpcRegistry) {
@@ -80,7 +83,7 @@ public final class BgpPeer implements PeerBean, BGPPeerStateConsumer, BGPPeerRun
     }
 
     @Override
-    public void start(final RIB rib, final Neighbor neighbor, final BGPTableTypeRegistryConsumer tableTypeRegistry,
+    public synchronized void start(final RIB rib, final Neighbor neighbor, final BGPTableTypeRegistryConsumer tableTypeRegistry,
         final WriteConfiguration configurationWriter) {
         Preconditions.checkState(this.bgpPeerSingletonService == null, "Previous peer instance was not closed.");
         this.bgpPeerSingletonService = new BgpPeerSingletonService(rib, neighbor, tableTypeRegistry, configurationWriter);
@@ -88,13 +91,13 @@ public final class BgpPeer implements PeerBean, BGPPeerStateConsumer, BGPPeerRun
     }
 
     @Override
-    public void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
+    public synchronized void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
         Preconditions.checkState(this.currentConfiguration != null);
         start(rib, this.currentConfiguration, tableTypeRegistry, null);
     }
 
     @Override
-    public void close() {
+    public synchronized void close() {
         closeSingletonService();
         if (this.serviceRegistration != null) {
             this.serviceRegistration.unregister();
@@ -103,7 +106,7 @@ public final class BgpPeer implements PeerBean, BGPPeerStateConsumer, BGPPeerRun
     }
 
     @Override
-    public ListenableFuture<Void> closeServiceInstance() {
+    public synchronized ListenableFuture<Void> closeServiceInstance() {
         if (this.bgpPeerSingletonService != null) {
             return this.bgpPeerSingletonService.closeServiceInstance();
         }
@@ -111,11 +114,13 @@ public final class BgpPeer implements PeerBean, BGPPeerStateConsumer, BGPPeerRun
     }
 
     private void closeSingletonService() {
-        try {
-            this.bgpPeerSingletonService.close();
-            this.bgpPeerSingletonService = null;
-        } catch (final Exception e) {
-            LOG.warn("Failed to close peer instance", e);
+        if (this.bgpPeerSingletonService != null) {
+            try {
+                this.bgpPeerSingletonService.close();
+                this.bgpPeerSingletonService = null;
+            } catch (final Exception e) {
+                LOG.warn("Failed to close peer instance", e);
+            }
         }
     }
 
@@ -209,7 +214,7 @@ public final class BgpPeer implements PeerBean, BGPPeerStateConsumer, BGPPeerRun
         return this.bgpPeerSingletonService.getPeerState();
     }
 
-    void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
+    synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
         this.serviceRegistration = serviceRegistration;
     }