2 * Copyright (c) 2013, 2015 EBay Software Foundation 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
9 package org.opendaylight.ovsdb.lib.jsonrpc;
11 import io.netty.channel.ChannelDuplexHandler;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.DecoderException;
14 import io.netty.handler.codec.TooLongFrameException;
15 import io.netty.handler.timeout.IdleState;
16 import io.netty.handler.timeout.IdleStateEvent;
17 import io.netty.handler.timeout.ReadTimeoutException;
18 import java.io.IOException;
19 import org.opendaylight.ovsdb.lib.OvsdbClient;
20 import org.opendaylight.ovsdb.lib.OvsdbConnection;
21 import org.opendaylight.ovsdb.lib.error.InvalidEncodingException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class ExceptionHandler extends ChannelDuplexHandler {
27 private static final Logger LOG = LoggerFactory.getLogger(ExceptionHandler.class);
29 private final OvsdbConnection ovsdbConnectionService;
31 public ExceptionHandler(OvsdbConnection ovsdbConnectionService) {
32 this.ovsdbConnectionService = ovsdbConnectionService;
36 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
37 if (ctx.channel().isActive()) {
38 LOG.warn("Exception occurred while processing connection pipeline", cause);
39 if ((cause instanceof InvalidEncodingException)
40 || (cause instanceof TooLongFrameException || (cause instanceof DecoderException))) {
41 LOG.info("Disconnecting channel to ovsdb {}", ctx.channel());
42 ctx.channel().disconnect();
46 /* In cases where a connection is quickly established and the closed
47 Catch the IOException and close the channel. Similarly if the peer is
48 powered off, Catch the read time out exception and close the channel
50 if ((cause instanceof IOException) || (cause instanceof ReadTimeoutException)) {
51 LOG.info("Closing channel to ovsdb {}", ctx.channel());
52 ctx.channel().close();
56 LOG.error("Exception was not handled by the exception handler, re-throwing it for next handler");
57 ctx.fireExceptionCaught(cause);
62 public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
63 if (evt instanceof IdleStateEvent) {
64 LOG.debug("Get idle state event");
65 IdleStateEvent event = (IdleStateEvent) evt;
66 if (event.state() == IdleState.READER_IDLE) {
67 LOG.debug("Reader idle state. Send echo message to peer");
68 //Send echo message to peer
69 OvsdbClient client = ovsdbConnectionService.getClient(ctx.channel());