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.concurrent.Future;
14 import java.util.concurrent.TimeUnit;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
24 import org.opendaylight.yangtools.yang.common.RpcError;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
32 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
34 private final ConnectionContext connectionContext;
35 private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
37 static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
38 private final long echoReplyTimeout;
40 public SystemNotificationsListenerImpl(@Nonnull final ConnectionContext connectionContext, long echoReplyTimeout) {
41 this.connectionContext = Preconditions.checkNotNull(connectionContext);
42 this.echoReplyTimeout = echoReplyTimeout;
46 public void onDisconnectEvent(final DisconnectEvent notification) {
47 connectionContext.onConnectionClosed();
51 public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
52 new Thread(new Runnable() {
55 boolean shouldBeDisconnected = true;
57 final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
59 if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
60 FeaturesReply features = connectionContext.getFeatures();
61 LOG.debug("Switch Idle state occured, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
62 connectionContext.changeStateToTimeouting();
63 EchoInputBuilder builder = new EchoInputBuilder();
64 builder.setVersion(features.getVersion());
65 Xid xid = new Xid(0L);
66 builder.setXid(xid.getValue());
68 Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
71 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
72 if (echoReplyValue.isSuccessful()) {
73 connectionContext.changeStateToWorking();
74 shouldBeDisconnected = false;
76 for (RpcError replyError : echoReplyValue.getErrors()) {
77 Throwable cause = replyError.getCause();
78 if (LOG.isWarnEnabled()) {
79 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
82 if (LOG.isTraceEnabled()) {
83 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
88 } catch (Exception e) {
89 if (LOG.isWarnEnabled()) {
90 LOG.warn("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
93 if (LOG.isTraceEnabled()) {
94 LOG.trace("Exception while waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
99 if (shouldBeDisconnected) {
100 connectionContext.closeConnection(true);