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);
47 enableOF13Decoder(chc);
48 enableOF13Encoder(chc);
50 LOGGER.warn("detected version: " + version + " - currently not supported");
54 ByteBuf messageBuffer = bb.slice();
55 list.add(new VersionMessageWrapper(version, messageBuffer));
56 messageBuffer.retain();
57 bb.skipBytes(bb.readableBytes());
60 private static void enableOF13Encoder(ChannelHandlerContext chc) {
61 if (chc.pipeline().get(COMPONENT_NAMES.OF_ENCODER.name()) == null) {
62 LOGGER.info("Engaging OF13Encoder");
63 chc.pipeline().addBefore(COMPONENT_NAMES.DELEGATING_INBOUND_HANDLER.name(),
64 COMPONENT_NAMES.OF_ENCODER.name(), new OF13Encoder());
66 LOGGER.debug("OF13Encoder already in pipeline");
71 private static void enableOF13Decoder(ChannelHandlerContext chc) {
72 if (chc.pipeline().get(COMPONENT_NAMES.OF_DECODER.name()) == null) {
73 LOGGER.info("Engaging OF13Decoder");
74 chc.pipeline().addBefore(COMPONENT_NAMES.DELEGATING_INBOUND_HANDLER.name(),
75 COMPONENT_NAMES.OF_DECODER.name(), new OF13Decoder());
77 LOGGER.debug("OF13Decoder already in pipeline");