5084109fb1129dadba6ba8ac510ccdeb790f7827
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / SystemNotificationsListenerImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.connection.listener;
9
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 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;
31
32 public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
33
34     private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
35     private static final Logger OF_EVENT_LOG = LoggerFactory.getLogger("OfEventLog");
36     private static final Xid ECHO_XID = new Xid(0L);
37
38     private final ConnectionContext connectionContext;
39     @VisibleForTesting
40     static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
41     private final long echoReplyTimeout;
42     private final ExecutorService executorService;
43
44     public SystemNotificationsListenerImpl(@Nonnull final ConnectionContext connectionContext,
45                                            long echoReplyTimeout,
46                                            @Nonnull final ExecutorService executorService) {
47         this.executorService = executorService;
48         this.connectionContext = Preconditions.checkNotNull(connectionContext);
49         this.echoReplyTimeout = echoReplyTimeout;
50     }
51
52     @Override
53     public void onDisconnectEvent(final DisconnectEvent notification) {
54         OF_EVENT_LOG.debug("Disconnect, Node: {}", connectionContext.getSafeNodeIdForLOG());
55         LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}",
56                 connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG());
57         connectionContext.onConnectionClosed();
58     }
59
60     @Override
61     public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
62         executorService.execute(this::executeOnSwitchIdleEvent);
63     }
64
65     @SuppressWarnings("checkstyle:IllegalCatch")
66     private void executeOnSwitchIdleEvent() {
67         boolean shouldBeDisconnected = true;
68
69         final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
70
71         if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
72             FeaturesReply features = connectionContext.getFeatures();
73             LOG.debug("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
74             OF_EVENT_LOG.debug("Switch idle state, Node: {}", features.getDatapathId());
75             connectionContext.changeStateToTimeouting();
76             EchoInputBuilder builder = new EchoInputBuilder();
77             builder.setVersion(features.getVersion());
78             builder.setXid(ECHO_XID.getValue());
79
80             Future<RpcResult<EchoOutput>> echoReplyFuture =
81                     connectionContext.getConnectionAdapter().echo(builder.build());
82
83             try {
84                 RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
85                 if (echoReplyValue.isSuccessful()
86                         && Objects.equals(echoReplyValue.getResult().getXid(), ECHO_XID.getValue())) {
87                     connectionContext.changeStateToWorking();
88                     shouldBeDisconnected = false;
89                 } else {
90                     logErrors(remoteAddress, echoReplyValue);
91                 }
92             } catch (Exception e) {
93                 if (LOG.isWarnEnabled()) {
94                     LOG.warn("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}",
95                             remoteAddress, e.getMessage());
96                 }
97
98                 if (LOG.isTraceEnabled()) {
99                     LOG.trace("Exception while  waiting for echoReply from [{}] in TIMEOUTING state",
100                             remoteAddress, e);
101                 }
102
103             }
104         }
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),
109                         remoteAddress, connectionContext.getSafeNodeIdForLOG());
110             }
111
112             connectionContext.closeConnection(true);
113         }
114     }
115
116     private void logErrors(InetSocketAddress remoteAddress, RpcResult<EchoOutput> echoReplyValue) {
117         for (RpcError replyError : echoReplyValue.getErrors()) {
118             Throwable cause = replyError.getCause();
119             if (LOG.isWarnEnabled()) {
120                 LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}",
121                         remoteAddress, cause.getMessage());
122             }
123
124             if (LOG.isTraceEnabled()) {
125                 LOG.trace("Received EchoReply from [{}] in TIMEOUTING state", remoteAddress, cause);
126             }
127         }
128     }
129 }