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.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
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 java.math.BigInteger;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
35 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
36 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
37 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
39 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
40 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50 @RunWith(MockitoJUnitRunner.class)
51 public class LifecycleConductorImplTest {
53 private LifecycleConductorImpl lifecycleConductor;
56 private MessageIntelligenceAgency messageIntelligenceAgency;
58 private ServiceChangeListener serviceChangeListener;
60 private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
62 private DeviceContext deviceContext;
64 private DeviceManager deviceManager;
66 private DeviceState deviceState;
68 private ConnectionContext connectionContext;
70 private FeaturesReply featuresReply;
72 private TimerTask timerTask;
74 private TimeUnit timeUnit;
76 private HashedWheelTimer hashedWheelTimer;
78 private ListenableFuture<Void> listenableFuture;
80 private StatisticsManager statisticsManager;
82 private RpcManager rpcManager;
84 private RpcContext rpcContext;
86 private DeviceInfo deviceInfo;
88 private NodeId nodeId = new NodeId("openflow-junit:1");
89 private OfpRole ofpRole = OfpRole.NOCHANGE;
90 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
94 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
96 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
97 lifecycleConductor.setSafelyManager(deviceManager);
98 lifecycleConductor.setSafelyManager(statisticsManager);
99 lifecycleConductor.setSafelyManager(rpcManager);
101 when(deviceManager.gainContext(Mockito.any())).thenReturn(deviceContext);
102 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
103 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
104 when(rpcManager.gainContext(Mockito.any())).thenReturn(rpcContext);
105 when(deviceInfo.getNodeId()).thenReturn(nodeId);
106 when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
107 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
108 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
109 when(rpcManager.gainContext(Mockito.any())).thenReturn(rpcContext);
113 public void addOneTimeListenerWhenServicesChangesDoneTest() {
114 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
115 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
120 * If serviceChangeListeners is empty NOTHING should happen
123 public void notifyServiceChangeListenersTest1() {
124 lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
125 when(serviceChangeListeners.size()).thenReturn(0);
126 verify(serviceChangeListeners,times(0)).remove(deviceInfo);
130 * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
133 public void notifyServiceChangeListenersTest2() {
134 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
135 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
136 lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
137 assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
142 * When success flag is set to FALSE nodeID connection should be closed
145 public void roleInitializationDoneTest1() {
146 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
147 lifecycleConductor.roleInitializationDone(deviceInfo,false);
148 verify(deviceContext,times(1)).shutdownConnection();
152 * When success flag is set to TRUE LOG should be printed
155 public void roleInitializationDoneTest2() {
156 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
157 lifecycleConductor.roleInitializationDone(deviceInfo,true);
158 verify(deviceContext,times(0)).shutdownConnection();
162 * When getDeviceContext returns null raise exception
164 @Test(expected = NullPointerException.class)
165 public void roleChangeOnDeviceTest1() {
166 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
167 lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
168 verify(deviceContext,times(0)).shutdownConnection();
169 lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
170 verify(deviceContext,times(0)).shutdownConnection();
174 * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
177 public void roleChangeOnDeviceTest4() {
178 final DataBroker dataBroker = mock(DataBroker.class);
180 when(deviceContext.getDeviceState()).thenReturn(deviceState);
181 when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier));
182 when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
183 when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
184 lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMEMASTER);
185 verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
189 * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
192 public void roleChangeOnDeviceTest5() {
193 final DataBroker dataBroker = mock(DataBroker.class);
195 when(deviceContext.getDeviceState()).thenReturn(deviceState);
196 when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier));
197 when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
198 when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
200 lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMESLAVE);
201 verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
205 * If getDeviceContext return null then null should be returned
208 public void gainConnectionStateSafelyTest1() {
209 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
210 assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
214 * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
217 public void gainConnectionStateSafelyTest2() {
218 lifecycleConductor.gainConnectionStateSafely(deviceInfo);
219 verify(deviceContext,times(1)).getPrimaryConnectionContext();
223 * If getDeviceContext returns null then null should be returned
226 public void reserveXidForDeviceMessageTest1() {
227 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
228 assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
232 * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
235 public void reserveXidForDeviceMessageTest2() {
236 lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
237 verify(deviceContext,times(1)).reserveXidForDeviceMessage();
241 * When succes flag is set to FALSE connection should be closed
244 public void deviceStartInitializationDoneTest() {
245 lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
246 verify(deviceContext,times(1)).shutdownConnection();
250 * When succes flag is set to FALSE connection should be closed
253 public void deviceInitializationDoneTest() {
254 lifecycleConductor.deviceInitializationDone(deviceInfo, false);
255 verify(deviceContext,times(1)).shutdownConnection();