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.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;
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;
45 @RunWith(MockitoJUnitRunner.class)
46 public class LifecycleConductorImplTest {
48 private LifecycleConductorImpl lifecycleConductor;
51 private MessageIntelligenceAgency messageIntelligenceAgency;
53 private ServiceChangeListener serviceChangeListener;
55 private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
57 private DeviceContext deviceContext;
59 private DeviceManager deviceManager;
61 private DeviceState deviceState;
63 private ConnectionContext connectionContext;
65 private FeaturesReply featuresReply;
67 private TimerTask timerTask;
69 private TimeUnit timeUnit;
71 private HashedWheelTimer hashedWheelTimer;
73 private ListenableFuture<Void> listenableFuture;
75 private StatisticsManager statisticsManager;
77 private RpcManager rpcManager;
79 private RpcContext rpcContext;
81 private DeviceInfo deviceInfo;
83 private NodeId nodeId = new NodeId("openflow-junit:1");
84 private OfpRole ofpRole = OfpRole.NOCHANGE;
85 private long delay = 42;
90 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
91 lifecycleConductor.setSafelyManager(deviceManager);
92 lifecycleConductor.setSafelyManager(statisticsManager);
93 lifecycleConductor.setSafelyManager(rpcManager);
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);
108 public void addOneTimeListenerWhenServicesChangesDoneTest() {
109 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
110 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
115 * If serviceChangeListeners is empty NOTHING should happen
118 public void notifyServiceChangeListenersTest1() {
119 lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
120 when(serviceChangeListeners.size()).thenReturn(0);
121 verify(serviceChangeListeners,times(0)).remove(deviceInfo);
125 * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
128 public void notifyServiceChangeListenersTest2() {
129 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
130 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
131 lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
132 assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
137 * When success flag is set to FALSE nodeID connection should be closed
140 public void roleInitializationDoneTest1() {
141 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
142 lifecycleConductor.roleInitializationDone(deviceInfo,false);
143 verify(deviceContext,times(1)).shutdownConnection();
147 * When success flag is set to TRUE LOG should be printed
150 public void roleInitializationDoneTest2() {
151 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
152 lifecycleConductor.roleInitializationDone(deviceInfo,true);
153 verify(deviceContext,times(0)).shutdownConnection();
157 * When getDeviceContext returns null nothing should happen
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();
169 * When success flag is set to FALSE connection should be closed
172 public void roleChangeOnDeviceTest2() {
173 when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
174 lifecycleConductor.roleChangeOnDevice(deviceInfo,false,ofpRole,false);
175 verify(deviceContext,times(1)).shutdownConnection();
179 * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
180 * device should be skipped
183 public void roleChangeOnDeviceTest3() {
184 when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
185 lifecycleConductor.roleChangeOnDevice(deviceInfo,true,ofpRole,true);
186 verify(deviceContext,times(0)).shutdownConnection();
190 * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
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());
202 * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
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());
214 * If getDeviceContext return null then null should be returned
217 public void gainConnectionStateSafelyTest1() {
218 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
219 assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
223 * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
226 public void gainConnectionStateSafelyTest2() {
227 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
228 lifecycleConductor.gainConnectionStateSafely(deviceInfo);
229 verify(deviceContext,times(1)).getPrimaryConnectionContext();
233 * If getDeviceContext returns null then null should be returned
236 public void reserveXidForDeviceMessageTest1() {
237 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
238 assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
242 * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
245 public void reserveXidForDeviceMessageTest2() {
246 when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
247 lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
248 verify(deviceContext,times(1)).reserveXidForDeviceMessage();
252 * When succes flag is set to FALSE connection should be closed
255 public void deviceStartInitializationDoneTest() {
256 lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
257 verify(deviceContext,times(1)).shutdownConnection();
261 * When succes flag is set to FALSE connection should be closed
264 public void deviceInitializationDoneTest() {
265 lifecycleConductor.deviceInitializationDone(deviceInfo, false);
266 verify(deviceContext,times(1)).shutdownConnection();