2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.core;
10 import static java.util.Objects.requireNonNull;
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;
22 * Holds reference to {@link ConnectionAdapterImpl} and passes messages for further processing.
23 * Also informs on switch disconnection.
24 * @author michal.polkorab
26 public class DelegatingInboundHandler extends ChannelInboundHandlerAdapter {
28 private static final Logger LOG = LoggerFactory.getLogger(DelegatingInboundHandler.class);
29 private final MessageConsumer consumer;
30 private boolean inactiveMessageSent = false;
33 * Constructs class + creates and sets MessageConsumer.
34 * @param connectionAdapter reference for adapter communicating with upper layers outside library
36 public DelegatingInboundHandler(final MessageConsumer connectionAdapter) {
37 LOG.trace("Creating DelegatingInboundHandler");
38 consumer = requireNonNull(connectionAdapter);
42 public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
43 LOG.trace("ChannelRead Message : {}", msg);
44 consumer.consume((DataObject) msg);
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;
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;