Conductor RPC registration moved
[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
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.role.RoleManager;
35 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
36 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
37 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class LifecycleConductorImplTest {
45
46     private LifecycleConductorImpl lifecycleConductor;
47
48     @Mock
49     private MessageIntelligenceAgency messageIntelligenceAgency;
50     @Mock
51     private ServiceChangeListener serviceChangeListener;
52     @Mock
53     private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
54     @Mock
55     private DeviceContext deviceContext;
56     @Mock
57     private DeviceManager deviceManager;
58     @Mock
59     private DeviceState deviceState;
60     @Mock
61     private ConnectionContext connectionContext;
62     @Mock
63     private FeaturesReply featuresReply;
64     @Mock
65     private TimerTask timerTask;
66     @Mock
67     private TimeUnit timeUnit;
68     @Mock
69     private HashedWheelTimer hashedWheelTimer;
70     @Mock
71     private ListenableFuture<Void> listenableFuture;
72     @Mock
73     private StatisticsManager statisticsManager;
74     @Mock
75     private RpcManager rpcManager;
76     @Mock
77     private RpcContext rpcContext;
78     @Mock
79     private DeviceInfo deviceInfo;
80
81     private NodeId nodeId = new NodeId("openflow-junit:1");
82     private OfpRole ofpRole = OfpRole.NOCHANGE;
83     private long delay = 42;
84
85     @Before
86     public void setUp() {
87         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
88         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
89
90         lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
91         lifecycleConductor.setSafelyManager(deviceManager);
92         lifecycleConductor.setSafelyManager(statisticsManager);
93         lifecycleConductor.setSafelyManager(rpcManager);
94
95         when(connectionContext.getFeatures()).thenReturn(featuresReply);
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.getDeviceContextFromNodeId(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.getDeviceContextFromNodeId(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.getDeviceContextFromNodeId(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.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
193         when(deviceContext.onClusterRoleChange(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.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
205         when(deviceContext.onClusterRoleChange(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 returns null nothing should happen
212      */
213     @Test
214     public void gainVersionSafelyTest1() {
215         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
216         assertNull(lifecycleConductor.gainVersionSafely(deviceInfo));
217     }
218
219     /**
220      * If getDeviceContext returns deviceContext getPrimaryConnectionContext() should be called
221      */
222     @Test
223     public void gainVersionSafelyTest2() {
224         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
225         lifecycleConductor.gainVersionSafely(deviceInfo);
226         verify(deviceContext,times(1)).getPrimaryConnectionContext();
227     }
228
229     /**
230      * If getDeviceContext return null then null should be returned
231      */
232     @Test
233     public void gainConnectionStateSafelyTest1() {
234         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
235         assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
236     }
237
238     /**
239      * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
240      */
241     @Test
242     public void gainConnectionStateSafelyTest2() {
243         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
244         lifecycleConductor.gainConnectionStateSafely(deviceInfo);
245         verify(deviceContext,times(1)).getPrimaryConnectionContext();
246     }
247
248     /**
249      * If getDeviceContext returns null then null should be returned
250      */
251     @Test
252     public void reserveXidForDeviceMessageTest1() {
253         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
254         assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
255     }
256
257     /**
258      * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
259      */
260     @Test
261     public void reserveXidForDeviceMessageTest2() {
262         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
263         lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
264         verify(deviceContext,times(1)).reserveXidForDeviceMessage();
265     }
266
267     /**
268      * When succes flag is set to FALSE connection should be closed
269      */
270     @Test
271     public void deviceStartInitializationDoneTest() {
272         lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
273         verify(deviceContext,times(1)).shutdownConnection();
274     }
275
276     /**
277      * When succes flag is set to FALSE connection should be closed
278      */
279     @Test
280     public void deviceInitializationDoneTest() {
281         lifecycleConductor.deviceInitializationDone(deviceInfo, false);
282         verify(deviceContext,times(1)).shutdownConnection();
283     }
284 }