- 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);
-
- 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
- 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
- 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;
- }
-
- private void injectMockTranslatorLibrary(final DeviceManagerImpl deviceManager) {
- deviceManager.setTranslatorLibrary(translatorLibrary);
- }
-