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