Move netconf-console to apps/
[netconf.git] / plugins / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / KeepaliveSalFacadeTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.isNull;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.timeout;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21
22 import com.google.common.util.concurrent.Futures;
23 import java.net.InetSocketAddress;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.ScheduledExecutorService;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
33 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
34 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
35 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
36 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
37 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
38 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.common.RpcError;
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 KeepaliveSalFacadeTest {
46     private static final RemoteDeviceId REMOTE_DEVICE_ID =
47             new RemoteDeviceId("test", new InetSocketAddress("localhost", 22));
48
49     @Mock
50     private RemoteDeviceHandler underlyingSalFacade;
51     @Mock
52     private NetconfDeviceCommunicator listener;
53     @Mock
54     private Rpcs.Normalized deviceRpc;
55
56     private ScheduledExecutorService executorServiceSpy;
57     private KeepaliveSalFacade keepaliveSalFacade;
58     private Rpcs proxyRpc;
59
60     @Before
61     public void setUp() throws Exception {
62         executorServiceSpy = Executors.newScheduledThreadPool(1);
63
64         doNothing().when(listener).disconnect();
65         doNothing().when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
66
67         keepaliveSalFacade =
68                 new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 1L, 1L);
69         keepaliveSalFacade.setListener(listener);
70     }
71
72     @After
73     public void tearDown() throws Exception {
74         executorServiceSpy.shutdownNow();
75     }
76
77     @Test
78     public void testKeepaliveSuccess() throws Exception {
79         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(Builders.containerBuilder()
80             .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID)
81             .build()))).when(deviceRpc).invokeNetconf(any(), any());
82
83         final var services = new RemoteDeviceServices(deviceRpc, null);
84         keepaliveSalFacade.onDeviceConnected(null, null, services);
85
86         verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
87
88         verify(deviceRpc, timeout(15000).times(5)).invokeNetconf(any(), any());
89     }
90
91     @Test
92     public void testKeepaliveRpcFailure() {
93         doReturn(Futures.immediateFailedFuture(new IllegalStateException("illegal-state")))
94                 .when(deviceRpc).invokeNetconf(any(), any());
95
96         keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
97
98         verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
99
100         // Should disconnect the session
101         verify(listener, timeout(15000).times(1)).disconnect();
102         verify(deviceRpc, times(1)).invokeNetconf(any(), any());
103     }
104
105     @Test
106     public void testKeepaliveSuccessWithRpcError() {
107
108         final var rpcSuccessWithError = new DefaultDOMRpcResult(mock(RpcError.class));
109
110         doReturn(Futures.immediateFuture(rpcSuccessWithError)).when(deviceRpc).invokeNetconf(any(), any());
111
112         keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
113
114         verify(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
115
116         // Shouldn't disconnect the session
117         verify(listener, times(0)).disconnect();
118         verify(deviceRpc, timeout(15000).times(1)).invokeNetconf(any(), any());
119     }
120
121     @Test
122     public void testNonKeepaliveRpcFailure() throws Exception {
123         doAnswer(invocation -> proxyRpc = invocation.getArgument(2, RemoteDeviceServices.class).rpcs())
124                 .when(underlyingSalFacade).onDeviceConnected(isNull(), isNull(), any(RemoteDeviceServices.class));
125
126         doReturn(Futures.immediateFailedFuture(new IllegalStateException("illegal-state")))
127                 .when(deviceRpc).invokeRpc(any(), any());
128
129         keepaliveSalFacade =
130                 new KeepaliveSalFacade(REMOTE_DEVICE_ID, underlyingSalFacade, executorServiceSpy, 100L, 1L);
131         keepaliveSalFacade.setListener(listener);
132
133         keepaliveSalFacade.onDeviceConnected(null, null, new RemoteDeviceServices(deviceRpc, null));
134
135         assertThat(proxyRpc, instanceOf(Rpcs.Normalized.class));
136         ((Rpcs.Normalized) proxyRpc).invokeRpc(QName.create("foo", "bar"), mock(ContainerNode.class));
137
138         verify(listener, times(1)).disconnect();
139     }
140 }