1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.openflow.core;
4 import io.netty.buffer.ByteBuf;
5 import io.netty.channel.ChannelHandlerContext;
6 import io.netty.handler.codec.ByteToMessageDecoder;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.openflow.core.TCPHandler.COMPONENT_NAMES;
13 * Class that detects version of used OpenFlow Protocol and engages right OFCodec into
16 * @author michal.polkorab
18 public class OFVersionDetector extends ByteToMessageDecoder {
20 private static final byte OF13_VERSION_ID = 0x04;
21 private static final Logger LOGGER = LoggerFactory.getLogger(OFVersionDetector.class);
24 * Constructor of class.
26 public OFVersionDetector() {
27 LOGGER.info("Creating OFVersionDetector");
31 protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
32 LOGGER.info("Decoding frame");
34 LOGGER.debug("RI: " + bb.readerIndex());
35 byte version = bb.readByte();
37 if (version == OF13_VERSION_ID) {
38 LOGGER.debug("detected version: " + version);
41 LOGGER.warn("detected version: " + version + " - currently not supported");
45 ByteBuf messageBuffer = bb.slice();
46 list.add(messageBuffer);
47 messageBuffer.retain();
48 bb.skipBytes(bb.readableBytes());
51 private void enableOF13Codec(ChannelHandlerContext chc) {
52 if (chc.pipeline().get(COMPONENT_NAMES.OF_CODEC.name()) == null) {
53 LOGGER.info("Engaging OF13Codec");
54 chc.pipeline().addLast(COMPONENT_NAMES.OF_CODEC.name(), new OF13Codec());
56 LOGGER.debug("OF13Codec already in pipeline");