import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+
import com.google.common.util.concurrent.ListenableFuture;
import io.netty.util.HashedWheelTimer;
import io.netty.util.TimerTask;
-
import java.math.BigInteger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
-import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
+import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
@RunWith(MockitoJUnitRunner.class)
public class LifecycleConductorImplTest {
private NodeId nodeId = new NodeId("openflow-junit:1");
private OfpRole ofpRole = OfpRole.NOCHANGE;
- private long delay = 42;
+ private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
@Before
public void setUp() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
- when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+ nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
+ final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
- lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
+ lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency, convertorManager);
lifecycleConductor.setSafelyManager(deviceManager);
lifecycleConductor.setSafelyManager(statisticsManager);
lifecycleConductor.setSafelyManager(rpcManager);
- when(connectionContext.getFeatures()).thenReturn(featuresReply);
+ when(deviceManager.gainContext(Mockito.any())).thenReturn(deviceContext);
+ when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
+ when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
+ when(rpcManager.gainContext(Mockito.any())).thenReturn(rpcContext);
when(deviceInfo.getNodeId()).thenReturn(nodeId);
when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
+ when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeInstanceIdentifier);
when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
- when(rpcManager.gainContext(Mockito.<DeviceInfo>any())).thenReturn(rpcContext);
}
-
-
@Test
public void addOneTimeListenerWhenServicesChangesDoneTest() {
lifecycleConductor.addOneTimeListenerWhenServicesChangesDone(serviceChangeListener, deviceInfo);
}
/**
- * When getDeviceContext returns null nothing should happen
+ * When getDeviceContext returns null raise exception
*/
- @Test
+ @Test(expected = NullPointerException.class)
public void roleChangeOnDeviceTest1() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
- lifecycleConductor.roleChangeOnDevice(deviceInfo,true,ofpRole,false);
- verify(deviceContext,times(0)).shutdownConnection();
- lifecycleConductor.roleChangeOnDevice(deviceInfo,false,ofpRole,false);
+ when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
+ lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
verify(deviceContext,times(0)).shutdownConnection();
- }
-
- /**
- * When success flag is set to FALSE connection should be closed
- */
- @Test
- public void roleChangeOnDeviceTest2() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
- lifecycleConductor.roleChangeOnDevice(deviceInfo,false,ofpRole,false);
- verify(deviceContext,times(1)).shutdownConnection();
- }
-
- /**
- * When success flag is set to TRUE and initializationPahse flag is set to TRUE starting
- * device should be skipped
- */
- @Test
- public void roleChangeOnDeviceTest3() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
- lifecycleConductor.roleChangeOnDevice(deviceInfo,true,ofpRole,true);
+ lifecycleConductor.roleChangeOnDevice(deviceInfo,ofpRole);
verify(deviceContext,times(0)).shutdownConnection();
}
*/
@Test
public void roleChangeOnDeviceTest4() {
+ final DataBroker dataBroker = mock(DataBroker.class);
+
when(deviceContext.getDeviceState()).thenReturn(deviceState);
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
- when(deviceContext.onClusterRoleChange(OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
- lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMEMASTER,false);
+ when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier));
+ when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
+ lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMEMASTER);
verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
}
*/
@Test
public void roleChangeOnDeviceTest5() {
- when(deviceContext.getDeviceState()).thenReturn(deviceState);
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
- when(deviceContext.onClusterRoleChange(OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
- lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMESLAVE,false);
- verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
- }
+ final DataBroker dataBroker = mock(DataBroker.class);
- /**
- * If getDeviceContext returns null nothing should happen
- */
- @Test
- public void gainVersionSafelyTest1() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
- assertNull(lifecycleConductor.gainVersionSafely(deviceInfo));
- }
+ when(deviceContext.getDeviceState()).thenReturn(deviceState);
+ when(deviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier));
+ when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
- /**
- * If getDeviceContext returns deviceContext getPrimaryConnectionContext() should be called
- */
- @Test
- public void gainVersionSafelyTest2() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
- lifecycleConductor.gainVersionSafely(deviceInfo);
- verify(deviceContext,times(1)).getPrimaryConnectionContext();
+ lifecycleConductor.roleChangeOnDevice(deviceInfo,OfpRole.BECOMESLAVE);
+ verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
}
/**
*/
@Test
public void gainConnectionStateSafelyTest1() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
+ when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
assertNull(lifecycleConductor.gainConnectionStateSafely(deviceInfo));
}
*/
@Test
public void gainConnectionStateSafelyTest2() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
lifecycleConductor.gainConnectionStateSafely(deviceInfo);
verify(deviceContext,times(1)).getPrimaryConnectionContext();
}
*/
@Test
public void reserveXidForDeviceMessageTest1() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(null);
+ when(deviceManager.gainContext(deviceInfo)).thenReturn(null);
assertNull(lifecycleConductor.reserveXidForDeviceMessage(deviceInfo));
}
*/
@Test
public void reserveXidForDeviceMessageTest2() {
- when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
lifecycleConductor.reserveXidForDeviceMessage(deviceInfo);
verify(deviceContext,times(1)).reserveXidForDeviceMessage();
}