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.ThreadPoolExecutor;
16 import java.util.concurrent.TimeUnit;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
26 import org.opendaylight.yangtools.yang.common.RpcError;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
34 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
36 private final ConnectionContext connectionContext;
37 private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
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.getNodeId());
55 connectionContext.onConnectionClosed();
59 public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
60 threadPool.execute(() -> {
61 boolean shouldBeDisconnected = true;
63 final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
65 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
66 FeaturesReply features = connectionContext.getFeatures();
67 LOG.info("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
68 connectionContext.changeStateToTimeouting();
69 EchoInputBuilder builder = new EchoInputBuilder();
70 builder.setVersion(features.getVersion());
71 Xid xid = new Xid(0L);
72 builder.setXid(xid.getValue());
74 Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
77 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
78 if (echoReplyValue.isSuccessful()) {
79 connectionContext.changeStateToWorking();
80 shouldBeDisconnected = false;
82 for (RpcError replyError : echoReplyValue.getErrors()) {
83 Throwable cause = replyError.getCause();
84 if (LOG.isWarnEnabled()) {
85 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
88 if (LOG.isTraceEnabled()) {
89 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
94 } catch (Exception e) {
95 if (LOG.isWarnEnabled()) {
96 LOG.warn("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
99 if (LOG.isTraceEnabled()) {
100 LOG.trace("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
105 if (shouldBeDisconnected) {
106 if (LOG.isInfoEnabled()) {
107 LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
108 new Date(System.currentTimeMillis() - echoReplyTimeout), remoteAddress, connectionContext.getNodeId());
111 connectionContext.closeConnection(true);