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.client.mdsal.spi;
10 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
11 import static org.mockito.Mockito.after;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.verify;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.net.InetSocketAddress;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.junit.jupiter.api.AfterEach;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.jupiter.MockitoExtension;
24 import org.opendaylight.mdsal.dom.api.DOMNotification;
25 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
26 import org.opendaylight.mdsal.dom.api.DOMRpcService;
27 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
28 import org.opendaylight.netconf.client.mdsal.NetconfDeviceCommunicator;
29 import org.opendaylight.netconf.client.mdsal.NetconfDeviceSchema;
30 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
31 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceHandler;
32 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
33 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
34 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
35 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
36 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
37 import org.opendaylight.netconf.common.impl.DefaultNetconfTimer;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.GetConfig;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
42 @ExtendWith(MockitoExtension.class)
43 class KeepaliveSalFacadeResponseWaitingTest {
44 private static final RemoteDeviceId REMOTE_DEVICE_ID =
45 new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
46 private static final @NonNull ContainerNode KEEPALIVE_PAYLOAD = NetconfMessageTransformUtil.wrap(
47 NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID,
48 NetconfBaseOps.getSourceNode(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID),
49 NetconfMessageTransformUtil.EMPTY_FILTER);
52 private Rpcs.Normalized deviceRpc;
54 private DOMRpcService deviceDomRpc;
56 private NetconfDeviceCommunicator listener;
57 private DefaultNetconfTimer timer;
59 private KeepaliveSalFacade keepaliveSalFacade;
60 private LocalNetconfSalFacade underlyingSalFacade;
64 timer = new DefaultNetconfTimer();
66 underlyingSalFacade = new LocalNetconfSalFacade();
67 keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, timer, 2L, 10000L);
68 keepaliveSalFacade.setListener(listener);
77 * Not sending keepalive rpc test while the repsonse is processing.
80 void testKeepaliveSalResponseWaiting() {
81 //This settable future object will be never set to any value. The test wants to simulate waiting for the result
82 //of the future object.
83 final var settableFuture = SettableFuture.<DOMRpcResult>create();
84 doReturn(settableFuture).when(deviceDomRpc).invokeRpc(null, null);
85 doReturn(deviceDomRpc).when(deviceRpc).domRpcService();
87 //This settable future will be used to check the invokation of keepalive RPC. Should be never invoked.
88 final var keepaliveSettableFuture = SettableFuture.<DOMRpcResult>create();
89 keepaliveSettableFuture.set(new DefaultDOMRpcResult(Builders.containerBuilder()
90 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID)
93 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
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(deviceDomRpc, after(2000).times(1)).invokeRpc(null, null);
103 //verify the keepalive RPC invoke. Should be never happen.
104 verify(deviceDomRpc, after(2000).never()).invokeRpc(GetConfig.QNAME, KEEPALIVE_PAYLOAD);
107 private static final class LocalNetconfSalFacade implements RemoteDeviceHandler {
108 private volatile Rpcs.Normalized rpcs;
111 public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
112 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
113 rpcs = assertInstanceOf(Rpcs.Normalized.class, services.rpcs());
117 public void onDeviceDisconnected() {
122 public void onDeviceFailed(final Throwable throwable) {
127 public void onNotification(final DOMNotification domNotification) {
132 public void close() {
136 public void invokeNullRpc() {
137 final var local = rpcs;
139 local.domRpcService().invokeRpc(null, null);