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 org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.concurrent.TimeUnit;
37 @RunWith(MockitoJUnitRunner.class)
38 public class LifecycleConductorImplTest {
40 private LifecycleConductorImpl lifecycleConductor;
43 private MessageIntelligenceAgency messageIntelligenceAgency;
45 private ServiceChangeListener serviceChangeListener;
47 private ConcurrentHashMap<NodeId, ServiceChangeListener> serviceChangeListeners;
49 private DeviceContext deviceContext;
51 private DeviceManager deviceManager;
53 private DeviceState deviceState;
55 private ConnectionContext connectionContext;
57 private FeaturesReply featuresReply;
59 private TimerTask timerTask;
61 private TimeUnit timeUnit;
63 private HashedWheelTimer hashedWheelTimer;
65 private ListenableFuture<Void> listenableFuture;
67 private NodeId nodeId = new NodeId("openflow-junit:1");
68 private OfpRole ofpRole = OfpRole.NOCHANGE;
69 private long delay = 42;
73 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
74 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
76 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
77 lifecycleConductor.setSafelyDeviceManager(deviceManager);
79 when(connectionContext.getFeatures()).thenReturn(featuresReply);
85 public void addOneTimeListenerWhenServicesChangesDoneTest() {
86 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
87 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
92 * If serviceChangeListeners is empty NOTHING should happen
95 public void notifyServiceChangeListenersTest1() {
96 lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
97 when(serviceChangeListeners.size()).thenReturn(0);
98 verify(serviceChangeListeners,times(0)).remove(nodeId);
102 * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
105 public void notifyServiceChangeListenersTest2() {
106 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
107 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
108 lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
109 assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
114 * When success flag is set to FALSE nodeID connection should be closed
117 public void roleInitializationDoneTest1() {
118 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
119 lifecycleConductor.roleInitializationDone(nodeId,false);
120 verify(deviceContext,times(1)).shutdownConnection();
124 * When success flag is set to TRUE LOG should be printed
127 public void roleInitializationDoneTest2() {
128 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
129 lifecycleConductor.roleInitializationDone(nodeId,true);
130 verify(deviceContext,times(0)).shutdownConnection();
134 * When getDeviceContext returns null nothing should happen
137 public void roleChangeOnDeviceTest1() {
138 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
139 lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,false);
140 verify(deviceContext,times(0)).shutdownConnection();
141 lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
142 verify(deviceContext,times(0)).shutdownConnection();
146 * When success flag is set to FALSE connection should be closed
149 public void roleChangeOnDeviceTest2() {
150 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
151 lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
152 verify(deviceContext,times(1)).shutdownConnection();
156 * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
157 * device should be skipped
160 public void roleChangeOnDeviceTest3() {
161 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
162 lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,true);
163 verify(deviceContext,times(0)).shutdownConnection();
167 * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
170 public void roleChangeOnDeviceTest4() {
171 when(deviceContext.getDeviceState()).thenReturn(deviceState);
172 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
173 when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
174 lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMEMASTER,false);
175 verify(deviceState,times(1)).setRole(OfpRole.BECOMEMASTER);
176 verify(deviceState,times(0)).setRole(OfpRole.BECOMESLAVE);
180 * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
183 public void roleChangeOnDeviceTest5() {
184 when(deviceContext.getDeviceState()).thenReturn(deviceState);
185 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
186 when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
187 lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMESLAVE,false);
188 verify(deviceState,times(1)).setRole(OfpRole.BECOMESLAVE);
189 verify(deviceState,times(0)).setRole(OfpRole.BECOMEMASTER);
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();