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.spi.DefaultDOMRpcResult;
27 import org.opendaylight.netconf.client.mdsal.NetconfDeviceCommunicator;
28 import org.opendaylight.netconf.client.mdsal.NetconfDeviceSchema;
29 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
30 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceHandler;
31 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
32 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
33 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
34 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
35 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
36 import org.opendaylight.netconf.common.impl.DefaultNetconfTimer;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.GetConfig;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 @ExtendWith(MockitoExtension.class)
42 class KeepaliveSalFacadeResponseWaitingTest {
43 private static final RemoteDeviceId REMOTE_DEVICE_ID =
44 new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
45 private static final @NonNull ContainerNode KEEPALIVE_PAYLOAD = NetconfMessageTransformUtil.wrap(
46 NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID,
47 NetconfBaseOps.getSourceNode(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID),
48 NetconfMessageTransformUtil.EMPTY_FILTER);
51 private Rpcs.Normalized deviceRpc;
53 private NetconfDeviceCommunicator listener;
54 private DefaultNetconfTimer timer;
56 private KeepaliveSalFacade keepaliveSalFacade;
57 private LocalNetconfSalFacade underlyingSalFacade;
61 timer = new DefaultNetconfTimer();
63 underlyingSalFacade = new LocalNetconfSalFacade();
64 keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, timer, 2L, 10000L);
65 keepaliveSalFacade.setListener(listener);
74 * Not sending keepalive rpc test while the repsonse is processing.
77 void testKeepaliveSalResponseWaiting() {
78 //This settable future object will be never set to any value. The test wants to simulate waiting for the result
79 //of the future object.
80 final var settableFuture = SettableFuture.<DOMRpcResult>create();
81 doReturn(settableFuture).when(deviceRpc).invokeRpc(null, null);
83 //This settable future will be used to check the invokation of keepalive RPC. Should be never invoked.
84 final var keepaliveSettableFuture = SettableFuture.<DOMRpcResult>create();
85 keepaliveSettableFuture.set(new DefaultDOMRpcResult(Builders.containerBuilder()
86 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID)
89 keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
91 //Invoke general RPC on simulated local facade without args (or with null args). Will be returned
92 //settableFuture variable without any set value. WaitingShaduler in keepalive sal facade should wait for any
93 //result from the RPC and reset keepalive scheduler.
94 underlyingSalFacade.invokeNullRpc();
96 //Invoking of general RPC.
97 verify(deviceRpc, after(2000).times(1)).invokeRpc(null, null);
99 //verify the keepalive RPC invoke. Should be never happen.
100 verify(deviceRpc, after(2000).never()).invokeRpc(GetConfig.QNAME, KEEPALIVE_PAYLOAD);
103 private static final class LocalNetconfSalFacade implements RemoteDeviceHandler {
104 private volatile Rpcs.Normalized rpcs;
107 public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
108 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
109 rpcs = assertInstanceOf(Rpcs.Normalized.class, services.rpcs());
113 public void onDeviceDisconnected() {
118 public void onDeviceFailed(final Throwable throwable) {
123 public void onNotification(final DOMNotification domNotification) {
128 public void close() {
132 public void invokeNullRpc() {
133 final var local = rpcs;
135 local.invokeRpc(null, null);