2 * Copyright (c) 2019 Lumina Networks, Inc. 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.mockito.Mockito.after;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.verify;
13 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps.getSourceNode;
14 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID;
18 import com.google.common.util.concurrent.SettableFuture;
19 import java.net.InetSocketAddress;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ScheduledExecutorService;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.mdsal.dom.api.DOMActionService;
30 import org.opendaylight.mdsal.dom.api.DOMNotification;
31 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
32 import org.opendaylight.mdsal.dom.api.DOMRpcService;
33 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
34 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
35 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
36 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
37 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
38 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
39 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class KeepaliveSalFacadeResponseWaitingTest {
46 private static final RemoteDeviceId REMOTE_DEVICE_ID =
47 new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
48 private static final @NonNull ContainerNode KEEPALIVE_PAYLOAD =
49 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID,
50 getSourceNode(NETCONF_RUNNING_NODEID), NetconfMessageTransformUtil.EMPTY_FILTER);
52 private KeepaliveSalFacade keepaliveSalFacade;
53 private ScheduledExecutorService executorService;
55 private LocalNetconfSalFacade underlyingSalFacade;
58 private DOMRpcService deviceRpc;
61 private NetconfDeviceCommunicator listener;
64 public void setUp() throws Exception {
65 executorService = Executors.newScheduledThreadPool(2);
67 underlyingSalFacade = new LocalNetconfSalFacade();
68 keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorService, 2L, 10000L);
69 keepaliveSalFacade.setListener(listener);
73 public void tearDown() {
74 executorService.shutdown();
78 * Not sending keepalive rpc test while the repsonse is processing.
81 public void testKeepaliveSalResponseWaiting() {
82 //This settable future object will be never set to any value. The test wants to simulate waiting for the result
83 //of the future object.
84 final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
85 doReturn(settableFuture).when(deviceRpc).invokeRpc(null, null);
87 //This settable future will be used to check the invokation of keepalive RPC. Should be never invoked.
88 final SettableFuture<DOMRpcResult> keepaliveSettableFuture = SettableFuture.create();
89 keepaliveSettableFuture.set(new DefaultDOMRpcResult(Builders.containerBuilder()
90 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID)
93 keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
95 //Invoke general RPC on simulated local facade without args (or with null args). Will be returned
96 //settableFuture variable without any set value. WaitingShaduler in keepalive sal facade should wait for any
97 //result from the RPC and reset keepalive scheduler.
98 underlyingSalFacade.invokeNullRpc();
100 //Invoking of general RPC.
101 verify(deviceRpc, after(2000).times(1)).invokeRpc(null, null);
103 //verify the keepalive RPC invoke. Should be never happen.
104 verify(deviceRpc, after(2000).never()).invokeRpc(NETCONF_GET_CONFIG_QNAME, KEEPALIVE_PAYLOAD);
107 private static final class LocalNetconfSalFacade implements RemoteDeviceHandler {
108 private DOMRpcService localDeviceRpc;
111 public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
112 final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService currentDeviceRpc,
113 final DOMActionService deviceAction) {
114 localDeviceRpc = currentDeviceRpc;
118 public void onDeviceDisconnected() {
122 public void onDeviceFailed(final Throwable throwable) {
126 public void onNotification(final DOMNotification domNotification) {
130 public void close() {
133 public void invokeNullRpc() {
134 if (localDeviceRpc != null) {
135 localDeviceRpc.invokeRpc(null, null);