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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.mockito.Mockito.after;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps.getSourceNode;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID;
20 import com.google.common.util.concurrent.SettableFuture;
21 import java.net.InetSocketAddress;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.opendaylight.mdsal.dom.api.DOMNotification;
32 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
33 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
34 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
35 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
36 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
37 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
38 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
39 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
40 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
41 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class KeepaliveSalFacadeResponseWaitingTest {
48 private static final RemoteDeviceId REMOTE_DEVICE_ID =
49 new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
50 private static final @NonNull ContainerNode KEEPALIVE_PAYLOAD =
51 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID,
52 getSourceNode(NETCONF_RUNNING_NODEID), NetconfMessageTransformUtil.EMPTY_FILTER);
54 private KeepaliveSalFacade keepaliveSalFacade;
55 private ScheduledExecutorService executorService;
57 private LocalNetconfSalFacade underlyingSalFacade;
60 private Rpcs.Normalized deviceRpc;
63 private NetconfDeviceCommunicator listener;
66 public void setUp() throws Exception {
67 executorService = Executors.newScheduledThreadPool(2);
69 underlyingSalFacade = new LocalNetconfSalFacade();
70 keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorService, 2L, 10000L);
71 keepaliveSalFacade.setListener(listener);
75 public void tearDown() {
76 executorService.shutdown();
80 * Not sending keepalive rpc test while the repsonse is processing.
83 public void testKeepaliveSalResponseWaiting() {
84 //This settable future object will be never set to any value. The test wants to simulate waiting for the result
85 //of the future object.
86 final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
87 doReturn(settableFuture).when(deviceRpc).invokeRpc(null, null);
89 //This settable future will be used to check the invokation of keepalive RPC. Should be never invoked.
90 final SettableFuture<DOMRpcResult> keepaliveSettableFuture = SettableFuture.create();
91 keepaliveSettableFuture.set(new DefaultDOMRpcResult(Builders.containerBuilder()
92 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID)
95 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
97 //Invoke general RPC on simulated local facade without args (or with null args). Will be returned
98 //settableFuture variable without any set value. WaitingShaduler in keepalive sal facade should wait for any
99 //result from the RPC and reset keepalive scheduler.
100 underlyingSalFacade.invokeNullRpc();
102 //Invoking of general RPC.
103 verify(deviceRpc, after(2000).times(1)).invokeRpc(null, null);
105 //verify the keepalive RPC invoke. Should be never happen.
106 verify(deviceRpc, after(2000).never()).invokeRpc(NETCONF_GET_CONFIG_QNAME, KEEPALIVE_PAYLOAD);
109 private static final class LocalNetconfSalFacade implements RemoteDeviceHandler {
110 private volatile Rpcs.Normalized rpcs;
113 public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
114 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
115 final var newRpcs = services.rpcs();
116 assertThat(newRpcs, instanceOf(Rpcs.Normalized.class));
117 rpcs = (Rpcs.Normalized) newRpcs;
121 public void onDeviceDisconnected() {
126 public void onDeviceFailed(final Throwable throwable) {
130 public void onNotification(final DOMNotification domNotification) {
134 public void close() {
137 public void invokeNullRpc() {
138 final var local = rpcs;
140 local.invokeRpc(null, null);