671b8dd97fa730a3d1a831c569d30b9e5dd2a110
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFVersionDetector.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.core;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.handler.codec.ByteToMessageDecoder;
14
15 import java.util.List;
16
17 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Detects version of used OpenFlow Protocol and discards unsupported version messages
23  * @author michal.polkorab
24  */
25 public class OFVersionDetector extends ByteToMessageDecoder {
26
27     /** Version number of OpenFlow 1.0 protocol */
28     private static final byte OF10_VERSION_ID = EncodeConstants.OF10_VERSION_ID;
29     /** Version number of OpenFlow 1.3 protocol */
30     private static final byte OF13_VERSION_ID = EncodeConstants.OF13_VERSION_ID;
31     private static final Logger LOGGER = LoggerFactory.getLogger(OFVersionDetector.class);
32
33     /**
34      * Constructor of class.
35      */
36     public OFVersionDetector() {
37         LOGGER.debug("Creating OFVersionDetector");
38     }
39
40     @Override
41     protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
42         if (bb.readableBytes() == 0) {
43             LOGGER.debug("not enough data");
44             bb.release();
45             return;
46         }
47         LOGGER.debug("RI: " + bb.readerIndex());
48         byte version = bb.readByte();
49
50         if ((version == OF13_VERSION_ID) || (version == OF10_VERSION_ID)) {
51             LOGGER.debug("detected version: " + version);
52         } else {
53             LOGGER.warn("detected version: " + version + " - currently not supported");
54             bb.skipBytes(bb.readableBytes());
55             return;
56         }
57
58         ByteBuf messageBuffer = bb.slice();
59         list.add(new VersionMessageWrapper(version, messageBuffer));
60         messageBuffer.retain();
61         bb.skipBytes(bb.readableBytes());
62     }
63
64 }