TLS support
[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         LOGGER.warn("Unexpected exception from downstream.", cause);
48         ctx.close();
49     }
50
51     @Override
52     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
53         if (!started) {
54             connectionFacade.fireConnectionReadyNotification();
55             started = true;
56         }
57         int readableBytes = bb.readableBytes();
58         if (readableBytes < LENGTH_OF_HEADER) {
59             LOGGER.debug("skipping bytebuf - too few bytes for header: " + readableBytes + " < " + LENGTH_OF_HEADER );
60             LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
61             return;
62         }
63         
64         int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
65         LOGGER.debug("length of actual message: {}", length);
66         
67         if (readableBytes < length) {
68                 LOGGER.debug("skipping bytebuf - too few bytes for msg: " +
69                         readableBytes + " < " + length);
70                 LOGGER.debug("bytebuffer: " + ByteBufUtils.byteBufToHexString(bb));
71             return;
72         }
73         LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
74         
75         ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
76         list.add(messageBuffer);
77         messageBuffer.retain();
78         bb.skipBytes(length);
79     }
80
81 }