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;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import io.netty.util.HashedWheelTimer;
18 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.rpc.RpcContext;
35 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
36 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
37 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
42 @RunWith(MockitoJUnitRunner.class)
43 public class LifecycleConductorImplTest {
45 private LifecycleConductorImpl lifecycleConductor;
48 private MessageIntelligenceAgency messageIntelligenceAgency;
50 private ServiceChangeListener serviceChangeListener;
52 private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
54 private DeviceContext deviceContext;
56 private DeviceManager deviceManager;
58 private DeviceState deviceState;
60 private ConnectionContext connectionContext;
62 private FeaturesReply featuresReply;
64 private TimerTask timerTask;
66 private TimeUnit timeUnit;
68 private HashedWheelTimer hashedWheelTimer;
70 private ListenableFuture<Void> listenableFuture;
72 private StatisticsManager statisticsManager;
74 private RpcManager rpcManager;
76 private RpcContext rpcContext;
78 private DeviceInfo deviceInfo;
80 private NodeId nodeId = new NodeId("openflow-junit:1");
81 private OfpRole ofpRole = OfpRole.NOCHANGE;
82 private long delay = 42;
87 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
88 lifecycleConductor.setSafelyManager(deviceManager);
89 lifecycleConductor.setSafelyManager(statisticsManager);
90 lifecycleConductor.setSafelyManager(rpcManager);
92 when(deviceManager.gainContext(Mockito.<DeviceInfo>any())).thenReturn(deviceContext);
93 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
94 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
95 when(rpcManager.gainContext(Mockito.<DeviceInfo>any())).thenReturn(rpcContext);
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.gainContext(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.gainContext(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.gainContext(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.gainContext(deviceInfo)).thenReturn(deviceContext);
193 when(deviceManager.onClusterRoleChange(deviceInfo, 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.gainContext(deviceInfo)).thenReturn(deviceContext);
205 when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
206 lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMESLAVE,false);
207 verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
211 * If getDeviceContext return null then null should be returned
214 public void gainConnectionStateSafelyTest1() {
215 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
216 assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
220 * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
223 public void gainConnectionStateSafelyTest2() {
224 lifecycleConductor.gainConnectionStateSafely(deviceInfo);
225 verify(deviceContext,times(1)).getPrimaryConnectionContext();
229 * If getDeviceContext returns null then null should be returned
232 public void reserveXidForDeviceMessageTest1() {
233 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
234 assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
238 * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
241 public void reserveXidForDeviceMessageTest2() {
242 lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
243 verify(deviceContext,times(1)).reserveXidForDeviceMessage();
247 * When succes flag is set to FALSE connection should be closed
250 public void deviceStartInitializationDoneTest() {
251 lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
252 verify(deviceContext,times(1)).shutdownConnection();
256 * When succes flag is set to FALSE connection should be closed
259 public void deviceInitializationDoneTest() {
260 lifecycleConductor.deviceInitializationDone(deviceInfo, false);
261 verify(deviceContext,times(1)).shutdownConnection();