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.client.mdsal.spi;
10 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.isNull;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.never;
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 com.google.common.util.concurrent.SettableFuture;
24 import java.net.InetSocketAddress;
25 import org.junit.jupiter.api.AfterEach;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.extension.ExtendWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.jupiter.MockitoExtension;
31 import org.opendaylight.mdsal.dom.api.DOMRpcService;
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.netconf.common.impl.DefaultNetconfTimer;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
45 @ExtendWith(MockitoExtension.class)
46 class KeepaliveSalFacadeTest {
47 private static final RemoteDeviceId REMOTE_DEVICE_ID =
48 new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
51 private RemoteDeviceHandler underlyingSalFacade;
53 private NetconfDeviceCommunicator listener;
55 private Rpcs.Normalized deviceRpc;
57 private DOMRpcService deviceDomRpc;
59 private DefaultNetconfTimer timer;
60 private KeepaliveSalFacade keepaliveSalFacade;
61 private Rpcs proxyRpc;
65 timer = new DefaultNetconfTimer();
66 keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, timer, 1L, 1L);
67 keepaliveSalFacade.setListener(listener);
68 doReturn(deviceDomRpc).when(deviceRpc).domRpcService();
77 void testKeepaliveSuccess() throws Exception {
78 doNothing().when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any());
79 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(ImmutableNodes.newContainerBuilder()
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());
87 verify(deviceRpc, timeout(15000).times(5)).invokeNetconf(any(), any());
91 void testKeepaliveRpcFailure() {
92 doNothing().when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any());
93 doReturn(Futures.immediateFailedFuture(new IllegalStateException("illegal-state")))
94 .when(deviceRpc).invokeNetconf(any(), any());
95 doNothing().when(listener).disconnect();
97 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
99 verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any());
101 // Should disconnect the session
102 verify(listener, timeout(15000).times(1)).disconnect();
103 verify(deviceRpc, times(1)).invokeNetconf(any(), any());
107 void testKeepaliveSuccessWithRpcError() {
108 doNothing().when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any());
109 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(mock(RpcError.class)))).when(deviceRpc)
110 .invokeNetconf(any(), any());
112 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
114 verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any());
116 // Shouldn't disconnect the session
117 verify(listener, never()).disconnect();
118 verify(deviceRpc, timeout(15000).times(1)).invokeNetconf(any(), any());
122 void testNonKeepaliveRpcFailure() throws Exception {
123 doAnswer(invocation -> proxyRpc = invocation.getArgument(2, RemoteDeviceServices.class).rpcs())
124 .when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
125 doReturn(Futures.immediateFailedFuture(new IllegalStateException("illegal-state")))
126 .when(deviceDomRpc).invokeRpc(any(), any());
128 keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, timer, 100L, 1L);
129 keepaliveSalFacade.setListener(listener);
131 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
133 assertInstanceOf(Rpcs.Normalized.class, proxyRpc).domRpcService()
134 .invokeRpc(QName.create("foo", "bar"), mock(ContainerNode.class));
136 verify(listener, times(1)).disconnect();
140 void testKeepaliveRpcResponseTimeout() {
141 doNothing().when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any());
142 doNothing().when(listener).disconnect();
143 doReturn(SettableFuture.create()).when(deviceRpc).invokeNetconf(any(), any());
145 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
147 verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
149 // Should disconnect the session because RPC result future is never resolved and keepalive delay is 1 sec
150 verify(listener, timeout(115000).times(1)).disconnect();
151 verify(deviceRpc, times(1)).invokeNetconf(any(), any());