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