a73e74b0dfca98ec8a377d060b8593af892f9d84
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / spi / KeepaliveSalFacadeResponseWaitingTest.java
1 /*
2  * Copyright (c) 2019 Lumina Networks, Inc. All Rights Reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.client.mdsal.spi;
9
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;
14
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;
41
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);
50
51     @Mock
52     private Rpcs.Normalized deviceRpc;
53     @Mock
54     private DOMRpcService deviceDomRpc;
55     @Mock
56     private NetconfDeviceCommunicator listener;
57     private DefaultNetconfTimer timer;
58
59     private KeepaliveSalFacade keepaliveSalFacade;
60     private LocalNetconfSalFacade underlyingSalFacade;
61
62     @BeforeEach
63     void beforeEach() {
64         timer = new DefaultNetconfTimer();
65
66         underlyingSalFacade = new LocalNetconfSalFacade();
67         keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, timer, 2L, 10000L);
68         keepaliveSalFacade.setListener(listener);
69     }
70
71     @AfterEach
72     void afterEach() {
73         timer.close();
74     }
75
76     /**
77      * Not sending keepalive rpc test while the repsonse is processing.
78      */
79     @Test
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();
86
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)
91             .build()));
92
93         keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
94
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();
99
100         //Invoking of general RPC.
101         verify(deviceDomRpc, after(2000).times(1)).invokeRpc(null, null);
102
103         //verify the keepalive RPC invoke. Should be never happen.
104         verify(deviceDomRpc, after(2000).never()).invokeRpc(GetConfig.QNAME, KEEPALIVE_PAYLOAD);
105     }
106
107     private static final class LocalNetconfSalFacade implements RemoteDeviceHandler {
108         private volatile Rpcs.Normalized rpcs;
109
110         @Override
111         public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
112                 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
113             rpcs = assertInstanceOf(Rpcs.Normalized.class, services.rpcs());
114         }
115
116         @Override
117         public void onDeviceDisconnected() {
118             rpcs = null;
119         }
120
121         @Override
122         public void onDeviceFailed(final Throwable throwable) {
123             // No-op
124         }
125
126         @Override
127         public void onNotification(final DOMNotification domNotification) {
128             // No-op
129         }
130
131         @Override
132         public void close() {
133             // No-op
134         }
135
136         public void invokeNullRpc() {
137             final var local = rpcs;
138             if (local != null) {
139                 local.domRpcService().invokeRpc(null, null);
140             }
141         }
142     }
143 }
144