Renamed packages to org.opendaylight.openflowjava.protocol.impl.*
[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 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.COMPONENT_NAMES;
13
14 /**
15  * Class that detects version of used OpenFlow Protocol and engages right OFCodec into
16  * pipeline.
17  *
18  * @author michal.polkorab
19  */
20 public class OFVersionDetector extends ByteToMessageDecoder {
21
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);
25
26     /**
27      * Constructor of class.
28      */
29     public OFVersionDetector() {
30         LOGGER.info("Creating OFVersionDetector");
31     }
32
33     @Override
34     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
35         LOGGER.info("Decoding frame");
36
37         if (bb.readableBytes() == 0) {
38             LOGGER.info("not enough data");
39             bb.release();
40             return;
41         }
42         LOGGER.debug("RI: " + bb.readerIndex());
43         byte version = bb.readByte();
44
45         if (version == OF13_VERSION_ID) {
46             LOGGER.debug("detected version: " + version);
47             enableOF13Codec(chc);
48         } else {
49             LOGGER.warn("detected version: " + version + " - currently not supported");
50             return;
51         }
52
53         ByteBuf messageBuffer = bb.slice();
54         list.add(messageBuffer);
55         messageBuffer.retain();
56         bb.skipBytes(bb.readableBytes());
57     }
58
59     private static void enableOF13Codec(ChannelHandlerContext chc) {
60         if (chc.pipeline().get(COMPONENT_NAMES.OF_CODEC.name()) == null) {
61             LOGGER.info("Engaging OF13Codec");
62             chc.pipeline().addLast(COMPONENT_NAMES.OF_CODEC.name(), new OF13Codec());
63         } else {
64             LOGGER.debug("OF13Codec already in pipeline");
65         }
66     }
67 }