Javadoc update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoder.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2
3 package org.opendaylight.openflowjava.protocol.impl.core;
4
5 import io.netty.buffer.ByteBuf;
6 import io.netty.channel.ChannelHandlerContext;
7 import io.netty.handler.codec.ByteToMessageDecoder;
8
9 import java.util.List;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 /**
15  * Decodes incoming messages into message frames.
16  * @author michal.polkorab
17  */
18 public class OFFrameDecoder extends ByteToMessageDecoder {
19
20     /** Length of OpenFlow 1.3 header */
21     public static final byte LENGTH_OF_HEADER = 8;
22     private static final byte LENGTH_INDEX_IN_HEADER = 2;
23     private static final Logger LOGGER = LoggerFactory.getLogger(OFFrameDecoder.class);
24
25     /**
26      * Constructor of class.
27      */
28     public OFFrameDecoder() {
29         LOGGER.debug("Creating OFFrameDecoder");
30     }
31
32     @Override
33     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
34         LOGGER.warn("Unexpected exception from downstream.", cause);
35         ctx.close();
36     }
37
38     @Override
39     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
40         if (bb.readableBytes() < LENGTH_OF_HEADER) {
41             LOGGER.debug("skipping bb - too few data for header: " + bb.readableBytes());
42             return;
43         }
44
45         int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER);
46         if (bb.readableBytes() < length) {
47             LOGGER.debug("skipping bb - too few data for msg: " +
48                     bb.readableBytes() + " < " + length);
49             return;
50         }
51         LOGGER.info("OF Protocol message received, type:{}", bb.getByte(1));
52
53         ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
54         list.add(messageBuffer);
55         messageBuffer.retain();
56         bb.skipBytes(length);
57     }
58
59 }