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