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;
15 import java.util.List;
16 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Decodes incoming messages into message frames.
23 * @author michal.polkorab
25 public class OFFrameDecoder extends ByteToMessageDecoder {
27 /** Length of OpenFlow header */
28 public static final byte LENGTH_OF_HEADER = 8;
29 private static final byte LENGTH_INDEX_IN_HEADER = 2;
30 private static final Logger LOG = LoggerFactory.getLogger(OFFrameDecoder.class);
31 private ConnectionFacade connectionFacade;
32 private boolean firstTlsPass = false;
35 * Constructor of class.
36 * @param connectionFacade ConnectionFacade that will be notified
37 * with ConnectionReadyNotification after TLS has been successfully set up.
38 * @param tlsPresent true is TLS is required, false otherwise
40 public OFFrameDecoder(ConnectionFacade connectionFacade, boolean tlsPresent) {
41 LOG.trace("Creating OFFrameDecoder");
45 this.connectionFacade = connectionFacade;
49 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
50 if (cause instanceof io.netty.handler.ssl.NotSslRecordException) {
51 LOG.warn("Not an TLS record exception - please verify TLS configuration.");
53 LOG.warn("Unexpected exception from downstream.", cause);
55 LOG.warn("Closing connection.");
60 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
62 connectionFacade.fireConnectionReadyNotification();
65 int readableBytes = bb.readableBytes();
66 if (readableBytes < LENGTH_OF_HEADER) {
67 if (LOG.isDebugEnabled()) {
68 LOG.debug("skipping bytebuf - too few bytes for header: {} < {}", readableBytes, LENGTH_OF_HEADER);
69 LOG.debug("bb: {}", ByteBufUtils.byteBufToHexString(bb));
74 int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
75 LOG.debug("length of actual message: {}", length);
77 if (readableBytes < length) {
78 if (LOG.isDebugEnabled()) {
79 LOG.debug("skipping bytebuf - too few bytes for msg: {} < {}", readableBytes, length);
80 LOG.debug("bytebuffer: {}", ByteBufUtils.byteBufToHexString(bb));
84 LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
86 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
87 list.add(messageBuffer);
88 messageBuffer.retain();