Merge "Bug 5577 Retry mechanism"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / LifecycleConductorImplTest.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 package org.opendaylight.openflowplugin.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import com.google.common.util.concurrent.ListenableFuture;
17 import io.netty.util.HashedWheelTimer;
18 import io.netty.util.TimerTask;
19 import java.math.BigInteger;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.TimeUnit;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
33 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
34 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
35 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
36 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
37 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class LifecycleConductorImplTest {
44
45     private LifecycleConductorImpl lifecycleConductor;
46
47     @Mock
48     private MessageIntelligenceAgency messageIntelligenceAgency;
49     @Mock
50     private ServiceChangeListener serviceChangeListener;
51     @Mock
52     private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
53     @Mock
54     private DeviceContext deviceContext;
55     @Mock
56     private DeviceManager deviceManager;
57     @Mock
58     private DeviceState deviceState;
59     @Mock
60     private ConnectionContext connectionContext;
61     @Mock
62     private FeaturesReply featuresReply;
63     @Mock
64     private TimerTask timerTask;
65     @Mock
66     private TimeUnit timeUnit;
67     @Mock
68     private HashedWheelTimer hashedWheelTimer;
69     @Mock
70     private ListenableFuture<Void> listenableFuture;
71     @Mock
72     private StatisticsManager statisticsManager;
73     @Mock
74     private RpcManager rpcManager;
75     @Mock
76     private RpcContext rpcContext;
77     @Mock
78     private DeviceInfo deviceInfo;
79
80     private NodeId nodeId = new NodeId("openflow-junit:1");
81     private OfpRole ofpRole = OfpRole.NOCHANGE;
82     private long delay = 42;
83
84     @Before
85     public void setUp() {
86
87         lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
88         lifecycleConductor.setSafelyManager(deviceManager);
89         lifecycleConductor.setSafelyManager(statisticsManager);
90         lifecycleConductor.setSafelyManager(rpcManager);
91
92         when(deviceManager.gainContext(Mockito.<DeviceInfo>any())).thenReturn(deviceContext);
93         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
94         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
95         when(rpcManager.gainContext(Mockito.<DeviceInfo>any())).thenReturn(rpcContext);
96         when(deviceInfo.getNodeId()).thenReturn(nodeId);
97         when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
98         when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
99         when(rpcManager.gainContext(Mockito.<DeviceInfo>any())).thenReturn(rpcContext);
100     }
101
102
103
104     @Test
105     public void addOneTimeListenerWhenServicesChangesDoneTest() {
106         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
107         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
108     }
109
110
111     /**
112      * If serviceChangeListeners is empty NOTHING should happen
113      */
114     @Test
115     public void notifyServiceChangeListenersTest1() {
116         lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
117         when(serviceChangeListeners.size()).thenReturn(0);
118         verify(serviceChangeListeners,times(0)).remove(deviceInfo);
119     }
120
121     /**
122      * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
123      */
124     @Test
125     public void notifyServiceChangeListenersTest2() {
126         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
127         assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
128         lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
129         assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
130     }
131
132
133     /**
134      * When success flag is set to FALSE nodeID connection should be closed
135      */
136     @Test
137     public void roleInitializationDoneTest1() {
138         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
139         lifecycleConductor.roleInitializationDone(deviceInfo,false);
140         verify(deviceContext,times(1)).shutdownConnection();
141     }
142
143     /**
144      * When success flag is set to TRUE LOG should be printed
145      */
146     @Test
147     public void roleInitializationDoneTest2() {
148         lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
149         lifecycleConductor.roleInitializationDone(deviceInfo,true);
150         verify(deviceContext,times(0)).shutdownConnection();
151     }
152
153     /**
154      * When getDeviceContext returns null nothing should happen
155      */
156     @Test
157     public void roleChangeOnDeviceTest1() {
158         when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
159         lifecycleConductor.roleChangeOnDevice(deviceInfo,true,ofpRole,false);
160         verify(deviceContext,times(0)).shutdownConnection();
161         lifecycleConductor.roleChangeOnDevice(deviceInfo,false,ofpRole,false);
162         verify(deviceContext,times(0)).shutdownConnection();
163     }
164
165     /**
166      * When success flag is set to FALSE connection should be closed
167      */
168     @Test
169     public void roleChangeOnDeviceTest2() {
170         when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
171         lifecycleConductor.roleChangeOnDevice(deviceInfo,false,ofpRole,false);
172         verify(deviceContext,times(1)).shutdownConnection();
173     }
174
175     /**
176      * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
177      * device should be skipped
178      */
179     @Test
180     public void roleChangeOnDeviceTest3() {
181         when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
182         lifecycleConductor.roleChangeOnDevice(deviceInfo,true,ofpRole,true);
183         verify(deviceContext,times(0)).shutdownConnection();
184     }
185
186     /**
187      * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
188      */
189     @Test
190     public void roleChangeOnDeviceTest4() {
191         when(deviceContext.getDeviceState()).thenReturn(deviceState);
192         when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
193         when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
194         lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMEMASTER,false);
195         verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
196     }
197
198     /**
199      * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
200      */
201     @Test
202     public void roleChangeOnDeviceTest5() {
203         when(deviceContext.getDeviceState()).thenReturn(deviceState);
204         when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
205         when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
206         lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMESLAVE,false);
207         verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
208     }
209
210     /**
211      * If getDeviceContext return null then null should be returned
212      */
213     @Test
214     public void gainConnectionStateSafelyTest1() {
215         when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
216         assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
217     }
218
219     /**
220      * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
221      */
222     @Test
223     public void gainConnectionStateSafelyTest2() {
224         lifecycleConductor.gainConnectionStateSafely(deviceInfo);
225         verify(deviceContext,times(1)).getPrimaryConnectionContext();
226     }
227
228     /**
229      * If getDeviceContext returns null then null should be returned
230      */
231     @Test
232     public void reserveXidForDeviceMessageTest1() {
233         when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
234         assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
235     }
236
237     /**
238      * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
239      */
240     @Test
241     public void reserveXidForDeviceMessageTest2() {
242         lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
243         verify(deviceContext,times(1)).reserveXidForDeviceMessage();
244     }
245
246     /**
247      * When succes flag is set to FALSE connection should be closed
248      */
249     @Test
250     public void deviceStartInitializationDoneTest() {
251         lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
252         verify(deviceContext,times(1)).shutdownConnection();
253     }
254
255     /**
256      * When succes flag is set to FALSE connection should be closed
257      */
258     @Test
259     public void deviceInitializationDoneTest() {
260         lifecycleConductor.deviceInitializationDone(deviceInfo, false);
261         verify(deviceContext,times(1)).shutdownConnection();
262     }
263 }