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