Remove trailing whitespace
[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.core.connection.ConnectionFacade;
19 import org.opendaylight.openflowjava.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 firstTlsPass = false;
35
36     /**
37      * Constructor of class.
38      * @param connectionFacade  ConnectionFacade that will be notified
39      * with ConnectionReadyNotification after TLS has been successfully set up.
40      * @param tlsPresent true is TLS is required, false otherwise
41      */
42     public OFFrameDecoder(ConnectionFacade connectionFacade, boolean tlsPresent) {
43         LOGGER.trace("Creating OFFrameDecoder");
44         if (tlsPresent) {
45             firstTlsPass = true;
46         }
47         this.connectionFacade = connectionFacade;
48     }
49
50     @Override
51     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
52         if (cause instanceof io.netty.handler.ssl.NotSslRecordException) {
53             LOGGER.warn("Not an TLS record exception - please verify TLS configuration.");
54         } else {
55             LOGGER.warn("Unexpected exception from downstream.", cause);
56         }
57         LOGGER.warn("Closing connection.");
58         ctx.close();
59     }
60
61     @Override
62     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
63         if (firstTlsPass) {
64             connectionFacade.fireConnectionReadyNotification();
65             firstTlsPass = false;
66         }
67         int readableBytes = bb.readableBytes();
68         if (readableBytes < LENGTH_OF_HEADER) {
69             if (LOGGER.isDebugEnabled()) {
70                 LOGGER.debug("skipping bytebuf - too few bytes for header: " + readableBytes + " < " + LENGTH_OF_HEADER );
71                 LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
72             }
73             return;
74         }
75
76         int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
77         LOGGER.debug("length of actual message: {}", length);
78
79         if (readableBytes < length) {
80             if (LOGGER.isDebugEnabled()) {
81                 LOGGER.debug("skipping bytebuf - too few bytes for msg: " +
82                         readableBytes + " < " + length);
83                 LOGGER.debug("bytebuffer: " + ByteBufUtils.byteBufToHexString(bb));
84             }
85             return;
86         }
87         LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
88
89         ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
90         list.add(messageBuffer);
91         messageBuffer.retain();
92         bb.skipBytes(length);
93     }
94
95 }