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.core.connection.ConnectionFacade;
19 import org.opendaylight.openflowjava.util.ByteBufUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Decodes incoming messages into message frames.
25 * @author michal.polkorab
27 public class OFFrameDecoder extends ByteToMessageDecoder {
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;
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
42 public OFFrameDecoder(ConnectionFacade connectionFacade, boolean tlsPresent) {
43 LOGGER.trace("Creating OFFrameDecoder");
47 this.connectionFacade = connectionFacade;
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.");
55 LOGGER.warn("Unexpected exception from downstream.", cause);
57 LOGGER.warn("Closing connection.");
62 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
64 connectionFacade.fireConnectionReadyNotification();
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));
76 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
77 LOGGER.debug("length of actual message: {}", length);
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));
87 LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
89 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
90 list.add(messageBuffer);
91 messageBuffer.retain();