Fixed netty & checkstyle failures
[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.core.connection.ConnectionAdapterImpl;
15 import org.opendaylight.openflowjava.protocol.impl.core.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 import com.google.common.base.Preconditions;
22
23 /**
24  * Holds reference to {@link ConnectionAdapterImpl} and passes messages for further processing.
25  * Also informs on switch disconnection.
26  * @author michal.polkorab
27  */
28 public class DelegatingInboundHandler extends ChannelInboundHandlerAdapter {
29
30     private static final Logger LOG = LoggerFactory.getLogger(DelegatingInboundHandler.class);
31
32     private final MessageConsumer consumer;
33     private boolean inactiveMessageSent = false;
34
35     /**
36      * Constructs class + creates and sets MessageConsumer
37      * @param connectionAdapter reference for adapter communicating with upper layers outside library
38      */
39     public DelegatingInboundHandler(final MessageConsumer connectionAdapter) {
40         LOG.trace("Creating DelegatingInboundHandler");
41         consumer = Preconditions.checkNotNull(connectionAdapter);
42     }
43
44     @Override
45     public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
46         consumer.consume((DataObject) msg);
47     }
48
49     @Override
50     public void channelInactive(final ChannelHandlerContext ctx) {
51         LOG.debug("Channel inactive");
52         if (!inactiveMessageSent) {
53             DisconnectEventBuilder builder = new DisconnectEventBuilder();
54             builder.setInfo("Channel inactive");
55             consumer.consume(builder.build());
56             inactiveMessageSent = true;
57         }
58     }
59
60     @Override
61     public void channelUnregistered(final ChannelHandlerContext ctx) {
62         LOG.debug("Channel unregistered");
63         if (!inactiveMessageSent) {
64             DisconnectEventBuilder builder = new DisconnectEventBuilder();
65             builder.setInfo("Channel unregistered");
66             consumer.consume(builder.build());
67             inactiveMessageSent = true;
68         }
69     }
70
71 }