2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.isNull;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.timeout;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
22 import com.google.common.util.concurrent.Futures;
23 import java.net.InetSocketAddress;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.ScheduledExecutorService;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
33 import org.opendaylight.netconf.client.mdsal.NetconfDeviceCommunicator;
34 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceHandler;
35 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
36 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
37 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
38 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.common.RpcError;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class KeepaliveSalFacadeTest {
46 private static final RemoteDeviceId REMOTE_DEVICE_ID =
47 new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
50 private RemoteDeviceHandler underlyingSalFacade;
52 private NetconfDeviceCommunicator listener;
54 private Rpcs.Normalized deviceRpc;
56 private ScheduledExecutorService executorServiceSpy;
57 private KeepaliveSalFacade keepaliveSalFacade;
58 private Rpcs proxyRpc;
61 public void setUp() throws Exception {
62 executorServiceSpy = Executors.newScheduledThreadPool(1);
64 doNothing().when(listener).disconnect();
65 doNothing().when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
68 new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 1L, 1L);
69 keepaliveSalFacade.setListener(listener);
73 public void tearDown() throws Exception {
74 executorServiceSpy.shutdownNow();
78 public void testKeepaliveSuccess() throws Exception {
79 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(Builders.containerBuilder()
80 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID)
81 .build()))).when(deviceRpc).invokeNetconf(any(), any());
83 final var services = new RemoteDeviceServices(deviceRpc, null);
84 keepaliveSalFacade.onDeviceConnected(null, null, services);
86 verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
88 verify(deviceRpc, timeout(15000).times(5)).invokeNetconf(any(), any());
92 public void testKeepaliveRpcFailure() {
93 doReturn(Futures.immediateFailedFuture(new IllegalStateException("illegal-state")))
94 .when(deviceRpc).invokeNetconf(any(), any());
96 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
98 verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
100 // Should disconnect the session
101 verify(listener, timeout(15000).times(1)).disconnect();
102 verify(deviceRpc, times(1)).invokeNetconf(any(), any());
106 public void testKeepaliveSuccessWithRpcError() {
108 final var rpcSuccessWithError = new DefaultDOMRpcResult(mock(RpcError.class));
110 doReturn(Futures.immediateFuture(rpcSuccessWithError)).when(deviceRpc).invokeNetconf(any(), any());
112 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
114 verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
116 // Shouldn't disconnect the session
117 verify(listener, times(0)).disconnect();
118 verify(deviceRpc, timeout(15000).times(1)).invokeNetconf(any(), any());
122 public void testNonKeepaliveRpcFailure() throws Exception {
123 doAnswer(invocation -> proxyRpc = invocation.getArgument(2, RemoteDeviceServices.class).rpcs())
124 .when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
126 doReturn(Futures.immediateFailedFuture(new IllegalStateException("illegal-state")))
127 .when(deviceRpc).invokeRpc(any(), any());
130 new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 100L, 1L);
131 keepaliveSalFacade.setListener(listener);
133 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
135 assertThat(proxyRpc, instanceOf(Rpcs.Normalized.class));
136 ((Rpcs.Normalized) proxyRpc).invokeRpc(QName.create("foo", "bar"), mock(ContainerNode.class));
138 verify(listener, times(1)).disconnect();