Add RemoteDeviceServices
[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 java.util.Objects.requireNonNull;
11 import static org.mockito.Mockito.after;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.verify;
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_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID;
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.eclipse.jdt.annotation.NonNull;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.mdsal.dom.api.DOMNotification;
31 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
32 import org.opendaylight.mdsal.dom.api.DOMRpcService;
33 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
34 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
35 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
36 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
37 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
38 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
39 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
40 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
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 @NonNull ContainerNode KEEPALIVE_PAYLOAD =
50         NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_NODEID,
51             getSourceNode(NETCONF_RUNNING_NODEID), NetconfMessageTransformUtil.EMPTY_FILTER);
52
53     private KeepaliveSalFacade keepaliveSalFacade;
54     private ScheduledExecutorService executorService;
55
56     private LocalNetconfSalFacade underlyingSalFacade;
57
58     @Mock
59     private DOMRpcService deviceRpc;
60
61     @Mock
62     private NetconfDeviceCommunicator listener;
63
64     @Before
65     public void setUp() throws Exception {
66         executorService = Executors.newScheduledThreadPool(2);
67
68         underlyingSalFacade = new LocalNetconfSalFacade();
69         keepaliveSalFacade = new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorService, 2L, 10000L);
70         keepaliveSalFacade.setListener(listener);
71     }
72
73     @After
74     public void tearDown() {
75         executorService.shutdown();
76     }
77
78     /**
79      * Not sending keepalive rpc test while the repsonse is processing.
80      */
81     @Test
82     public void testKeepaliveSalResponseWaiting() {
83         //This settable future object will be never set to any value. The test wants to simulate waiting for the result
84         //of the future object.
85         final SettableFuture<DOMRpcResult> settableFuture = SettableFuture.create();
86         doReturn(settableFuture).when(deviceRpc).invokeRpc(null, null);
87
88         //This settable future will be used to check the invokation of keepalive RPC. Should be never invoked.
89         final SettableFuture<DOMRpcResult> keepaliveSettableFuture = SettableFuture.create();
90         keepaliveSettableFuture.set(new DefaultDOMRpcResult(Builders.containerBuilder()
91             .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID)
92             .build()));
93
94         keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
95
96         //Invoke general RPC on simulated local facade without args (or with null args). Will be returned
97         //settableFuture variable without any set value. WaitingShaduler in keepalive sal facade should wait for any
98         //result from the RPC and reset keepalive scheduler.
99         underlyingSalFacade.invokeNullRpc();
100
101         //Invoking of general RPC.
102         verify(deviceRpc, after(2000).times(1)).invokeRpc(null, null);
103
104         //verify the keepalive RPC invoke. Should be never happen.
105         verify(deviceRpc, after(2000).never()).invokeRpc(NETCONF_GET_CONFIG_QNAME, KEEPALIVE_PAYLOAD);
106     }
107
108     private static final class LocalNetconfSalFacade implements RemoteDeviceHandler {
109         private volatile RemoteDeviceServices currentServices;
110
111         @Override
112         public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
113                 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
114             currentServices = requireNonNull(services);
115         }
116
117         @Override
118         public void onDeviceDisconnected() {
119             currentServices = null;
120         }
121
122         @Override
123         public void onDeviceFailed(final Throwable throwable) {
124         }
125
126         @Override
127         public void onNotification(final DOMNotification domNotification) {
128         }
129
130         @Override
131         public void close() {
132         }
133
134         public void invokeNullRpc() {
135             final var local = currentServices;
136             if (local != null) {
137                 local.rpcs().invokeRpc(null, null);
138             }
139         }
140     }
141 }
142