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