2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.connection.listener;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.net.InetSocketAddress;
14 import java.util.concurrent.SynchronousQueue;
15 import java.util.concurrent.TimeUnit;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
27 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEventBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41 * Testing basic bahavior of {@link SystemNotificationsListenerImpl}
43 @RunWith(MockitoJUnitRunner.class)
44 public class SystemNotificationsListenerImplTest {
46 public static final int SAFE_TIMEOUT = 1000;
47 private final static int ECHO_REPLY_TIMEOUT = 2000;
49 private org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter connectionAdapter;
51 private FeaturesReply features;
52 private ConnectionContext connectionContext;
54 private SystemNotificationsListenerImpl systemNotificationsListener;
55 private ConnectionContextImpl connectionContextGolem;
56 private static final NodeId nodeId = new NodeId("OFP:TEST");
60 connectionContextGolem = new ConnectionContextImpl(connectionAdapter);
61 connectionContextGolem.changeStateToWorking();
62 connectionContextGolem.setNodeId(nodeId);
63 connectionContext = Mockito.spy(connectionContextGolem);
65 Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(
66 InetSocketAddress.createUnresolved("unit-odl.example.org", 4242));
68 Mockito.when(features.getAuxiliaryId()).thenReturn((short) 0);
70 Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
71 Mockito.when(connectionContext.getFeatures()).thenReturn(features);
73 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
74 60L, TimeUnit.SECONDS,
75 new SynchronousQueue<>(), "opfpool");
77 systemNotificationsListener = new SystemNotificationsListenerImpl(connectionContext, ECHO_REPLY_TIMEOUT, threadPool);
81 public void tearDown() throws Exception {
82 Mockito.verifyNoMoreInteractions(connectionContext);
86 * successful scenario - connection is on and closes without errors
91 public void testOnDisconnectEvent1() throws Exception {
92 Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
93 Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.TRUE));
95 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
96 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
98 verifyCommonInvocationsSubSet();
99 Mockito.verify(connectionContext).onConnectionClosed();
100 Mockito.verify(connectionContext).getConnectionAdapter();
101 Mockito.verify(connectionContext).getNodeId();
105 * broken scenario - connection is on but fails to close
110 public void testOnDisconnectEvent2() throws Exception {
111 Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
112 Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.FALSE));
114 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
115 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
117 verifyCommonInvocationsSubSet();
118 Mockito.verify(connectionContext).onConnectionClosed();
119 Mockito.verify(connectionContext).getConnectionAdapter();
120 Mockito.verify(connectionContext).getNodeId();
124 * successful scenario - connection is already down
129 public void testOnDisconnectEvent3() throws Exception {
130 connectionContextGolem.changeStateToTimeouting();
132 Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
133 Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
135 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
136 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
138 verifyCommonInvocationsSubSet();
139 Mockito.verify(connectionContext).onConnectionClosed();
140 Mockito.verify(connectionContext).getConnectionAdapter();
141 Mockito.verify(connectionContext).getNodeId();
145 * broken scenario - connection is on but throws error on close
150 public void testOnDisconnectEvent4() throws Exception {
151 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP);
152 Mockito.when(connectionAdapter.isAlive()).thenReturn(false);
154 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
155 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
157 verifyCommonInvocationsSubSet();
158 Mockito.verify(connectionContext).onConnectionClosed();
159 Mockito.verify(connectionContext).getConnectionAdapter();
160 Mockito.verify(connectionContext).getNodeId();
164 * first encounter of idle event, echo received successfully
169 public void testOnSwitchIdleEvent1() throws Exception {
170 final SettableFuture<RpcResult<EchoOutput>> echoReply = SettableFuture.create();
171 Mockito.when(connectionAdapter.echo(Matchers.any(EchoInput.class))).thenReturn(echoReply);
173 SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
174 systemNotificationsListener.onSwitchIdleEvent(notification);
176 // make sure that the idle notification processing thread started
177 Thread.sleep(SAFE_TIMEOUT);
178 EchoOutput echoReplyVal = new EchoOutputBuilder().build();
179 echoReply.set(RpcResultBuilder.success(echoReplyVal).build());
181 verifyCommonInvocations();
182 Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
183 Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
184 Mockito.verify(connectionContext).changeStateToTimeouting();
185 Mockito.verify(connectionContext).changeStateToWorking();
189 * first encounter of idle event, echo not receive
194 public void testOnSwitchIdleEvent2() throws Exception {
195 final SettableFuture<RpcResult<EchoOutput>> echoReply = SettableFuture.create();
196 Mockito.when(connectionAdapter.echo(Matchers.any(EchoInput.class))).thenReturn(echoReply);
197 Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
198 Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
200 SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
201 systemNotificationsListener.onSwitchIdleEvent(notification);
203 Thread.sleep(SystemNotificationsListenerImpl.MAX_ECHO_REPLY_TIMEOUT + SAFE_TIMEOUT);
205 verifyCommonInvocations();
206 Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
207 Mockito.verify(connectionAdapter).disconnect();
208 Mockito.verify(connectionContext).changeStateToTimeouting();
209 Mockito.verify(connectionContext).closeConnection(true);
210 Mockito.verify(connectionContext).getNodeId();
214 private void verifyCommonInvocations() {
215 verifyCommonInvocationsSubSet();
216 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
219 private void verifyCommonInvocationsSubSet() {
220 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionState();
221 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();