1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
3 package org.opendaylight.openflowjava.protocol.impl.core;
5 import io.netty.buffer.ByteBuf;
6 import io.netty.channel.ChannelHandlerContext;
7 import io.netty.handler.codec.ByteToMessageDecoder;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.COMPONENT_NAMES;
16 * Class for decoding incoming messages into message frames.
18 * @author michal.polkorab
20 public class OFFrameDecoder extends ByteToMessageDecoder {
22 /** Length of OpenFlow 1.3 header */
23 public static final byte LENGTH_OF_HEADER = 8;
24 private static final byte LENGTH_INDEX_IN_HEADER = 2;
25 private static final Logger LOGGER = LoggerFactory.getLogger(OFFrameDecoder.class);
28 * Constructor of class.
30 public OFFrameDecoder() {
31 LOGGER.info("Creating OFFrameDecoder");
35 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
36 LOGGER.warn("Unexpected exception from downstream.", cause);
41 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
42 if (bb.readableBytes() < LENGTH_OF_HEADER) {
43 LOGGER.debug("skipping bb - too few data for header");
47 int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER);
48 if (bb.readableBytes() < length) {
49 LOGGER.debug("skipping bb - too few data for msg");
53 LOGGER.info("OF Protocol message received");
55 enableOFVersionDetector(chc);
57 List<String> componentList = chc.pipeline().names();
58 LOGGER.debug(componentList.toString());
60 ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length);
61 list.add(messageBuffer);
62 messageBuffer.retain();
66 private static void enableOFVersionDetector(ChannelHandlerContext ctx) {
67 if (ctx.pipeline().get(COMPONENT_NAMES.OF_VERSION_DETECTOR.name()) == null) {
68 LOGGER.info("Adding OFVD");
69 ctx.pipeline().addLast(COMPONENT_NAMES.OF_VERSION_DETECTOR.name(), new OFVersionDetector());
71 LOGGER.debug("OFVD already in pipeline");