Allow any hello mesage and extend hello support for v1.4, v1.5
[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 com.google.common.base.Preconditions;
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 = Preconditions.checkNotNull(connectionAdapter);
39     }
40
41     @Override
42     public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
43         consumer.consume((DataObject) msg);
44     }
45
46     @Override
47     public void channelInactive(final ChannelHandlerContext ctx) {
48         LOG.debug("Channel inactive");
49         if (!inactiveMessageSent) {
50             DisconnectEventBuilder builder = new DisconnectEventBuilder();
51             builder.setInfo("Channel inactive");
52             consumer.consume(builder.build());
53             inactiveMessageSent = true;
54         }
55     }
56
57     @Override
58     public void channelUnregistered(final ChannelHandlerContext ctx) {
59         LOG.debug("Channel unregistered");
60         if (!inactiveMessageSent) {
61             DisconnectEventBuilder builder = new DisconnectEventBuilder();
62             builder.setInfo("Channel unregistered");
63             consumer.consume(builder.build());
64             inactiveMessageSent = true;
65         }
66     }
67
68 }