1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
4 import io.netty.buffer.ByteBuf;
5 import io.netty.channel.ChannelHandlerContext;
6 import io.netty.handler.codec.ByteToMessageDecoder;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.COMPONENT_NAMES;
15 * Class that detects version of used OpenFlow Protocol and engages right OFCodec into
18 * @author michal.polkorab
20 public class OFVersionDetector extends ByteToMessageDecoder {
22 /** Version number of OpenFlow 1.3 protocol */
23 public static final byte OF13_VERSION_ID = 0x04;
24 private static final Logger LOGGER = LoggerFactory.getLogger(OFVersionDetector.class);
27 * Constructor of class.
29 public OFVersionDetector() {
30 LOGGER.info("Creating OFVersionDetector");
34 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
35 LOGGER.info("Decoding frame");
37 if (bb.readableBytes() == 0) {
38 LOGGER.info("not enough data");
42 LOGGER.debug("RI: " + bb.readerIndex());
43 byte version = bb.readByte();
45 if (version == OF13_VERSION_ID) {
46 LOGGER.debug("detected version: " + version);
49 LOGGER.warn("detected version: " + version + " - currently not supported");
53 ByteBuf messageBuffer = bb.slice();
54 list.add(messageBuffer);
55 messageBuffer.retain();
56 bb.skipBytes(bb.readableBytes());
59 private static void enableOF13Codec(ChannelHandlerContext chc) {
60 if (chc.pipeline().get(COMPONENT_NAMES.OF_CODEC.name()) == null) {
61 LOGGER.info("Engaging OF13Codec");
62 chc.pipeline().addLast(COMPONENT_NAMES.OF_CODEC.name(), new OF13Codec());
64 LOGGER.debug("OF13Codec already in pipeline");