Do not use OpenflowProtocolListener in ConnectionAdapter
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / SystemNotificationsListenerImpl.java
index 5bbfc273b449a927d378c7a7002cba4d16742ba9..e98cca75de4e76269dbd57acc03ca60d02b8c51d 100644 (file)
@@ -17,11 +17,13 @@ import java.util.concurrent.Executor;
 import java.util.concurrent.Future;
 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;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.ssl.connection.error.service.rev190723.SslErrorBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.ssl.connection.error.service.rev190723.SslErrorType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.ssl.connection.error.service.rev190723.ssl.error.SwitchCertificateBuilder;
@@ -31,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;
@@ -39,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);
@@ -53,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;
@@ -140,25 +151,17 @@ public class SystemNotificationsListenerImpl implements SystemNotificationsListe
         }
     }
 
-    @Override
-    public void onSslConnectionError(final SslConnectionError notification) {
-        IpAddress ip = null;
-        if (connectionContext.getConnectionAdapter() != null
-                && connectionContext.getConnectionAdapter().getRemoteAddress() != null
-                && connectionContext.getConnectionAdapter().getRemoteAddress().getAddress() != null) {
-            ip = IpAddressBuilder.getDefaultInstance(
-                    connectionContext.getConnectionAdapter().getRemoteAddress().getAddress().getHostAddress());
-        }
-        SwitchCertificateBuilder switchCertificateBuilder = new SwitchCertificateBuilder();
-        if (notification.getSwitchCertificate() != null) {
-            switchCertificateBuilder = new SwitchCertificateBuilder(notification.getSwitchCertificate());
+    private @Nullable IpAddress remoteAddress() {
+        final var connectionAdapter = connectionContext.getConnectionAdapter();
+        if (connectionAdapter != null) {
+            final var remoteAddress = connectionAdapter.getRemoteAddress();
+            if (remoteAddress != null) {
+                final var inetAddress = remoteAddress.getAddress();
+                if (inetAddress != null) {
+                    return IetfInetUtil.ipAddressFor(inetAddress.getHostAddress());
+                }
+            }
         }
-        notificationPublishService.offerNotification(new SslErrorBuilder()
-            .setType(SslErrorType.SslConFailed)
-            .setCode(Uint16.valueOf(SslErrorType.SslConFailed.getIntValue()))
-            .setNodeIpAddress(ip)
-            .setData(notification.getInfo())
-            .setSwitchCertificate(notification.getSwitchCertificate() != null ? switchCertificateBuilder.build() : null)
-            .build());
+        return null;
     }
 }