2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl;
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;
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;
43 @RunWith(MockitoJUnitRunner.class)
44 public class LifecycleConductorImplTest {
46 private LifecycleConductorImpl lifecycleConductor;
49 private MessageIntelligenceAgency messageIntelligenceAgency;
51 private ServiceChangeListener serviceChangeListener;
53 private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
55 private DeviceContext deviceContext;
57 private DeviceManager deviceManager;
59 private DeviceState deviceState;
61 private ConnectionContext connectionContext;
63 private FeaturesReply featuresReply;
65 private TimerTask timerTask;
67 private TimeUnit timeUnit;
69 private HashedWheelTimer hashedWheelTimer;
71 private ListenableFuture<Void> listenableFuture;
73 private StatisticsManager statisticsManager;
75 private RpcManager rpcManager;
77 private RpcContext rpcContext;
79 private DeviceInfo deviceInfo;
81 private NodeId nodeId = new NodeId("openflow-junit:1");
82 private OfpRole ofpRole = OfpRole.NOCHANGE;
83 private long delay = 42;
87 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
88 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
90 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
91 lifecycleConductor.setSafelyManager(deviceManager);
92 lifecycleConductor.setSafelyManager(statisticsManager);
93 lifecycleConductor.setSafelyManager(rpcManager);
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);
105 public void addOneTimeListenerWhenServicesChangesDoneTest() {
106 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
107 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
112 * If serviceChangeListeners is empty NOTHING should happen
115 public void notifyServiceChangeListenersTest1() {
116 lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
117 when(serviceChangeListeners.size()).thenReturn(0);
118 verify(serviceChangeListeners,times(0)).remove(deviceInfo);
122 * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
125 public void notifyServiceChangeListenersTest2() {
126 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
127 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
128 lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
129 assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
134 * When success flag is set to FALSE nodeID connection should be closed
137 public void roleInitializationDoneTest1() {
138 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
139 lifecycleConductor.roleInitializationDone(deviceInfo,false);
140 verify(deviceContext,times(1)).shutdownConnection();
144 * When success flag is set to TRUE LOG should be printed
147 public void roleInitializationDoneTest2() {
148 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
149 lifecycleConductor.roleInitializationDone(deviceInfo,true);
150 verify(deviceContext,times(0)).shutdownConnection();
154 * When getDeviceContext returns null nothing should happen
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();
166 * When success flag is set to FALSE connection should be closed
169 public void roleChangeOnDeviceTest2() {
170 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
171 lifecycleConductor.roleChangeOnDevice(deviceInfo,false,ofpRole,false);
172 verify(deviceContext,times(1)).shutdownConnection();
176 * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
177 * device should be skipped
180 public void roleChangeOnDeviceTest3() {
181 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
182 lifecycleConductor.roleChangeOnDevice(deviceInfo,true,ofpRole,true);
183 verify(deviceContext,times(0)).shutdownConnection();
187 * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
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());
199 * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
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());
211 * If getDeviceContext returns null nothing should happen
214 public void gainVersionSafelyTest1() {
215 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
216 assertNull(lifecycleConductor.gainVersionSafely(deviceInfo));
220 * If getDeviceContext returns deviceContext getPrimaryConnectionContext() should be called
223 public void gainVersionSafelyTest2() {
224 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
225 lifecycleConductor.gainVersionSafely(deviceInfo);
226 verify(deviceContext,times(1)).getPrimaryConnectionContext();
230 * If getDeviceContext return null then null should be returned
233 public void gainConnectionStateSafelyTest1() {
234 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
235 assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
239 * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
242 public void gainConnectionStateSafelyTest2() {
243 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
244 lifecycleConductor.gainConnectionStateSafely(deviceInfo);
245 verify(deviceContext,times(1)).getPrimaryConnectionContext();
249 * If getDeviceContext returns null then null should be returned
252 public void reserveXidForDeviceMessageTest1() {
253 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
254 assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
258 * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
261 public void reserveXidForDeviceMessageTest2() {
262 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
263 lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
264 verify(deviceContext,times(1)).reserveXidForDeviceMessage();
268 * When succes flag is set to FALSE connection should be closed
271 public void deviceStartInitializationDoneTest() {
272 lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
273 verify(deviceContext,times(1)).shutdownConnection();
277 * When succes flag is set to FALSE connection should be closed
280 public void deviceInitializationDoneTest() {
281 lifecycleConductor.deviceInitializationDone(deviceInfo, false);
282 verify(deviceContext,times(1)).shutdownConnection();