Merge "Drop the odlparent.netty property"
[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.Future;
15 import java.util.concurrent.SynchronousQueue;
16 import java.util.concurrent.TimeUnit;
17
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;
41
42 /**
43  * Testing basic bahavior of {@link SystemNotificationsListenerImpl}
44  */
45 @RunWith(MockitoJUnitRunner.class)
46 public class SystemNotificationsListenerImplTest {
47
48     private static final int SAFE_TIMEOUT = 1000;
49     private final static int ECHO_REPLY_TIMEOUT = 2000;
50
51     @Mock
52     private ConnectionAdapter connectionAdapter;
53     @Mock
54     private FeaturesReply features;
55
56     private ConnectionContext connectionContext;
57     private ConnectionContextImpl connectionContextGolem;
58     private SystemNotificationsListenerImpl systemNotificationsListener;
59
60     private static final NodeId nodeId =
61             new NodeId("OFP:TEST");
62
63     private final ThreadPoolLoggingExecutor threadPool =
64             new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), "opfpool");
65
66     @Before
67     public void setUp() {
68         connectionContextGolem = new ConnectionContextImpl(connectionAdapter);
69         connectionContextGolem.changeStateToWorking();
70         connectionContextGolem.setNodeId(nodeId);
71         connectionContext = Mockito.spy(connectionContextGolem);
72
73         Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(
74                 InetSocketAddress.createUnresolved("unit-odl.example.org", 4242));
75
76         Mockito.when(features.getAuxiliaryId()).thenReturn((short) 0);
77
78         Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
79         Mockito.when(connectionContext.getFeatures()).thenReturn(features);
80
81         systemNotificationsListener =
82                 new SystemNotificationsListenerImpl(connectionContext, ECHO_REPLY_TIMEOUT, threadPool);
83
84     }
85
86     @After
87     public void tearDown() throws Exception {
88         Mockito.verifyNoMoreInteractions(connectionContext);
89     }
90
91     /**
92      * successful scenario - connection is on and closes without errors
93      *
94      * @throws Exception
95      */
96     @Test
97     public void testOnDisconnectEvent1() throws Exception {
98         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
99         Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.TRUE));
100
101         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
102         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
103
104         verifyCommonInvocationsSubSet();
105         Mockito.verify(connectionContext).onConnectionClosed();
106         Mockito.verify(connectionContext).getConnectionAdapter();
107         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
108     }
109
110     /**
111      * broken scenario - connection is on but fails to close
112      *
113      * @throws Exception
114      */
115     @Test
116     public void testOnDisconnectEvent2() throws Exception {
117         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
118         Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.immediateFuture(Boolean.FALSE));
119
120         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
121         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
122
123         verifyCommonInvocationsSubSet();
124         Mockito.verify(connectionContext).onConnectionClosed();
125         Mockito.verify(connectionContext).getConnectionAdapter();
126         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
127     }
128
129     /**
130      * successful scenario - connection is already down
131      *
132      * @throws Exception
133      */
134     @Test
135     public void testOnDisconnectEvent3() throws Exception {
136         connectionContextGolem.changeStateToTimeouting();
137
138         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
139         Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
140
141         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
142         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
143
144         verifyCommonInvocationsSubSet();
145         Mockito.verify(connectionContext).onConnectionClosed();
146         Mockito.verify(connectionContext).getConnectionAdapter();
147         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
148     }
149
150     /**
151      * broken scenario - connection is on but throws error on close
152      *
153      * @throws Exception
154      */
155     @Test
156     public void testOnDisconnectEvent4() throws Exception {
157         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP);
158         Mockito.when(connectionAdapter.isAlive()).thenReturn(false);
159
160         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
161         systemNotificationsListener.onDisconnectEvent(disconnectNotification);
162
163         verifyCommonInvocationsSubSet();
164         Mockito.verify(connectionContext).onConnectionClosed();
165         Mockito.verify(connectionContext).getConnectionAdapter();
166         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
167     }
168
169     /**
170      * first encounter of idle event, echo received successfully
171      *
172      * @throws Exception
173      */
174     @Test
175     public void testOnSwitchIdleEvent1() throws Exception {
176         final Future<RpcResult<EchoOutput>> echoReply = Futures.immediateFuture(RpcResultBuilder.success(new EchoOutputBuilder().setXid(0L).build()).build());
177
178         Mockito.when(connectionAdapter.echo(Matchers.any(EchoInput.class))).thenReturn(echoReply);
179
180         SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
181         systemNotificationsListener.onSwitchIdleEvent(notification);
182
183         // make sure that the idle notification processing thread started
184         Thread.sleep(SAFE_TIMEOUT);
185
186         verifyCommonInvocations();
187         Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
188         Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
189         Mockito.verify(connectionContext).changeStateToTimeouting();
190         Mockito.verify(connectionContext).changeStateToWorking();
191     }
192
193     /**
194      * first encounter of idle event, echo not receive
195      *
196      * @throws Exception
197      */
198     @Test
199     public void testOnSwitchIdleEvent2() throws Exception {
200         final SettableFuture<RpcResult<EchoOutput>> echoReply = SettableFuture.create();
201         Mockito.when(connectionAdapter.echo(Matchers.any(EchoInput.class))).thenReturn(echoReply);
202         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
203         Mockito.when(connectionAdapter.disconnect()).thenReturn(Futures.<Boolean>immediateFailedFuture(new Exception("unit exception")));
204
205         SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build();
206         systemNotificationsListener.onSwitchIdleEvent(notification);
207
208         Thread.sleep(SystemNotificationsListenerImpl.MAX_ECHO_REPLY_TIMEOUT + SAFE_TIMEOUT);
209
210         verifyCommonInvocations();
211         Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(Matchers.any(EchoInput.class));
212         Mockito.verify(connectionAdapter).disconnect();
213         Mockito.verify(connectionContext).changeStateToTimeouting();
214         Mockito.verify(connectionContext).closeConnection(true);
215         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
216
217     }
218
219     private void verifyCommonInvocations() {
220         verifyCommonInvocationsSubSet();
221         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
222     }
223
224     private void verifyCommonInvocationsSubSet() {
225         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionState();
226         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();
227     }
228 }