9026dcc68d957b0b3cc323b7462896c5880d6f94
[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.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 /**
15  * Detects version of used OpenFlow Protocol and discards unsupported version messages
16  * @author michal.polkorab
17  */
18 public class OFVersionDetector extends ByteToMessageDecoder {
19
20     /** Version number of OpenFlow 1.0 protocol */
21     private static final byte OF10_VERSION_ID = EncodeConstants.OF10_VERSION_ID;
22     /** Version number of OpenFlow 1.3 protocol */
23     private static final byte OF13_VERSION_ID = EncodeConstants.OF13_VERSION_ID;
24     private static final Logger LOGGER = LoggerFactory.getLogger(OFVersionDetector.class);
25
26     /**
27      * Constructor of class.
28      */
29     public OFVersionDetector() {
30         LOGGER.debug("Creating OFVersionDetector");
31     }
32
33     @Override
34     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
35         if (bb.readableBytes() == 0) {
36             LOGGER.debug("not enough data");
37             bb.release();
38             return;
39         }
40         LOGGER.debug("RI: " + bb.readerIndex());
41         byte version = bb.readByte();
42
43         if ((version == OF13_VERSION_ID) || (version == OF10_VERSION_ID)) {
44             LOGGER.debug("detected version: " + version);
45         } else {
46             LOGGER.warn("detected version: " + version + " - currently not supported");
47             bb.skipBytes(bb.readableBytes());
48             return;
49         }
50
51         ByteBuf messageBuffer = bb.slice();
52         list.add(new VersionMessageWrapper(version, messageBuffer));
53         messageBuffer.retain();
54         bb.skipBytes(bb.readableBytes());
55     }
56
57 }