d5b31e261651088f868119c68713856a961298e5
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFVersionDetector.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
3
4 import io.netty.buffer.ByteBuf;
5 import io.netty.channel.ChannelHandlerContext;
6 import io.netty.handler.codec.ByteToMessageDecoder;
7
8 import java.util.List;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 /**
14  * Class that detects version of used OpenFlow Protocol and engages right OFCodec into
15  * pipeline.
16  *
17  * @author michal.polkorab
18  */
19 public class OFVersionDetector extends ByteToMessageDecoder {
20
21     /** Version number of OpenFlow 1.0 protocol */
22     public static final byte OF10_VERSION_ID = 0x01;
23     /** Version number of OpenFlow 1.3 protocol */
24     public static final byte OF13_VERSION_ID = 0x04;
25     private static final Logger LOGGER = LoggerFactory.getLogger(OFVersionDetector.class);
26
27     /**
28      * Constructor of class.
29      */
30     public OFVersionDetector() {
31         LOGGER.debug("Creating OFVersionDetector");
32     }
33
34     @Override
35     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
36         if (bb.readableBytes() == 0) {
37             LOGGER.debug("not enough data");
38             bb.release();
39             return;
40         }
41         LOGGER.debug("RI: " + bb.readerIndex());
42         byte version = bb.readByte();
43
44         if ((version == OF13_VERSION_ID) || (version == OF10_VERSION_ID)) {
45             LOGGER.debug("detected version: " + version);
46         } else {
47             LOGGER.warn("detected version: " + version + " - currently not supported");
48             bb.skipBytes(bb.readableBytes());
49             return;
50         }
51
52         ByteBuf messageBuffer = bb.slice();
53         list.add(new VersionMessageWrapper(version, messageBuffer));
54         messageBuffer.retain();
55         bb.skipBytes(bb.readableBytes());
56     }
57
58 }