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
10 package org.opendaylight.openflowjava.protocol.impl.core;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
16 import java.util.List;
18 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Decodes incoming messages into message frames.
24 * @author michal.polkorab
26 public class OFFrameDecoder extends ByteToMessageDecoder {
28 /** Length of OpenFlow 1.3 header */
29 public static final byte LENGTH_OF_HEADER = 8;
30 private static final byte LENGTH_INDEX_IN_HEADER = 2;
31 private static final Logger LOGGER = LoggerFactory.getLogger(OFFrameDecoder.class);
34 * Constructor of class.
36 public OFFrameDecoder() {
37 LOGGER.trace("Creating OFFrameDecoder");
41 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
42 LOGGER.warn("Unexpected exception from downstream.", cause);
47 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
48 int readableBytes = bb.readableBytes();
49 if (readableBytes < LENGTH_OF_HEADER) {
50 LOGGER.debug("skipping bb - too few data for header: " + readableBytes);
54 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
55 LOGGER.debug("length of actual message: {}", length);
57 if (readableBytes < length) {
58 if (LOGGER.isDebugEnabled()) {
59 LOGGER.debug("skipping bb - too few data for msg: " +
60 readableBytes + " < " + length);
61 LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
65 LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
67 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
68 list.add(messageBuffer);
69 messageBuffer.retain();