Change log level from ERROR to warn.
[ovsdb.git] / library / impl / src / main / java / org / opendaylight / ovsdb / lib / jsonrpc / ExceptionHandler.java
1 /*
2  * Copyright (c) 2013, 2015 EBay Software Foundation 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
9 package org.opendaylight.ovsdb.lib.jsonrpc;
10
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;
24
25 public class ExceptionHandler extends ChannelDuplexHandler {
26
27     private static final Logger LOG = LoggerFactory.getLogger(ExceptionHandler.class);
28
29     private final OvsdbConnection ovsdbConnectionService;
30
31     public ExceptionHandler(OvsdbConnection ovsdbConnectionService) {
32         this.ovsdbConnectionService = ovsdbConnectionService;
33     }
34
35     @Override
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();
43                 return;
44             }
45
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
49          */
50             if ((cause instanceof IOException) || (cause instanceof ReadTimeoutException)) {
51                 LOG.info("Closing channel to ovsdb {}", ctx.channel());
52                 ctx.channel().close();
53                 return;
54             }
55
56             LOG.error("Exception was not handled by the exception handler, re-throwing it for next handler");
57             ctx.fireExceptionCaught(cause);
58         }
59     }
60
61     @Override
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());
70                 client.echo();
71             }
72         }
73     }
74 }