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.Future;
15 import java.util.concurrent.SynchronousQueue;
16 import java.util.concurrent.TimeUnit;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
27 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
28 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
29 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEventBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43 * Testing basic bahavior of {@link SystemNotificationsListenerImpl}.
45 @RunWith(MockitoJUnitRunner.class)
46 public class SystemNotificationsListenerImplTest {
48 private static final int SAFE_TIMEOUT = 1000;
49 private static final int ECHO_REPLY_TIMEOUT = 2000;
52 private ConnectionAdapter connectionAdapter;
54 private FeaturesReply features;
56 private ConnectionContext connectionContext;
57 private ConnectionContextImpl connectionContextGolem;
58 private SystemNotificationsListenerImpl systemNotificationsListener;
60 private static final NodeId NODE_ID =
61 new NodeId("OFP:TEST");
63 private final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(
64 0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), "opfpool");
68 connectionContextGolem = new ConnectionContextImpl(connectionAdapter);
69 connectionContextGolem.changeStateToWorking();
70 connectionContextGolem.setNodeId(NODE_ID);
71 connectionContext = Mockito.spy(connectionContextGolem);
73 Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(
74 InetSocketAddress.createUnresolved("unit-odl.example.org", 4242));
76 Mockito.when(features.getAuxiliaryId()).thenReturn((short) 0);
78 Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
79 Mockito.when(connectionContext.getFeatures()).thenReturn(features);
81 systemNotificationsListener =
82 new SystemNotificationsListenerImpl(connectionContext, ECHO_REPLY_TIMEOUT, threadPool);
87 public void tearDown() throws Exception {
88 Mockito.verifyNoMoreInteractions(connectionContext);
92 * Successful scenario - connection is on and closes without errors.
95 public void testOnDisconnectEvent1() throws Exception {
96 Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
97 Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.TRUE));
99 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
100 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
102 verifyCommonInvocationsSubSet();
103 Mockito.verify(connectionContext).onConnectionClosed();
104 Mockito.verify(connectionContext).getConnectionAdapter();
105 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
109 * Broken scenario - connection is on but fails to close.
112 public void testOnDisconnectEvent2() throws Exception {
113 Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
114 Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.FALSE));
116 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
117 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
119 verifyCommonInvocationsSubSet();
120 Mockito.verify(connectionContext).onConnectionClosed();
121 Mockito.verify(connectionContext).getConnectionAdapter();
122 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
126 * 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())
134 .thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
136 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
137 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
139 verifyCommonInvocationsSubSet();
140 Mockito.verify(connectionContext).onConnectionClosed();
141 Mockito.verify(connectionContext).getConnectionAdapter();
142 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
146 * Broken scenario - connection is on but throws error on close.
149 public void testOnDisconnectEvent4() throws Exception {
150 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP);
151 Mockito.when(connectionAdapter.isAlive()).thenReturn(false);
153 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
154 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
156 verifyCommonInvocationsSubSet();
157 Mockito.verify(connectionContext).onConnectionClosed();
158 Mockito.verify(connectionContext).getConnectionAdapter();
159 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
163 * First encounter of idle event, echo received successfully.
166 public void testOnSwitchIdleEvent1() throws Exception {
167 final Future<RpcResult<EchoOutput>> echoReply =
168 Futures.immediateFuture(RpcResultBuilder.success(new EchoOutputBuilder().setXid(0L).build()).build());
170 Mockito.when(connectionAdapter.echo(Matchers.any(EchoInput.class))).thenReturn(echoReply);
172 SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
173 systemNotificationsListener.onSwitchIdleEvent(notification);
175 // make sure that the idle notification processing thread started
176 Thread.sleep(SAFE_TIMEOUT);
178 verifyCommonInvocations();
179 Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
180 Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
181 Mockito.verify(connectionContext).changeStateToTimeouting();
182 Mockito.verify(connectionContext).changeStateToWorking();
186 * First encounter of idle event, echo not receive.
189 public void testOnSwitchIdleEvent2() throws Exception {
190 final SettableFuture<RpcResult<EchoOutput>> echoReply = SettableFuture.create();
191 Mockito.when(connectionAdapter.echo(Matchers.any(EchoInput.class))).thenReturn(echoReply);
192 Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
193 Mockito.when(connectionAdapter.disconnect())
194 .thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
196 SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
197 systemNotificationsListener.onSwitchIdleEvent(notification);
199 Thread.sleep(SystemNotificationsListenerImpl.MAX_ECHO_REPLY_TIMEOUT + SAFE_TIMEOUT);
201 verifyCommonInvocations();
202 Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
203 Mockito.verify(connectionAdapter).disconnect();
204 Mockito.verify(connectionContext).changeStateToTimeouting();
205 Mockito.verify(connectionContext).closeConnection(true);
206 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
210 private void verifyCommonInvocations() {
211 verifyCommonInvocationsSubSet();
212 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
215 private void verifyCommonInvocationsSubSet() {
216 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionState();
217 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();