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.OFPContext;
31 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
36 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
37 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
38 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
39 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
40 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
41 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
42 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
43 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53 @RunWith(MockitoJUnitRunner.class)
54 public class LifecycleConductorImplTest {
56 private LifecycleConductorImpl lifecycleConductor;
59 private MessageIntelligenceAgency messageIntelligenceAgency;
61 private ServiceChangeListener serviceChangeListener;
63 private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners;
65 private DeviceContext deviceContext;
67 private DeviceManager deviceManager;
69 private DeviceState deviceState;
71 private ConnectionContext connectionContext;
73 private FeaturesReply featuresReply;
75 private TimerTask timerTask;
77 private TimeUnit timeUnit;
79 private HashedWheelTimer hashedWheelTimer;
81 private ListenableFuture<Void> listenableFuture;
83 private StatisticsManager statisticsManager;
85 private RpcManager rpcManager;
87 private RpcContext rpcContext;
89 private DeviceInfo deviceInfo;
91 private NodeId nodeId = new NodeId("openflow-junit:1");
92 private OfpRole ofpRole = OfpRole.NOCHANGE;
93 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
97 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
98 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
100 lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency, convertorManager);
101 lifecycleConductor.setSafelyManager(deviceManager);
102 lifecycleConductor.setSafelyManager(statisticsManager);
103 lifecycleConductor.setSafelyManager(rpcManager);
105 when(deviceManager.gainContext(Mockito.any())).thenReturn(deviceContext);
106 when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
107 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
108 when(rpcManager.gainContext(Mockito.any())).thenReturn(rpcContext);
109 when(deviceInfo.getNodeId()).thenReturn(nodeId);
110 when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
111 when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
112 when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
116 public void addOneTimeListenerWhenServicesChangesDoneTest() {
117 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
118 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
123 * If serviceChangeListeners is empty NOTHING should happen
126 public void notifyServiceChangeListenersTest1() {
127 lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
128 when(serviceChangeListeners.size()).thenReturn(0);
129 verify(serviceChangeListeners,times(0)).remove(deviceInfo);
133 * If serviceChangeListeners is NOT empty remove(nodeID) should be removed
136 public void notifyServiceChangeListenersTest2() {
137 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
138 assertEquals(false,lifecycleConductor.isServiceChangeListenersEmpty());
139 lifecycleConductor.notifyServiceChangeListeners(deviceInfo,true);
140 assertEquals(true,lifecycleConductor.isServiceChangeListenersEmpty());
145 * When success flag is set to FALSE nodeID connection should be closed
148 public void roleInitializationDoneTest1() {
149 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
150 lifecycleConductor.roleInitializationDone(deviceInfo,false);
151 verify(deviceContext,times(1)).shutdownConnection();
155 * When success flag is set to TRUE LOG should be printed
158 public void roleInitializationDoneTest2() {
159 lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
160 lifecycleConductor.roleInitializationDone(deviceInfo,true);
161 verify(deviceContext,times(0)).shutdownConnection();
165 * When getDeviceContext returns null raise exception
167 @Test(expected = NullPointerException.class)
168 public void roleChangeOnDeviceTest1() {
169 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
170 lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
171 verify(deviceContext,times(0)).shutdownConnection();
172 lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
173 verify(deviceContext,times(0)).shutdownConnection();
177 * When OfpRole == BECOMEMASTER setRole(OfpRole.BECOMEMASTER) should be called
180 public void roleChangeOnDeviceTest4() {
181 final DataBroker dataBroker = mock(DataBroker.class);
183 when(deviceContext.getDeviceState()).thenReturn(deviceState);
184 when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier));
185 when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
186 lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMEMASTER);
187 verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
191 * When OfpRole != BECOMEMASTER setRole(OfpRole.ECOMESLAVE) should be called
194 public void roleChangeOnDeviceTest5() {
195 final DataBroker dataBroker = mock(DataBroker.class);
197 when(deviceContext.getDeviceState()).thenReturn(deviceState);
198 when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier));
199 when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
201 lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMESLAVE);
202 verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
206 * If getDeviceContext return null then null should be returned
209 public void gainConnectionStateSafelyTest1() {
210 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
211 assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
215 * If getDeviceContext return deviceContext then getPrimaryConnectionContext should be called
218 public void gainConnectionStateSafelyTest2() {
219 lifecycleConductor.gainConnectionStateSafely(deviceInfo);
220 verify(deviceContext,times(1)).getPrimaryConnectionContext();
224 * If getDeviceContext returns null then null should be returned
227 public void reserveXidForDeviceMessageTest1() {
228 when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
229 assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
233 * If getDeviceContext returns deviceContext reserveXidForDeviceMessage() should be called
236 public void reserveXidForDeviceMessageTest2() {
237 lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
238 verify(deviceContext,times(1)).reserveXidForDeviceMessage();
242 * When succes flag is set to FALSE connection should be closed
245 public void deviceStartInitializationDoneTest() {
246 lifecycleConductor.deviceStartInitializationDone(deviceInfo, false);
247 verify(deviceContext,times(1)).shutdownConnection();
251 * When succes flag is set to FALSE connection should be closed
254 public void deviceInitializationDoneTest() {
255 lifecycleConductor.deviceInitializationDone(deviceInfo, false);
256 verify(deviceContext,times(1)).shutdownConnection();