fa419b92c11a2789687fee6b567ef59f99f138de
[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.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.info("Creating OFVersionDetector");
30     }
31
32     @Override
33     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
34         LOGGER.info("Decoding frame");
35
36         if (bb.readableBytes() == 0) {
37             LOGGER.info("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) {
45             LOGGER.debug("detected version: " + version);
46         } else {
47             LOGGER.warn("detected version: " + version + " - currently not supported");
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 }