b6fe7827700481fa4c4f56c6d2a1537ac55b8926
[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.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;
40
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);
49
50     @Mock
51     private Rpcs.Normalized deviceRpc;
52     @Mock
53     private NetconfDeviceCommunicator listener;
54     private DefaultNetconfTimer timer;
55
56     private KeepaliveSalFacade keepaliveSalFacade;
57     private LocalNetconfSalFacade underlyingSalFacade;
58
59     @BeforeEach
60     void beforeEach() {
61         timer = new DefaultNetconfTimer();
62
63         underlyingSalFacade = new LocalNetconfSalFacade();
64         keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, timer, 2L, 10000L);
65         keepaliveSalFacade.setListener(listener);
66     }
67
68     @AfterEach
69     void afterEach() {
70         timer.close();
71     }
72
73     /**
74      * Not sending keepalive rpc test while the repsonse is processing.
75      */
76     @Test
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);
82
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)
87             .build()));
88
89         keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
90
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();
95
96         //Invoking of general RPC.
97         verify(deviceRpc, after(2000).times(1)).invokeRpc(null, null);
98
99         //verify the keepalive RPC invoke. Should be never happen.
100         verify(deviceRpc, after(2000).never()).invokeRpc(GetConfig.QNAME, KEEPALIVE_PAYLOAD);
101     }
102
103     private static final class LocalNetconfSalFacade implements RemoteDeviceHandler {
104         private volatile Rpcs.Normalized rpcs;
105
106         @Override
107         public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
108                 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
109             rpcs = assertInstanceOf(Rpcs.Normalized.class, services.rpcs());
110         }
111
112         @Override
113         public void onDeviceDisconnected() {
114             rpcs = null;
115         }
116
117         @Override
118         public void onDeviceFailed(final Throwable throwable) {
119             // No-op
120         }
121
122         @Override
123         public void onNotification(final DOMNotification domNotification) {
124             // No-op
125         }
126
127         @Override
128         public void close() {
129             // No-op
130         }
131
132         public void invokeNullRpc() {
133             final var local = rpcs;
134             if (local != null) {
135                 local.invokeRpc(null, null);
136             }
137         }
138     }
139 }
140