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.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
31 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
38 @RunWith(MockitoJUnitRunner.class)
39 public class LifecycleConductorImplTest {
41 private LifecycleConductorImpl lifecycleConductor;
44 private MessageIntelligenceAgency messageIntelligenceAgency;
46 private ServiceChangeListener serviceChangeListener;
48 private ConcurrentHashMap<NodeId, ServiceChangeListener> serviceChangeListeners;
50 private DeviceContext deviceContext;
52 private DeviceManager deviceManager;
54 private DeviceState deviceState;
56 private ConnectionContext connectionContext;
58 private FeaturesReply featuresReply;
60 private TimerTask timerTask;
62 private TimeUnit timeUnit;
64 private HashedWheelTimer hashedWheelTimer;
66 private ListenableFuture<Void> listenableFuture;
68 private StatisticsManager statisticsManager;
70 private DeviceInfo deviceInfo;
72 private NodeId nodeId = new NodeId("openflow-junit:1");
73 private OfpRole ofpRole = OfpRole.NOCHANGE;
74 private long delay = 42;
78 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
79 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
81 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
82 lifecycleConductor.setSafelyDeviceManager(deviceManager);
83 lifecycleConductor.setSafelyStatisticsManager(statisticsManager);
85 when(connectionContext.getFeatures()).thenReturn(featuresReply);
86 when(deviceInfo.getNodeId()).thenReturn(nodeId);
92 public void addOneTimeListenerWhenServicesChangesDoneTest() {
93 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
94 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
99 * If serviceChangeListeners is empty NOTHING should happen
102 public void notifyServiceChangeListenersTest1() {
103 lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
104 when(serviceChangeListeners.size()).thenReturn(0);
105 verify(serviceChangeListeners,times(0)).remove(nodeId);
109 * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
112 public void notifyServiceChangeListenersTest2() {
113 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
114 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
115 lifecycleConductor.notifyServiceChangeListeners(nodeId,true);
116 assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
121 * When success flag is set to FALSE nodeID connection should be closed
124 public void roleInitializationDoneTest1() {
125 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
126 lifecycleConductor.roleInitializationDone(nodeId,false);
127 verify(deviceContext,times(1)).shutdownConnection();
131 * When success flag is set to TRUE LOG should be printed
134 public void roleInitializationDoneTest2() {
135 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, nodeId);
136 lifecycleConductor.roleInitializationDone(nodeId,true);
137 verify(deviceContext,times(0)).shutdownConnection();
141 * When getDeviceContext returns null nothing should happen
144 public void roleChangeOnDeviceTest1() {
145 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
146 lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,false);
147 verify(deviceContext,times(0)).shutdownConnection();
148 lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
149 verify(deviceContext,times(0)).shutdownConnection();
153 * When success flag is set to FALSE connection should be closed
156 public void roleChangeOnDeviceTest2() {
157 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
158 lifecycleConductor.roleChangeOnDevice(nodeId,false,ofpRole,false);
159 verify(deviceContext,times(1)).shutdownConnection();
163 * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
164 * device should be skipped
167 public void roleChangeOnDeviceTest3() {
168 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
169 lifecycleConductor.roleChangeOnDevice(nodeId,true,ofpRole,true);
170 verify(deviceContext,times(0)).shutdownConnection();
174 * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
177 public void roleChangeOnDeviceTest4() {
178 when(deviceContext.getDeviceState()).thenReturn(deviceState);
179 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
180 when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
181 lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMEMASTER,false);
182 verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
186 * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
189 public void roleChangeOnDeviceTest5() {
190 when(deviceContext.getDeviceState()).thenReturn(deviceState);
191 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
192 when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
193 lifecycleConductor.roleChangeOnDevice(nodeId,true,OfpRole.BECOMESLAVE,false);
194 verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
198 * If getDeviceContext returns null nothing should happen
201 public void gainVersionSafelyTest1() {
202 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
203 assertNull(lifecycleConductor.gainVersionSafely(nodeId));
207 * If getDeviceContext returns deviceContext getPrimaryConnectionContext() should be called
210 public void gainVersionSafelyTest2() {
211 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
212 lifecycleConductor.gainVersionSafely(nodeId);
213 verify(deviceContext,times(1)).getPrimaryConnectionContext();
217 * If getDeviceContext return null then null should be returned
220 public void gainConnectionStateSafelyTest1() {
221 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
222 assertNull(lifecycleConductor.gainConnectionStateSafely(nodeId));
226 * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
229 public void gainConnectionStateSafelyTest2() {
230 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
231 lifecycleConductor.gainConnectionStateSafely(nodeId);
232 verify(deviceContext,times(1)).getPrimaryConnectionContext();
236 * If getDeviceContext returns null then null should be returned
239 public void reserveXidForDeviceMessageTest1() {
240 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(null);
241 assertNull(lifecycleConductor.reserveXidForDeviceMessage(nodeId));
245 * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
248 public void reserveXidForDeviceMessageTest2() {
249 when(deviceManager.getDeviceContextFromNodeId(nodeId)).thenReturn(deviceContext);
250 lifecycleConductor.reserveXidForDeviceMessage(nodeId);
251 verify(deviceContext,times(1)).reserveXidForDeviceMessage();
255 * When succes flag is set to FALSE connection should be closed
258 public void deviceStartInitializationDoneTest() {
259 lifecycleConductor.deviceStartInitializationDone(nodeId, false);
260 verify(deviceContext,times(1)).shutdownConnection();
264 * When succes flag is set to FALSE connection should be closed
267 public void deviceInitializationDoneTest() {
268 lifecycleConductor.deviceInitializationDone(nodeId, false);
269 verify(deviceContext,times(1)).shutdownConnection();