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 static org.mockito.ArgumentMatchers.any;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.net.InetSocketAddress;
17 import java.util.concurrent.SynchronousQueue;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.junit.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 {
97 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
98 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
100 verifyCommonInvocationsSubSet();
101 Mockito.verify(connectionContext).onConnectionClosed();
102 Mockito.verify(connectionContext).getConnectionAdapter();
103 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
107 * Broken scenario - connection is on but fails to close.
110 public void testOnDisconnectEvent2() throws Exception {
112 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
113 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
115 verifyCommonInvocationsSubSet();
116 Mockito.verify(connectionContext).onConnectionClosed();
117 Mockito.verify(connectionContext).getConnectionAdapter();
118 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
122 * Successful scenario - connection is already down.
125 public void testOnDisconnectEvent3() throws Exception {
126 connectionContextGolem.changeStateToTimeouting();
128 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
129 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
131 verifyCommonInvocationsSubSet();
132 Mockito.verify(connectionContext).onConnectionClosed();
133 Mockito.verify(connectionContext).getConnectionAdapter();
134 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
138 * Broken scenario - connection is on but throws error on close.
141 public void testOnDisconnectEvent4() throws Exception {
142 Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP);
144 DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
145 systemNotificationsListener.onDisconnectEvent(disconnectNotification);
147 verifyCommonInvocationsSubSet();
148 Mockito.verify(connectionContext).onConnectionClosed();
149 Mockito.verify(connectionContext).getConnectionAdapter();
150 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
154 * First encounter of idle event, echo received successfully.
157 public void testOnSwitchIdleEvent1() throws Exception {
158 final ListenableFuture<RpcResult<EchoOutput>> echoReply =
159 Futures.immediateFuture(RpcResultBuilder.success(new EchoOutputBuilder().setXid(0L).build()).build());
161 Mockito.when(connectionAdapter.echo(any(EchoInput.class))).thenReturn(echoReply);
163 SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
164 systemNotificationsListener.onSwitchIdleEvent(notification);
166 // make sure that the idle notification processing thread started
167 Thread.sleep(SAFE_TIMEOUT);
169 verifyCommonInvocations();
170 Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(any(EchoInput.class));
171 Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
172 Mockito.verify(connectionContext).changeStateToTimeouting();
173 Mockito.verify(connectionContext).changeStateToWorking();
177 * First encounter of idle event, echo not receive.
180 public void testOnSwitchIdleEvent2() throws Exception {
181 final SettableFuture<RpcResult<EchoOutput>> echoReply = SettableFuture.create();
182 Mockito.when(connectionAdapter.echo(any(EchoInput.class))).thenReturn(echoReply);
183 Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
184 Mockito.when(connectionAdapter.disconnect())
185 .thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
187 SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
188 systemNotificationsListener.onSwitchIdleEvent(notification);
190 Thread.sleep(SystemNotificationsListenerImpl.MAX_ECHO_REPLY_TIMEOUT + SAFE_TIMEOUT);
192 verifyCommonInvocations();
193 Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(any(EchoInput.class));
194 Mockito.verify(connectionAdapter).disconnect();
195 Mockito.verify(connectionContext).changeStateToTimeouting();
196 Mockito.verify(connectionContext).closeConnection(true);
197 Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
201 private void verifyCommonInvocations() {
202 verifyCommonInvocationsSubSet();
203 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
206 private void verifyCommonInvocationsSubSet() {
207 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionState();
208 Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();