ded7bc8655547d8229b5f2f6dd8e16ac1a1e148b
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / DelegatingInboundHandler.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core;
10
11 import io.netty.channel.ChannelHandlerContext;
12 import io.netty.channel.ChannelInboundHandlerAdapter;
13
14 import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterImpl;
15 import org.opendaylight.openflowjava.protocol.impl.connection.MessageConsumer;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEventBuilder;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Holds reference to {@link ConnectionAdapterImpl} and passes messages for further processing.
23  * Also informs on switch disconnection.
24  * @author michal.polkorab
25  */
26 public class DelegatingInboundHandler extends ChannelInboundHandlerAdapter {
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(DelegatingInboundHandler.class);
29     
30     protected MessageConsumer consumer;
31     private boolean inactiveMessageSent = false;
32     
33     /** 
34      * Constructs class + creates and sets MessageConsumer
35      * @param connectionAdapter reference for adapter communicating with upper layers outside library
36      */
37     public DelegatingInboundHandler(MessageConsumer connectionAdapter) {
38         LOGGER.trace("Creating DelegatingInboundHandler");
39         consumer = connectionAdapter;
40     }
41     
42     @Override
43     public void channelRead(ChannelHandlerContext ctx, final Object msg)
44             throws Exception {
45         consumer.consume((DataObject) msg);
46     }
47     
48     @Override
49     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
50         LOGGER.debug("Channel inactive");
51         if (!inactiveMessageSent) {
52             DisconnectEventBuilder builder = new DisconnectEventBuilder();
53             builder.setInfo("Channel inactive");
54             consumer.consume(builder.build());
55             inactiveMessageSent = true;
56         }
57     }
58
59     @Override
60     public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
61         LOGGER.debug("Channel unregistered");
62         if (!inactiveMessageSent) {
63             DisconnectEventBuilder builder = new DisconnectEventBuilder();
64             builder.setInfo("Channel unregistered");
65             consumer.consume(builder.build());
66             inactiveMessageSent = true;
67         }
68     }
69     
70 }