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.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
25 import org.opendaylight.yangtools.yang.common.RpcError;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
33 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
35 private final ConnectionContext connectionContext;
36 private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
38 static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
39 private final long echoReplyTimeout;
41 public SystemNotificationsListenerImpl(@Nonnull final ConnectionContext connectionContext, long echoReplyTimeout) {
42 this.connectionContext = Preconditions.checkNotNull(connectionContext);
43 this.echoReplyTimeout = echoReplyTimeout;
47 public void onDisconnectEvent(final DisconnectEvent notification) {
48 LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}",
49 connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getNodeId());
50 connectionContext.onConnectionClosed();
54 public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
55 new Thread(new Runnable() {
58 boolean shouldBeDisconnected = true;
60 final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
62 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
63 FeaturesReply features = connectionContext.getFeatures();
64 LOG.debug("Switch Idle state occured, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
65 connectionContext.changeStateToTimeouting();
66 EchoInputBuilder builder = new EchoInputBuilder();
67 builder.setVersion(features.getVersion());
68 Xid xid = new Xid(0L);
69 builder.setXid(xid.getValue());
71 Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
74 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
75 if (echoReplyValue.isSuccessful()) {
76 connectionContext.changeStateToWorking();
77 shouldBeDisconnected = false;
79 for (RpcError replyError : echoReplyValue.getErrors()) {
80 Throwable cause = replyError.getCause();
81 if (LOG.isWarnEnabled()) {
82 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
85 if (LOG.isTraceEnabled()) {
86 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
91 } catch (Exception e) {
92 if (LOG.isWarnEnabled()) {
93 LOG.warn("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
96 if (LOG.isTraceEnabled()) {
97 LOG.trace("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
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), remoteAddress, connectionContext.getNodeId());
108 connectionContext.closeConnection(true);