Merge "Bug 1277 - Move ByteBuffUtils to separate bundle"
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFFrameDecoder.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
10 package org.opendaylight.openflowjava.protocol.impl.core;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.handler.codec.ByteToMessageDecoder;
15
16 import java.util.List;
17
18 import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionFacade;
19 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Decodes incoming messages into message frames.
25  * @author michal.polkorab
26  */
27 public class OFFrameDecoder extends ByteToMessageDecoder {
28
29     /** Length of OpenFlow 1.3 header */
30     public static final byte LENGTH_OF_HEADER = 8;
31     private static final byte LENGTH_INDEX_IN_HEADER = 2;
32     private static final Logger LOGGER = LoggerFactory.getLogger(OFFrameDecoder.class);
33     private ConnectionFacade connectionFacade;
34     private boolean started = false;
35
36     /**
37      * Constructor of class.
38      * @param connectionFacade 
39      */
40     public OFFrameDecoder(ConnectionFacade connectionFacade) {
41         LOGGER.trace("Creating OFFrameDecoder");
42         this.connectionFacade = connectionFacade;
43     }
44
45     @Override
46     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
47         if (cause instanceof io.netty.handler.ssl.NotSslRecordException) {
48             LOGGER.warn("Not an TLS record exception - please verify TLS configuration.");
49         } else {
50             LOGGER.warn("Unexpected exception from downstream.", cause);
51         }
52         LOGGER.warn("Closing connection.");
53         ctx.close();
54     }
55
56     @Override
57     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
58         if (!started) {
59             connectionFacade.fireConnectionReadyNotification();
60             started = true;
61         }
62         int readableBytes = bb.readableBytes();
63         if (readableBytes < LENGTH_OF_HEADER) {
64             LOGGER.debug("skipping bytebuf - too few bytes for header: " + readableBytes + " < " + LENGTH_OF_HEADER );
65             LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
66             return;
67         }
68         
69         int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
70         LOGGER.debug("length of actual message: {}", length);
71         
72         if (readableBytes < length) {
73                 LOGGER.debug("skipping bytebuf - too few bytes for msg: " +
74                         readableBytes + " < " + length);
75                 LOGGER.debug("bytebuffer: " + ByteBufUtils.byteBufToHexString(bb));
76             return;
77         }
78         LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
79         
80         ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
81         list.add(messageBuffer);
82         messageBuffer.retain();
83         bb.skipBytes(length);
84     }
85
86 }