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;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.TimeUnit;
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 @RunWith(MockitoJUnitRunner.class)
37 public class LifecycleConductorImplTest {
39 private LifecycleConductorImpl lifecycleConductor;
42 private MessageIntelligenceAgency messageIntelligenceAgency;
44 private ServiceChangeListener serviceChangeListener;
46 private ConcurrentHashMap<NodeId, ServiceChangeListener> serviceChangeListeners;
48 private DeviceContext deviceContext;
50 private DeviceManager deviceManager;
52 private DeviceState deviceState;
54 private ConnectionContext connectionContext;
56 private FeaturesReply featuresReply;
58 private TimerTask timerTask;
60 private TimeUnit timeUnit;
62 private HashedWheelTimer hashedWheelTimer;
64 private ListenableFuture<Void> listenableFuture;
66 private StatisticsManager statisticsManager;
68 private NodeId nodeId = new NodeId("openflow-junit:1");
69 private OfpRole ofpRole = OfpRole.NOCHANGE;
70 private long delay = 42;
74 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
75 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
77 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
78 lifecycleConductor.setSafelyDeviceManager(deviceManager);
79 lifecycleConductor.setSafelyStatisticsManager(statisticsManager);
81 when(connectionContext.getFeatures()).thenReturn(featuresReply);
87 public void addOneTimeListenerWhenServicesChangesDoneTest() {
88 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
89 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
94 * If serviceChangeListeners is empty NOTHING should happen
97 public void notifyServiceChangeListenersTest1() {
98 lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
99 when(serviceChangeListeners.size()).thenReturn(0);
100 verify(serviceChangeListeners,times(0)).remove(nodeId);
104 * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
107 public void notifyServiceChangeListenersTest2() {
108 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
109 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
110 lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
111 assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
116 * When success flag is set to FALSE nodeID connection should be closed
119 public void roleInitializationDoneTest1() {
120 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
121 lifecycleConductor.roleInitializationDone(nodeId,false);
122 verify(deviceContext,times(1)).shutdownConnection();
126 * When success flag is set to TRUE LOG should be printed
129 public void roleInitializationDoneTest2() {
130 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
131 lifecycleConductor.roleInitializationDone(nodeId,true);
132 verify(deviceContext,times(0)).shutdownConnection();
136 * When getDeviceContext returns null nothing should happen
139 public void roleChangeOnDeviceTest1() {
140 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
141 lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,false);
142 verify(deviceContext,times(0)).shutdownConnection();
143 lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
144 verify(deviceContext,times(0)).shutdownConnection();
148 * When success flag is set to FALSE connection should be closed
151 public void roleChangeOnDeviceTest2() {
152 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
153 lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
154 verify(deviceContext,times(1)).shutdownConnection();
158 * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
159 * device should be skipped
162 public void roleChangeOnDeviceTest3() {
163 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
164 lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,true);
165 verify(deviceContext,times(0)).shutdownConnection();
169 * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
172 public void roleChangeOnDeviceTest4() {
173 when(deviceContext.getDeviceState()).thenReturn(deviceState);
174 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
175 when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
176 lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMEMASTER,false);
177 verify(statisticsManager).startScheduling(nodeId);
181 * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
184 public void roleChangeOnDeviceTest5() {
185 when(deviceContext.getDeviceState()).thenReturn(deviceState);
186 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
187 when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
188 lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMESLAVE,false);
189 verify(statisticsManager).stopScheduling(nodeId);
193 * If getDeviceContext returns null nothing should happen
196 public void gainVersionSafelyTest1() {
197 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
198 assertNull(lifecycleConductor.gainVersionSafely(nodeId));
202 * If getDeviceContext returns deviceContext getPrimaryConnectionContext() should be called
205 public void gainVersionSafelyTest2() {
206 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
207 lifecycleConductor.gainVersionSafely(nodeId);
208 verify(deviceContext,times(1)).getPrimaryConnectionContext();
212 * If getDeviceContext return null then null should be returned
215 public void gainConnectionStateSafelyTest1() {
216 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
217 assertNull(lifecycleConductor.gainConnectionStateSafely(nodeId));
221 * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
224 public void gainConnectionStateSafelyTest2() {
225 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
226 lifecycleConductor.gainConnectionStateSafely(nodeId);
227 verify(deviceContext,times(1)).getPrimaryConnectionContext();
231 * If getDeviceContext returns null then null should be returned
234 public void reserveXidForDeviceMessageTest1() {
235 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
236 assertNull(lifecycleConductor.reserveXidForDeviceMessage(nodeId));
240 * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
243 public void reserveXidForDeviceMessageTest2() {
244 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
245 lifecycleConductor.reserveXidForDeviceMessage(nodeId);
246 verify(deviceContext,times(1)).reserveXidForDeviceMessage();
250 * When succes flag is set to FALSE connection should be closed
253 public void deviceStartInitializationDoneTest() {
254 lifecycleConductor.deviceStartInitializationDone(nodeId, false);
255 verify(deviceContext,times(1)).shutdownConnection();
259 * When succes flag is set to FALSE connection should be closed
262 public void deviceInitializationDoneTest() {
263 lifecycleConductor.deviceInitializationDone(nodeId, false);
264 verify(deviceContext,times(1)).shutdownConnection();