Change-Id: I2ef4153b5910e56c3401c08d57465741f1002691
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
@Override
public void onSuccess(final DOMRpcResult result) {
@Override
public void onSuccess(final DOMRpcResult result) {
+ // No matter what response we got, rpc-reply or rpc-error,
+ // we got it from device so the netconf session is OK
if (result != null && result.getResult() != null) {
LOG.debug("{}: Keepalive RPC successful with response: {}", id, result.getResult());
scheduleKeepalive();
if (result != null && result.getResult() != null) {
LOG.debug("{}: Keepalive RPC successful with response: {}", id, result.getResult());
scheduleKeepalive();
+ } else if (result != null && result.getErrors() != null) {
+ LOG.warn("{}: Keepalive RPC failed with error: {}", id, result.getErrors());
+ scheduleKeepalive();
} else {
LOG.warn("{} Keepalive RPC returned null with response: {}. Reconnecting netconf session", id, result);
reconnect();
} else {
LOG.warn("{} Keepalive RPC returned null with response: {}. Reconnecting netconf session", id, result);
reconnect();
@Mock
private ScheduledFuture currentKeepalive;
@Mock
private ScheduledFuture currentKeepalive;
+ private KeepaliveSalFacade keepaliveSalFacade;
+
@Before
public void setUp() throws Exception {
executorServiceSpy = Executors.newScheduledThreadPool(1);
@Before
public void setUp() throws Exception {
executorServiceSpy = Executors.newScheduledThreadPool(1);
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
executorServiceSpy = Mockito.spy(executorService);
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
executorServiceSpy = Mockito.spy(executorService);
doAnswer(
invocationOnMock -> {
invocationOnMock.callRealMethod();
doAnswer(
invocationOnMock -> {
invocationOnMock.callRealMethod();
}).when(executorServiceSpy).schedule(Mockito.<Runnable>any(), Mockito.anyLong(), Matchers.any());
Mockito.when(currentKeepalive.isDone()).thenReturn(true);
}).when(executorServiceSpy).schedule(Mockito.<Runnable>any(), Mockito.anyLong(), Matchers.any());
Mockito.when(currentKeepalive.isDone()).thenReturn(true);
+
+ keepaliveSalFacade =
+ new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 1L, 1L);
+ keepaliveSalFacade.setListener(listener);
- public void testKeepaliveFail() throws Exception {
- final DOMRpcResult result = new DefaultDOMRpcResult(Builders.containerBuilder().withNodeIdentifier(
- new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME)).build());
-
- RpcError error = mock(RpcError.class);
- doReturn("Failure").when(error).toString();
-
- final DOMRpcResult resultFailWithResultAndError = new DefaultDOMRpcResult(mock(NormalizedNode.class), error);
+ public void testKeepaliveRpcFailure() {
- doReturn(Futures.immediateCheckedFuture(result))
- .doReturn(Futures.immediateCheckedFuture(resultFailWithResultAndError))
- .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("illegal-state")))
+ doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("illegal-state")))
.when(deviceRpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
final KeepaliveSalFacade keepaliveSalFacade =
.when(deviceRpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
final KeepaliveSalFacade keepaliveSalFacade =
verify(underlyingSalFacade).onDeviceConnected(
any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
verify(underlyingSalFacade).onDeviceConnected(
any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
- // 1 failed that results in disconnect
+ // Should disconnect the session
verify(listener, timeout(15000).times(1)).disconnect();
verify(listener, timeout(15000).times(1)).disconnect();
- // 3 attempts total
- verify(deviceRpc, times(3)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
-
- // Reconnect with same keepalive responses
- doReturn(Futures.immediateCheckedFuture(result))
- .doReturn(Futures.immediateCheckedFuture(resultFailWithResultAndError))
- .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("illegal-state")))
- .when(deviceRpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
-
- keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
+ verify(deviceRpc, times(1)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
+ }
- // 1 failed that results in disconnect, 2 total with previous fail
- verify(listener, timeout(15000).times(2)).disconnect();
- // 6 attempts now total
- verify(deviceRpc, times(3 * 2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
+ @Test
+ public void testKeepaliveSuccessWithRpcError() {
- final DOMRpcResult resultFailwithError = new DefaultDOMRpcResult(error);
+ final DOMRpcResult rpcSuccessWithError = new DefaultDOMRpcResult(mock(RpcError.class));
- doReturn(Futures.immediateCheckedFuture(resultFailwithError))
+ doReturn(Futures.immediateCheckedFuture(rpcSuccessWithError))
.when(deviceRpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
.when(deviceRpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
- // 1 failed that results in disconnect, 3 total with previous fail
- verify(listener, timeout(15000).times(3)).disconnect();
-
+ verify(underlyingSalFacade).onDeviceConnected(
+ any(SchemaContext.class), any(NetconfSessionPreferences.class), any(DOMRpcService.class));
- Mockito.when(currentKeepalive.isDone()).thenReturn(false);
- keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
- // 1 failed that results in disconnect, 4 total with previous fail
- verify(listener, timeout(15000).times(4)).disconnect();
+ // Shouldn't disconnect the session
+ verify(listener, times(0)).disconnect();
+ verify(deviceRpc, timeout(15000).times(1)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));