BUG-8275: Close ReadOnly transaction 13/55813/4
authorClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Fri, 21 Apr 2017 12:36:32 +0000 (14:36 +0200)
committerRobert Varga <nite@hq.sk>
Sat, 22 Apr 2017 15:20:58 +0000 (15:20 +0000)
PCEPStatefulPeerProposal needs to
close ReadOnlyTransaction created
after use it

Change-Id: I1515f09028c9e6bd8403d6ea2851196511d4da24
Signed-off-by: Claudio D. Gasparini <claudio.gasparini@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
pcep/topology-provider/src/main/java/org/opendaylight/bgpcep/pcep/topology/provider/PCEPStatefulPeerProposal.java
pcep/topology-provider/src/test/java/org/opendaylight/bgpcep/pcep/topology/provider/PCEPStatefulPeerProposalTest.java

index 75daaa46e8844912b521a7a101be272a8442a3ff..dbcf4e588cf7710ceeb4956f48e5fff8aebea6a2 100644 (file)
@@ -14,6 +14,7 @@ import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.PathComputationClient1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev150714.Stateful1;
@@ -51,25 +52,28 @@ public final class PCEPStatefulPeerProposal {
 
     void setPeerProposal(final NodeId nodeId, final TlvsBuilder openTlvsBuilder) {
         if (isSynOptimizationEnabled(openTlvsBuilder)) {
-            final ListenableFuture<Optional<LspDbVersion>> future = this.dataBroker.newReadOnlyTransaction().read(
+            try (final ReadOnlyTransaction rTx = this.dataBroker.newReadOnlyTransaction()) {
+                final ListenableFuture<Optional<LspDbVersion>> future = rTx.read(
                     LogicalDatastoreType.OPERATIONAL,
                     this.topologyId.child(Node.class, new NodeKey(nodeId)).augmentation(Node1.class)
-                            .child(PathComputationClient.class).augmentation(PathComputationClient1.class)
-                            .child(LspDbVersion.class));
-            Futures.addCallback(future, new FutureCallback<Optional<LspDbVersion>>() {
-                @Override
-                public void onSuccess(final Optional<LspDbVersion> result) {
-                    if (result.isPresent()) {
-                        openTlvsBuilder.addAugmentation(Tlvs3.class, new Tlvs3Builder().setLspDbVersion(result.get())
-                                .build());
+                    .child(PathComputationClient.class).augmentation(PathComputationClient1.class)
+                    .child(LspDbVersion.class));
+                Futures.addCallback(future, new FutureCallback<Optional<LspDbVersion>>() {
+                    @Override
+                    public void onSuccess(final Optional<LspDbVersion> result) {
+                        if (result.isPresent()) {
+                            openTlvsBuilder.addAugmentation(Tlvs3.class,
+                                new Tlvs3Builder().setLspDbVersion(result.get()).build());
+                        }
                     }
-                }
 
-                @Override
-                public void onFailure(final Throwable t) {
-                    LOG.warn("Failed to read toplogy {}.", InstanceIdentifier.keyOf(PCEPStatefulPeerProposal.this.topologyId), t);
-                }
-            });
+                    @Override
+                    public void onFailure(final Throwable t) {
+                        LOG.warn("Failed to read toplogy {}.", InstanceIdentifier.keyOf(
+                            PCEPStatefulPeerProposal.this.topologyId), t);
+                    }
+                });
+            }
         }
     }
 
index 213c9481676ad2b2d66cedcd9015fc85bc000cf7..091b2c53fcf8c020a7b239a9fbecb7363771db58 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.bgpcep.pcep.topology.provider;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.*;
 
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.CheckedFuture;
@@ -19,7 +20,6 @@ import java.util.concurrent.Executor;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
@@ -53,7 +53,8 @@ public class PCEPStatefulPeerProposalTest {
     private DataBroker dataBroker;
     @Mock
     private CheckedFuture<Optional<LspDbVersion>, ReadFailedException> listenableFutureMock;
-
+    @Mock
+    private ReadOnlyTransaction rTxMock;
     private TlvsBuilder tlvsBuilder;
 
     @SuppressWarnings("unchecked")
@@ -65,21 +66,21 @@ public class PCEPStatefulPeerProposalTest {
                 new Tlvs1Builder().setStateful(
                         new StatefulBuilder().addAugmentation(Stateful1.class, new Stateful1Builder().build()).build())
                         .build());
-        final ReadOnlyTransaction rTxMock = Mockito.mock(ReadOnlyTransaction.class);
-        Mockito.doReturn(rTxMock).when(this.dataBroker).newReadOnlyTransaction();
-        Mockito.doReturn(this.listenableFutureMock).when(rTxMock)
-                .read(Mockito.any(LogicalDatastoreType.class), Mockito.any(InstanceIdentifier.class));
+        doReturn(this.rTxMock).when(this.dataBroker).newReadOnlyTransaction();
+        doNothing().when(this.rTxMock).close();
+        doReturn(this.listenableFutureMock).when(this.rTxMock)
+                .read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
 
-        Mockito.doAnswer(invocation -> {
+        doAnswer(invocation -> {
             final Runnable runnable = (Runnable) invocation.getArguments()[0];
             runnable.run();
             return null;
-        }).when(this.listenableFutureMock).addListener(Mockito.any(Runnable.class), Mockito.any(Executor.class));
+        }).when(this.listenableFutureMock).addListener(any(Runnable.class), any(Executor.class));
     }
 
     @Test
     public void testSetPeerProposalSuccess() throws InterruptedException, ExecutionException {
-        Mockito.doReturn(Optional.of(LSP_DB_VERSION)).when(this.listenableFutureMock).get();
+        doReturn(Optional.of(LSP_DB_VERSION)).when(this.listenableFutureMock).get();
         final PCEPStatefulPeerProposal peerProposal = PCEPStatefulPeerProposal.createStatefulPeerProposal(this.dataBroker,
                 TOPOLOGY_IID);
         peerProposal.setPeerProposal(NODE_ID, this.tlvsBuilder);
@@ -88,7 +89,7 @@ public class PCEPStatefulPeerProposalTest {
 
     @Test
     public void testSetPeerProposalAbsent() throws InterruptedException, ExecutionException {
-        Mockito.doReturn(Optional.absent()).when(this.listenableFutureMock).get();
+        doReturn(Optional.absent()).when(this.listenableFutureMock).get();
         final PCEPStatefulPeerProposal peerProposal = PCEPStatefulPeerProposal.createStatefulPeerProposal(this.dataBroker,
                 TOPOLOGY_IID);
         peerProposal.setPeerProposal(NODE_ID, this.tlvsBuilder);
@@ -97,7 +98,7 @@ public class PCEPStatefulPeerProposalTest {
 
     @Test
     public void testSetPeerProposalFailure() throws InterruptedException, ExecutionException {
-        Mockito.doThrow(new RuntimeException()).when(this.listenableFutureMock).get();
+        doThrow(new RuntimeException()).when(this.listenableFutureMock).get();
         final PCEPStatefulPeerProposal peerProposal = PCEPStatefulPeerProposal.createStatefulPeerProposal(this.dataBroker,
                 TOPOLOGY_IID);
         peerProposal.setPeerProposal(NODE_ID, this.tlvsBuilder);