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.verifyNoMoreInteractions;
15 import static org.mockito.Mockito.when;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import io.netty.util.HashedWheelTimer;
19 import io.netty.util.TimerTask;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.concurrent.TimeUnit;
39 @RunWith(MockitoJUnitRunner.class)
40 public class LifecycleConductorImplTest {
42 private LifecycleConductorImpl lifecycleConductor;
45 private MessageIntelligenceAgency messageIntelligenceAgency;
47 private ServiceChangeListener serviceChangeListener;
49 private ConcurrentHashMap<NodeId, ServiceChangeListener> serviceChangeListeners;
51 private DeviceContext deviceContext;
53 private DeviceManager deviceManager;
55 private DeviceState deviceState;
57 private ConnectionContext connectionContext;
59 private FeaturesReply featuresReply;
61 private TimerTask timerTask;
63 private TimeUnit timeUnit;
65 private HashedWheelTimer hashedWheelTimer;
67 private ListenableFuture<Void> listenableFuture;
69 private StatisticsManager statisticsManager;
71 private NodeId nodeId = new NodeId("openflow-junit:1");
72 private OfpRole ofpRole = OfpRole.NOCHANGE;
73 private long delay = 42;
77 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
78 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
80 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
81 lifecycleConductor.setSafelyDeviceManager(deviceManager);
82 lifecycleConductor.setSafelyStatisticsManager(statisticsManager);
84 when(connectionContext.getFeatures()).thenReturn(featuresReply);
90 public void addOneTimeListenerWhenServicesChangesDoneTest() {
91 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
92 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
97 * If serviceChangeListeners is empty NOTHING should happen
100 public void notifyServiceChangeListenersTest1() {
101 lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
102 when(serviceChangeListeners.size()).thenReturn(0);
103 verify(serviceChangeListeners,times(0)).remove(nodeId);
107 * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
110 public void notifyServiceChangeListenersTest2() {
111 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
112 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
113 lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
114 assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
119 * When success flag is set to FALSE nodeID connection should be closed
122 public void roleInitializationDoneTest1() {
123 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
124 lifecycleConductor.roleInitializationDone(nodeId,false);
125 verify(deviceContext,times(1)).shutdownConnection();
129 * When success flag is set to TRUE LOG should be printed
132 public void roleInitializationDoneTest2() {
133 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
134 lifecycleConductor.roleInitializationDone(nodeId,true);
135 verify(deviceContext,times(0)).shutdownConnection();
139 * When getDeviceContext returns null nothing should happen
142 public void roleChangeOnDeviceTest1() {
143 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
144 lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,false);
145 verify(deviceContext,times(0)).shutdownConnection();
146 lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
147 verify(deviceContext,times(0)).shutdownConnection();
151 * When success flag is set to FALSE connection should be closed
154 public void roleChangeOnDeviceTest2() {
155 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
156 lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
157 verify(deviceContext,times(1)).shutdownConnection();
161 * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
162 * device should be skipped
165 public void roleChangeOnDeviceTest3() {
166 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
167 lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,true);
168 verify(deviceContext,times(0)).shutdownConnection();
172 * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
175 public void roleChangeOnDeviceTest4() {
176 when(deviceContext.getDeviceState()).thenReturn(deviceState);
177 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
178 when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
179 lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMEMASTER,false);
180 verify(statisticsManager).startScheduling(nodeId);
184 * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
187 public void roleChangeOnDeviceTest5() {
188 when(deviceContext.getDeviceState()).thenReturn(deviceState);
189 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
190 when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
191 lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMESLAVE,false);
192 verify(statisticsManager).stopScheduling(nodeId);
196 * If getDeviceContext returns null nothing should happen
199 public void gainVersionSafelyTest1() {
200 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
201 assertNull(lifecycleConductor.gainVersionSafely(nodeId));
205 * If getDeviceContext returns deviceContext getPrimaryConnectionContext() should be called
208 public void gainVersionSafelyTest2() {
209 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
210 lifecycleConductor.gainVersionSafely(nodeId);
211 verify(deviceContext,times(1)).getPrimaryConnectionContext();
215 * If getDeviceContext return null then null should be returned
218 public void gainConnectionStateSafelyTest1() {
219 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
220 assertNull(lifecycleConductor.gainConnectionStateSafely(nodeId));
224 * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
227 public void gainConnectionStateSafelyTest2() {
228 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
229 lifecycleConductor.gainConnectionStateSafely(nodeId);
230 verify(deviceContext,times(1)).getPrimaryConnectionContext();
234 * If getDeviceContext returns null then null should be returned
237 public void reserveXidForDeviceMessageTest1() {
238 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
239 assertNull(lifecycleConductor.reserveXidForDeviceMessage(nodeId));
243 * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
246 public void reserveXidForDeviceMessageTest2() {
247 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
248 lifecycleConductor.reserveXidForDeviceMessage(nodeId);
249 verify(deviceContext,times(1)).reserveXidForDeviceMessage();
253 * When succes flag is set to FALSE connection should be closed
256 public void deviceStartInitializationDoneTest() {
257 lifecycleConductor.deviceStartInitializationDone(nodeId, false);
258 verify(deviceContext,times(1)).shutdownConnection();
262 * When succes flag is set to FALSE connection should be closed
265 public void deviceInitializationDoneTest() {
266 lifecycleConductor.deviceInitializationDone(nodeId, false);
267 verify(deviceContext,times(1)).shutdownConnection();