NETCONF-608 - Change Netconf keepalives to not send during large payload replies
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / 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.sal.connect.netconf.sal;
9
10 import static org.mockito.Mockito.after;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps.getSourceNode;
15 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_NODEID;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_PATH;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
18
19 import com.google.common.util.concurrent.SettableFuture;
20 import java.net.InetSocketAddress;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.ScheduledExecutorService;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMActionService;
30 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
31 import org.opendaylight.mdsal.dom.api.DOMNotification;
32 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
33 import org.opendaylight.mdsal.dom.api.DOMRpcService;
34 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
35 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
36 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
37 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
38 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
39 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44
45 public class KeepaliveSalFacadeResponseWaitingTest {
46
47     private static final RemoteDeviceId REMOTE_DEVICE_ID =
48             new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
49     private static final ContainerNode KEEPALIVE_PAYLOAD = NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID,
50             getSourceNode(NETCONF_RUNNING_QNAME), NetconfMessageTransformUtil.EMPTY_FILTER);
51
52     private KeepaliveSalFacade keepaliveSalFacade;
53     private ScheduledExecutorService executorService;
54
55     private LocalNetconfSalFacade underlyingSalFacade;
56
57     @Mock
58     private DOMRpcService deviceRpc;
59
60     @Mock
61     private DOMMountPointService mountPointService;
62
63     @Mock
64     private DataBroker dataBroker;
65
66     @Mock
67     private NetconfDeviceCommunicator listener;
68
69     @Before
70     public void setUp() throws Exception {
71         MockitoAnnotations.initMocks(this);
72
73         executorService = Executors.newScheduledThreadPool(2);
74
75         underlyingSalFacade = new LocalNetconfSalFacade();
76         doNothing().when(listener).disconnect();
77         keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorService, 2L, 10000L);
78         keepaliveSalFacade.setListener(listener);
79     }
80
81     @After
82     public void tearDown() {
83         executorService.shutdown();
84     }
85
86     /**
87      * Not sending keepalive rpc test while the repsonse is processing.
88      */
89     @Test
90     public void testKeepaliveSalResponseWaiting() {
91         //This settable future object will be never set to any value. The test wants to simulate waiting for the result
92         //of the future object.
93         final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
94         when(deviceRpc.invokeRpc(null, null)).thenReturn(settableFuture);
95
96         //This settable future will be used to check the invokation of keepalive RPC. Should be never invoked.
97         final SettableFuture<DOMRpcResult> keepaliveSettableFuture = SettableFuture.create();
98         when(deviceRpc.invokeRpc(NETCONF_GET_CONFIG_PATH, KEEPALIVE_PAYLOAD)).thenReturn(keepaliveSettableFuture);
99         final DOMRpcResult keepaliveResult = new DefaultDOMRpcResult(Builders.containerBuilder().withNodeIdentifier(
100                 new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME)).build());
101         keepaliveSettableFuture.set(keepaliveResult);
102
103         keepaliveSalFacade.onDeviceConnected(null, null, deviceRpc);
104
105         //Invoke general RPC on simulated local facade without args (or with null args). Will be returned
106         //settableFuture variable without any set value. WaitingShaduler in keepalive sal facade should wait for any
107         //result from the RPC and reset keepalive scheduler.
108         underlyingSalFacade.invokeNullRpc();
109
110         //Invoking of general RPC.
111         verify(deviceRpc, after(2000).times(1)).invokeRpc(null, null);
112
113         //verify the keepalive RPC invoke. Should be never happen.
114         verify(deviceRpc, after(2000).never()).invokeRpc(NETCONF_GET_CONFIG_PATH, KEEPALIVE_PAYLOAD);
115     }
116
117     private final class LocalNetconfSalFacade implements RemoteDeviceHandler<NetconfSessionPreferences> {
118
119         private DOMRpcService localDeviceRpc;
120
121         @Override
122         public void onDeviceConnected(final SchemaContext remoteSchemaContext,
123                 final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService currentDeviceRpc,
124                 final DOMActionService deviceAction) {
125             localDeviceRpc = currentDeviceRpc;
126         }
127
128         @Override
129         public void onDeviceDisconnected() {
130         }
131
132         @Override
133         public void onDeviceFailed(final Throwable throwable) {
134         }
135
136         @Override
137         public void onNotification(final DOMNotification domNotification) {
138         }
139
140         @Override
141         public void close() {
142         }
143
144         public void invokeNullRpc() {
145             if (localDeviceRpc != null) {
146                 localDeviceRpc.invokeRpc(null, null);
147             }
148         }
149     }
150 }
151