Ditch use of SystemNotificationsListener
[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 static org.mockito.ArgumentMatchers.any;
12
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.mdsal.binding.api.NotificationPublishService;
27 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
28 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter.SystemListener;
29 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
30 import org.opendaylight.openflowplugin.api.openflow.connection.DeviceConnectionStatusProvider;
31 import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl;
32 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEventBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43 import org.opendaylight.yangtools.yang.common.Uint32;
44 import org.opendaylight.yangtools.yang.common.Uint8;
45
46 /**
47  * Testing basic bahavior of {@link SystemNotificationsListenerImpl}.
48  */
49 @RunWith(MockitoJUnitRunner.class)
50 public class SystemNotificationsListenerImplTest {
51
52     private static final int SAFE_TIMEOUT = 1000;
53     private static final int ECHO_REPLY_TIMEOUT = 2000;
54
55     @Mock
56     private ConnectionAdapter connectionAdapter;
57     @Mock
58     private FeaturesReply features;
59     @Mock
60     private DeviceConnectionStatusProvider deviceConnectionStatusProvider;
61     @Mock
62     private NotificationPublishService notificationPublishService;
63
64     private ConnectionContext connectionContext;
65     private ConnectionContextImpl connectionContextGolem;
66     private SystemListener systemNotificationsListener;
67
68     private static final NodeId NODE_ID =
69             new NodeId("OFP:TEST");
70
71     private final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(
72             0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), "opfpool");
73
74     @Before
75     public void setUp() {
76         connectionContextGolem = new ConnectionContextImpl(connectionAdapter, deviceConnectionStatusProvider);
77         connectionContextGolem.changeStateToWorking();
78         connectionContextGolem.setNodeId(NODE_ID);
79         connectionContextGolem.setFeatures(features);
80         connectionContext = Mockito.spy(connectionContextGolem);
81
82         Mockito.when(connectionAdapter.getRemoteAddress()).thenReturn(
83                 InetSocketAddress.createUnresolved("unit-odl.example.org", 4242));
84
85         Mockito.when(features.getAuxiliaryId()).thenReturn(Uint8.ZERO);
86
87         systemNotificationsListener = new SystemNotificationsListenerImpl(connectionContext, ECHO_REPLY_TIMEOUT,
88                 threadPool, notificationPublishService);
89     }
90
91     @After
92     public void tearDown() {
93         Mockito.verifyNoMoreInteractions(connectionContext);
94     }
95
96     /**
97      * Successful scenario - connection is on and closes without errors.
98      */
99     @Test
100     public void testOnDisconnectEvent1() {
101
102         DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build();
103         systemNotificationsListener.onDisconnect(disconnectNotification);
104
105         verifyCommonInvocationsSubSet();
106         Mockito.verify(connectionContext).onConnectionClosed();
107         Mockito.verify(connectionContext).getConnectionAdapter();
108         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
109     }
110
111     /**
112      * Broken scenario - connection is on but fails to close.
113      */
114     @Test
115     public void testOnDisconnectEvent2() {
116
117         systemNotificationsListener.onDisconnect(new DisconnectEventBuilder().setInfo("testing disconnect").build());
118
119         verifyCommonInvocationsSubSet();
120         Mockito.verify(connectionContext).onConnectionClosed();
121         Mockito.verify(connectionContext).getConnectionAdapter();
122         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
123     }
124
125     /**
126      * Successful scenario - connection is already down.
127      */
128     @Test
129     public void testOnDisconnectEvent3() {
130         connectionContextGolem.changeStateToTimeouting();
131
132         systemNotificationsListener.onDisconnect(new DisconnectEventBuilder().setInfo("testing disconnect").build());
133
134         verifyCommonInvocationsSubSet();
135         Mockito.verify(connectionContext).onConnectionClosed();
136         Mockito.verify(connectionContext).getConnectionAdapter();
137         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
138     }
139
140     /**
141      * Broken scenario - connection is on but throws error on close.
142      */
143     @Test
144     public void testOnDisconnectEvent4() {
145         Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP);
146
147         systemNotificationsListener.onDisconnect(new DisconnectEventBuilder().setInfo("testing disconnect").build());
148
149         verifyCommonInvocationsSubSet();
150         Mockito.verify(connectionContext).onConnectionClosed();
151         Mockito.verify(connectionContext).getConnectionAdapter();
152         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
153     }
154
155     /**
156      * First encounter of idle event, echo received successfully.
157      */
158     @Test
159     public void testOnSwitchIdleEvent1() throws Exception {
160         final ListenableFuture<RpcResult<EchoOutput>> echoReply =
161                 RpcResultBuilder.success(new EchoOutputBuilder().setXid(Uint32.ZERO).build()).buildFuture();
162
163         Mockito.when(connectionAdapter.echo(any(EchoInput.class))).thenReturn(echoReply);
164
165         systemNotificationsListener.onSwitchIdle(
166             new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build());
167
168         // make sure that the idle notification processing thread started
169         Thread.sleep(SAFE_TIMEOUT);
170
171         verifyCommonInvocations();
172         Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(any(EchoInput.class));
173         Mockito.verify(connectionAdapter, Mockito.never()).disconnect();
174         Mockito.verify(connectionContext).changeStateToTimeouting();
175         Mockito.verify(connectionContext).changeStateToWorking();
176     }
177
178     /**
179      * First encounter of idle event, echo not receive.
180      */
181     @Test
182     public void testOnSwitchIdleEvent2() throws Exception {
183         final SettableFuture<RpcResult<EchoOutput>> echoReply = SettableFuture.create();
184         Mockito.when(connectionAdapter.echo(any(EchoInput.class))).thenReturn(echoReply);
185         Mockito.when(connectionAdapter.isAlive()).thenReturn(true);
186         Mockito.when(connectionAdapter.disconnect())
187                 .thenReturn(Futures.immediateFailedFuture(new Exception("unit exception")));
188
189         systemNotificationsListener.onSwitchIdle(
190             new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build());
191
192         Thread.sleep(SystemNotificationsListenerImpl.MAX_ECHO_REPLY_TIMEOUT + SAFE_TIMEOUT);
193
194         verifyCommonInvocations();
195         Mockito.verify(connectionAdapter, Mockito.timeout(SAFE_TIMEOUT)).echo(any(EchoInput.class));
196         Mockito.verify(connectionAdapter).disconnect();
197         Mockito.verify(connectionContext).changeStateToTimeouting();
198         Mockito.verify(connectionContext).closeConnection(true);
199         Mockito.verify(connectionContext, Mockito.atLeastOnce()).getSafeNodeIdForLOG();
200
201     }
202
203     private void verifyCommonInvocations() {
204         verifyCommonInvocationsSubSet();
205         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getFeatures();
206         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionAdapter();
207     }
208
209     private void verifyCommonInvocationsSubSet() {
210         Mockito.verify(connectionContext, Mockito.timeout(SAFE_TIMEOUT).atLeastOnce()).getConnectionState();
211     }
212 }