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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import java.net.InetSocketAddress;
13 import java.util.Date;
14 import java.util.Objects;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
20 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.ssl.connection.error.service.rev190723.SslErrorBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.node.ssl.connection.error.service.rev190723.SslErrorType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SslConnectionError;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.Uint32;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
41 private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
42 private static final Logger OF_EVENT_LOG = LoggerFactory.getLogger("OfEventLog");
43 private static final Xid ECHO_XID = new Xid(Uint32.ZERO);
45 private final ConnectionContext connectionContext;
47 static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
48 private final long echoReplyTimeout;
49 private final ExecutorService executorService;
50 private final NotificationPublishService notificationPublishService;
52 public SystemNotificationsListenerImpl(@NonNull final ConnectionContext connectionContext,
53 final long echoReplyTimeout,
54 @NonNull final ExecutorService executorService,
55 @NonNull final NotificationPublishService notificationPublishService) {
56 this.executorService = executorService;
57 this.connectionContext = Preconditions.checkNotNull(connectionContext);
58 this.echoReplyTimeout = echoReplyTimeout;
59 this.notificationPublishService = notificationPublishService;
63 public void onDisconnectEvent(final DisconnectEvent notification) {
64 OF_EVENT_LOG.debug("Disconnect, Node: {}", connectionContext.getSafeNodeIdForLOG());
65 LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}",
66 connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG());
67 connectionContext.onConnectionClosed();
71 public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
72 executorService.execute(this::executeOnSwitchIdleEvent);
75 @SuppressWarnings("checkstyle:IllegalCatch")
76 private void executeOnSwitchIdleEvent() {
77 boolean shouldBeDisconnected = true;
79 final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
81 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
82 FeaturesReply features = connectionContext.getFeatures();
83 LOG.debug("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
84 OF_EVENT_LOG.debug("Switch idle state, Node: {}", features.getDatapathId());
85 connectionContext.changeStateToTimeouting();
86 EchoInputBuilder builder = new EchoInputBuilder();
87 builder.setVersion(features.getVersion());
88 builder.setXid(ECHO_XID.getValue());
90 Future<RpcResult<EchoOutput>> echoReplyFuture =
91 connectionContext.getConnectionAdapter().echo(builder.build());
94 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
95 if (echoReplyValue.isSuccessful()
96 && Objects.equals(echoReplyValue.getResult().getXid(), ECHO_XID.getValue())) {
97 connectionContext.changeStateToWorking();
98 shouldBeDisconnected = false;
100 logErrors(remoteAddress, echoReplyValue);
102 } catch (Exception e) {
103 if (LOG.isWarnEnabled()) {
104 LOG.warn("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}",
105 remoteAddress, e.getMessage());
108 if (LOG.isTraceEnabled()) {
109 LOG.trace("Exception while waiting for echoReply from [{}] in TIMEOUTING state",
115 if (shouldBeDisconnected) {
116 if (LOG.isInfoEnabled()) {
117 LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
118 new Date(System.currentTimeMillis() - echoReplyTimeout),
119 remoteAddress, connectionContext.getSafeNodeIdForLOG());
122 connectionContext.closeConnection(true);
126 private void logErrors(final InetSocketAddress remoteAddress, final RpcResult<EchoOutput> echoReplyValue) {
127 for (RpcError replyError : echoReplyValue.getErrors()) {
128 Throwable cause = replyError.getCause();
129 if (LOG.isWarnEnabled()) {
130 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}",
131 remoteAddress, cause.getMessage());
134 if (LOG.isTraceEnabled()) {
135 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state", remoteAddress, cause);
141 public void onSslConnectionError(SslConnectionError notification) {
143 if ((connectionContext.getConnectionAdapter() != null)
144 && (connectionContext.getConnectionAdapter().getRemoteAddress() != null)
145 && (connectionContext.getConnectionAdapter().getRemoteAddress().getAddress() != null)) {
146 ip = IpAddressBuilder.getDefaultInstance(
147 connectionContext.getConnectionAdapter().getRemoteAddress().getAddress().getHostAddress());
149 notificationPublishService
151 new SslErrorBuilder().setType(SslErrorType.SslConFailed)
152 .setCode(SslErrorType.SslConFailed.getIntValue())
153 .setNodeIpAddress(ip)
154 .setData(notification.getInfo())