From b7a5e5d534f1345d75aa998f8d7feee70c0b9d62 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 7 Feb 2024 03:26:24 +0100 Subject: [PATCH] Ditch use of SystemNotificationsListener Introduce SystemListener, which does essentially the same thing. Change-Id: I97ae5237e3313d757e1a01ad9d022cfbfda498e1 Signed-off-by: Robert Varga --- .../api/connection/ConnectionAdapter.java | 15 +++- .../connection/ConnectionAdapterImpl.java | 75 +++++++++--------- .../ConnectionAdapterImplStatisticsTest.java | 77 ++++++++++++------ .../connection/ConnectionAdapterImplTest.java | 27 ++++--- .../protocol/it/integration/MockPlugin.java | 79 +++++++++---------- .../connection/ConnectionManagerImpl.java | 12 +-- .../OpenflowProtocolListenerInitialImpl.java | 27 +++++-- .../SystemNotificationsListenerImpl.java | 51 ++++++------ .../OpenflowProtocolListenerFullImpl.java | 33 +++++--- ...enflowProtocolListenerInitialImplTest.java | 36 +++++---- .../SystemNotificationsListenerImplTest.java | 23 +++--- .../OpenflowProtocolListenerFullImplTest.java | 41 +++++----- 12 files changed, 276 insertions(+), 220 deletions(-) diff --git a/openflowjava/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/connection/ConnectionAdapter.java b/openflowjava/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/connection/ConnectionAdapter.java index c60311af64..9a9e9a3067 100644 --- a/openflowjava/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/connection/ConnectionAdapter.java +++ b/openflowjava/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/connection/ConnectionAdapter.java @@ -14,11 +14,14 @@ import java.security.cert.X509Certificate; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.openflowjava.protocol.api.extensibility.AlienMessageListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SslConnectionError; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent; /** * Manages a switch connection. @@ -27,6 +30,14 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.S * @author michal.polkorab */ public interface ConnectionAdapter extends OpenflowProtocolService { + @NonNullByDefault + interface SystemListener { + void onDisconnect(DisconnectEvent disconnect); + + void onSwitchIdle(SwitchIdleEvent switchIdle); + + void onSslConnectionError(SslConnectionError sslConnectionError); + } /** * Disconnect corresponding switch. @@ -61,7 +72,7 @@ public interface ConnectionAdapter extends OpenflowProtocolService { * * @param systemListener here will be pushed all system messages from library */ - void setSystemListener(SystemNotificationsListener systemListener); + void setSystemListener(SystemListener systemListener); /** * Set handler for alien messages received from device. diff --git a/openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImpl.java b/openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImpl.java index 94ce4e18ab..3efd60fcdd 100644 --- a/openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImpl.java +++ b/openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImpl.java @@ -48,7 +48,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.D import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SslConnectionError; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SslConnectionErrorBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927._switch.certificate.IssuerBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927._switch.certificate.SubjectBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.ssl.connection.error.SwitchCertificate; @@ -68,7 +67,7 @@ public class ConnectionAdapterImpl extends AbstractConnectionAdapterStatistics i private ConnectionReadyListener connectionReadyListener; private OpenflowProtocolListener messageListener; - private SystemNotificationsListener systemListener; + private SystemListener systemListener; private AlienMessageListener alienMessageListener; private AbstractOutboundQueueManager outputManager; private OFVersionDetector versionDetector; @@ -102,7 +101,7 @@ public class ConnectionAdapterImpl extends AbstractConnectionAdapterStatistics i } @Override - public void setSystemListener(final SystemNotificationsListener systemListener) { + public void setSystemListener(final SystemListener systemListener) { this.systemListener = systemListener; } @@ -120,65 +119,63 @@ public class ConnectionAdapterImpl extends AbstractConnectionAdapterStatistics i return; } if (message instanceof Notification) { - // System events - if (message instanceof DisconnectEvent) { - systemListener.onDisconnectEvent((DisconnectEvent) message); + if (message instanceof DisconnectEvent disconnect) { + systemListener.onDisconnect(disconnect); responseCache.invalidateAll(); disconnectOccured = true; - } else if (message instanceof SwitchIdleEvent) { - systemListener.onSwitchIdleEvent((SwitchIdleEvent) message); - } else if (message instanceof SslConnectionError) { + } else if (message instanceof SwitchIdleEvent switchIdle) { + systemListener.onSwitchIdle(switchIdle); + } else if (message instanceof SslConnectionError sslError) { systemListener.onSslConnectionError(new SslConnectionErrorBuilder() - .setInfo(((SslConnectionError) message).getInfo()) - .setSwitchCertificate(buildSwitchCertificate()) - .build()); + .setInfo(sslError.getInfo()) + .setSwitchCertificate(buildSwitchCertificate()) + .build()); // OpenFlow messages - } else if (message instanceof EchoRequestMessage) { + } else if (message instanceof EchoRequestMessage echoRequest) { if (outputManager != null) { - outputManager.onEchoRequest((EchoRequestMessage) message, datapathId); + outputManager.onEchoRequest(echoRequest, datapathId); } else { - messageListener.onEchoRequestMessage((EchoRequestMessage) message); + messageListener.onEchoRequestMessage(echoRequest); } - } else if (message instanceof ErrorMessage) { + } else if (message instanceof ErrorMessage error) { // Send only unmatched errors - if (outputManager == null || !outputManager.onMessage((OfHeader) message)) { - messageListener.onErrorMessage((ErrorMessage) message); + if (outputManager == null || !outputManager.onMessage(error)) { + messageListener.onErrorMessage(error); } - } else if (message instanceof ExperimenterMessage) { + } else if (message instanceof ExperimenterMessage experimenter) { if (outputManager != null) { - outputManager.onMessage((OfHeader) message); + outputManager.onMessage(experimenter); } - messageListener.onExperimenterMessage((ExperimenterMessage) message); - } else if (message instanceof FlowRemovedMessage) { - messageListener.onFlowRemovedMessage((FlowRemovedMessage) message); - } else if (message instanceof HelloMessage) { + messageListener.onExperimenterMessage(experimenter); + } else if (message instanceof FlowRemovedMessage flowRemoved) { + messageListener.onFlowRemovedMessage(flowRemoved); + } else if (message instanceof HelloMessage hello) { LOG.info("Hello received"); - messageListener.onHelloMessage((HelloMessage) message); - } else if (message instanceof MultipartReplyMessage) { + messageListener.onHelloMessage(hello); + } else if (message instanceof MultipartReplyMessage multipartReply) { if (outputManager != null) { - outputManager.onMessage((OfHeader) message); + outputManager.onMessage(multipartReply); } - messageListener.onMultipartReplyMessage((MultipartReplyMessage) message); - } else if (message instanceof PacketInMessage) { - messageListener.onPacketInMessage((PacketInMessage) message); - } else if (message instanceof PortStatusMessage) { - messageListener.onPortStatusMessage((PortStatusMessage) message); + messageListener.onMultipartReplyMessage(multipartReply); + } else if (message instanceof PacketInMessage packetIn) { + messageListener.onPacketInMessage(packetIn); + } else if (message instanceof PortStatusMessage portStatus) { + messageListener.onPortStatusMessage(portStatus); } else { LOG.warn("message listening not supported for type: {}", message.getClass()); } - } else if (message instanceof OfHeader) { + } else if (message instanceof OfHeader header) { LOG.debug("OF header msg received"); - if (alienMessageListener != null && alienMessageListener.onAlienMessage((OfHeader) message)) { - LOG.debug("Alien message {} received", message.implementedInterface()); - } else if (outputManager == null || !outputManager.onMessage((OfHeader) message) - || message instanceof EchoOutput) { - final RpcResponseKey key = createRpcResponseKey((OfHeader) message); + if (alienMessageListener != null && alienMessageListener.onAlienMessage(header)) { + LOG.debug("Alien message {} received", header.implementedInterface()); + } else if (outputManager == null || !outputManager.onMessage(header) || header instanceof EchoOutput) { + final RpcResponseKey key = createRpcResponseKey(header); final ResponseExpectedRpcListener listener = findRpcResponse(key); if (listener != null) { LOG.debug("Corresponding rpcFuture found"); - listener.completed((OfHeader) message); + listener.completed(header); LOG.debug("After setting rpcFuture"); responseCache.invalidate(key); } diff --git a/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImplStatisticsTest.java b/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImplStatisticsTest.java index 69bebc495d..6b44deb04a 100644 --- a/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImplStatisticsTest.java +++ b/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImplStatisticsTest.java @@ -27,6 +27,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter.SystemListener; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener; import org.opendaylight.openflowjava.statistics.CounterEventTypes; import org.opendaylight.openflowjava.statistics.StatisticsCounters; @@ -59,7 +60,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.common.Uint32; @@ -77,31 +77,56 @@ public class ConnectionAdapterImplStatisticsTest { private static final RemovalListener> REMOVAL_LISTENER = notification -> notification.getValue().discard(); - @Mock SystemNotificationsListener systemListener; - @Mock ConnectionReadyListener readyListener; - @Mock ChannelFuture channelFuture; - @Mock OpenflowProtocolListener messageListener; - @Mock SocketChannel channel; - @Mock ChannelPipeline pipeline; - @Mock EchoInput echoInput; - @Mock BarrierInput barrierInput; - @Mock EchoReplyInput echoReplyInput; - @Mock ExperimenterInput experimenterInput; - @Mock FlowModInput flowModInput; - @Mock GetConfigInput getConfigInput; - @Mock GetFeaturesInput getFeaturesInput; - @Mock GetQueueConfigInput getQueueConfigInput; - @Mock GroupModInput groupModInput; - @Mock HelloInput helloInput; - @Mock MeterModInput meterModInput; - @Mock PacketOutInput packetOutInput; - @Mock MultipartRequestInput multipartRequestInput; - @Mock PortModInput portModInput; - @Mock RoleRequestInput roleRequestInput; - @Mock SetConfigInput setConfigInput; - @Mock TableModInput tableModInput; - @Mock GetAsyncInput getAsyncInput; - @Mock SetAsyncInput setAsyncInput; + @Mock + private SystemListener systemListener; + @Mock + private ConnectionReadyListener readyListener; + @Mock + private ChannelFuture channelFuture; + @Mock + private OpenflowProtocolListener messageListener; + @Mock + private SocketChannel channel; + @Mock + private ChannelPipeline pipeline; + @Mock + private EchoInput echoInput; + @Mock + private BarrierInput barrierInput; + @Mock + private EchoReplyInput echoReplyInput; + @Mock + private ExperimenterInput experimenterInput; + @Mock + private FlowModInput flowModInput; + @Mock + private GetConfigInput getConfigInput; + @Mock + private GetFeaturesInput getFeaturesInput; + @Mock + private GetQueueConfigInput getQueueConfigInput; + @Mock + private GroupModInput groupModInput; + @Mock + private HelloInput helloInput; + @Mock + private MeterModInput meterModInput; + @Mock + private PacketOutInput packetOutInput; + @Mock + private MultipartRequestInput multipartRequestInput; + @Mock + private PortModInput portModInput; + @Mock + private RoleRequestInput roleRequestInput; + @Mock + private SetConfigInput setConfigInput; + @Mock + private TableModInput tableModInput; + @Mock + private GetAsyncInput getAsyncInput; + @Mock + private SetAsyncInput setAsyncInput; private ConnectionAdapterImpl adapter; private Cache> cache; diff --git a/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImplTest.java b/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImplTest.java index 6f5ce57893..af201c0c5e 100644 --- a/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImplTest.java +++ b/openflowjava/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImplTest.java @@ -27,6 +27,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter.SystemListener; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener; import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput; @@ -55,7 +56,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.D import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEventBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.common.Uint32; @@ -73,13 +73,20 @@ public class ConnectionAdapterImplTest { private static final RemovalListener> REMOVAL_LISTENER = notification -> notification.getValue().discard(); - @Mock SocketChannel channel; - @Mock ChannelPipeline pipeline; - @Mock OpenflowProtocolListener messageListener; - @Mock SystemNotificationsListener systemListener; - @Mock ConnectionReadyListener readyListener; - @Mock Cache> mockCache; - @Mock ChannelFuture channelFuture; + @Mock + private SocketChannel channel; + @Mock + ChannelPipeline pipeline; + @Mock + private OpenflowProtocolListener messageListener; + @Mock + private SystemListener systemListener; + @Mock + private ConnectionReadyListener readyListener; + @Mock + private Cache> mockCache; + @Mock + private ChannelFuture channelFuture; private ConnectionAdapterImpl adapter; private Cache> cache; @@ -132,10 +139,10 @@ public class ConnectionAdapterImplTest { verify(messageListener, times(1)).onPortStatusMessage((PortStatusMessage) message); message = new SwitchIdleEventBuilder().build(); adapter.consume(message); - verify(systemListener, times(1)).onSwitchIdleEvent((SwitchIdleEvent) message); + verify(systemListener, times(1)).onSwitchIdle((SwitchIdleEvent) message); message = new DisconnectEventBuilder().build(); adapter.consume(message); - verify(systemListener, times(1)).onDisconnectEvent((DisconnectEvent) message); + verify(systemListener, times(1)).onDisconnect((DisconnectEvent) message); message = new EchoRequestMessageBuilder().build(); adapter.consume(message); verify(messageListener, times(1)).onEchoRequestMessage((EchoRequestMessage) message); diff --git a/openflowjava/openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/it/integration/MockPlugin.java b/openflowjava/openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/it/integration/MockPlugin.java index 133561d399..b366515a2d 100644 --- a/openflowjava/openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/it/integration/MockPlugin.java +++ b/openflowjava/openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/it/integration/MockPlugin.java @@ -15,6 +15,7 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter; +import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter.SystemListener; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener; import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler; import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl; @@ -37,7 +38,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SslConnectionError; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.Uint32; @@ -50,16 +50,18 @@ import org.slf4j.LoggerFactory; * * @author michal.polkorab */ -public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler, - SystemNotificationsListener, ConnectionReadyListener { - +public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler, SystemListener, + ConnectionReadyListener { protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class); - protected volatile ConnectionAdapter adapter; + private final SettableFuture finishedFuture; - private int idleCounter = 0; private final ExecutorService executorService; - public MockPlugin(ExecutorService executorService) { + private int idleCounter = 0; + + protected volatile ConnectionAdapter adapter; + + public MockPlugin(final ExecutorService executorService) { LOGGER.trace("Creating MockPlugin"); finishedFuture = SettableFuture.create(); this.executorService = executorService; @@ -67,9 +69,9 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan } @Override - public void onSwitchConnected(ConnectionAdapter connection) { + public void onSwitchConnected(final ConnectionAdapter connection) { LOGGER.debug("onSwitchConnected: {}", connection); - this.adapter = connection; + adapter = connection; connection.setMessageListener(this); connection.setSystemListener(this); connection.setConnectionReadyListener(this); @@ -77,12 +79,27 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan } @Override - public boolean accept(InetAddress switchAddress) { + public boolean accept(final InetAddress switchAddress) { LOGGER.debug("MockPlugin.accept(): {}", switchAddress.toString()); - return true; } + @Override + public void onDisconnect(final DisconnectEvent disconnect) { + LOGGER.debug("disconnection occured: {}", disconnect.getInfo()); + } + + @Override + public void onSslConnectionError(final SslConnectionError sslConnectionError) { + LOGGER.debug("Ssl error occured: {}", sslConnectionError.getInfo()); + } + + @Override + public void onSwitchIdle(final SwitchIdleEvent switchIdle) { + LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: {}", switchIdle.getInfo()); + idleCounter++; + } + @Override public void onEchoRequestMessage(final EchoRequestMessage notification) { LOGGER.debug("MockPlugin.onEchoRequestMessage() adapter: {}", adapter); @@ -99,25 +116,22 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan } @Override - public void onErrorMessage(ErrorMessage notification) { + public void onErrorMessage(final ErrorMessage notification) { LOGGER.debug("Error message received"); - } @Override - public void onExperimenterMessage(ExperimenterMessage notification) { + public void onExperimenterMessage(final ExperimenterMessage notification) { LOGGER.debug("Experimenter message received"); - } @Override - public void onFlowRemovedMessage(FlowRemovedMessage notification) { + public void onFlowRemovedMessage(final FlowRemovedMessage notification) { LOGGER.debug("FlowRemoved message received"); - } @Override - public void onHelloMessage(HelloMessage notification) { + public void onHelloMessage(final HelloMessage notification) { new Thread(() -> { LOGGER.debug("MockPlugin.onHelloMessage().run() Hello message received"); HelloInputBuilder hib = new HelloInputBuilder(); @@ -128,7 +142,6 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan LOGGER.debug("hello msg sent"); new Thread(this::getSwitchFeatures).start(); }).start(); - } protected void getSwitchFeatures() { @@ -164,13 +177,12 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan } @Override - public void onMultipartReplyMessage(MultipartReplyMessage notification) { + public void onMultipartReplyMessage(final MultipartReplyMessage notification) { LOGGER.debug("MultipartReply message received"); - } @Override - public void onPacketInMessage(PacketInMessage notification) { + public void onPacketInMessage(final PacketInMessage notification) { LOGGER.debug("PacketIn message received"); LOGGER.debug("BufferId: {}", notification.getBufferId()); LOGGER.debug("TotalLength: {}", notification.getTotalLen()); @@ -183,26 +195,14 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan } @Override - public void onPortStatusMessage(PortStatusMessage notification) { + public void onPortStatusMessage(final PortStatusMessage notification) { LOGGER.debug("MockPlugin.onPortStatusMessage() message received"); - - } - - @Override - public void onDisconnectEvent(DisconnectEvent notification) { - LOGGER.debug("disconnection occured: {}", notification.getInfo()); } public SettableFuture getFinishedFuture() { return finishedFuture; } - @Override - public void onSwitchIdleEvent(SwitchIdleEvent notification) { - LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: {}", notification.getInfo()); - idleCounter++; - } - /** * Returns number of occurred idleEvents. */ @@ -222,14 +222,9 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan * @param host - host IP * @param port - port number */ - public void initiateConnection(SwitchConnectionProviderImpl switchConnectionProvider, String host, int port) { + public void initiateConnection(final SwitchConnectionProviderImpl switchConnectionProvider, final String host, + final int port) { LOGGER.trace("MockPlugin().initiateConnection()"); switchConnectionProvider.initiateConnection(host, port); } - - @Override - public void onSslConnectionError(SslConnectionError notification) { - LOGGER.debug("Ssl error occured: {}", notification.getInfo()); - - } } diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/ConnectionManagerImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/ConnectionManagerImpl.java index f68b00fe7a..3810f3e08f 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/ConnectionManagerImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/ConnectionManagerImpl.java @@ -46,8 +46,6 @@ import org.opendaylight.openflowplugin.impl.connection.listener.OpenflowProtocol import org.opendaylight.openflowplugin.impl.connection.listener.SystemNotificationsListenerImpl; 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.openflow.protocol.rev130731.OpenflowProtocolListener; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; @@ -111,13 +109,11 @@ public class ConnectionManagerImpl implements ConnectionManager { connectionContext, handshakeContext); connectionAdapter.setConnectionReadyListener(connectionReadyListener); - final OpenflowProtocolListener ofMessageListener = - new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext); - connectionAdapter.setMessageListener(ofMessageListener); + connectionAdapter.setMessageListener( + new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext)); - final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext, - config.getEchoReplyTimeout().getValue().toJava(), executorService, notificationPublishService); - connectionAdapter.setSystemListener(systemListener); + connectionAdapter.setSystemListener(new SystemNotificationsListenerImpl(connectionContext, + config.getEchoReplyTimeout().getValue().toJava(), executorService, notificationPublishService)); LOG.trace("connection ballet finished"); } diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/OpenflowProtocolListenerInitialImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/OpenflowProtocolListenerInitialImpl.java index eb9c190661..d31cef0087 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/OpenflowProtocolListenerInitialImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/OpenflowProtocolListenerInitialImpl.java @@ -7,11 +7,15 @@ */ package org.opendaylight.openflowplugin.impl.connection.listener; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.MoreExecutors; import java.util.Objects; import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext; import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext; import org.opendaylight.openflowplugin.impl.connection.HandshakeStepWrapper; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage; @@ -21,6 +25,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage; +import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,15 +49,21 @@ public class OpenflowProtocolListenerInitialImpl implements OpenflowProtocolList @Override public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) { - if (LOG.isDebugEnabled()) { - LOG.debug("echo request received: {}", echoRequestMessage.getXid()); - } - EchoReplyInputBuilder builder = new EchoReplyInputBuilder(); - builder.setVersion(echoRequestMessage.getVersion()); - builder.setXid(echoRequestMessage.getXid()); - builder.setData(echoRequestMessage.getData()); + final var xid = echoRequestMessage.getXid(); + LOG.debug("echo request received: {}", xid); + Futures.addCallback(connectionContext.getConnectionAdapter().echoReply( + new EchoReplyInputBuilder().setXid(xid).setData(echoRequestMessage.getData()).build()), + new FutureCallback<>() { + @Override + public void onSuccess(final RpcResult result) { + LOG.debug("echo reply sent: {}", xid); + } - connectionContext.getConnectionAdapter().echoReply(builder.build()); + @Override + public void onFailure(final Throwable cause) { + LOG.debug("echo reply failed: {}", xid, cause); + } + }, MoreExecutors.directExecutor()); } @Override diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImpl.java index 9211867f39..e98cca75de 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImpl.java @@ -19,6 +19,7 @@ import java.util.concurrent.TimeUnit; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.binding.api.NotificationPublishService; +import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter.SystemListener; import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext; import org.opendaylight.openflowplugin.api.openflow.device.Xid; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil; @@ -32,7 +33,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SslConnectionError; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.Uint16; @@ -40,8 +40,7 @@ import org.opendaylight.yangtools.yang.common.Uint32; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SystemNotificationsListenerImpl implements SystemNotificationsListener { - +public class SystemNotificationsListenerImpl implements SystemListener { private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class); private static final Logger OF_EVENT_LOG = LoggerFactory.getLogger("OfEventLog"); private static final Xid ECHO_XID = new Xid(Uint32.ZERO); @@ -54,28 +53,39 @@ public class SystemNotificationsListenerImpl implements SystemNotificationsListe private final NotificationPublishService notificationPublishService; public SystemNotificationsListenerImpl(@NonNull final ConnectionContext connectionContext, - final long echoReplyTimeout, - @NonNull final Executor executor, - @NonNull final NotificationPublishService notificationPublishService) { - this.executor = requireNonNull(executor); + final long echoReplyTimeout, final @NonNull Executor executor, + @NonNull final NotificationPublishService notificationPublishService) { this.connectionContext = requireNonNull(connectionContext); this.echoReplyTimeout = echoReplyTimeout; - this.notificationPublishService = notificationPublishService; + this.executor = requireNonNull(executor); + this.notificationPublishService = requireNonNull(notificationPublishService); } @Override - public void onDisconnectEvent(final DisconnectEvent notification) { - OF_EVENT_LOG.debug("Disconnect, Node: {}", connectionContext.getSafeNodeIdForLOG()); - LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}", - connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG()); - connectionContext.onConnectionClosed(); + public void onSslConnectionError(final SslConnectionError sslConnectionError) { + final var switchCert = sslConnectionError.getSwitchCertificate(); + notificationPublishService.offerNotification(new SslErrorBuilder() + .setType(SslErrorType.SslConFailed) + .setCode(Uint16.valueOf(SslErrorType.SslConFailed.getIntValue())) + .setNodeIpAddress(remoteAddress()) + .setData(sslConnectionError.getInfo()) + .setSwitchCertificate(switchCert == null ? null : new SwitchCertificateBuilder(switchCert).build()) + .build()); } @Override - public void onSwitchIdleEvent(final SwitchIdleEvent notification) { + public void onSwitchIdle(final SwitchIdleEvent switchIdle) { executor.execute(this::executeOnSwitchIdleEvent); } + @Override + public void onDisconnect(final DisconnectEvent notification) { + OF_EVENT_LOG.debug("Disconnect, Node: {}", connectionContext.getSafeNodeIdForLOG()); + LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}", + connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG()); + connectionContext.onConnectionClosed(); + } + @SuppressWarnings("checkstyle:IllegalCatch") private void executeOnSwitchIdleEvent() { boolean shouldBeDisconnected = true; @@ -141,19 +151,6 @@ public class SystemNotificationsListenerImpl implements SystemNotificationsListe } } - @Override - public void onSslConnectionError(final SslConnectionError notification) { - final var switchCert = notification.getSwitchCertificate(); - - notificationPublishService.offerNotification(new SslErrorBuilder() - .setType(SslErrorType.SslConFailed) - .setCode(Uint16.valueOf(SslErrorType.SslConFailed.getIntValue())) - .setNodeIpAddress(remoteAddress()) - .setData(notification.getInfo()) - .setSwitchCertificate(switchCert == null ? null : new SwitchCertificateBuilder(switchCert).build()) - .build()); - } - private @Nullable IpAddress remoteAddress() { final var connectionAdapter = connectionContext.getConnectionAdapter(); if (connectionAdapter != null) { diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/listener/OpenflowProtocolListenerFullImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/listener/OpenflowProtocolListenerFullImpl.java index 574fa1c4db..546b3f30c1 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/listener/OpenflowProtocolListenerFullImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/listener/OpenflowProtocolListenerFullImpl.java @@ -7,11 +7,15 @@ */ package org.opendaylight.openflowplugin.impl.device.listener; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.MoreExecutors; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter; import org.opendaylight.openflowjava.protocol.api.extensibility.AlienMessageListener; import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor; import org.opendaylight.openflowplugin.api.openflow.device.listener.OpenflowMessageListenerFacade; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage; @@ -21,6 +25,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage; +import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,15 +50,25 @@ public class OpenflowProtocolListenerFullImpl implements AlienMessageListener, O @Override public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) { - if (LOG.isDebugEnabled()) { - LOG.debug("echo request received: {}", echoRequestMessage.getXid()); - } - final EchoReplyInputBuilder builder = new EchoReplyInputBuilder(); - builder.setVersion(echoRequestMessage.getVersion()); - builder.setXid(echoRequestMessage.getXid()); - builder.setData(echoRequestMessage.getData()); - - connectionAdapter.echoReply(builder.build()); + final var xid = echoRequestMessage.getXid(); + LOG.debug("echo request received: {}", xid); + Futures.addCallback(connectionAdapter.echoReply( + new EchoReplyInputBuilder() + .setVersion(echoRequestMessage.getVersion()) + .setXid(xid) + .setData(echoRequestMessage.getData()) + .build()), + new FutureCallback<>() { + @Override + public void onSuccess(final RpcResult result) { + LOG.debug("echo reply sent: {}", xid); + } + + @Override + public void onFailure(final Throwable cause) { + LOG.debug("echo reply failed: {}", xid, cause); + } + }, MoreExecutors.directExecutor()); } @Override diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/connection/listener/OpenflowProtocolListenerInitialImplTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/connection/listener/OpenflowProtocolListenerInitialImplTest.java index d5114bb476..7efa4e7956 100644 --- a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/connection/listener/OpenflowProtocolListenerInitialImplTest.java +++ b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/connection/listener/OpenflowProtocolListenerInitialImplTest.java @@ -7,13 +7,17 @@ */ package org.opendaylight.openflowplugin.impl.connection.listener; -import org.junit.Assert; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.util.concurrent.Futures; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentMatchers; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter; import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; @@ -29,7 +33,6 @@ import org.opendaylight.yangtools.yang.common.Uint32; */ @RunWith(MockitoJUnitRunner.class) public class OpenflowProtocolListenerInitialImplTest { - @Mock private ConnectionContext connectionContext; @Mock @@ -43,23 +46,26 @@ public class OpenflowProtocolListenerInitialImplTest { @Before public void setUp() { - Mockito.when(connectionAdapter.isAlive()).thenReturn(true); - Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter); - Mockito.when(connectionContext.getConnectionState()) + when(connectionAdapter.isAlive()).thenReturn(true); + when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter); + when(connectionContext.getConnectionState()) .thenReturn(null, ConnectionContext.CONNECTION_STATE.HANDSHAKING); - Mockito.when(handshakeContext.getHandshakeManager()).thenReturn(handshakeManager); + when(handshakeContext.getHandshakeManager()).thenReturn(handshakeManager); openflowProtocolListenerInitial = new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext); } @Test public void testOnEchoRequestMessage() { - EchoRequestMessageBuilder echoRequestMessageBld = new EchoRequestMessageBuilder() + when(connectionAdapter.echoReply(any())).thenReturn(Futures.immediateFuture(null)); + + openflowProtocolListenerInitial.onEchoRequestMessage( + new EchoRequestMessageBuilder() .setXid(Uint32.valueOf(42)) - .setVersion(EncodeConstants.OF_VERSION_1_3); - openflowProtocolListenerInitial.onEchoRequestMessage(echoRequestMessageBld.build()); + .setVersion(EncodeConstants.OF_VERSION_1_3) + .build()); - Mockito.verify(connectionAdapter).echoReply(ArgumentMatchers.any()); + verify(connectionAdapter).echoReply(any()); } @Test @@ -69,12 +75,12 @@ public class OpenflowProtocolListenerInitialImplTest { .setVersion(EncodeConstants.OF_VERSION_1_3); openflowProtocolListenerInitial.onHelloMessage(helloMessageBld.build()); - Mockito.verify(handshakeManager).shake(ArgumentMatchers.any()); + verify(handshakeManager).shake(any()); } @Test public void testCheckState() { - Assert.assertFalse(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)); - Assert.assertTrue(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)); + assertFalse(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)); + assertTrue(openflowProtocolListenerInitial.checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)); } } \ No newline at end of file diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImplTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImplTest.java index effbf13bf8..dffd353204 100644 --- a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImplTest.java +++ b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImplTest.java @@ -25,6 +25,7 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.mdsal.binding.api.NotificationPublishService; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter; +import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter.SystemListener; import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext; import org.opendaylight.openflowplugin.api.openflow.connection.DeviceConnectionStatusProvider; import org.opendaylight.openflowplugin.impl.connection.ConnectionContextImpl; @@ -36,7 +37,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEventBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; @@ -63,7 +63,7 @@ public class SystemNotificationsListenerImplTest { private ConnectionContext connectionContext; private ConnectionContextImpl connectionContextGolem; - private SystemNotificationsListenerImpl systemNotificationsListener; + private SystemListener systemNotificationsListener; private static final NodeId NODE_ID = new NodeId("OFP:TEST"); @@ -100,7 +100,7 @@ public class SystemNotificationsListenerImplTest { public void testOnDisconnectEvent1() { DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build(); - systemNotificationsListener.onDisconnectEvent(disconnectNotification); + systemNotificationsListener.onDisconnect(disconnectNotification); verifyCommonInvocationsSubSet(); Mockito.verify(connectionContext).onConnectionClosed(); @@ -114,8 +114,7 @@ public class SystemNotificationsListenerImplTest { @Test public void testOnDisconnectEvent2() { - DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build(); - systemNotificationsListener.onDisconnectEvent(disconnectNotification); + systemNotificationsListener.onDisconnect(new DisconnectEventBuilder().setInfo("testing disconnect").build()); verifyCommonInvocationsSubSet(); Mockito.verify(connectionContext).onConnectionClosed(); @@ -130,8 +129,7 @@ public class SystemNotificationsListenerImplTest { public void testOnDisconnectEvent3() { connectionContextGolem.changeStateToTimeouting(); - DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build(); - systemNotificationsListener.onDisconnectEvent(disconnectNotification); + systemNotificationsListener.onDisconnect(new DisconnectEventBuilder().setInfo("testing disconnect").build()); verifyCommonInvocationsSubSet(); Mockito.verify(connectionContext).onConnectionClosed(); @@ -146,8 +144,7 @@ public class SystemNotificationsListenerImplTest { public void testOnDisconnectEvent4() { Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.RIP); - DisconnectEvent disconnectNotification = new DisconnectEventBuilder().setInfo("testing disconnect").build(); - systemNotificationsListener.onDisconnectEvent(disconnectNotification); + systemNotificationsListener.onDisconnect(new DisconnectEventBuilder().setInfo("testing disconnect").build()); verifyCommonInvocationsSubSet(); Mockito.verify(connectionContext).onConnectionClosed(); @@ -165,8 +162,8 @@ public class SystemNotificationsListenerImplTest { Mockito.when(connectionAdapter.echo(any(EchoInput.class))).thenReturn(echoReply); - SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build(); - systemNotificationsListener.onSwitchIdleEvent(notification); + systemNotificationsListener.onSwitchIdle( + new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build()); // make sure that the idle notification processing thread started Thread.sleep(SAFE_TIMEOUT); @@ -189,8 +186,8 @@ public class SystemNotificationsListenerImplTest { Mockito.when(connectionAdapter.disconnect()) .thenReturn(Futures.immediateFailedFuture(new Exception("unit exception"))); - SwitchIdleEvent notification = new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build(); - systemNotificationsListener.onSwitchIdleEvent(notification); + systemNotificationsListener.onSwitchIdle( + new SwitchIdleEventBuilder().setInfo("wake up, device sleeps").build()); Thread.sleep(SystemNotificationsListenerImpl.MAX_ECHO_REPLY_TIMEOUT + SAFE_TIMEOUT); diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/listener/OpenflowProtocolListenerFullImplTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/listener/OpenflowProtocolListenerFullImplTest.java index ffe7362f6d..2de02d448e 100644 --- a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/listener/OpenflowProtocolListenerFullImplTest.java +++ b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/listener/OpenflowProtocolListenerFullImplTest.java @@ -8,21 +8,22 @@ package org.opendaylight.openflowplugin.impl.device.listener; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; +import com.google.common.util.concurrent.Futures; import java.net.InetSocketAddress; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentMatchers; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter; import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessageBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder; @@ -48,9 +49,6 @@ import org.opendaylight.yangtools.yang.common.Uint32; */ @RunWith(MockitoJUnitRunner.class) public class OpenflowProtocolListenerFullImplTest { - - private OpenflowProtocolListenerFullImpl ofProtocolListener; - @Mock private DeviceReplyProcessor deviceReplyProcessor; @Mock @@ -58,19 +56,21 @@ public class OpenflowProtocolListenerFullImplTest { private final Uint32 xid = Uint32.valueOf(42); + private OpenflowProtocolListenerFullImpl ofProtocolListener; + @Before public void setUp() { // place for mocking method's general behavior for HandshakeContext and ConnectionContext ofProtocolListener = new OpenflowProtocolListenerFullImpl(connectionAdapter, deviceReplyProcessor); connectionAdapter.setMessageListener(ofProtocolListener); - Mockito.when(connectionAdapter.getRemoteAddress()) + when(connectionAdapter.getRemoteAddress()) .thenReturn(InetSocketAddress.createUnresolved("ofp-junit.example.org", 6663)); - Mockito.verify(connectionAdapter).setMessageListener(any(OpenflowProtocolListener.class)); + verify(connectionAdapter).setMessageListener(any(OpenflowProtocolListener.class)); } @After public void tearDown() { - Mockito.verifyNoMoreInteractions(connectionAdapter, deviceReplyProcessor); + verifyNoMoreInteractions(connectionAdapter, deviceReplyProcessor); } /** @@ -80,11 +80,11 @@ public class OpenflowProtocolListenerFullImplTest { */ @Test public void testOnEchoRequestMessage() { - EchoRequestMessage echoRequestMessage = new EchoRequestMessageBuilder() - .setVersion(EncodeConstants.OF_VERSION_1_3).setXid(xid).build(); - ofProtocolListener.onEchoRequestMessage(echoRequestMessage); + when(connectionAdapter.echoReply(any())).thenReturn(Futures.immediateFuture(null)); + ofProtocolListener.onEchoRequestMessage( + new EchoRequestMessageBuilder().setVersion(EncodeConstants.OF_VERSION_1_3).setXid(xid).build()); - Mockito.verify(connectionAdapter).echoReply(any(EchoReplyInput.class)); + verify(connectionAdapter).echoReply(any(EchoReplyInput.class)); } /** @@ -98,7 +98,7 @@ public class OpenflowProtocolListenerFullImplTest { .setVersion(EncodeConstants.OF_VERSION_1_3).setXid(xid).build(); ofProtocolListener.onErrorMessage(errorMessage); - Mockito.verify(deviceReplyProcessor).processReply(any(ErrorMessage.class)); + verify(deviceReplyProcessor).processReply(any(ErrorMessage.class)); } /** @@ -112,7 +112,7 @@ public class OpenflowProtocolListenerFullImplTest { .setVersion(EncodeConstants.OF_VERSION_1_3).setXid(xid).build(); ofProtocolListener.onExperimenterMessage(experimenterMessage); - Mockito.verify(deviceReplyProcessor).processExperimenterMessage(ArgumentMatchers.any()); + verify(deviceReplyProcessor).processExperimenterMessage(any()); } /** @@ -126,7 +126,7 @@ public class OpenflowProtocolListenerFullImplTest { .setVersion(EncodeConstants.OF_VERSION_1_3).setXid(xid).build(); ofProtocolListener.onFlowRemovedMessage(flowRemovedMessage); - Mockito.verify(deviceReplyProcessor).processFlowRemovedMessage(any(FlowRemovedMessage.class)); + verify(deviceReplyProcessor).processFlowRemovedMessage(any(FlowRemovedMessage.class)); } /** @@ -140,8 +140,8 @@ public class OpenflowProtocolListenerFullImplTest { .setVersion(EncodeConstants.OF_VERSION_1_3).setXid(xid).build(); ofProtocolListener.onHelloMessage(helloMessage); - Mockito.verify(connectionAdapter).getRemoteAddress(); - Mockito.verify(connectionAdapter).disconnect(); + verify(connectionAdapter).getRemoteAddress(); + verify(connectionAdapter).disconnect(); } /** @@ -155,7 +155,7 @@ public class OpenflowProtocolListenerFullImplTest { .setVersion(EncodeConstants.OF_VERSION_1_3).setXid(xid).build(); ofProtocolListener.onPacketInMessage(packetInMessage); - Mockito.verify(deviceReplyProcessor).processPacketInMessage(any(PacketInMessage.class)); + verify(deviceReplyProcessor).processPacketInMessage(any(PacketInMessage.class)); } /** @@ -169,7 +169,6 @@ public class OpenflowProtocolListenerFullImplTest { .setVersion(EncodeConstants.OF_VERSION_1_3).setXid(xid).build(); ofProtocolListener.onPortStatusMessage(portStatusMessage); - Mockito.verify(deviceReplyProcessor).processPortStatusMessage(any(PortStatusMessage.class)); + verify(deviceReplyProcessor).processPortStatusMessage(any(PortStatusMessage.class)); } - } -- 2.36.6