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