2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.connection.listener;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import java.net.InetSocketAddress;
14 import java.util.Date;
15 import java.util.Objects;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.TimeUnit;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.ssl.connection.error.service.rev190723.SslErrorBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.ssl.connection.error.service.rev190723.SslErrorType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.ssl.connection.error.service.rev190723.ssl.error.SwitchCertificateBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SslConnectionError;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
36 import org.opendaylight.yangtools.yang.common.RpcError;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.Uint16;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
45 private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
46 private static final Logger OF_EVENT_LOG = LoggerFactory.getLogger("OfEventLog");
47 private static final Xid ECHO_XID = new Xid(Uint32.ZERO);
49 private final ConnectionContext connectionContext;
51 static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
52 private final long echoReplyTimeout;
53 private final Executor executor;
54 private final NotificationPublishService notificationPublishService;
56 public SystemNotificationsListenerImpl(@NonNull final ConnectionContext connectionContext,
57 final long echoReplyTimeout,
58 @NonNull final Executor executor,
59 @NonNull final NotificationPublishService notificationPublishService) {
60 this.executor = requireNonNull(executor);
61 this.connectionContext = requireNonNull(connectionContext);
62 this.echoReplyTimeout = echoReplyTimeout;
63 this.notificationPublishService = notificationPublishService;
67 public void onDisconnectEvent(final DisconnectEvent notification) {
68 OF_EVENT_LOG.debug("Disconnect, Node: {}", connectionContext.getSafeNodeIdForLOG());
69 LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}",
70 connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG());
71 connectionContext.onConnectionClosed();
75 public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
76 executor.execute(this::executeOnSwitchIdleEvent);
79 @SuppressWarnings("checkstyle:IllegalCatch")
80 private void executeOnSwitchIdleEvent() {
81 boolean shouldBeDisconnected = true;
83 final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
85 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
86 FeaturesReply features = connectionContext.getFeatures();
87 LOG.debug("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
88 OF_EVENT_LOG.debug("Switch idle state, Node: {}", features.getDatapathId());
89 connectionContext.changeStateToTimeouting();
90 EchoInputBuilder builder = new EchoInputBuilder();
91 builder.setVersion(features.getVersion());
92 builder.setXid(ECHO_XID.getValue());
94 Future<RpcResult<EchoOutput>> echoReplyFuture =
95 connectionContext.getConnectionAdapter().echo(builder.build());
98 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
99 if (echoReplyValue.isSuccessful()
100 && Objects.equals(echoReplyValue.getResult().getXid(), ECHO_XID.getValue())) {
101 connectionContext.changeStateToWorking();
102 shouldBeDisconnected = false;
104 logErrors(remoteAddress, echoReplyValue);
106 } catch (Exception e) {
107 if (LOG.isWarnEnabled()) {
108 LOG.warn("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}",
109 remoteAddress, e.getMessage());
112 if (LOG.isTraceEnabled()) {
113 LOG.trace("Exception while waiting for echoReply from [{}] in TIMEOUTING state",
119 if (shouldBeDisconnected) {
120 if (LOG.isInfoEnabled()) {
121 LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
122 new Date(System.currentTimeMillis() - echoReplyTimeout),
123 remoteAddress, connectionContext.getSafeNodeIdForLOG());
126 connectionContext.closeConnection(true);
130 private static void logErrors(final InetSocketAddress remoteAddress, final RpcResult<EchoOutput> echoReplyValue) {
131 for (RpcError replyError : echoReplyValue.getErrors()) {
132 Throwable cause = replyError.getCause();
133 if (LOG.isWarnEnabled()) {
134 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}",
135 remoteAddress, cause.getMessage());
138 if (LOG.isTraceEnabled()) {
139 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state", remoteAddress, cause);
145 public void onSslConnectionError(final SslConnectionError notification) {
146 final var switchCert = notification.getSwitchCertificate();
148 notificationPublishService.offerNotification(new SslErrorBuilder()
149 .setType(SslErrorType.SslConFailed)
150 .setCode(Uint16.valueOf(SslErrorType.SslConFailed.getIntValue()))
151 .setNodeIpAddress(remoteAddress())
152 .setData(notification.getInfo())
153 .setSwitchCertificate(switchCert == null ? null : new SwitchCertificateBuilder(switchCert).build())
157 private @Nullable IpAddress remoteAddress() {
158 final var connectionAdapter = connectionContext.getConnectionAdapter();
159 if (connectionAdapter != null) {
160 final var remoteAddress = connectionAdapter.getRemoteAddress();
161 if (remoteAddress != null) {
162 final var inetAddress = remoteAddress.getAddress();
163 if (inetAddress != null) {
164 return IetfInetUtil.ipAddressFor(inetAddress.getHostAddress());