Merge "Bug 5924 - Reuse Threads using ThreadPool in SystemNotificationListenerImpl"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / listener / SystemNotificationsListenerImplTest.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
9 package org.opendaylight.openflowplugin.impl.connection.listener;
10
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;
16
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;
39
40 /**
41  * Testing basic bahavior of {@link SystemNotificationsListenerImpl}
42  */
43 @RunWith(MockitoJUnitRunner.class)
44 public class SystemNotificationsListenerImplTest {
45
46     public static final int SAFE_TIMEOUT = 1000;
47     private final static int ECHO_REPLY_TIMEOUT = 2000;
48     @Mock
49     private org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter connectionAdapter;
50     @Mock
51     private FeaturesReply features;
52     private ConnectionContext connectionContext;
53
54     private SystemNotificationsListenerImpl systemNotificationsListener;
55     private ConnectionContextImpl connectionContextGolem;
56     private static final NodeId nodeId = new NodeId("OFP:TEST");
57
58     @Before
59     public void setUp() {
60         connectionContextGolem = new ConnectionContextImpl(connectionAdapter);
61         connectionContextGolem.changeStateToWorking();
62         connectionContextGolem.setNodeId(nodeId);
63         connectionContext = Mockito.spy(connectionContextGolem);
64
65         Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(
66                 InetSocketAddress.createUnresolved("unit-odl.example.org", 4242));
67
68         Mockito.when(features.getAuxiliaryId()).thenReturn((short) 0);
69
70         Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
71         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
72
73         final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
74                 60L, TimeUnit.SECONDS,
75                 new SynchronousQueue<>(), "opfpool");
76
77         systemNotificationsListener = new SystemNotificationsListenerImpl(connectionContext, ECHO_REPLY_TIMEOUT, threadPool);
78     }
79
80     @After
81     public void tearDown() throws Exception {
82         Mockito.verifyNoMoreInteractions(connectionContext);
83     }
84
85     /**
86      * successful scenario - connection is on and closes without errors
87      *
88      * @throws Exception
89      */
90     @Test
91     public void testOnDisconnectEvent1() throws Exception {
92         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
93         Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.TRUE));
94
95         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
96         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
97
98         verifyCommonInvocationsSubSet();
99         Mockito.verify(connectionContext).onConnectionClosed();
100         Mockito.verify(connectionContext).getConnectionAdapter();
101         Mockito.verify(connectionContext).getNodeId();
102     }
103
104     /**
105      * broken scenario - connection is on but fails to close
106      *
107      * @throws Exception
108      */
109     @Test
110     public void testOnDisconnectEvent2() throws Exception {
111         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
112         Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.FALSE));
113
114         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
115         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
116
117         verifyCommonInvocationsSubSet();
118         Mockito.verify(connectionContext).onConnectionClosed();
119         Mockito.verify(connectionContext).getConnectionAdapter();
120         Mockito.verify(connectionContext).getNodeId();
121     }
122
123     /**
124      * successful scenario - connection is already down
125      *
126      * @throws Exception
127      */
128     @Test
129     public void testOnDisconnectEvent3() throws Exception {
130         connectionContextGolem.changeStateToTimeouting();
131
132         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
133         Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
134
135         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
136         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
137
138         verifyCommonInvocationsSubSet();
139         Mockito.verify(connectionContext).onConnectionClosed();
140         Mockito.verify(connectionContext).getConnectionAdapter();
141         Mockito.verify(connectionContext).getNodeId();
142     }
143
144     /**
145      * broken scenario - connection is on but throws error on close
146      *
147      * @throws Exception
148      */
149     @Test
150     public void testOnDisconnectEvent4() throws Exception {
151         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP);
152         Mockito.when(connectionAdapter.isAlive()).thenReturn(false);
153
154         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
155         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
156
157         verifyCommonInvocationsSubSet();
158         Mockito.verify(connectionContext).onConnectionClosed();
159         Mockito.verify(connectionContext).getConnectionAdapter();
160         Mockito.verify(connectionContext).getNodeId();
161     }
162
163     /**
164      * first encounter of idle event, echo received successfully
165      *
166      * @throws Exception
167      */
168     @Test
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);
172
173         SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
174         systemNotificationsListener.onSwitchIdleEvent(notification);
175
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());
180
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();
186     }
187
188     /**
189      * first encounter of idle event, echo not receive
190      *
191      * @throws Exception
192      */
193     @Test
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")));
199
200         SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
201         systemNotificationsListener.onSwitchIdleEvent(notification);
202
203         Thread.sleep(SystemNotificationsListenerImpl.MAX_ECHO_REPLY_TIMEOUT + SAFE_TIMEOUT);
204
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();
211
212     }
213
214     private void verifyCommonInvocations() {
215         verifyCommonInvocationsSubSet();
216         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
217     }
218
219     private void verifyCommonInvocationsSubSet() {
220         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionState();
221         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();
222     }
223 }