Merge "Use String(byte[], Charset)"
[openflowplugin.git] / openflowjava / 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 package org.opendaylight.openflowjava.protocol.impl.core;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelInboundHandlerAdapter;
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 /**
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 LOG = LoggerFactory.getLogger(DelegatingInboundHandler.class);
29     private final MessageConsumer consumer;
30     private boolean inactiveMessageSent = false;
31
32     /**
33      * Constructs class + creates and sets MessageConsumer.
34      * @param connectionAdapter reference for adapter communicating with upper layers outside library
35      */
36     public DelegatingInboundHandler(final MessageConsumer connectionAdapter) {
37         LOG.trace("Creating DelegatingInboundHandler");
38         consumer = requireNonNull(connectionAdapter);
39     }
40
41     @Override
42     public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
43         LOG.trace("ChannelRead Message : {}", msg);
44         consumer.consume((DataObject) msg);
45     }
46
47     @Override
48     public void channelInactive(final ChannelHandlerContext ctx) {
49         LOG.debug("Channel inactive {}", ctx.channel());
50         if (!inactiveMessageSent) {
51             DisconnectEventBuilder builder = new DisconnectEventBuilder();
52             builder.setInfo("Channel inactive");
53             consumer.consume(builder.build());
54             inactiveMessageSent = true;
55         }
56     }
57
58     @Override
59     public void channelUnregistered(final ChannelHandlerContext ctx) {
60         LOG.debug("Channel unregistered {}", ctx.channel());
61         if (!inactiveMessageSent) {
62             DisconnectEventBuilder builder = new DisconnectEventBuilder();
63             builder.setInfo("Channel unregistered");
64             consumer.consume(builder.build());
65             inactiveMessageSent = true;
66         }
67     }
68
69 }