b635ef2567610076a3b982db2dfb688ed387f209
[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 import java.util.List;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.openflowjava.statistics.CounterEventTypes;
17 import org.opendaylight.openflowjava.statistics.StatisticsCounters;
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 short OF_PACKETIN = 10;
32     private static final Logger LOG = LoggerFactory.getLogger(OFVersionDetector.class);
33     private final StatisticsCounters statisticsCounters;
34     private volatile boolean filterPacketIns;
35
36     /**
37      * Constructor of class.
38      */
39     public OFVersionDetector() {
40         LOG.trace("Creating OFVersionDetector");
41         statisticsCounters = StatisticsCounters.getInstance();
42     }
43
44     public void setFilterPacketIns(final boolean enabled) {
45         filterPacketIns = enabled;
46     }
47
48     @Override
49     protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) {
50         if (!in.isReadable()) {
51             LOG.debug("not enough data");
52             in.release();
53             return;
54         }
55
56         final byte version = in.readByte();
57         if (version == OF13_VERSION_ID || version == OF10_VERSION_ID) {
58             LOG.debug("detected version: {}", version);
59             if (!filterPacketIns || OF_PACKETIN != in.getUnsignedByte(in.readerIndex())) {
60                 ByteBuf messageBuffer = in.slice();
61                 out.add(new VersionMessageWrapper(version, messageBuffer));
62                 messageBuffer.retain();
63             } else {
64                 LOG.debug("dropped packetin");
65                 statisticsCounters.incrementCounter(CounterEventTypes.US_DROPPED_PACKET_IN);
66             }
67         } else {
68             LOG.warn("detected version: {} - currently not supported", version);
69         }
70         in.skipBytes(in.readableBytes());
71     }
72 }