Merge "Refactor LeaderTest"
[controller.git] / opendaylight / commons / protocol-framework / src / main / java / org / opendaylight / protocol / framework / AbstractProtocolSession.java
1 /*
2  * Copyright (c) 2013 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.protocol.framework;
9
10 import io.netty.channel.ChannelHandlerContext;
11 import io.netty.channel.SimpleChannelInboundHandler;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 @Deprecated
17 public abstract class AbstractProtocolSession<M> extends SimpleChannelInboundHandler<Object> implements ProtocolSession<M> {
18     private static final Logger LOG = LoggerFactory.getLogger(AbstractProtocolSession.class);
19
20     /**
21      * Handles incoming message (parsing, reacting if necessary).
22      *
23      * @param msg incoming message
24      */
25     protected abstract void handleMessage(final M msg);
26
27     /**
28      * Called when reached the end of input stream while reading.
29      */
30     protected abstract void endOfInput();
31
32     /**
33      * Called when the session is added to the pipeline.
34      */
35     protected abstract void sessionUp();
36
37     @Override
38     public final void channelInactive(final ChannelHandlerContext ctx) {
39         LOG.debug("Channel {} inactive.", ctx.channel());
40         endOfInput();
41         try {
42             // Forward channel inactive event, all handlers in pipeline might be interested in the event e.g. close channel handler of reconnect promise
43             super.channelInactive(ctx);
44         } catch (final Exception e) {
45             throw new RuntimeException("Failed to delegate channel inactive event on channel " + ctx.channel(), e);
46         }
47     }
48
49     @Override
50     @SuppressWarnings("unchecked")
51     protected final void channelRead0(final ChannelHandlerContext ctx, final Object msg) {
52         LOG.debug("Message was received: {}", msg);
53         handleMessage((M) msg);
54     }
55
56     @Override
57     public final void handlerAdded(final ChannelHandlerContext ctx) {
58         sessionUp();
59     }
60 }