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;
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);
+ }
+ });
+ }
}
}
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;
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;
private DataBroker dataBroker;
@Mock
private CheckedFuture<Optional<LspDbVersion>, ReadFailedException> listenableFutureMock;
-
+ @Mock
+ private ReadOnlyTransaction rTxMock;
private TlvsBuilder tlvsBuilder;
@SuppressWarnings("unchecked")
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);
@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);
@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);