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.Future;
16 import java.util.concurrent.ThreadPoolExecutor;
17 import java.util.concurrent.TimeUnit;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
27 import org.opendaylight.yangtools.yang.common.RpcError;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
34 private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
35 private static final Xid ECHO_XID = new Xid(0L);
37 private final ConnectionContext connectionContext;
39 static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
40 private final long echoReplyTimeout;
41 private final ThreadPoolExecutor threadPool;
43 public SystemNotificationsListenerImpl(@Nonnull final ConnectionContext connectionContext,
44 long echoReplyTimeout,
45 @Nonnull final ThreadPoolExecutor threadPool) {
46 this.threadPool = threadPool;
47 this.connectionContext = Preconditions.checkNotNull(connectionContext);
48 this.echoReplyTimeout = echoReplyTimeout;
52 public void onDisconnectEvent(final DisconnectEvent notification) {
53 LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}",
54 connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG());
55 connectionContext.onConnectionClosed();
59 public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
60 threadPool.execute(this::executeOnSwitchIdleEvent);
63 @SuppressWarnings("checkstyle:IllegalCatch")
64 private void executeOnSwitchIdleEvent() {
65 boolean shouldBeDisconnected = true;
67 final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
69 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
70 FeaturesReply features = connectionContext.getFeatures();
71 LOG.info("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
72 connectionContext.changeStateToTimeouting();
73 EchoInputBuilder builder = new EchoInputBuilder();
74 builder.setVersion(features.getVersion());
75 builder.setXid(ECHO_XID.getValue());
77 Future<RpcResult<EchoOutput>> echoReplyFuture =
78 connectionContext.getConnectionAdapter().echo(builder.build());
81 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
82 if (echoReplyValue.isSuccessful()
83 && Objects.equals(echoReplyValue.getResult().getXid(), ECHO_XID.getValue())) {
84 connectionContext.changeStateToWorking();
85 shouldBeDisconnected = false;
87 logErrors(remoteAddress, echoReplyValue);
89 } catch (Exception e) {
90 if (LOG.isWarnEnabled()) {
91 LOG.warn("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}",
92 remoteAddress, e.getMessage());
95 if (LOG.isTraceEnabled()) {
96 LOG.trace("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}",
102 if (shouldBeDisconnected) {
103 if (LOG.isInfoEnabled()) {
104 LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
105 new Date(System.currentTimeMillis() - echoReplyTimeout),
106 remoteAddress, connectionContext.getSafeNodeIdForLOG());
109 connectionContext.closeConnection(true);
113 private void logErrors(InetSocketAddress remoteAddress, RpcResult<EchoOutput> echoReplyValue) {
114 for (RpcError replyError : echoReplyValue.getErrors()) {
115 Throwable cause = replyError.getCause();
116 if (LOG.isWarnEnabled()) {
117 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}",
118 remoteAddress, cause.getMessage());
121 if (LOG.isTraceEnabled()) {
122 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);