/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
- * *
- * * This program and the accompanying materials are made available under the
- * * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * * and is available at http://www.eclipse.org/legal/epl-v10.html
- *
- *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.openflowplugin.impl.device;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.inOrder;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
-import io.netty.util.TimerTask;
+import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.ListenableFuture;
+import io.netty.util.HashedWheelTimer;
import java.lang.reflect.Field;
-import java.math.BigInteger;
-import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.InOrder;
-import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.mockito.stubbing.Answer;
-import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
+import org.opendaylight.mdsal.binding.api.TransactionChain;
+import org.opendaylight.mdsal.binding.api.WriteTransaction;
+import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
-import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
-import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandler;
-import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
-import org.opendaylight.openflowplugin.api.OFConstants;
import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
-import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
-import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
-import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
-import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
-import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
+import org.opendaylight.openflowplugin.impl.device.initialization.DeviceInitializerProviderFactory;
+import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
+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.common.types.rev130731.Capabilities;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint16Type;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
+import org.opendaylight.yangtools.util.concurrent.FluentFutures;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint32;
@RunWith(MockitoJUnitRunner.class)
public class DeviceManagerImplTest {
- private static final long TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA = 2000l;
- private static final int barrierCountLimit = 25600;
- private static final int barrierIntervalNanos = 500;
- public static final NodeId DUMMY_NODE_ID = new NodeId("dummyNodeId");
+ private static final Uint32 TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA = Uint32.valueOf(2000);
+ private static final Uint16 BARRIER_COUNT_LIMIT = Uint16.valueOf(25600);
+ private static final Uint32 BARRIER_INTERVAL_NANOS = Uint32.valueOf(500);
+ private static final NodeId DUMMY_NODE_ID = new NodeId("dummyNodeId");
+ private static final KeyedInstanceIdentifier<Node, NodeKey> DUMMY_IDENTIFIER = DeviceStateUtil
+ .createNodeInstanceIdentifier(DUMMY_NODE_ID);
@Mock
- CheckedFuture<Void, TransactionCommitFailedException> mockedFuture;
+ private FluentFuture<CommitInfo> mockedFuture;
@Mock
private FeaturesReply mockFeatures;
@Mock
- private OutboundQueue outboundQueueProvider;
+ private ConnectionContext mockConnectionContext;
@Mock
- private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
+ private ConnectionAdapter mockedConnectionAdapter;
@Mock
- private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
+ private DeviceContextImpl mockedDeviceContext;
@Mock
- private TranslatorLibrary translatorLibrary;
+ private MessageIntelligenceAgency messageIntelligenceAgency;
@Mock
- private ConnectionContext mockConnectionContext;
+ private DeviceInfo deviceInfo;
@Mock
- private ConnectionAdapter mockedConnectionAdapter;
+ private ConvertorExecutor convertorExecutor;
@Mock
- private DeviceContextImpl mockedDeviceContext;
+ private DataBroker dataBroker;
@Mock
- private NodeId mockedNodeId;
+ private WriteTransaction writeTransaction;
@Mock
- private LifecycleConductor lifecycleConductor;
+ private TransactionChain transactionChain;
@Mock
- private MessageIntelligenceAgency messageIntelligenceAgency;
+ private Capabilities capabilities;
@Mock
- private DeviceInfo deviceInfo;
+ private CapabilitiesV10 capabilitiesV10;
+ @Mock
+ private NotificationPublishService notificationPublishService;
+ @Mock
+ private TranslatorLibrary translatorLibrary;
+ @Mock
+ private ExecutorService executorService;
- @Before
- public void setUp() throws Exception {
- OpenflowPortsUtil.init();
+ private DeviceManagerImpl deviceManager;
- when(mockConnectionContext.getNodeId()).thenReturn(DUMMY_NODE_ID);
+ @Before
+ public void setUp() {
when(mockConnectionContext.getFeatures()).thenReturn(mockFeatures);
when(mockConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
when(mockConnectionContext.getDeviceInfo()).thenReturn(deviceInfo);
- when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
+ when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_IDENTIFIER);
when(deviceInfo.getNodeId()).thenReturn(DUMMY_NODE_ID);
- final Capabilities capabilitiesV13 = mock(Capabilities.class);
- final CapabilitiesV10 capabilitiesV10 = mock(CapabilitiesV10.class);
- when(mockFeatures.getCapabilities()).thenReturn(capabilitiesV13);
- when(mockFeatures.getCapabilitiesV10()).thenReturn(capabilitiesV10);
- when(mockFeatures.getDatapathId()).thenReturn(BigInteger.valueOf(21L));
-
- when(lifecycleConductor.getMessageIntelligenceAgency()).thenReturn(messageIntelligenceAgency);
- }
+ when(mockedFuture.isDone()).thenReturn(true);
+ doReturn(mockedFuture).when(writeTransaction).commit();
+ when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+
+ deviceManager = new DeviceManagerImpl(
+ new OpenflowProviderConfigBuilder()
+ .setBarrierCountLimit(new NonZeroUint16Type(BARRIER_COUNT_LIMIT))
+ .setBarrierIntervalTimeoutLimit(new NonZeroUint32Type(BARRIER_INTERVAL_NANOS))
+ .setGlobalNotificationQuota(TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA)
+ .setSwitchFeaturesMandatory(false)
+ .setEnableFlowRemovedNotification(true)
+ .setSkipTableFeatures(false)
+ .setUseSingleLayerSerialization(true)
+ .setIsStatisticsPollingOn(false)
+ .build(),
+ dataBroker,
+ messageIntelligenceAgency,
+ notificationPublishService,
+ new HashedWheelTimer(),
+ convertorExecutor,
+ DeviceInitializerProviderFactory.createDefaultProvider(),
+ executorService);
- @Test(expected = IllegalStateException.class)
- public void onDeviceContextLevelUpFailTest() throws Exception {
- onDeviceContextLevelUp(true);
+ deviceManager.setTranslatorLibrary(translatorLibrary);
+ verify(dataBroker).newWriteOnlyTransaction();
+ verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL), any(), any());
+ verify(writeTransaction).commit();
}
@Test
- public void onDeviceContextLevelUpSuccessTest() throws Exception {
- onDeviceContextLevelUp(false);
- }
-
- private DeviceManagerImpl prepareDeviceManager() {
- return prepareDeviceManager(false);
- }
-
- private DeviceManagerImpl prepareDeviceManager(final boolean withException) {
- final DataBroker mockedDataBroker = mock(DataBroker.class);
- final WriteTransaction mockedWriteTransaction = mock(WriteTransaction.class);
-
- final BindingTransactionChain mockedTxChain = mock(BindingTransactionChain.class);
- final WriteTransaction mockedWTx = mock(WriteTransaction.class);
- when(mockedTxChain.newWriteOnlyTransaction()).thenReturn(mockedWTx);
- when(mockedDataBroker.createTransactionChain(any(TransactionChainListener.class))).thenReturn
- (mockedTxChain);
- when(mockedDataBroker.newWriteOnlyTransaction()).thenReturn(mockedWriteTransaction);
+ public void createContext() {
+ final DeviceContext context = deviceManager.createContext(mockConnectionContext);
+ assertEquals(deviceInfo, context.getDeviceInfo());
- when(mockedWriteTransaction.submit()).thenReturn(mockedFuture);
-
- final DeviceManagerImpl deviceManager = new DeviceManagerImpl(mockedDataBroker,
- TEST_VALUE_GLOBAL_NOTIFICATION_QUOTA, false, barrierIntervalNanos, barrierCountLimit, lifecycleConductor, true);
-
- deviceManager.setDeviceInitializationPhaseHandler(deviceInitPhaseHandler);
- deviceManager.setDeviceTerminationPhaseHandler(deviceTerminationPhaseHandler);
-
- return deviceManager;
- }
-
- public void onDeviceContextLevelUp(final boolean withException) throws Exception {
- final DeviceManagerImpl deviceManager = prepareDeviceManager(withException);
- final DeviceState mockedDeviceState = mock(DeviceState.class);
- when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
-
- if (withException) {
- doThrow(new IllegalStateException("dummy")).when(mockedDeviceContext).initialSubmitTransaction();
- }
- deviceManager.addDeviceContextToMap(deviceInfo, mockedDeviceContext);
- deviceManager.onDeviceContextLevelUp(deviceInfo);
- if (withException) {
- verify(mockedDeviceContext).close();
- } else {
- verify(mockedDeviceContext).initialSubmitTransaction();
- verify(mockedDeviceContext).onPublished();
- }
}
@Test
- public void deviceConnectedTest() throws Exception{
- final DeviceManagerImpl deviceManager = prepareDeviceManager();
- injectMockTranslatorLibrary(deviceManager);
- final ConnectionContext mockConnectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
-
- deviceManager.deviceConnected(mockConnectionContext);
+ public void removeDeviceFromOperationalDS() throws Exception {
+ final ListenableFuture<?> future = deviceManager
+ .removeDeviceFromOperationalDS(DUMMY_IDENTIFIER);
- final InOrder order = inOrder(mockConnectionContext);
- order.verify(mockConnectionContext).setOutboundQueueProvider(any(OutboundQueueProvider.class));
- order.verify(mockConnectionContext).setOutboundQueueHandleRegistration(
- Mockito.<OutboundQueueHandlerRegistration<OutboundQueueProvider>>any());
- verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceInfo>any());
+ future.get();
+ assertTrue(future.isDone());
+ verify(writeTransaction).delete(LogicalDatastoreType.OPERATIONAL, DUMMY_IDENTIFIER);
}
- @Test
- public void deviceConnectedV10Test() throws Exception{
- final DeviceManagerImpl deviceManager = prepareDeviceManager();
- injectMockTranslatorLibrary(deviceManager);
- final ConnectionContext mockConnectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_0);
-
- final PhyPortBuilder phyPort = new PhyPortBuilder()
- .setPortNo(41L);
- when(mockFeatures.getPhyPort()).thenReturn(Collections.singletonList(phyPort.build()));
- final MessageTranslator<Object, Object> mockedTranslator = mock(MessageTranslator.class);
- when(mockedTranslator.translate(Matchers.<Object>any(), Matchers.<DeviceInfo>any(), Matchers.any()))
- .thenReturn(null);
- when(translatorLibrary.lookupTranslator(Matchers.<TranslatorKey>any())).thenReturn(mockedTranslator);
-
- deviceManager.deviceConnected(mockConnectionContext);
-
- final InOrder order = inOrder(mockConnectionContext);
- order.verify(mockConnectionContext).setOutboundQueueProvider(any(OutboundQueueProvider.class));
- order.verify(mockConnectionContext).setOutboundQueueHandleRegistration(
- Mockito.<OutboundQueueHandlerRegistration<OutboundQueueProvider>>any());
- verify(deviceInitPhaseHandler).onDeviceContextLevelUp(Matchers.<DeviceInfo>any());
+ @Test(expected = ExecutionException.class)
+ public void removeDeviceFromOperationalDSException() throws Exception {
+ final FluentFuture<?> failedFuture = FluentFutures.immediateFailedFluentFuture(
+ new ExecutionException(new Throwable("Test failed transaction")));
+ Mockito.doReturn(failedFuture).when(writeTransaction).commit();
+ final ListenableFuture<?> future = deviceManager.removeDeviceFromOperationalDS(DUMMY_IDENTIFIER);
+ future.get();
+ assertTrue(future.isDone());
+ verify(writeTransaction).delete(LogicalDatastoreType.OPERATIONAL, DUMMY_IDENTIFIER);
}
@Test
- public void deviceDisconnectedTest() throws Exception {
- final DeviceState deviceState = mock(DeviceState.class);
-
- final DeviceManagerImpl deviceManager = prepareDeviceManager();
- injectMockTranslatorLibrary(deviceManager);
-
- final ConnectionContext connectionContext = buildMockConnectionContext(OFConstants.OFP_VERSION_1_3);
- when(connectionContext.getNodeId()).thenReturn(mockedNodeId);
-
- final DeviceContext deviceContext = mock(DeviceContext.class);
- when(deviceContext.shuttingDownDataStoreTransactions()).thenReturn(Futures.immediateCheckedFuture(null));
- when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
- when(deviceContext.getDeviceState()).thenReturn(deviceState);
-
- final ConcurrentHashMap<DeviceInfo, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
- deviceContexts.put(deviceInfo, deviceContext);
-
- deviceManager.onDeviceDisconnected(connectionContext);
-
- verify(lifecycleConductor).newTimeout(Mockito.<TimerTask>any(), Mockito.anyLong(), Mockito.<TimeUnit>any());
- }
-
- protected ConnectionContext buildMockConnectionContext(final short ofpVersion) {
- when(mockFeatures.getVersion()).thenReturn(ofpVersion);
- when(outboundQueueProvider.reserveEntry()).thenReturn(43L);
- Mockito.doAnswer(new Answer<Void>() {
- @Override
- public Void answer(final InvocationOnMock invocation) throws Throwable {
- final FutureCallback<OfHeader> callBack = (FutureCallback<OfHeader>) invocation.getArguments()[2];
- callBack.onSuccess(null);
- return null;
- }
- })
- .when(outboundQueueProvider)
- .commitEntry(Matchers.anyLong(), Matchers.<MultipartRequestInput>any(), Matchers.<FutureCallback<OfHeader>>any());
-
- when(mockedConnectionAdapter.registerOutboundQueueHandler(Matchers.<OutboundQueueHandler>any(), Matchers.anyInt(), Matchers.anyLong()))
- .thenAnswer(new Answer<OutboundQueueHandlerRegistration<OutboundQueueHandler>>() {
- @Override
- public OutboundQueueHandlerRegistration<OutboundQueueHandler> answer(final InvocationOnMock invocation) throws Throwable {
- final OutboundQueueHandler handler = (OutboundQueueHandler) invocation.getArguments()[0];
- handler.onConnectionQueueChanged(outboundQueueProvider);
- return null;
- }
- });
-
- when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
- return mockConnectionContext;
+ public void sendNodeAddedNotification() {
+ deviceManager.sendNodeAddedNotification(DUMMY_IDENTIFIER);
+ deviceManager.sendNodeAddedNotification(DUMMY_IDENTIFIER);
+ verify(notificationPublishService).offerNotification(new NodeUpdatedBuilder()
+ .setId(DUMMY_NODE_ID)
+ .setNodeRef(new NodeRef(DUMMY_IDENTIFIER))
+ .build());
}
- private void injectMockTranslatorLibrary(final DeviceManagerImpl deviceManager) {
- deviceManager.setTranslatorLibrary(translatorLibrary);
+ @Test
+ public void sendNodeRemovedNotification() {
+ deviceManager.sendNodeAddedNotification(DUMMY_IDENTIFIER);
+ deviceManager.sendNodeRemovedNotification(DUMMY_IDENTIFIER);
+ deviceManager.sendNodeRemovedNotification(DUMMY_IDENTIFIER);
+ verify(notificationPublishService).offerNotification(new NodeRemovedBuilder()
+ .setNodeRef(new NodeRef(DUMMY_IDENTIFIER))
+ .build());
}
@Test
- public void testClose() throws Exception {
+ public void close() throws Exception {
final DeviceContext deviceContext = mock(DeviceContext.class);
- final DeviceManagerImpl deviceManager = prepareDeviceManager();
final ConcurrentHashMap<DeviceInfo, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
deviceContexts.put(deviceInfo, deviceContext);
Assert.assertEquals(1, deviceContexts.size());
-
deviceManager.close();
-
- verify(deviceContext).shutdownConnection();
- verify(deviceContext, Mockito.never()).close();
+ verify(deviceContext).close();
}
- private static ConcurrentHashMap<DeviceInfo, DeviceContext> getContextsCollection(final DeviceManagerImpl deviceManager) throws NoSuchFieldException, IllegalAccessException {
+ @SuppressWarnings("unchecked")
+ private static ConcurrentHashMap<DeviceInfo, DeviceContext> getContextsCollection(
+ final DeviceManagerImpl deviceManager) throws NoSuchFieldException, IllegalAccessException {
// HACK: contexts collection for testing shall be accessed in some more civilized way
final Field contextsField = DeviceManagerImpl.class.getDeclaredField("deviceContexts");
Assert.assertNotNull(contextsField);