1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
3 package org.opendaylight.openflowjava.protocol.impl.core;
5 import io.netty.buffer.ByteBuf;
6 import io.netty.channel.ChannelHandlerContext;
7 import io.netty.handler.codec.ByteToMessageDecoder;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
15 * Class for decoding incoming messages into message frames.
17 * @author michal.polkorab
19 public class OFFrameDecoder extends ByteToMessageDecoder {
21 /** Length of OpenFlow 1.3 header */
22 public static final byte LENGTH_OF_HEADER = 8;
23 private static final byte LENGTH_INDEX_IN_HEADER = 2;
24 private static final Logger LOGGER = LoggerFactory.getLogger(OFFrameDecoder.class);
27 * Constructor of class.
29 public OFFrameDecoder() {
30 LOGGER.debug("Creating OFFrameDecoder");
34 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
35 LOGGER.warn("Unexpected exception from downstream.", cause);
40 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
41 if (bb.readableBytes() < LENGTH_OF_HEADER) {
42 LOGGER.debug("skipping bb - too few data for header: " + bb.readableBytes());
46 int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER);
47 if (bb.readableBytes() < length) {
48 LOGGER.debug("skipping bb - too few data for msg: " +
49 bb.readableBytes() + " < " + length);
52 LOGGER.info("OF Protocol message received");
54 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
55 list.add(messageBuffer);
56 messageBuffer.retain();