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.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 started = false;
37 * Constructor of class.
38 * @param connectionFacade
40 public OFFrameDecoder(ConnectionFacade connectionFacade) {
41 LOGGER.trace("Creating OFFrameDecoder");
42 this.connectionFacade = connectionFacade;
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.");
50 LOGGER.warn("Unexpected exception from downstream.", cause);
52 LOGGER.warn("Closing connection.");
57 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
59 connectionFacade.fireConnectionReadyNotification();
62 int readableBytes = bb.readableBytes();
63 if (readableBytes < LENGTH_OF_HEADER) {
64 if (LOGGER.isDebugEnabled()) {
65 LOGGER.debug("skipping bytebuf - too few bytes for header: " + readableBytes + " < " + LENGTH_OF_HEADER );
66 LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb));
71 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
72 LOGGER.debug("length of actual message: {}", length);
74 if (readableBytes < length) {
75 if (LOGGER.isDebugEnabled()) {
76 LOGGER.debug("skipping bytebuf - too few bytes for msg: " +
77 readableBytes + " < " + length);
78 LOGGER.debug("bytebuffer: " + ByteBufUtils.byteBufToHexString(bb));
82 LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
84 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
85 list.add(messageBuffer);
86 messageBuffer.retain();