Merge "Remove redundant type specifiers"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / OFDatagramPacketHandler.java
1 /*
2  * Copyright (c) 2014 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.channel.socket.DatagramPacket;
14 import io.netty.handler.codec.MessageToMessageDecoder;
15 import java.util.List;
16 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;
19 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactoryImpl;
20 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
21 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageConsumer;
22 import org.opendaylight.openflowjava.util.ByteBufUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Handler for datagram packets.
28  *
29  * @author michal.polkorab
30  */
31 public class OFDatagramPacketHandler extends MessageToMessageDecoder<DatagramPacket> {
32
33     private static final Logger LOG = LoggerFactory.getLogger(OFDatagramPacketHandler.class);
34
35     /** Length of OpenFlow 1.3 header. */
36     public static final byte LENGTH_OF_HEADER = 8;
37     private static final byte LENGTH_INDEX_IN_HEADER = 2;
38     private final ConnectionAdapterFactory adapterFactory = new ConnectionAdapterFactoryImpl();
39     private final SwitchConnectionHandler connectionHandler;
40     private int channelOutboundQueueSize;
41
42     /**
43      * Default constructor.
44      *
45      * @param sch the switchConnectionHandler that decides what to do with incomming message / channel
46      * @param channelOutboundQueueSize the queue size is made configurable
47      */
48
49     public OFDatagramPacketHandler(SwitchConnectionHandler sch, int channelOutboundQueueSize) {
50         this.connectionHandler = sch;
51         this.channelOutboundQueueSize = channelOutboundQueueSize;
52     }
53
54     @Override
55     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
56         LOG.warn("Unexpected exception from downstream.", cause);
57         LOG.warn("Closing connection.");
58         ctx.close();
59     }
60
61     @Override
62     protected void decode(ChannelHandlerContext ctx, DatagramPacket msg,
63            List<Object> out) throws Exception {
64         LOG.debug("OFDatagramPacketFramer");
65         MessageConsumer consumer = UdpConnectionMap.getMessageConsumer(msg.sender());
66         if (consumer == null) {
67             ConnectionFacade connectionFacade =
68                     adapterFactory.createConnectionFacade(ctx.channel(), msg.sender(), false,
69                             channelOutboundQueueSize);
70             connectionHandler.onSwitchConnected(connectionFacade);
71             connectionFacade.checkListeners();
72             UdpConnectionMap.addConnection(msg.sender(), connectionFacade);
73         }
74         ByteBuf bb = msg.content();
75         int readableBytes = bb.readableBytes();
76         if (readableBytes < LENGTH_OF_HEADER) {
77             if (LOG.isDebugEnabled()) {
78                 LOG.debug("skipping bytebuf - too few bytes for header: {} < {}", readableBytes, LENGTH_OF_HEADER);
79                 LOG.debug("bb: {}", ByteBufUtils.byteBufToHexString(bb));
80             }
81             return;
82         }
83
84         int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER);
85         LOG.debug("length of actual message: {}", length);
86
87         if (readableBytes < length) {
88             if (LOG.isDebugEnabled()) {
89                 LOG.debug("skipping bytebuf - too few bytes for msg: {} < {}", readableBytes, length);
90                 LOG.debug("bytebuffer: {}", ByteBufUtils.byteBufToHexString(bb));
91             }
92             return;
93         }
94         LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1));
95
96
97         byte version = bb.readByte();
98         if (version == EncodeConstants.OF13_VERSION_ID || version == EncodeConstants.OF10_VERSION_ID) {
99             LOG.debug("detected version: {}", version);
100             ByteBuf messageBuffer = bb.slice();
101             out.add(new VersionMessageUdpWrapper(version, messageBuffer, msg.sender()));
102             messageBuffer.retain();
103         } else {
104             LOG.warn("detected version: {} - currently not supported", version);
105         }
106         bb.skipBytes(bb.readableBytes());
107     }
108 }